Advertisement
thecplusplusguy

menu2 test

Jul 28th, 2012
266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <SDL/SDL.h>
  3. #include <SDL/SDL_ttf.h>
  4. int showmenu(SDL_Surface* screen,TTF_Font* font)
  5. {
  6.     int x,y;
  7.     const int NUMMENU = 2;
  8.     const char* labels[NUMMENU] = {"Play Game","Exit Game"};
  9.     SDL_Surface* menus[NUMMENU];
  10.     bool selected[NUMMENU]={0,0};
  11.     SDL_Color color[2] = {{255,255,255},{255,0,0}};
  12.  
  13.     menus[0] = TTF_RenderText_Solid(font,labels[0],color[0]);
  14.     menus[1] = TTF_RenderText_Solid(font,labels[1],color[0]);
  15.  
  16.     SDL_Rect  pos[NUMMENU];
  17.    
  18.     pos[1].x=screen->clip_rect.w/2 - menus[0]->clip_rect.w/2;
  19.     pos[1].y=screen->clip_rect.h/2 - menus[0]->clip_rect.h;
  20.  
  21.     pos[0].x=screen->clip_rect.w/2 - menus[0]->clip_rect.w/2;
  22.     pos[0].y=(screen->clip_rect.h/2 - menus[0]->clip_rect.h)-20;
  23.  
  24.     SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0,0,0));
  25.  
  26.  
  27.  
  28.     SDL_Event event;
  29.     while(1)
  30.     {
  31.         while(SDL_PollEvent(&event))
  32.         {
  33.             switch(event.type)
  34.             {
  35.                 case SDL_QUIT:
  36.                 for(int i = 0;i<NUMMENU;i++)
  37.                  SDL_FreeSurface(menus[i]);
  38.                 return 1;
  39.                 case SDL_MOUSEMOTION:
  40.                       x = event.motion.x;
  41.                       y = event.motion.y;
  42.                       for(int i = 0;i<NUMMENU;i++)
  43.  
  44.                           if(x >= pos[i].x && x <= pos[i].x+pos[i].w && y >= pos[i].y && y <= pos[i].y+pos[i].h)
  45.                           {
  46.                              if(!selected[i])
  47.                              {
  48.                                  selected[i]=1;
  49.                                  SDL_FreeSurface(menus[i]);
  50.                                  menus[i]=TTF_RenderText_Solid(font,labels[i],color[1]);
  51.                              }
  52.                           }else{
  53.                                         if(selected[i])
  54.                                         {
  55.                                      selected[i]=0;
  56.                                      SDL_FreeSurface(menus[i]);
  57.                                      menus[i] = TTF_RenderText_Solid(font,labels[i],color[0]);
  58.                                                             }
  59.                              }
  60.                       break;
  61.                 case SDL_MOUSEBUTTONDOWN:
  62.                  x = event.button.x;
  63.                  y = event.button.y;
  64.                  for(int i = 0;i<NUMMENU;i++)
  65.  
  66.                      if(x >= pos[i].x && x <= pos[i].x+pos[i].w && y >= pos[i].y && y <= pos[i].y+pos[i].h)
  67.                     {
  68.                         for(int j=0;j<NUMMENU;j++)
  69.                         SDL_FreeSurface(menus[j]);
  70.                         return i;
  71.                     }
  72.  
  73.                  break;
  74.                 case SDL_KEYDOWN:
  75.                  if(event.key.keysym.sym==SDLK_ESCAPE)
  76.                    {
  77.                         for(int i=0;i<NUMMENU;i++)
  78.                         SDL_FreeSurface(menus[i]);
  79.                         return 0;
  80.                     }
  81.                  }
  82.  
  83.             }
  84.             for(int i=0;i<NUMMENU;i++)
  85.              SDL_BlitSurface(menus[i],NULL,screen,&pos[i]);
  86.             SDL_Flip(screen);
  87.         }
  88.  
  89. }
  90.  
  91. int main()
  92. {
  93.     SDL_Init(SDL_INIT_EVERYTHING);
  94.     TTF_Init();
  95.     SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
  96.     TTF_Font* font=TTF_OpenFont("air.ttf",12);
  97.     std::cout << showmenu(screen,font);
  98.     TTF_CloseFont(font);
  99.     TTF_Quit();
  100.     SDL_Quit();
  101.    
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement