incoporated patches from Duong-Khang (stereo sound!) and Ricardo Cruz <rick2@aeiou...
[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   level_editor_started = 0;
47
48   /* Init menu variables */
49   initmenu();
50
51   updatescreen();
52
53
54   /* Load images: */
55
56   title = load_image(DATA_PREFIX "/images/title/title.png", IGNORE_ALPHA);
57   anim1 = load_image(DATA_PREFIX "/images/title/title-anim2.png",
58                      IGNORE_ALPHA);
59   anim2 = load_image(DATA_PREFIX "/images/title/title-anim1.png",
60                      IGNORE_ALPHA);
61
62
63   /* --- Main title loop: --- */
64
65   done = 0;
66   quit = 0;
67   show_menu = 1;
68
69   frame = 0;
70
71
72   /* Draw the title background: */
73   drawimage(title, 0, 0, NO_UPDATE);
74
75
76   /* Draw the high score: */
77   last_highscore = load_hs();
78   sprintf(str, "High score: %d", last_highscore);
79   drawcenteredtext(str, 460, letters_red, NO_UPDATE);
80
81   while (!done && !quit)
82     {
83       frame++;
84
85
86       /* Handle events: */
87
88       while (SDL_PollEvent(&event))
89         {
90           if (event.type == SDL_QUIT)
91             {
92               /* Quit event - quit: */
93
94               quit = 1;
95             }
96           else if (event.type == SDL_KEYDOWN)
97             {
98               /* Keypress... */
99
100               key = event.key.keysym.sym;
101
102               /* Check for menu events */
103               menu_event(key);
104               
105               if (key == SDLK_ESCAPE)
106                 {
107                   /* Escape: Quit: */
108
109                   quit = 1;
110                 }
111             }
112 #ifdef JOY_YES
113           else if (event.type == SDL_JOYAXISMOTION)
114             {
115               if (event.jaxis.value > 256)
116                 menuaction = MN_DOWN;
117               else
118                 menuaction = MN_UP;
119             }
120           else if (event.type == SDL_JOYBUTTONDOWN)
121             {
122               /* Joystick button: Continue: */
123
124               menuaction = MN_HIT;
125             }
126
127 #endif
128
129         }
130
131       if(menu_change)
132         {
133           /* Draw the title background: */
134
135           drawimage(title, 0, 0, NO_UPDATE);
136
137           /* Draw the high score: */
138           sprintf(str, "High score: %d", last_highscore);
139           drawcenteredtext(str, 460, letters_red, NO_UPDATE);
140         }
141
142       /* Don't draw menu, if quit is true */
143       if(show_menu && !quit)
144         quit = drawmenu();
145
146       if(game_started || level_editor_started)
147         done = 1;
148
149       /* Animate title screen: */
150
151       pict = (frame / 5) % 3;
152
153       if (pict == 0)
154         drawpart(title, 560, 270, 80, 75, NO_UPDATE);
155       else if (pict == 1)
156         drawimage(anim1, 560, 270, NO_UPDATE);
157       else if (pict == 2)
158         drawimage(anim2, 560, 270, NO_UPDATE);
159
160
161      SDL_Flip(screen);
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 }