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