many code-cleanups. merged leveleditor patch from Ricardo Cruz. Fixed bugs. many...
[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       /* Animate title screen: */
139
140       pict = (frame / 5) % 3;
141
142       if (pict == 0)
143         texture_draw_part(&title, 560, 270, 560, 270, 80, 75, NO_UPDATE);
144       else if (pict == 1)
145         texture_draw(&anim1, 560, 270, NO_UPDATE);
146       else if (pict == 2)
147         texture_draw(&anim2, 560, 270, NO_UPDATE);
148         
149       /* Don't draw menu, if quit is true */
150       if(show_menu && !quit)
151         menu_process_current();
152
153       if(current_menu == &main_menu)
154         {
155           switch (menu_check(&main_menu))
156             {
157             case 0:
158               done = 0;
159               i = 0;
160               if(level_subsets != NULL)
161                 {
162                   subset_load(&subset,level_subsets[0]);
163                   while(!done)
164                     {
165                       texture_draw(&img_choose_subset,(screen->w - img_choose_subset.w) / 2, 0, NO_UPDATE);
166                       if(subsets_num != 0)
167                         {
168                           texture_draw(&subset.image,(screen->w - subset.image.w) / 2 + 25,78,NO_UPDATE);
169                           text_drawf(&gold_text, subset.title, 0, 20, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
170                         }
171                       updatescreen();
172                       SDL_Delay(50);
173                       while(SDL_PollEvent(&event) && !done)
174                         {
175                           switch(event.type)
176                             {
177                             case SDL_QUIT:
178                               done = 1;
179                               quit = 1;
180                             case SDL_KEYDOWN:           // key pressed
181                               /* Keypress... */
182
183                               key = event.key.keysym.sym;
184
185                               if(key == SDLK_LEFT)
186                                 {
187                                   if(i > 0)
188                                     {
189                                       --i;
190                                       subset_free(&subset);
191                                       subset_load(&subset,level_subsets[i]);
192                                     }
193                                 }
194                               else if(key == SDLK_RIGHT)
195                                 {
196                                   if(i < subsets_num -1)
197                                     {
198                                       ++i;
199                                       subset_free(&subset);
200                                       subset_load(&subset,level_subsets[i]);
201                                     }
202                                 }
203                               else if(key == SDLK_SPACE || key == SDLK_RETURN)
204                                 {
205                                   done = YES;
206                                   quit = gameloop(subset.name,1,ST_GL_PLAY);
207                                   subset_free(&subset);
208                                 }
209                               else if(key == SDLK_ESCAPE)
210                                 {
211                                   done = YES;
212                                 }
213                               break;
214                             default:
215                               break;
216                             }
217                         }
218                     }
219                 }
220               break;
221             case 1:
222               update_load_save_game_menu(&load_game_menu, YES);
223               break;
224             case 3:
225               done = 1;
226               quit = leveleditor(1);
227               break;
228             case 4:
229               quit = 1;
230               break;
231             }
232         }
233       else if(current_menu == &options_menu)
234         {
235           process_options_menu();
236         }
237       else if(current_menu == &load_game_menu)
238         {
239           process_save_load_game_menu(NO);
240         }
241
242       flipscreen();
243
244       /* Pause: */
245
246       SDL_Delay(50);
247
248     }
249   /* Free surfaces: */
250
251   texture_free(&title);
252   texture_free(&anim1);
253   texture_free(&anim2);
254   free_strings(level_subsets,subsets_num);
255
256   /* Return to main! */
257
258   return(quit);
259 }