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