little switch() fix
[supertux.git] / src / title.c
1 /*
2   title.c
3   
4   Super Tux - Title Screen
5   
6   by Bill Kendrick
7   bill@newbreedsoftware.com
8   http://www.newbreedsoftware.com/supertux/
9   
10   April 11, 2000 - December 29, 2003
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <errno.h>
17 #include <unistd.h>
18 #include <SDL.h>
19 #include <SDL_image.h>
20
21 #ifdef LINUX
22 #include <pwd.h>
23 #include <sys/types.h>
24 #include <ctype.h>
25 #endif
26
27 #include "defines.h"
28 #include "globals.h"
29 #include "title.h"
30 #include "screen.h"
31 #include "high_scores.h"
32 #include "menu.h"
33 #include "texture.h"
34 #include "timer.h"
35 #include "setup.h"
36 #include "level.h"
37 #include "gameloop.h"
38 #include "leveleditor.h"
39
40 /* --- TITLE SCREEN --- */
41
42 int title(void)
43 {
44   texture_type title, img_choose_subset, anim1, anim2;
45   SDL_Event event;
46   SDLKey key;
47   int done, quit, frame, pict, i;
48   char str[80];
49   char **level_subsets;
50   level_subsets = NULL;
51   int subsets_num;
52   level_subsets = dsubdirs("/levels", "info", &subsets_num);
53   st_subset subset;
54   subset_init(&subset);
55
56   /* Rest menu variables */
57   menu_reset();
58   menu_set_current(&main_menu);
59
60   clearscreen(0, 0, 0);
61   updatescreen();
62
63   /* Load images: */
64
65   texture_load(&title,DATA_PREFIX "/images/title/title.png", IGNORE_ALPHA);
66   texture_load(&anim1,DATA_PREFIX "/images/title/title-anim2.png", IGNORE_ALPHA);
67   texture_load(&anim2,DATA_PREFIX "/images/title/title-anim1.png", IGNORE_ALPHA);
68   texture_load(&img_choose_subset,DATA_PREFIX "/images/status/choose-level-subset.png", IGNORE_ALPHA);
69
70   /* --- Main title loop: --- */
71
72   done = 0;
73   quit = 0;
74   show_menu = 1;
75   frame = 0;
76
77   /* Draw the title background: */
78   texture_draw_bg(&title, NO_UPDATE);
79
80   load_hs();
81
82   while (!done && !quit)
83     {
84       frame++;
85       /* Handle events: */
86
87       while (SDL_PollEvent(&event))
88         {
89           if (event.type == SDL_QUIT)
90             {
91               /* Quit event - quit: */
92               quit = 1;
93             }
94           else if (event.type == SDL_KEYDOWN)
95             {
96               /* Keypress... */
97
98               key = event.key.keysym.sym;
99
100               /* Check for menu events */
101               menu_event(&event.key.keysym);
102
103               if (key == SDLK_ESCAPE)
104                 {
105                   /* Escape: Quit: */
106
107                   quit = 1;
108                 }
109             }
110 #ifdef JOY_YES
111           else if (event.type == SDL_JOYAXISMOTION && event.jaxis.axis == JOY_Y)
112             {
113               if (event.jaxis.value > 1024)
114                 menuaction = MN_DOWN;
115               else if (event.jaxis.value < -1024)
116                 menuaction = MN_UP;
117             }
118           else if (event.type == SDL_JOYBUTTONDOWN)
119             {
120               /* Joystick button: Continue: */
121
122               menuaction = MN_HIT;
123             }
124 #endif
125
126         }
127
128       /* Draw the title background: */
129
130       texture_draw_bg(&title, NO_UPDATE);
131
132       /* Draw the high score: */
133       sprintf(str, "High score: %d", hs_score);
134       text_drawf(&gold_text, str, 0, -40, A_HMIDDLE, A_BOTTOM, 1, NO_UPDATE);
135       sprintf(str, "by %s", hs_name);
136       text_drawf(&gold_text, str, 0, -20, A_HMIDDLE, A_BOTTOM, 1, NO_UPDATE);
137
138       /* Don't draw menu, if quit is true */
139       if(show_menu && !quit)
140         menu_process_current();
141
142       if(current_menu == &main_menu)
143         {
144           switch (menu_check(&main_menu))
145             {
146             case 0:
147               done = 0;
148               i = 0;
149               if(level_subsets != NULL)
150                 {
151                   subset_load(&subset,level_subsets[0]);
152                   while(!done)
153                     {
154                       texture_draw(&img_choose_subset, 50, 0, NO_UPDATE);
155                       if(subsets_num != 0)
156                         {
157                           texture_draw(&subset.image,135,78,NO_UPDATE);
158                           text_drawf(&gold_text, subset.title, 0, 20, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
159                         }
160                       updatescreen();
161                       SDL_Delay(50);
162                       while(SDL_PollEvent(&event) && !done)
163                         {
164                           switch(event.type)
165                             {
166                             case SDL_QUIT:
167                               done = 1;
168                               quit = 1;
169                             case SDL_KEYDOWN:           // key pressed
170                               /* Keypress... */
171
172                               key = event.key.keysym.sym;
173
174                               if(key == SDLK_LEFT)
175                                 {
176                                   if(i > 0)
177                                     {
178                                       --i;
179                                       subset_free(&subset);
180                                       subset_load(&subset,level_subsets[i]);
181                                     }
182                                 }
183                               else if(key == SDLK_RIGHT)
184                                 {
185                                   if(i < subsets_num -1)
186                                     {
187                                       ++i;
188                                       subset_free(&subset);
189                                       subset_load(&subset,level_subsets[i]);
190                                     }
191                                 }
192                               else if(key == SDLK_SPACE || key == SDLK_RETURN)
193                                 {
194                                   done = YES;
195                                   quit = gameloop(subset.name,1,ST_GL_PLAY);
196                                   subset_free(&subset);
197                                 }
198                               else if(key == SDLK_ESCAPE)
199                                 {
200                                   done = YES;
201                                 }
202                               break;
203                             default:
204                               break;
205                             }
206                         }
207                     }
208                 }
209               break;
210             case 3:
211               done = 1;
212               quit = leveleditor(1);
213               break;
214             case 4:
215               quit = 1;
216               break;
217             }
218         }
219       else if(current_menu == &options_menu)
220         {
221           process_options_menu();
222         }
223       /* Animate title screen: */
224
225       pict = (frame / 5) % 3;
226
227       if (pict == 0)
228         texture_draw_part(&title, 560, 270, 560, 270, 80, 75, NO_UPDATE);
229       else if (pict == 1)
230         texture_draw(&anim1, 560, 270, NO_UPDATE);
231       else if (pict == 2)
232         texture_draw(&anim2, 560, 270, NO_UPDATE);
233
234       flipscreen();
235
236       /* Pause: */
237
238       SDL_Delay(50);
239
240     }
241   /* Free surfaces: */
242
243   texture_free(&title);
244   texture_free(&anim1);
245   texture_free(&anim2);
246   free_strings(level_subsets,subsets_num);
247
248   /* Return to main! */
249
250   return(quit);
251 }