- fixed joystick support a bit for analog joysticks, menu is still not really useable...
[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 "type.h"
34
35
36 /* --- TITLE SCREEN --- */
37
38 int title(void)
39 {
40   texture_type title, anim1, anim2;
41   SDL_Event event;
42   SDLKey key;
43   int done, quit, frame, pict, last_highscore;
44   char str[80];
45
46   game_started = 0;
47   level_editor_started = 0;
48
49   /* Init menu variables */
50   initmenu();
51
52   clearscreen(0, 0, 0);
53   updatescreen();
54
55   /* Load images: */
56
57   texture_load(&title,DATA_PREFIX "/images/title/title.png", IGNORE_ALPHA);
58   texture_load(&anim1,DATA_PREFIX "/images/title/title-anim2.png", IGNORE_ALPHA);
59   texture_load(&anim2,DATA_PREFIX "/images/title/title-anim1.png", 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   texture_draw(&title, 0, 0, NO_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, NO_UPDATE, 1);
79
80   while (!done && !quit)
81     {
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               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 && event.jaxis.axis == JOY_Y)
113             {
114               if (event.jaxis.value > 1024)
115                 menuaction = MN_DOWN;
116               else if (event.jaxis.value < -1024)
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(use_gl || menu_change)
131         {
132           /* Draw the title background: */
133
134           texture_draw_bg(&title, NO_UPDATE);
135
136           /* Draw the high score: */
137           sprintf(str, "High score: %d", last_highscore);
138           drawcenteredtext(str, 460, letters_red, NO_UPDATE, 1);
139         }
140
141       /* Don't draw menu, if quit is true */
142       if(show_menu && !quit)
143         quit = drawmenu();
144
145       if(game_started || level_editor_started)
146         done = 1;
147
148       /* Animate title screen: */
149
150       pict = (frame / 5) % 3;
151
152       if (pict == 0)
153         texture_draw_part(&title, 560, 270, 80, 75, NO_UPDATE);
154       else if (pict == 1)
155         texture_draw(&anim1, 560, 270, NO_UPDATE);
156       else if (pict == 2)
157         texture_draw(&anim2, 560, 270, NO_UPDATE);
158
159       flipscreen();
160
161       /* Pause: */
162
163       SDL_Delay(50);
164
165     }
166
167
168   /* Free surfaces: */
169
170   texture_free(&title);
171   texture_free(&anim1);
172   texture_free(&anim2);
173
174
175   /* Return to main! */
176
177   return(quit);
178 }