- changed way badguy removal is handled (ie. with a flag now)
[supertux.git] / src / intro.cpp
1 //  $Id$
2 //
3 //  SuperTux -  A Jump'n Run
4 //  Copyright (C) 2000 Bill Kendrick <bill@newbreedsoftware.com>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 //
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <SDL.h>
26 #include <SDL_image.h>
27
28 #include <sys/types.h>
29 #include <ctype.h>
30
31 #include "defines.h"
32 #include "globals.h"
33 #include "intro.h"
34 #include "screen.h"
35 #include "texture.h"
36 #include "timer.h"
37
38 char * intro_text[] = {
39                         "Tux and Gown were having a nice picnic..",
40                         "when suddenly...",
41                         "Gown is beamed away!!!",
42                         "This looks like a job for ---"
43                       };
44
45
46 /* --- INTRO --- */
47
48 int intro(void)
49 {
50   SDL_Event event;
51   texture_type bkgd, copter_squish,  copter_stretch, beam,
52   gown_sit,  gown_lookup,  gown_upset,
53   tux_sit, tux_upset, tux_mad;
54   texture_type copter[2];
55   SDL_Rect src, dest;
56   int done, i, quit, j, scene;
57   int * height, * height_speed;
58   Timer timer;
59
60   /* Load sprite images: */
61   texture_load(&bkgd, datadir + "/images/intro/intro.png", IGNORE_ALPHA);
62   texture_load(&gown_sit, datadir + "/images/intro/gown-sit.png", USE_ALPHA);
63   texture_load(&gown_lookup, datadir + "/images/intro/gown-lookup.png", USE_ALPHA);
64   texture_load(&gown_upset, datadir + "/images/intro/gown-upset.png", USE_ALPHA);
65   texture_load(&tux_sit, datadir + "/images/intro/tux-sit.png", USE_ALPHA);
66   texture_load(&tux_upset, datadir + "/images/intro/tux-upset.png", USE_ALPHA);
67   texture_load(&tux_mad, datadir + "/images/intro/tux-mad.png", USE_ALPHA);
68   texture_load(&copter[0], datadir + "/images/intro/copter1.png", USE_ALPHA);
69   texture_load(&copter[1], datadir + "/images/intro/copter2.png", USE_ALPHA);
70   texture_load(&copter_squish, datadir + "/images/intro/copter-squish.png", USE_ALPHA);
71   texture_load(&copter_stretch, datadir + "/images/intro/copter-stretch.png", USE_ALPHA);
72   texture_load(&beam, datadir + "/images/intro/beam.png", USE_ALPHA);
73
74   /* Allocate buffer for height array: */
75
76   height = (int*) malloc(sizeof(int) * (gown_upset.w));
77   height_speed = (int*) malloc(sizeof(int) * (gown_upset.w));
78
79
80   /* Initialize height arrays: */
81
82   for (j = 0; j < (gown_upset.w); j++)
83     {
84       height[j] = 400;
85       height_speed[j] = (rand() % 10) + 1;
86     }
87
88   /* Display background: */
89
90   texture_draw_bg(&bkgd, 255, UPDATE);
91
92   /* Animation: */
93
94   done = 0;
95   quit = 0;
96   scene = 0;
97   i = 0;
98
99   timer.init(false);
100   timer.start(10000);
101
102   while (timer.check() && !done && !quit)
103     {
104
105
106       done = wait_for_event(event,0);
107       if(done == 2)
108         {
109           quit = 1;
110           done = 0;
111         }
112
113
114       /* Display background: */
115
116       /* Draw things: */
117
118       if (timer.get_gone() < 2000 && scene == 0)
119         {
120           ++scene;
121           /* Gown and tux sitting: */
122
123           texture_draw(&tux_sit, 270, 400, 255, UPDATE);
124           texture_draw(&gown_sit, 320, 400, 255, UPDATE);
125
126           text_drawf(&white_text, intro_text[0], 0, -8, A_HMIDDLE, A_BOTTOM, 0);
127         }
128
129
130       if (timer.get_gone() >= 2000 && scene == 1)
131         {
132           ++scene;
133           /* Helicopter begins to fly in: */
134
135           erasecenteredtext(&white_text, intro_text[0], 454, &bkgd, NO_UPDATE, 1);
136           text_drawf(&white_text, intro_text[1], 0,-8, A_HMIDDLE, A_BOTTOM, 0);
137         }
138
139
140       if (timer.get_gone() >= 2000 && timer.get_gone() < 4000)
141         {
142           /* Helicopter flying in: */
143           texture_draw_part(&bkgd,0,32, 0, 32, screen->w, (copter[0].h));
144
145           texture_draw(&copter[i % 2],
146                        (float)(timer.get_gone() - 2000) / 5  - (copter[0].w), 32);
147
148           update_rect(screen, 0, 32, screen->w, (copter[0].h));
149         }
150
151
152       if (timer.get_gone() >= 2500 && scene == 2)
153         {
154           ++scene;
155           /* Gown notices something... */
156
157           texture_draw(&gown_lookup, 320, 400, 255, UPDATE);
158         }
159
160
161       if (timer.get_gone() >= 3500 && scene == 3)
162         {
163           ++scene;
164           /* Gown realizes it's bad! */
165           
166           texture_draw(&gown_upset, 320, 400, 255, UPDATE);
167         }
168
169
170       if (timer.get_gone() >= 4000 && timer.get_gone() < 8000)
171         {
172           /* Helicopter sits: */
173           texture_draw_part(&bkgd,0,32, 0, 32, screen->w, (copter[0].h));
174
175           texture_draw(&copter[i % 2], 400 - (copter[0].w), 32);
176           update_rect(screen, 0, 32, screen->w, (copter[0].h));
177         }
178
179
180       if (timer.get_gone() >= 5000 && scene == 4)
181         {
182           ++scene;
183           /* Tux realizes something's happening: */
184
185           texture_draw(&tux_upset, 270, 400, 255, UPDATE);
186
187
188           erasecenteredtext(&white_text, intro_text[1], 454, &bkgd, UPDATE, 1);
189           text_drawf(&white_text, intro_text[2], 0,-8, A_HMIDDLE, A_BOTTOM, 0);
190         }
191
192
193       if (timer.get_gone() >= 5000 && timer.get_gone() <= 8000)
194         {
195           /* Beam gown up! */
196
197           texture_draw_part(&bkgd,
198                             310, 32 + (copter[0].h), 310,
199                             32 + (copter[0].h),
200                             (gown_upset.w) + 20,
201                             376 + (gown_upset.h) - (copter[0].h));
202
203
204           for (j = 0; j < (gown_upset.sdl_surface -> w); j++)
205             {
206               texture_draw(&beam, 320 + j - ((beam.w) / 2), height[j]);
207
208               src.x = j;
209               src.y = 0;
210               src.w = 1;
211               src.h = (gown_upset.h);
212
213               dest.x = 320 + j;
214               dest.y = height[j];
215               dest.w = src.w;
216               dest.h = src.h;
217
218               texture_draw_part(&gown_upset,src.x,src.y,dest.x,dest.y,dest.w,dest.h);
219
220               height[j] = 400 + rand() % 10 - (int)(300. * ((float)(timer.get_gone() - 5000)/(float)3000.));
221               if(height[j] < 105)
222                 height[j] = 105;
223             }
224
225           update_rect(screen,
226                       310,
227                       32 + (copter[0].h),
228                       (gown_upset.w) + 20,
229                       400 + (gown_upset.h) - (copter[0].h));
230         }
231
232
233       if (timer.get_gone() >= 8000 && scene == 5)
234         {
235           texture_draw_part(&bkgd,
236                             310, 32 + (copter[0].h), 310,
237                             32 + (copter[0].h),
238                             (gown_upset.w) + 20,
239                             368 + (gown_upset.h) - (copter[0].h));
240
241           ++scene;
242           /* Tux gets mad! */
243
244           texture_draw(&tux_mad, 270, 400, UPDATE);
245
246           erasecenteredtext(&white_text, intro_text[2], 454, &bkgd, UPDATE, 1);
247           text_drawf(&white_text, intro_text[3], 0,-8, A_HMIDDLE, A_BOTTOM, 0);
248         }
249
250
251       if (timer.get_gone() >= 8000 && timer.get_gone() <= 8250)
252         {
253           /* Helicopter starting to speed off: */
254
255           texture_draw_part(&bkgd, 0, 32, 0, 32, screen->w, (copter_squish.h));
256
257           texture_draw(&copter_squish,
258                        400 - (copter[0].w), 32);
259
260           update_rect(screen, 0, 32, screen->w, (copter_squish.h));
261         }
262
263
264       if (timer.get_gone() >= 8250)
265         {
266           /* Helicopter speeding off: */
267
268           texture_draw_part(&bkgd, 0, 32, 0, 32, screen->w, (copter_stretch.h));
269
270           texture_draw(&copter_stretch,
271                        (timer.get_gone() - 8250) /*(i - (8250 / FPS)) * 30*/ + 400 - (copter[0].w),
272                        32);
273
274           update_rect(screen, 0, 32, screen->w, (copter_stretch.h));
275         }
276
277       flipscreen();
278
279       ++i;
280       /* Pause: */
281       SDL_Delay(20);
282     }
283
284
285   /* Free surfaces: */
286
287   delete bkgd;
288   delete gown_sit;
289   delete gown_lookup;
290   delete gown_upset;
291   delete tux_sit;
292   delete tux_upset;
293   delete tux_mad;
294   delete copter[0];
295   delete copter[1];
296   delete copter_squish;
297   delete copter_stretch;
298   delete beam;
299
300
301   /* Free array buffers: */
302
303   free(height);
304   free(height_speed);
305
306
307   /* Return to main! */
308
309   return(quit);
310 }