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