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