a88146dc27bb40ab4057f185016f00e5d69f3482
[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 #ifdef JOY_YES
117           else if (event.type == SDL_JOYBUTTONDOWN)
118             {
119               /* Fire button - skip intro: */
120               
121               done = 1;
122             }
123 #endif
124         }
125       
126       
127               /* Display background: */
128   
129       /* Draw things: */
130       
131       if (timer_get_gone(&timer) < 2000 && scene == 0)
132         {
133           ++scene;
134           /* Gown and tux sitting: */
135           
136           texture_draw(&tux_sit, 270, 400, UPDATE);
137           texture_draw(&gown_sit, 320, 400, UPDATE);
138           
139           text_drawf(&white_text, intro_text[0], 0, -8, A_HMIDDLE, A_BOTTOM, 0, NO_UPDATE);
140         }
141       
142       
143       if (timer_get_gone(&timer) >= 2000 && scene == 1)
144         {
145           ++scene;
146           /* Helicopter begins to fly in: */
147           
148           erasecenteredtext(&white_text, intro_text[0], 454, &bkgd, NO_UPDATE, 1);
149           text_drawf(&white_text, intro_text[1], 0,-8, A_HMIDDLE, A_BOTTOM, 0, NO_UPDATE);
150         }
151
152       
153       if (timer_get_gone(&timer) >= 2000 && timer_get_gone(&timer) < 4000)
154         {
155           /* Helicopter flying in: */
156           texture_draw_part(&bkgd,0,32, 0, 32, screen->w, (copter[0].h), NO_UPDATE);
157           
158           texture_draw(&copter[i % 2],
159                     (float)(timer_get_gone(&timer) - 2000) / 5  - (copter[0].w), 32,
160                     NO_UPDATE);
161
162           update_rect(screen, 0, 32, screen->w, (copter[0].h));
163         }
164
165       
166       if (timer_get_gone(&timer) >= 2500 && scene == 2)
167         {
168         ++scene;
169           /* Gown notices something... */
170           
171           texture_draw(&gown_lookup, 320, 400, UPDATE);
172         }
173
174       
175       if (timer_get_gone(&timer) >= 3500 && scene == 3)
176         {
177         ++scene;
178           /* Gown realizes it's bad! */
179           
180           texture_draw(&gown_upset, 320, 400, UPDATE);
181         }
182
183       
184       if (timer_get_gone(&timer) >= 4000 && timer_get_gone(&timer) < 8000)
185         {
186           /* Helicopter sits: */
187           texture_draw_part(&bkgd,0,32, 0, 32, screen->w, (copter[0].h), NO_UPDATE);
188           
189           texture_draw(&copter[i % 2], 400 - (copter[0].w), 32, NO_UPDATE);
190           update_rect(screen, 0, 32, screen->w, (copter[0].h));
191         }
192
193       
194       if (timer_get_gone(&timer) >= 5000 && scene == 4)
195         {
196         ++scene;
197           /* Tux realizes something's happening: */
198           
199           texture_draw(&tux_upset, 270, 400, UPDATE);
200           
201           
202           erasecenteredtext(&white_text, intro_text[1], 454, &bkgd, UPDATE, 1);
203           text_drawf(&white_text, intro_text[2], 0,-8, A_HMIDDLE, A_BOTTOM, 0, NO_UPDATE);
204         }
205       
206       
207       if (timer_get_gone(&timer) >= 5000 && timer_get_gone(&timer) <= 8000)
208         {
209           /* Beam gown up! */
210           
211           texture_draw_part(&bkgd,
212                    310, 32 + (copter[0].h), 310,
213                    32 + (copter[0].h),
214                    (gown_upset.w) + 20,
215                    376 + (gown_upset.h) - (copter[0].h), NO_UPDATE);
216           
217           
218           for (j = 0; j < (gown_upset.sdl_surface -> w); j++)
219             {
220               texture_draw(&beam, 320 + j - ((beam.w) / 2), height[j],
221                         NO_UPDATE);
222               
223               src.x = j;
224               src.y = 0;
225               src.w = 1;
226               src.h = (gown_upset.h);
227               
228               dest.x = 320 + j;
229               dest.y = height[j];
230               dest.w = src.w;
231               dest.h = src.h;
232               
233               texture_draw_part(&gown_upset,src.x,src.y,dest.x,dest.y,dest.w,dest.h,NO_UPDATE);
234               
235               height[j] = 400 + rand() % 10 - (int)(300. * ((float)(timer_get_gone(&timer) - 5000)/(float)3000.));
236               if(height[j] < 105)
237               height[j] = 105;
238             }
239
240           update_rect(screen,
241                          310,
242                          32 + (copter[0].h),
243                          (gown_upset.w) + 20,
244                          400 + (gown_upset.h) - (copter[0].h));
245         }
246       
247       
248       if (timer_get_gone(&timer) >= 8000 && scene == 5)
249         {
250                   texture_draw_part(&bkgd,
251                    310, 32 + (copter[0].h), 310,
252                    32 + (copter[0].h),
253                    (gown_upset.w) + 20,
254                    368 + (gown_upset.h) - (copter[0].h), NO_UPDATE);
255         
256         ++scene;
257           /* Tux gets mad! */
258           
259           texture_draw(&tux_mad, 270, 400, UPDATE);
260           
261           erasecenteredtext(&white_text, intro_text[2], 454, &bkgd, UPDATE, 1);
262           text_drawf(&white_text, intro_text[3], 0,-8, A_HMIDDLE, A_BOTTOM, 0, NO_UPDATE);
263         }
264       
265       
266       if (timer_get_gone(&timer) >= 8000 && timer_get_gone(&timer) <= 8250)
267         {
268           /* Helicopter starting to speed off: */
269           
270           texture_draw_part(&bkgd, 0, 32, 0, 32, screen->w, (copter_squish.h), NO_UPDATE);
271           
272           texture_draw(&copter_squish,
273                     400 - (copter[0].w), 32,
274                     NO_UPDATE);
275
276           update_rect(screen, 0, 32, screen->w, (copter_squish.h));
277         }      
278
279
280       if (timer_get_gone(&timer) >= 8250)
281         {
282           /* Helicopter speeding off: */
283           
284           texture_draw_part(&bkgd, 0, 32, 0, 32, screen->w, (copter_stretch.h), NO_UPDATE);
285           
286           texture_draw(&copter_stretch,
287                     (timer_get_gone(&timer) - 8250) /*(i - (8250 / FPS)) * 30*/ + 400 - (copter[0].w),
288                     32,
289                     NO_UPDATE);
290                     
291           update_rect(screen, 0, 32, screen->w, (copter_stretch.h));
292         }      
293         
294         flipscreen();
295
296       ++i;
297       /* Pause: */
298       SDL_Delay(20);
299     }
300
301   
302   /* Free surfaces: */
303   
304   texture_free(&bkgd);
305   texture_free(&gown_sit);
306   texture_free(&gown_lookup);
307   texture_free(&gown_upset);
308   texture_free(&tux_sit);
309   texture_free(&tux_upset);
310   texture_free(&tux_mad);
311   texture_free(&copter[0]);
312   texture_free(&copter[1]);
313   texture_free(&copter_squish);
314   texture_free(&copter_stretch);
315   texture_free(&beam);
316   
317   
318   /* Free array buffers: */
319   
320   free(height);
321   free(height_speed);
322   
323   
324   /* Return to main! */
325   
326   return(quit);
327 }