C++ SDL 2.0 main menu Function

I'm currently trying to write a menu function for one of my projects. I'm remaking pac-man and I'd like to be able to modularize the main menu ,calling it when I want it to appear. I'm thinking an SDL_surface and a TTF font are all I need as parameters for this particular function.

I ran into trouble when I used SDL functions only available in SDL 1.2 as opposed to 2.0. I would like this code to be as reusable as possible. How would you guys approach this problem? I have some sample code below, though not all of it is my own. I'd like to get something like this working:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
int showmenu(SDL_Surface* screen, TTF_Font* font)
{
  Uint32 time;
  int x, y;
  const int NUMMENU = 2;
  const char* labels[NUMMENU] = {"Continue","Exit"};
  SDL_Surface* menus[NUMMENU];
  bool selected[NUMMENU] = {0,0};
  SDL_Color color[2] = {{255,255,255},{255,0,0}};

  menus[0] = TTF_RenderText_Solid(font,labels[0],color[0]);
  menus[1] = TTF_RenderText_Solid(font,labels[1],color[0]);
  SDL_Rect pos[NUMMENU];
  pos[0].x = screen->clip_rect.w/2 - menus[0]->clip_rect.w/2;
  pos[0].y = screen->clip_rect.h/2 - menus[0]->clip_rect.h;
  pos[1].x = screen->clip_rect.w/2 - menus[0]->clip_rect.w/2;
  pos[1].y = screen->clip_rect.h/2 + menus[0]->clip_rect.h;

  SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0x00,0x00,0x00));

  SDL_Event event;
  while(1)
  {
    time = SDL_GetTicks();
    while(SDL_PollEvent(&event))
    {
      switch(event.type)
      {
        case SDL_QUIT:
        SDL_FreeSurface(menus[0]);
        SDL_FreeSurface(menus[1]);
        return 1;

        case SDL_MOUSEMOTION:
        x = event.motion.x;
        y = event.motion.y;
        for(int i = 0; i < NUMMENU; i += 1)
        {

          if(x >= pos[i].x && x <= pos[i].x + pos[i].w &&
             y >= pos[i].y && y <= pos[i].y + pos[i].h)
          {
            if(!selected[i])
            {
              selected[i] = 1;
              SDL_FreeSurface(menus[i]);
              menus[i] = TTF_RenderText_Solid(font,labels[i],color[1]);
            }
          }

          else
          {
            if(selected[i])
            {
              selected[i] = 0;
              SDL_FreeSurface(menus[i]);
              menus[i] = TTF_RenderText_Solid(font,labels[i],color[0]);
            }
          }
        }
          break;

          case SDL_MOUSEBUTTONDOWN:
          x = event.button.x;
          y = event.button.y;
          for(int i = 0; i < NUMMENU; i += 1)
          {
            if(x >= pos[i].x && x <= pos[i].x + pos[i].w &&
               y >= pos[i].y && y <= pos[i].y + pos[i].h)
            {
              SDL_FreeSurface(menus[0]);
              SDL_FreeSurface(menus[1]);
              return i;
            }
          }
          break;

          case SDL_KEYDOWN:
          if(event.key.keysym.sym == SDLK_ESCAPE)
          {
            SDL_FreeSurface(menus[0]);
            SDL_FreeSurface(menus[1]);
            return 0;
          }
        }
      }

      for(int i = 0; i < NUMMENU; i += 1)
      {
        SDL_BlitSurface(menus[i],NULL,screen,&pos[i]);
      }

    }
}
I would suggest not to use outdated function. If you have these old functions there is certainly a way to accomplish the same with new functions.
Topic archived. No new replies allowed.