moved savelevel() code to level.h/c where it belongs to. :)
[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;
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   load_hs();
78   sprintf(str, "High score: %d", hs_score);
79   text_drawf(&gold_text, str, 0, -40, A_HMIDDLE, A_BOTTOM, 1, NO_UPDATE);
80   sprintf(str, "by %s", hs_name);
81   text_drawf(&gold_text, str, 0, -20, A_HMIDDLE, A_BOTTOM, 1, NO_UPDATE);
82
83   while (!done && !quit)
84     {
85       
86       frame++;
87
88
89       /* Handle events: */
90
91       while (SDL_PollEvent(&event))
92         {
93           if (event.type == SDL_QUIT)
94             {
95               /* Quit event - quit: */
96               quit = 1;
97             }
98           else if (event.type == SDL_KEYDOWN)
99             {
100               /* Keypress... */
101
102               key = event.key.keysym.sym;
103
104               /* Check for menu events */
105               menu_event(&event.key.keysym);
106
107               if (key == SDLK_ESCAPE)
108                 {
109                   /* Escape: Quit: */
110
111                   quit = 1;
112                 }
113             }
114 #ifdef JOY_YES
115           else if (event.type == SDL_JOYAXISMOTION && event.jaxis.axis == JOY_Y)
116             {
117               if (event.jaxis.value > 1024)
118                 menuaction = MN_DOWN;
119               else if (event.jaxis.value < -1024)
120                 menuaction = MN_UP;
121             }
122           else if (event.type == SDL_JOYBUTTONDOWN)
123             {
124               /* Joystick button: Continue: */
125
126               menuaction = MN_HIT;
127             }
128
129 #endif
130
131         }
132
133       if(use_gl || menu_change)
134         {
135           /* Draw the title background: */
136
137           texture_draw_bg(&title, NO_UPDATE);
138
139           /* Draw the high score: */
140           sprintf(str, "High score: %d", hs_score);
141             text_drawf(&gold_text, str, 0, -40, A_HMIDDLE, A_BOTTOM, 1, NO_UPDATE);
142   sprintf(str, "by %s", hs_name);
143   text_drawf(&gold_text, str, 0, -20, A_HMIDDLE, A_BOTTOM, 1, NO_UPDATE);
144   
145         }
146
147       /* Don't draw menu, if quit is true */
148       if(show_menu && !quit)
149         quit = drawmenu();
150
151       if(game_started || level_editor_started)
152         done = 1;
153
154       /* Animate title screen: */
155
156       pict = (frame / 5) % 3;
157
158       if (pict == 0)
159         texture_draw_part(&title, 560, 270, 560, 270, 80, 75, NO_UPDATE);
160       else if (pict == 1)
161         texture_draw(&anim1, 560, 270, NO_UPDATE);
162       else if (pict == 2)
163         texture_draw(&anim2, 560, 270, NO_UPDATE);
164         
165       flipscreen();
166
167       /* Pause: */
168
169       SDL_Delay(50);
170
171     }
172
173
174   /* Free surfaces: */
175
176   texture_free(&title);
177   texture_free(&anim1);
178   texture_free(&anim2);
179
180
181   /* Return to main! */
182
183   return(quit);
184 }