SDL MENU.

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <SDL.h>
#include <SDL_ttf.h>


SDL_Surface *screen=NULL;

SDL_Surface *text=NULL;




SDL_Event event;
bool run=true;




class instructions
{

public:



    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(true)
       {
           time=SDL_GetTicks();
           while(SDL_PollEvent(&event))
           {
             switch(event.type)
             {
             case SDL_QUIT:
             for(int i=0;i<NUMMENU;i++)
             {
                SDL_FreeSurface(menus[i]);
             }
             return 1;

             case SDL_MOUSEMOTION:
               x=event.motion.x;
               y=event.motion.y;
               for(int i=0;i<NUMMENU;i++)
               {
                   if(x>=pos[1].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++)
               {
                 if(x>=pos[i].x && x<=pos[i].x+pos[i].w && y>=pos[i].y && y<=pos[i].y+pos[i].h)
                 {
                 for(int j=0;j<NUMMENU;j++)
                 SDL_FreeSurface(menus[j]);

                   return i;
                 }
              }
               break;

               case SDL_KEYDOWN:
               switch(event.key.keysym.sym)
               {
               case SDLK_ESCAPE:
                for(int i=0;i<NUMMENU;i++)
                SDL_FreeSurface(menus[i]);

               return 0;
               }

             }

         }

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


       SDL_Flip(screen);

       }

    }

};




int main(int argc, char** argv)
{

SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();

SDL_WM_SetCaption("RGP game",NULL);

screen=SDL_SetVideoMode(1000,1000,32,SDL_SWSURFACE);


instructions instr;

TTF_Font *font=TTF_OpenFont("256BYTES.ttf",80);

SDL_Color col{80,10,10};
text=TTF_RenderText_Solid(font,"GOOD\n\n\nLUCK !!!",col);


int i;
i=instr.showmenu(screen,font);

if(i==1)
run=false;

while(run)
{
    if(SDL_PollEvent(&event))
    {
        switch(event.type)
        {
            case SDL_QUIT:
            run=false;
            break;

                case SDLK_ESCAPE:
                int i=instr.showmenu(screen,font);
                 if(i==1)
                  run=false;
                break;

            }

    }

    SDL_Flip(screen);
}

SDL_FreeSurface(text);
TTF_CloseFont(font);

TTF_Quit();
SDL_Quit();
return 0;
}


Here is my source.
Is small and works good.
The problem is that the "continue"and "exit" is appeared with this way:

"excontitnue"(together.)

how could fix so apeared with that way:

continue

exit

.........


thanks.
You're blitting the two text surfaces in the same position.
On line 44 try
pos[1].y=screen->clip_rect.h/2 + menus[0]->h - menus[0]->clip_rect.h

and see if you like the outcome
Last edited on
it works.
Thanks you man.
cAN ADD ME in skype for more help?
Topic archived. No new replies allowed.