Added high score code by Adam Czachorowski.
[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 #ifndef NOSOUND
22 #include <SDL_mixer.h>
23 #endif
24
25 #ifdef LINUX
26 #include <pwd.h>
27 #include <sys/types.h>
28 #include <ctype.h>
29 #endif
30
31 #include "defines.h"
32 #include "globals.h"
33 #include "title.h"
34 #include "screen.h"
35 #include "high_scores.h"
36
37
38 /* --- TITLE SCREEN --- */
39
40 int title(void)
41 {
42   SDL_Surface * title, * anim1, * anim2;
43   SDL_Event event;
44   SDLKey key;
45   int done, quit, frame, pict;
46   char str[80];
47   
48   
49   /* Clear screen: */
50   
51   clearscreen(0, 0, 0);
52   updatescreen();
53   
54   
55   /* Load images: */
56   
57   title = load_image(DATA_PREFIX "/images/title/title.png", IGNORE_ALPHA);
58   anim1 = load_image(DATA_PREFIX "/images/title/title-anim2.png",
59                      IGNORE_ALPHA);
60   anim2 = load_image(DATA_PREFIX "/images/title/title-anim1.png",
61                      IGNORE_ALPHA);
62   
63   
64   /* Draw the title background: */
65   
66   drawimage(title, 0, 0, UPDATE);
67
68
69   /* Draw the high score: */
70
71   sprintf(str, "High score: %d", load_hs());
72   drawcenteredtext(str, 460, letters_red, UPDATE);
73   
74   
75   /* --- Main title loop: --- */
76   
77   done = 0;
78   quit = 0;
79   
80   frame = 0;
81   
82   do
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               
95               quit = 1;
96             }
97           else if (event.type == SDL_KEYDOWN)
98             {
99               /* Keypress... */
100               
101               key = event.key.keysym.sym;
102               
103               if (key == SDLK_ESCAPE)
104                 {
105                   /* Escape: Quit: */
106                   
107                   quit = 1;
108                 }
109               else if (key == SDLK_SPACE || key == SDLK_RETURN)
110                 {
111                   /* Space / Return: Continue: */
112                   
113                   done = 1;
114                 }
115             }
116 #ifdef JOY_YES
117           else if (event.type == SDL_JOYBUTTONDOWN)
118             {
119               /* Joystick button: Continue: */
120               
121               done = 1;
122             }
123 #endif
124         }
125       
126       
127       /* Animate title screen: */
128       
129       pict = (frame / 5) % 3;
130       
131       if (pict == 0)
132         drawpart(title, 560, 270, 80, 75, UPDATE);
133       else if (pict == 1)
134         drawimage(anim1, 560, 270, UPDATE);
135       else if (pict == 2)
136         drawimage(anim2, 560, 270, UPDATE);
137       
138       
139       /* Pause: */
140       
141       SDL_Delay(50);
142     }
143   while (!done && !quit);
144   
145   
146   /* Free surfaces: */
147   
148   SDL_FreeSurface(title);
149   SDL_FreeSurface(anim1);
150   SDL_FreeSurface(anim2);
151   
152   
153   /* Return to main! */
154   
155   return(quit);
156 }