little info-file format change
[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 <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   /* Load sprite images: */
57   texture_load(&bkgd, DATA_PREFIX "/images/intro/intro.png", IGNORE_ALPHA);  
58   texture_load(&gown_sit, DATA_PREFIX "/images/intro/gown-sit.png", USE_ALPHA);
59   texture_load(&gown_lookup, DATA_PREFIX "/images/intro/gown-lookup.png", USE_ALPHA);
60   texture_load(&gown_upset, DATA_PREFIX "/images/intro/gown-upset.png", USE_ALPHA);
61   texture_load(&tux_sit, DATA_PREFIX "/images/intro/tux-sit.png", USE_ALPHA);
62   texture_load(&tux_upset, DATA_PREFIX "/images/intro/tux-upset.png", USE_ALPHA);
63   texture_load(&tux_mad, DATA_PREFIX "/images/intro/tux-mad.png", USE_ALPHA);
64   texture_load(&copter[0], DATA_PREFIX "/images/intro/copter1.png", USE_ALPHA);
65   texture_load(&copter[1], DATA_PREFIX "/images/intro/copter2.png", USE_ALPHA); 
66   texture_load(&copter_squish, DATA_PREFIX "/images/intro/copter-squish.png", USE_ALPHA); 
67   texture_load(&copter_stretch, DATA_PREFIX "/images/intro/copter-stretch.png", USE_ALPHA); 
68   texture_load(&beam, DATA_PREFIX "/images/intro/beam.png", USE_ALPHA); 
69   
70   /* Allocate buffer for height array: */
71   
72   height = (int*) malloc(sizeof(int) * (gown_upset.w));
73   height_speed = (int*) malloc(sizeof(int) * (gown_upset.w));
74   
75   
76   /* Initialize height arrays: */
77   
78   for (j = 0; j < (gown_upset.w); j++)
79     {
80       height[j] = 400;
81       height_speed[j] = (rand() % 10) + 1;
82     }
83   
84         /* Display background: */
85   
86   texture_draw_bg(&bkgd, UPDATE);
87   
88   /* Animation: */
89   
90   done = 0;
91   quit = 0;
92   scene = 0;
93   i = 0;
94   
95   timer_init(&timer,NO);
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)
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                    376 + (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
241           update_rect(screen,
242                          310,
243                          32 + (copter[0].h),
244                          (gown_upset.w) + 20,
245                          400 + (gown_upset.h) - (copter[0].h));
246         }
247       
248       
249       if (timer_get_gone(&timer) >= 8000 && scene == 5)
250         {
251                   texture_draw_part(&bkgd,
252                    310, 32 + (copter[0].h), 310,
253                    32 + (copter[0].h),
254                    (gown_upset.w) + 20,
255                    368 + (gown_upset.h) - (copter[0].h), NO_UPDATE);
256         
257         ++scene;
258           /* Tux gets mad! */
259           
260           texture_draw(&tux_mad, 270, 400, UPDATE);
261           
262           erasecenteredtext(&white_text, intro_text[2], 454, &bkgd, UPDATE, 1);
263           text_drawf(&white_text, intro_text[3], 0,-8, A_HMIDDLE, A_BOTTOM, 0, NO_UPDATE);
264         }
265       
266       
267       if (timer_get_gone(&timer) >= 8000 && timer_get_gone(&timer) <= 8250)
268         {
269           /* Helicopter starting to speed off: */
270           
271           texture_draw_part(&bkgd, 0, 32, 0, 32, screen->w, (copter_squish.h), NO_UPDATE);
272           
273           texture_draw(&copter_squish,
274                     400 - (copter[0].w), 32,
275                     NO_UPDATE);
276
277           update_rect(screen, 0, 32, screen->w, (copter_squish.h));
278         }      
279
280
281       if (timer_get_gone(&timer) >= 8250)
282         {
283           /* Helicopter speeding off: */
284           
285           texture_draw_part(&bkgd, 0, 32, 0, 32, screen->w, (copter_stretch.h), NO_UPDATE);
286           
287           texture_draw(&copter_stretch,
288                     (timer_get_gone(&timer) - 8250) /*(i - (8250 / FPS)) * 30*/ + 400 - (copter[0].w),
289                     32,
290                     NO_UPDATE);
291                     
292           update_rect(screen, 0, 32, screen->w, (copter_stretch.h));
293         }      
294         
295         flipscreen();
296
297       ++i;
298       /* Pause: */
299       SDL_Delay(20);
300     }
301
302   
303   /* Free surfaces: */
304   
305   texture_free(&bkgd);
306   texture_free(&gown_sit);
307   texture_free(&gown_lookup);
308   texture_free(&gown_upset);
309   texture_free(&tux_sit);
310   texture_free(&tux_upset);
311   texture_free(&tux_mad);
312   texture_free(&copter[0]);
313   texture_free(&copter[1]);
314   texture_free(&copter_squish);
315   texture_free(&copter_stretch);
316   texture_free(&beam);
317   
318   
319   /* Free array buffers: */
320   
321   free(height);
322   free(height_speed);
323   
324   
325   /* Return to main! */
326   
327   return(quit);
328 }