993095c191c3e20a45435845b564efe2f2ebd58b
[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, last_highscore;
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   last_highscore = load_hs();
77   sprintf(str, "High score: %d", last_highscore);
78   drawcenteredtext(str, 460, letters_red, UPDATE);
79
80   while (!done && !quit)
81     {
82       frame++;
83
84
85       /* Handle events: */
86
87       while (SDL_PollEvent(&event))
88         {
89           if (event.type == SDL_QUIT)
90             {
91               /* Quit event - quit: */
92
93               quit = 1;
94             }
95           else if (event.type == SDL_KEYDOWN)
96             {
97               /* Keypress... */
98
99               key = event.key.keysym.sym;
100
101               /* Check for menu events */
102               menu_event(key);
103               
104               if (key == SDLK_ESCAPE)
105                 {
106                   /* Escape: Quit: */
107
108                   quit = 1;
109                 }
110             }
111 #ifdef JOY_YES
112           else if (event.type == SDL_JOYAXISMOTION)
113             {
114               if (event.jaxis.value > 256)
115                 menuaction = MN_DOWN;
116               else
117                 menuaction = MN_UP;
118             }
119           else if (event.type == SDL_JOYBUTTONDOWN)
120             {
121               /* Joystick button: Continue: */
122
123               menuaction = MN_HIT;
124             }
125
126 #endif
127
128         }
129
130       if(menu_change)
131         {
132           /* Draw the title background: */
133
134           drawimage(title, 0, 0, UPDATE);
135
136           /* Draw the high score: */
137           sprintf(str, "High score: %d", last_highscore);
138           drawcenteredtext(str, 460, letters_red, UPDATE);
139         }
140
141       /* Don't draw menu, if quit is true */
142       if(show_menu && !quit)
143         quit = drawmenu();
144
145       if(game_started)
146         done = 1;
147
148       /* Animate title screen: */
149
150       pict = (frame / 5) % 3;
151
152       if (pict == 0)
153         drawpart(title, 560, 270, 80, 75, UPDATE);
154       else if (pict == 1)
155         drawimage(anim1, 560, 270, UPDATE);
156       else if (pict == 2)
157         drawimage(anim2, 560, 270, UPDATE);
158
159
160
161       /* Pause: */
162
163       SDL_Delay(50);
164
165     }
166
167
168   /* Free surfaces: */
169
170   SDL_FreeSurface(title);
171   SDL_FreeSurface(anim1);
172   SDL_FreeSurface(anim2);
173
174
175   /* Return to main! */
176
177   return(quit);
178 }