arrays are dynamic now, fixed bugs, more code cleanups
[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
36
37 /* --- TITLE SCREEN --- */
38
39 int title(void)
40 {
41   texture_type title, anim1, anim2;
42   SDL_Event event;
43   SDLKey key;
44   int done, quit, frame, pict, last_highscore;
45   char str[80];
46
47   game_started = 0;
48   level_editor_started = 0;
49
50   /* Init menu variables */
51   initmenu();
52
53   clearscreen(0, 0, 0);
54   updatescreen();
55
56   /* Load images: */
57
58   texture_load(&title,DATA_PREFIX "/images/title/title.png", IGNORE_ALPHA);
59   texture_load(&anim1,DATA_PREFIX "/images/title/title-anim2.png", IGNORE_ALPHA);
60   texture_load(&anim2,DATA_PREFIX "/images/title/title-anim1.png", 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   texture_draw(&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, 1);
80
81   while (!done && !quit)
82     {
83       
84       frame++;
85
86
87       /* Handle events: */
88
89       while (SDL_PollEvent(&event))
90         {
91           if (event.type == SDL_QUIT)
92             {
93               /* Quit event - quit: */
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 && event.jaxis.axis == JOY_Y)
114             {
115               if (event.jaxis.value > 1024)
116                 menuaction = MN_DOWN;
117               else if (event.jaxis.value < -1024)
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(use_gl || menu_change)
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", last_highscore);
139           drawcenteredtext(str, 460, letters_red, NO_UPDATE, 1);
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         texture_draw_part(&title, 560, 270, 80, 75, NO_UPDATE);
155       else if (pict == 1)
156         texture_draw(&anim1, 560, 270, NO_UPDATE);
157       else if (pict == 2)
158         texture_draw(&anim2, 560, 270, NO_UPDATE);
159
160       flipscreen();
161
162       /* Pause: */
163
164       SDL_Delay(50);
165
166     }
167
168
169   /* Free surfaces: */
170
171   texture_free(&title);
172   texture_free(&anim1);
173   texture_free(&anim2);
174
175
176   /* Return to main! */
177
178   return(quit);
179 }