initial
[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   /* Draw the high score: */
81   load_hs();
82   sprintf(str, "High score: %d", hs_score);
83   text_drawf(&gold_text, str, 0, -40, A_HMIDDLE, A_BOTTOM, 1, NO_UPDATE);
84   sprintf(str, "by %s", hs_name);
85   text_drawf(&gold_text, str, 0, -20, A_HMIDDLE, A_BOTTOM, 1, NO_UPDATE);
86
87   while (!done && !quit)
88     {
89       frame++;
90       /* Handle events: */
91
92       while (SDL_PollEvent(&event))
93         {
94           if (event.type == SDL_QUIT)
95             {
96               /* Quit event - quit: */
97               quit = 1;
98             }
99           else if (event.type == SDL_KEYDOWN)
100             {
101               /* Keypress... */
102
103               key = event.key.keysym.sym;
104
105               /* Check for menu events */
106               menu_event(&event.key.keysym);
107
108               if (key == SDLK_ESCAPE)
109                 {
110                   /* Escape: Quit: */
111
112                   quit = 1;
113                 }
114             }
115 #ifdef JOY_YES
116           else if (event.type == SDL_JOYAXISMOTION && event.jaxis.axis == JOY_Y)
117             {
118               if (event.jaxis.value > 1024)
119                 menuaction = MN_DOWN;
120               else if (event.jaxis.value < -1024)
121                 menuaction = MN_UP;
122             }
123           else if (event.type == SDL_JOYBUTTONDOWN)
124             {
125               /* Joystick button: Continue: */
126
127               menuaction = MN_HIT;
128             }
129 #endif
130
131         }
132
133       /* Draw the title background: */
134
135       texture_draw_bg(&title, NO_UPDATE);
136
137       /* Draw the high score: */
138       sprintf(str, "High score: %d", hs_score);
139       text_drawf(&gold_text, str, 0, -40, A_HMIDDLE, A_BOTTOM, 1, NO_UPDATE);
140       sprintf(str, "by %s", hs_name);
141       text_drawf(&gold_text, str, 0, -20, A_HMIDDLE, A_BOTTOM, 1, NO_UPDATE);
142
143       /* Don't draw menu, if quit is true */
144       if(show_menu && !quit)
145         menu_process_current();
146
147       if(current_menu == &main_menu)
148         {
149           switch (menu_check(&main_menu))
150             {
151             case 0:
152               done = 0;
153               i = 0;
154               subset_load(&subset,level_subsets[0]);
155               while(!done)
156                 {
157                   texture_draw(&img_choose_subset, 50, 0, NO_UPDATE);
158                   if(subsets_num != 0)
159                     {
160                       texture_draw(&subset.image,135,78,NO_UPDATE);
161                       text_drawf(&gold_text, subset.title, 0, 20, A_HMIDDLE, A_TOP, 1, NO_UPDATE);
162                     }
163                   updatescreen();
164                   SDL_Delay(50);
165                   while(SDL_PollEvent(&event) && !done)
166                     {
167                       switch(event.type)
168                         {
169                         case SDL_QUIT:
170                           done = 1;
171                           quit = 1;
172                         case SDL_KEYDOWN:               // key pressed
173                           /* Keypress... */
174
175                           key = event.key.keysym.sym;
176
177                           if(key == SDLK_LEFT)
178                             {
179                               if(i > 0)
180                                 {
181                                   --i;
182                                   subset_free(&subset);
183                                   subset_load(&subset,level_subsets[i]);
184                                 }
185                             }
186                           else if(key == SDLK_RIGHT)
187                             {
188                               if(i < subsets_num -1)
189                                 {
190                                   ++i;
191                                   subset_free(&subset);
192                                   subset_load(&subset,level_subsets[i]);
193                                 }
194                             }
195                           else if(key == SDLK_SPACE || key == SDLK_RETURN)
196                             {
197                               done = YES;
198                               quit = gameloop(subset.name,1,ST_GL_PLAY);
199                               subset_free(&subset);
200                             }
201                           else if(key == SDLK_ESCAPE)
202                             {
203                               done = YES;
204                             }
205                           break;
206                         default:
207                           break;
208                         }
209                     }
210                 }
211               break;
212             case 3:
213               done = 1;
214               quit = leveleditor(1);
215               break;
216             case 4:
217               quit = 1;
218               break;
219             }
220         }
221       else if(current_menu == &options_menu)
222         {
223           process_options_menu();
224         }
225       /* Animate title screen: */
226
227       pict = (frame / 5) % 3;
228
229       if (pict == 0)
230         texture_draw_part(&title, 560, 270, 560, 270, 80, 75, NO_UPDATE);
231       else if (pict == 1)
232         texture_draw(&anim1, 560, 270, NO_UPDATE);
233       else if (pict == 2)
234         texture_draw(&anim2, 560, 270, NO_UPDATE);
235
236       flipscreen();
237
238       /* Pause: */
239
240       SDL_Delay(50);
241
242     }
243   /* Free surfaces: */
244
245   texture_free(&title);
246   texture_free(&anim1);
247   texture_free(&anim2);
248   free_strings(level_subsets,subsets_num);
249
250   /* Return to main! */
251
252   return(quit);
253 }