- #include "SDL.h"
- #include "SDL_gfxPrimitives.h"
- #define BMP_NAME "icon.bmp"
- #define ICON "sample.bmp"
- #define TITLE_NAME "TestSDL"
- #define ICON_NAME "My WinFrame" //最小化icon的name
- #define WIDTH 640
- #define HEIGTH 480
- static SDL_Surface *screen = NULL;
- int Init() //初始化SDL
- {
- if(SDL_Init(SDL_INIT_VIDEO) == -1)
- {
- fprintf(stderr,"SDL init error:%s",SDL_GetError());
- return -1;
- }
- return 0;
- }
- SDL_Surface *loadBMP(char *fileName) //加载一个bmp位图
- {
- SDL_Surface *bmp;
- bmp = SDL_LoadBMP(fileName);
- if(bmp == NULL)
- {
- fprintf(stderr,"Could not load %s: %s\n",fileName,SDL_GetError());
- exit(1);
- }
- return bmp;
- }
- void creatScreen(int width , int height , int bpp , Uint32 flags) //创建一个VideoMode
- {
- screen = SDL_SetVideoMode(width , height, bpp , flags);
- if(screen == NULL)
- {
- fprintf(stderr,"Could not Creat a Screen!:%s\n",SDL_GetError());
- exit(1);
- }
- return ;
- }
- int Destory(SDL_Surface *file) //释放空间
- {
- SDL_FreeSurface( file );
- return 0;
- }
- void show_bmp(SDL_Surface *bmp ,SDL_Rect *rect) //显示bmp图
- {
- SDL_BlitSurface(bmp , NULL , screen , rect);
- SDL_UpdateRect(screen , 0 , 0 , 0 , 0 );
- return ;
- }
- void draw_button(SDL_Rect *button_rect , int flag) //绘出BUTTON
- {
- boxColor(screen, button_rect->x , button_rect->y ,button_rect->x + button_rect->w , button_rect->y + button_rect->h, 0xffffffff);
- if(flag == 1)
- {
- hlineColor( screen, button_rect->x , button_rect->x + button_rect->w ,button_rect->y + button_rect->h , 0x000000ff);
- vlineColor( screen, button_rect->x + button_rect->w , button_rect->y , button_rect->y + button_rect->h , 0x000000ff);
- }
- show_bmp( screen , &(screen->clip_rect) );
- return ;
- }
- int main(int argc,char **argv)
- {
- const SDL_VideoInfo *info = NULL;
- int flag = 1;
- int width = WIDTH;
- int heigth = HEIGTH;
- int bpp = 0;
- SDL_Rect button_rect;
- Init();
- creatScreen(width , heigth, bpp , SDL_SWSURFACE);
- button_rect.x = ( screen->w / 2 )- 20;
- button_rect.y = ( screen->h / 2 )- 20;
- button_rect.w = 40;
- button_rect.h = 40;
- info = SDL_GetVideoInfo();
- if(info == NULL)
- {
- fprintf( stderr, "Video query failed: %s\n",SDL_GetError( ) );
- exit(1);
- }
- bpp = info->vfmt->BitsPerPixel ; //获得VideoMode的bpp
- SDL_WM_SetCaption(TITLE_NAME, ICON_NAME); //设置窗体的NAME和ICON的NAME
- SDL_WM_SetIcon(loadBMP(ICON) , NULL); //加载ICON
- SDL_FillRect(screen, &(screen->clip_rect) , 0xaac9ff);
- draw_button(&button_rect , 0);
- SDL_Event event;
- while(flag)
- {
- while(SDL_PollEvent(&event)) //把事件加入事件队列
- {
- switch(event.type)
- {
- case SDL_KEYDOWN:
- if(event.key.keysym.sym == SDLK_ESCAPE)
- {
- flag = 0;
- }
- SDL_FillRect(screen, &(screen->clip_rect) , 0x00); //填充screen
- SDL_UpdateRect(screen , 0 , 0 , 0 , 0 );
- break;
- case SDL_KEYUP:
- printf("Key up......\n");
- break;
- case SDL_MOUSEMOTION:
- break;
- case SDL_MOUSEBUTTONDOWN:
- printf("mouse: x = %d , y = %d\n",event.button.x,event.button.y);
- if((event.button.x >= button_rect.x && event.button.x <= (button_rect.x + button_rect.w)) && (event.button.y >= button_rect.y && event.button.y <= ( button_rect.y + button_rect.h )))
- {
- draw_button(&button_rect , 1);
- }
- break;
- case SDL_MOUSEBUTTONUP:
- draw_button(&button_rect , 0);
- break;
- case SDL_QUIT:
- printf("quit\n");
- flag = 0;
- break;
- }
- }
- }
- Destory(screen);
- SDL_Quit();
- return 0;
- }