SDLの簡単なButton
- #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
",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
",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
",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......
");
- break;
- case SDL_MOUSEMOTION:
-
- break;
- case SDL_MOUSEBUTTONDOWN:
- printf("mouse: x = %d , y = %d
",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
");
- flag = 0;
- break;
- }
- }
- }
-
- Destory(screen);
- SDL_Quit();
- return 0;
- }
この記事は「レコード」ブログから出ていますので、必ずこの出典を残してください.http://mjrao.blog.51cto.com/6086668/1008059