2e106acb4f6c7a53eba097e180b3617e17e2a08c
[supertux.git] / src / intro.c
1 /*
2   intro.c
3   
4   Super Tux - Intro Screen
5   
6   by Bill Kendrick
7   bill@newbreedsoftware.com
8   http://www.newbreedsoftware.com/supertux/
9   
10   April 11, 2000 - April 23, 2000
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 "intro.h"
30 #include "screen.h"
31 #include "texture.h"
32 #include "timer.h"
33
34 char * intro_text[] = {
35   "Tux and Gown were having a nice picnic..",
36   "when suddenly...",
37   "Gown is beamed away!!!",
38   "This looks like a job for ---"
39 };
40
41
42 /* --- INTRO --- */
43
44 int intro(void)
45 {
46   SDL_Event event;
47   texture_type bkgd,  copter_squish,  copter_stretch, beam,
48      gown_sit,  gown_lookup,  gown_upset,
49      tux_sit, tux_upset, tux_mad;
50   texture_type copter[2];
51   SDL_Rect src, dest;
52   int done, i, quit, j, scene;
53   int * height, * height_speed;
54   timer_type timer;
55   
56   
57   /* Load sprite images: */
58
59   texture_load(&bkgd, DATA_PREFIX "/images/intro/intro.png", IGNORE_ALPHA);
60   texture_load(&gown_sit, DATA_PREFIX "/images/intro/gown-sit.png", USE_ALPHA);
61   texture_load(&gown_lookup, DATA_PREFIX "/images/intro/gown-lookup.png", USE_ALPHA);
62   texture_load(&gown_upset, DATA_PREFIX "/images/intro/gown-upset.png", USE_ALPHA);
63   texture_load(&tux_sit, DATA_PREFIX "/images/intro/tux-sit.png", USE_ALPHA);
64   texture_load(&tux_upset, DATA_PREFIX "/images/intro/tux-upset.png", USE_ALPHA);
65   texture_load(&tux_mad, DATA_PREFIX "/images/intro/tux-mad.png", USE_ALPHA);
66   texture_load(&copter[0], DATA_PREFIX "/images/intro/copter1.png", USE_ALPHA);
67   texture_load(&copter[1], DATA_PREFIX "/images/intro/copter2.png", USE_ALPHA); 
68   texture_load(&copter_squish, DATA_PREFIX "/images/intro/copter-squish.png", USE_ALPHA); 
69   texture_load(&copter_stretch, DATA_PREFIX "/images/intro/copter-stretch.png", USE_ALPHA); 
70   texture_load(&beam, DATA_PREFIX "/images/intro/beam.png", USE_ALPHA); 
71   
72   /* Allocate buffer for height array: */
73   
74   height = malloc(sizeof(int) * (gown_upset.w));
75   height_speed = malloc(sizeof(int) * (gown_upset.w));
76   
77   
78   /* Initialize height arrays: */
79   
80   for (j = 0; j < (gown_upset.w); j++)
81     {
82       height[j] = 400;
83       height_speed[j] = (rand() % 10) + 1;
84     }
85   
86         /* Display background: */
87   
88   texture_draw_bg(&bkgd, UPDATE);
89   
90   /* Animation: */
91   
92   done = 0;
93   quit = 0;
94   scene = 0;
95   i = 0;
96   
97   timer_start(&timer,10000);
98   
99   while (timer_check(&timer) && !done && !quit)
100     {
101       
102       /* Handle events: */
103       
104       while (SDL_PollEvent(&event))
105         {
106           if (event.type == SDL_QUIT)
107             {
108               /* Quit event - quit: */
109               
110               quit = 1;
111             }
112           else if (event.type == SDL_KEYDOWN)
113             {
114               /* Keypress - skip intro: */
115               
116               done = 1;
117             }
118 #ifdef JOY_YES
119           else if (event.type == SDL_JOYBUTTONDOWN)
120             {
121               /* Fire button - skip intro: */
122               
123               done = 1;
124             }
125 #endif
126         }
127       
128       
129               /* Display background: */
130   
131       /* Draw things: */
132       
133       if (timer_get_gone(&timer) < 2000 && scene == 0)
134         {
135           ++scene;
136           /* Gown and tux sitting: */
137           
138           texture_draw(&tux_sit, 270, 400, UPDATE);
139           texture_draw(&gown_sit, 320, 400, UPDATE);
140           
141           drawcenteredtext(intro_text[0], 456, letters_blue, UPDATE, 1);
142         }
143       
144       
145       if (timer_get_gone(&timer) >= 2000 && scene == 1)
146         {
147           ++scene;
148           /* Helicopter begins to fly in: */
149           
150           erasecenteredtext(intro_text[0], 456, bkgd.sdl_surface, NO_UPDATE, 1);
151           drawcenteredtext(intro_text[1], 456, letters_red, NO_UPDATE, 1);
152         }
153
154       
155       if (timer_get_gone(&timer) >= 2000 && timer_get_gone(&timer) < 4000)
156         {
157           /* Helicopter flying in: */
158           texture_draw_part(&bkgd, 0, 32, screen->w, (copter[0].h), NO_UPDATE);
159           
160           texture_draw(&copter[i % 2],
161                     (float)(timer_get_gone(&timer) - 2000) / 5  - (copter[0].w), 32,
162                     NO_UPDATE);
163
164           update_rect(screen, 0, 32, screen->w, (copter[0].h));
165         }
166
167       
168       if (timer_get_gone(&timer) >= 2500 && scene == 2)
169         {
170         ++scene;
171           /* Gown notices something... */
172           
173           texture_draw(&gown_lookup, 320, 400, UPDATE);
174         }
175
176       
177       if (timer_get_gone(&timer) >= 3500 && scene == 3)
178         {
179         ++scene;
180           /* Gown realizes it's bad! */
181           
182           texture_draw(&gown_upset, 320, 400, UPDATE);
183         }
184
185       
186       if (timer_get_gone(&timer) >= 4000 && timer_get_gone(&timer) < 8000)
187         {
188           /* Helicopter sits: */
189           texture_draw_part(&bkgd, 0, 32, screen->w, (copter[0].h), NO_UPDATE);
190           
191           texture_draw(&copter[i % 2], 400 - (copter[0].w), 32, NO_UPDATE);
192           update_rect(screen, 0, 32, screen->w, (copter[0].h));
193         }
194
195       
196       if (timer_get_gone(&timer) >= 5000 && scene == 4)
197         {
198         ++scene;
199           /* Tux realizes something's happening: */
200           
201           texture_draw(&tux_upset, 270, 400, UPDATE);
202           
203           
204           erasecenteredtext(intro_text[1], 456, bkgd.sdl_surface, UPDATE, 1);
205           drawcenteredtext(intro_text[2], 456, letters_red, UPDATE, 1);
206         }
207       
208       
209       if (timer_get_gone(&timer) >= 5000)
210         {
211           /* Beam gown up! */
212           
213           texture_draw_part(&bkgd,
214                    320,
215                    32 + (copter[0].h),
216                    (gown_upset.w),
217                    368 + (gown_upset.h) - (copter[0].h), NO_UPDATE);
218           
219           
220           for (j = 0; j < (gown_upset.sdl_surface -> w); j++)
221             {
222               texture_draw(&beam, 320 + j - ((beam.w) / 2), height[j],
223                         NO_UPDATE);
224               
225               src.x = j;
226               src.y = 0;
227               src.w = 1;
228               src.h = (gown_upset.h);
229               
230               dest.x = 320 + j;
231               dest.y = height[j];
232               dest.w = src.w;
233               dest.h = src.h;
234               
235               SDL_BlitSurface(gown_upset.sdl_surface, &src, screen, &dest);
236               
237               height[j] = height[j] - height_speed[j];
238               
239               if ((i % 2) == 0)
240                 height_speed[j]++;
241             }
242
243           update_rect(screen,
244                          320,
245                          32 + (copter[0].h),
246                          (gown_upset.w),
247                          400 + (gown_upset.h) - (copter[0].h));
248         }
249       
250       
251       if (timer_get_gone(&timer) >= 8000 && scene == 5)
252         {
253         ++scene;
254           /* Tux gets mad! */
255           
256           texture_draw(&tux_mad, 270, 400, UPDATE);
257           
258           erasecenteredtext(intro_text[2], 456, bkgd.sdl_surface, UPDATE, 1);
259           drawcenteredtext(intro_text[3], 456, letters_gold, UPDATE, 1);
260         }
261       
262       
263       if (timer_get_gone(&timer) >= 8000 && timer_get_gone(&timer) <= 8250)
264         {
265           /* Helicopter starting to speed off: */
266           
267           texture_draw_part(&bkgd, 0, 32, screen->w, (copter_squish.h), NO_UPDATE);
268           
269           texture_draw(&copter_squish,
270                     400 - (copter[0].w), 32,
271                     NO_UPDATE);
272
273           update_rect(screen, 0, 32, screen->w, (copter_squish.h));
274         }      
275
276
277       if (timer_get_gone(&timer) >= 8250)
278         {
279           /* Helicopter speeding off: */
280           
281           texture_draw_part(&bkgd, 0, 32, screen->w, (copter_stretch.h), NO_UPDATE);
282           
283           texture_draw(&copter_stretch,
284                     (timer_get_gone(&timer) - 8250) /*(i - (8250 / FPS)) * 30*/ + 400 - (copter[0].w),
285                     32,
286                     NO_UPDATE);
287                     
288           update_rect(screen, 0, 32, screen->w, (copter_stretch.h));
289         }      
290         
291         flipscreen();
292
293       ++i;
294       /* Pause: */
295       SDL_Delay(20);
296     }
297
298   
299   /* Free surfaces: */
300   
301   texture_free(&bkgd);
302   texture_free(&gown_sit);
303   texture_free(&gown_lookup);
304   texture_free(&gown_upset);
305   texture_free(&tux_sit);
306   texture_free(&tux_upset);
307   texture_free(&tux_mad);
308   texture_free(&copter[0]);
309   texture_free(&copter[1]);
310   texture_free(&copter_squish);
311   texture_free(&copter_stretch);
312   texture_free(&beam);
313   
314   
315   /* Free array buffers: */
316   
317   free(height);
318   free(height_speed);
319   
320   
321   /* Return to main! */
322   
323   return(quit);
324 }