A brandnew MENU!
[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 9, 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
34
35 /* --- TITLE SCREEN --- */
36
37 int title(void)
38 {
39   SDL_Surface * title, * anim1, * anim2;
40   SDL_Event event;
41   SDLKey key;
42   int done, quit, frame, pict;
43   char str[80];
44
45   game_started = 0;
46
47   /* Init menu variables */
48   initmenu();
49
50   updatescreen();
51
52
53   /* Load images: */
54
55   title = load_image(DATA_PREFIX "/images/title/title.png", IGNORE_ALPHA);
56   anim1 = load_image(DATA_PREFIX "/images/title/title-anim2.png",
57                      IGNORE_ALPHA);
58   anim2 = load_image(DATA_PREFIX "/images/title/title-anim1.png",
59                      IGNORE_ALPHA);
60
61
62   /* --- Main title loop: --- */
63
64   done = 0;
65   quit = 0;
66   show_menu = 1;
67
68   frame = 0;
69
70
71   /* Draw the title background: */
72   drawimage(title, 0, 0, UPDATE);
73
74
75   /* Draw the high score: */
76   sprintf(str, "High score: %d", load_hs());
77   drawcenteredtext(str, 460, letters_red, UPDATE);
78
79   while (!done && !quit)
80     {
81       frame++;
82
83
84       /* Handle events: */
85
86       while (SDL_PollEvent(&event))
87         {
88           if (event.type == SDL_QUIT)
89             {
90               /* Quit event - quit: */
91
92               quit = 1;
93             }
94           else if (event.type == SDL_KEYDOWN)
95             {
96               /* Keypress... */
97
98               key = event.key.keysym.sym;
99
100               if (key == SDLK_ESCAPE)
101                 {
102                   /* Escape: Quit: */
103
104                   quit = 1;
105                 }
106             }
107           else if (event.type == SDL_KEYUP)
108             {
109               key = event.key.keysym.sym;
110               /* Check for menu events */
111               menu_event(key);
112             }
113 #ifdef JOY_YES
114           else if (event.type == SDL_JOYAXISMOTION)
115             {
116               if (event.jaxis.value > 256)
117                 menuaction = MN_DOWN;
118               else
119                 menuaction = MN_UP;
120             }
121           else if (event.type == SDL_JOYBUTTONDOWN)
122             {
123               /* Joystick button: Continue: */
124
125               menuaction = MN_HIT;
126             }
127
128 #endif
129
130         }
131
132       if(menu_change)
133         {
134           /* Draw the title background: */
135
136           drawimage(title, 0, 0, UPDATE);
137
138           /* Draw the high score: */
139           sprintf(str, "High score: %d", load_hs());
140           drawcenteredtext(str, 460, letters_red, UPDATE);
141         }
142
143       /* Don't draw menu, if quit is true */
144       if(show_menu && !quit)
145         quit = drawmenu();
146
147       if(game_started)
148         done = 1;
149
150       /* Animate title screen: */
151
152       pict = (frame / 5) % 3;
153
154       if (pict == 0)
155         drawpart(title, 560, 270, 80, 75, UPDATE);
156       else if (pict == 1)
157         drawimage(anim1, 560, 270, UPDATE);
158       else if (pict == 2)
159         drawimage(anim2, 560, 270, UPDATE);
160
161
162
163       /* Pause: */
164
165       SDL_Delay(50);
166
167     }
168
169
170   /* Free surfaces: */
171
172   SDL_FreeSurface(title);
173   SDL_FreeSurface(anim1);
174   SDL_FreeSurface(anim2);
175
176
177   /* Return to main! */
178
179   return(quit);
180 }