#include <sdl.h>
#include <stdio.h>
#include <windows.h>
SDL_Surface* MyLoadBMP( char* filename );
SDL_Event g_Event;
SDL_Surface* g_pDS = NULL;
SDL_Surface* g_pBitmap = NULL;
int x = 320;
int y = 240;
int SDL_main( int argc, char* argv[] )
{
if( SDL_Init(SDL_INIT_VIDEO) == -1 )
{
fprintf( stderr, "Could not initialize SDL!\n" );
SDL_Quit();
return 0;
}
else
fprintf( stdout, "SDL Initialized properly!\n" );
g_pDS = SDL_SetVideoMode( 640, 480, 0, SDL_ANYFORMAT );
// 비트맵을 불러온다.
g_pBitmap = MyLoadBMP( "test.bmp" );
if( g_pBitmap == NULL )
{
SDL_Quit();
return 0;
}
int mX = 0;
int mY = 0;
for( ;; )
{
if( SDL_PollEvent(&g_Event) == 0 )
{
// 마우스의 좌표를 얻어온다.
SDL_PumpEvents();
SDL_GetMouseState( &mX, &mY );
// 출력할 위치와 크기를 지정한다.
SDL_Rect rect;
rect.x = mX;
rect.y = mY;
// 화면을 지운다.
SDL_FillRect( g_pDS, NULL, SDL_MapRGB(g_pDS->format, 0, 0, 0) );
// 비트맵을 출력한다.
SDL_BlitSurface( g_pBitmap, NULL, g_pDS, &rect );
// 화면을 Update한다.
SDL_UpdateRect( g_pDS, 0, 0, 0, 0 );
}
else
{
if( g_Event.type == SDL_QUIT ) break;
}
}
// 비트맵을 지운다.
if( g_pBitmap != NULL )
SDL_FreeSurface( g_pBitmap );
SDL_Quit();
return 0;
}
SDL_Surface* MyLoadBMP( char* filename )
{
SDL_Surface* pBitmap = NULL;
// 파일을 연다.
FILE* fp = fopen( filename, "rb" );
if( fp == NULL )
return NULL;
// 비트맵 파일인지 확인한다.
BITMAPFILEHEADER bmFileHeader;
fread( &bmFileHeader, sizeof(BITMAPFILEHEADER), 1, fp );
if( bmFileHeader.bfType != 0x4D42 ) // 'BM'
{
fclose( fp );
return NULL;
}
// Surface를 만든다.
BITMAPINFO bmInfo;
fread( &bmInfo, sizeof(BITMAPINFO), 1, fp );
pBitmap = SDL_CreateRGBSurface( SDL_HWSURFACE,
bmInfo.bmiHeader.biWidth,
bmInfo.bmiHeader.biHeight,
bmInfo.bmiHeader.biBitCount,
0x00FF0000,
0x0000FF00,
0x000000FF,
0x00000000 );
if( pBitmap == NULL )
{
fclose( fp );
return NULL;
}
int color;
unsigned char r;
unsigned char g;
unsigned char b;
// 파일에 있는 정보를 Surface로 이동한다.
for( int y = bmInfo.bmiHeader.biHeight-1; y >= 0; y-- )
{
for( int x = 0; x < bmInfo.bmiHeader.biWidth; x++ )
{
// read color
fread( &r, sizeof(unsigned char), 1, fp );
fread( &g, sizeof(unsigned char), 1, fp );
fread( &b, sizeof(unsigned char), 1, fp );
color = SDL_MapRGB( pBitmap->format, r, g, b );
memcpy( ((char *)pBitmap->pixels) + (y*pBitmap->pitch) + (x*pBitmap->format->BytesPerPixel), &color, pBitmap->format->BytesPerPixel );
}
}
SDL_SetColorKey( pBitmap, SDL_SRCCOLORKEY, 0 );
// 파일을 닫는다.
fclose( fp );
return pBitmap;
}
-출처 백승지 교수님 블로그 ( http://blog.naver.com/conaman )