1 /* intro.c */
2 
3 # include "intro.h"
4 
game_intro(SDL_Surface * screen,uint * state,uint * level)5 void game_intro (SDL_Surface *screen, uint *state, uint *level) {
6 
7 	SDL_Surface *blackbox = NULL;
8 	SDL_Surface *karoshi = NULL;
9 	SDL_Surface *window = NULL;
10 	SDL_Surface *temp = NULL;
11 	SDL_Surface *doble = NULL;
12 	SDL_Surface *blocks = NULL;
13 	SDL_Surface *startscreen = NULL;
14 	SDL_Surface *startinfo = NULL;
15 	SDL_Surface *menu = NULL;
16 	SDL_Surface *arrow = NULL;
17 	SDL_Surface *passwords = NULL;
18 	SDL_Surface *fonts = NULL;
19 	Mix_Chunk *start = NULL;
20 	Mix_Chunk *poff = NULL;
21 	Mix_Chunk *error = NULL;
22 	Mix_Chunk *ding = NULL;
23 
24 	SDL_Event keystroke;
25 
26 	/* Load files */
27 	temp = IMG_Load(DATADIR "/png/blackbox.png");
28 	blackbox = SDL_DisplayFormat(temp);
29 	SDL_FreeSurface(temp);
30 	temp = IMG_Load(DATADIR "/png/karoshi.png");
31 	karoshi = SDL_DisplayFormat(temp);
32 	SDL_FreeSurface(temp);
33 	temp = IMG_Load(DATADIR "/png/blackbox.png");
34 	window = SDL_DisplayFormat(temp);
35 	SDL_FreeSurface(temp);
36 	temp = IMG_Load(DATADIR "/png/startscreen.png");
37 	startscreen = SDL_DisplayFormat(temp);
38 	SDL_FreeSurface(temp);
39 	temp = IMG_Load(DATADIR "/png/startinfo.png");
40 	startinfo = SDL_DisplayFormat(temp);
41 	SDL_FreeSurface(temp);
42 	temp = IMG_Load(DATADIR "/png/blocks.png");
43 	blocks = SDL_DisplayFormat(temp);
44 	SDL_FreeSurface(temp);
45 	temp = IMG_Load(DATADIR "/png/menu.png");
46 	menu = SDL_DisplayFormat(temp);
47 	SDL_FreeSurface(temp);
48 	temp = IMG_Load(DATADIR "/png/fonts.png");
49 	arrow = SDL_DisplayFormatAlpha(temp);
50 	SDL_FreeSurface(temp);
51 	temp = IMG_Load(DATADIR "/png/password.png");
52 	passwords = SDL_DisplayFormatAlpha(temp);
53 	SDL_FreeSurface(temp);
54 	temp = IMG_Load(DATADIR "/png/fonts.png");
55 	fonts = SDL_DisplayFormatAlpha(temp);
56 	SDL_FreeSurface(temp);
57 	start = Mix_LoadWAV(DATADIR "/fx/fx_start.ogg");
58 	poff = Mix_LoadWAV(DATADIR "/fx/fx_poff.ogg");
59 	error = Mix_LoadWAV(DATADIR "/fx/fx_error.ogg");
60 	ding = Mix_LoadWAV(DATADIR "/fx/fx_ding.ogg");
61 
62 	SDL_Rect srcscreen = {0,0,256,224};
63 	SDL_Rect destscreen = {0,0,256,224};
64 	SDL_Rect srctext = {0,0,200,20};
65 	SDL_Rect destext = {28,20,200,20};
66 	SDL_Rect srcblocks = {0,0,16,16};
67 	SDL_Rect destblocks = {0,0,16,16};
68 	SDL_Rect srcarrow = {40,40,8,8};
69 	SDL_Rect desarrow = {80,88,8,8};
70 	SDL_Rect srcselector = {0,224,14,14};
71 	SDL_Rect destselector = {61,101,14,14};
72 	SDL_Rect srcletters = {0,0,16,16};
73 	SDL_Rect destletters = {64,64,16,16};
74 
75 	uint step = 0;
76 	int framerate = 0;
77 	uint counter = 0;
78 	uint fadecounter = 255;
79 	uint animcounter = 0;
80 	uint posarrow = 0;
81 	uint selectorpos = 1;
82 	uint passint[8] = {0,0,0,0,0,0,0,0};
83 	uint n = 0;
84 	uint validatepass = 0;
85 	uint i = 0;
86 	uint result = 0;
87 
88 	/* Loop */
89 	while (*state == 0) {
90 		framerate = control_frames(1,0);
91 		switch (step) {
92 			case 0: /* Karoshi logo */
93 							SDL_BlitSurface(karoshi,&srcscreen,window,&destscreen);
94 							/* Apply transparency */
95 							SDL_SetAlpha(blackbox,SDL_RLEACCEL|SDL_SRCALPHA,(Uint8)fadecounter);
96 							SDL_BlitSurface(blackbox,&srcscreen,window,&destscreen);
97 							counter ++;
98 							while (SDL_PollEvent(&keystroke)) {
99 								if (keystroke.type == SDL_QUIT)
100 									exit(0);
101 								if (keystroke.type == SDL_KEYDOWN) {
102 									if (keystroke.key.keysym.sym == SDLK_ESCAPE)
103 										exit(0);
104 									if (keystroke.key.keysym.sym == SDLK_SPACE) {
105 										counter = 341;;
106 										step = 1;
107 										fadecounter = 255;
108 									}
109 								}
110 							}
111 							if (counter < 85)
112 								fadecounter-=3;
113 							if ((counter > 254) && (counter < 341))
114 								fadecounter+=3;
115 							if (counter == 340) {
116 								step = 1;
117 								fadecounter = 255;
118 							}
119 							break;
120 			case 1: /* Press start screen */
121 							SDL_BlitSurface(startscreen,&srcscreen,window,&destscreen);
122 							/* Apply transparency */
123 							SDL_SetAlpha(blackbox,SDL_RLEACCEL|SDL_SRCALPHA,(Uint8)fadecounter);
124 							SDL_BlitSurface(blackbox,&srcscreen,window,&destscreen);
125 							counter ++;
126 							while (SDL_PollEvent(&keystroke)) {
127 								if (keystroke.type == SDL_QUIT)
128 									exit(0);
129 								if (keystroke.type == SDL_KEYDOWN) {
130 									if (keystroke.key.keysym.sym == SDLK_ESCAPE)
131 										exit(0);
132 									if (keystroke.key.keysym.sym == SDLK_SPACE) {
133 										Mix_PlayChannel(-1,start,0);
134 										step = 3;
135 									}
136 								}
137 							}
138 							if ((counter > 340) && (counter < 426))
139 								fadecounter-=3;
140 							if ((counter > 1325) && (counter < 1387))
141 								fadecounter+=3;
142 							if (counter == 1387) {
143 								step = 2;
144 								fadecounter = 255;
145 							}
146 							break;
147 			case 2: /* Show instructions */
148 							SDL_BlitSurface(startinfo,&srcscreen,window,&destscreen);
149 							counter ++;
150 							while (SDL_PollEvent(&keystroke)) {
151 								if (keystroke.type == SDL_QUIT)
152 									exit(0);
153 								if (keystroke.type == SDL_KEYDOWN) {
154 									if (keystroke.key.keysym.sym == SDLK_ESCAPE)
155 										exit(0);
156 									if (keystroke.key.keysym.sym == SDLK_SPACE) {
157 										step = 1;
158 										counter = 341;
159 										fadecounter = 255;
160 									}
161 								}
162 							}
163 							if (animcounter < 59)
164 								animcounter ++;
165 							else
166 								animcounter = 0;
167 							/* Show animations */
168 							/* Hero */
169 							srcblocks.y = 32;
170 							srcblocks.x = (animcounter / 30) * 16 + 96;
171 							destblocks.x = 120;
172 							destblocks.y = 45;
173 							SDL_BlitSurface(blocks,&srcblocks,window,&destblocks);
174 							/* Demon */
175 							srcblocks.y = 16;
176 							srcblocks.x = (animcounter / 30) * 32 + 96;
177 							destblocks.x = 20;
178 							destblocks.y = 192;
179 							SDL_BlitSurface(blocks,&srcblocks,window,&destblocks);
180 							/* Ogre */
181 							srcblocks.x = (animcounter / 30) * 16 + 64;
182 							destblocks.y = 160;
183 							SDL_BlitSurface(blocks,&srcblocks,window,&destblocks);
184 							/* Ghost */
185 							srcblocks.x = (animcounter / 30) * 16 + 32;
186 							destblocks.y = 128;
187 							SDL_BlitSurface(blocks,&srcblocks,window,&destblocks);
188 							/* Slime */
189 							srcblocks.x = (animcounter / 30) * 16;
190 							destblocks.y = 96;
191 							SDL_BlitSurface(blocks,&srcblocks,window,&destblocks);
192 							if ((counter > 1387) && (counter < 1473))
193 								fadecounter-=3;
194 							if ((counter > 1387) && (counter < 1989)) {
195 								srctext.y = 224;
196 								srctext.h = 20;
197 								destext.h = 20;
198 								SDL_BlitSurface(startinfo,&srctext,window,&destext);
199 								SDL_SetAlpha(blackbox,SDL_RLEACCEL|SDL_SRCALPHA,(Uint8)fadecounter);
200 								SDL_BlitSurface(blackbox,&srcscreen,window,&destscreen);
201 							}
202 							if ((counter > 1988) && (counter < 2590)) {
203 								srctext.y = 244;
204 								srctext.h = 21;
205 								destext.h = 21;
206 								SDL_BlitSurface(startinfo,&srctext,window,&destext);
207 							}
208 							if ((counter > 2589) && (counter < 3275)) {
209 								srctext.y = 265;
210 								SDL_BlitSurface(startinfo,&srctext,window,&destext);
211 							}
212 							if ((counter > 3190) && (counter < 3275)) {
213 								fadecounter += 3;
214 								SDL_SetAlpha(blackbox,SDL_RLEACCEL|SDL_SRCALPHA,(Uint8)fadecounter);
215 								SDL_BlitSurface(blackbox,&srcscreen,window,&destscreen);
216 							}
217 							if (counter == 3274) {
218 								step = 0;
219 								counter = 0;
220 								animcounter = 0;
221 							}
222 							break;
223 			case 3: /* show menu */
224 							SDL_BlitSurface(menu,NULL,window,NULL);
225 							if (posarrow == 0)
226 								desarrow.y = 88;
227 							else
228 								desarrow.y = 104;
229 							SDL_BlitSurface(arrow,&srcarrow,window,&desarrow);
230 							while (SDL_PollEvent(&keystroke)) {
231 								if (keystroke.type == SDL_QUIT)
232 									exit(0);
233 								if (keystroke.type == SDL_KEYDOWN) {
234 									if (keystroke.key.keysym.sym == SDLK_ESCAPE)
235 										exit(0);
236 									if ((keystroke.key.keysym.sym == SDLK_UP) || (keystroke.key.keysym.sym == SDLK_DOWN)) {
237 										if (posarrow == 0)
238 											posarrow = 1;
239 										else
240 											posarrow = 0;
241 									}
242 									if ((keystroke.key.keysym.sym == SDLK_SPACE) || (keystroke.key.keysym.sym == SDLK_RETURN)) {
243 										if (posarrow == 0) {
244 											*state = 1;
245 											*level = 1;
246 										}
247 										if (posarrow == 1)
248 											step = 4;
249 										Mix_PlayChannel(-1,start,0);
250 									}
251 								}
252 							}
253 							break;
254 			case 4: /* show password selection */
255 							SDL_BlitSurface(passwords,&srcscreen,window,&destscreen);
256 							SDL_BlitSurface(passwords,&srcselector,window,&destselector);
257 							while (SDL_PollEvent(&keystroke)) {
258 								if (keystroke.type == SDL_QUIT)
259 									exit(0);
260 								if (keystroke.type == SDL_KEYDOWN) {
261 									if (keystroke.key.keysym.sym == SDLK_ESCAPE)
262 										exit(0);
263 									if (keystroke.key.keysym.sym == SDLK_RIGHT) {
264 										if (destselector.x < 173) {
265 											destselector.x += 16;
266 											selectorpos ++;
267 										}
268 										else {
269 											destselector.x -= 112;
270 											selectorpos -= 7;
271 										}
272 									}
273 									if (keystroke.key.keysym.sym == SDLK_LEFT) {
274 										if (destselector.x > 61) {
275 											destselector.x -= 16;
276 											selectorpos --;
277 										}
278 										else {
279 											destselector.x += 112;
280 											selectorpos += 7;
281 										}
282 									}
283 									if (keystroke.key.keysym.sym == SDLK_UP) {
284 										if (destselector.y > 101) {
285 											destselector.y -= 16;
286 											selectorpos -= 8;
287 										}
288 										else {
289 											destselector.y += 64;
290 											selectorpos += 32;
291 										}
292 									}
293 									if (keystroke.key.keysym.sym == SDLK_DOWN) {
294 										if (destselector.y < 165) {
295 											destselector.y += 16;
296 											selectorpos += 8;
297 										}
298 										else {
299 											destselector.y -= 64;
300 											selectorpos -= 32;
301 										}
302 									}
303 									if (keystroke.key.keysym.sym == SDLK_SPACE) {
304 										if (selectorpos < 37) {
305 											passint[n] = selectorpos;
306 											if (n < 7)
307 												n ++;
308 											else
309 												n = 1;
310 											Mix_PlayChannel(-1,start,0);
311 										}
312 										if (selectorpos == 37) { /* Tilde */
313 											passint[n] = 0;
314 											if (n < 7)
315 												n ++;
316 											else
317 												n = 1;
318 											Mix_PlayChannel(-1,start,0);
319 										}
320 										if (selectorpos == 38) { /* Delete */
321 											if (n > 0) {
322 												passint[n-1] = 0;
323 												n --;
324 											}
325 											else {
326 												n = 7;
327 												passint[n] = 0;
328 											}
329 											Mix_PlayChannel(-1,poff,0);
330 										}
331 										if (selectorpos == 39) /* Ok key */
332 											validatepass = 1;
333 									}
334 								}
335 							}
336 							/* Showing characters */
337 							for (i=0;i<8;i++) {
338 								if (passint[i] > 0) {
339 									if (passint[i] < 11) {
340 										srcletters.y = 80;
341 										srcletters.x = (passint[i] - 1) * 16;
342 									}
343 									if ((passint[i] > 10) && (passint[n] < 21)) {
344 										srcletters.y = 96;
345 										srcletters.x = (passint[i] - 11) * 16;
346 									}
347 									if ((passint[i] > 20) && (passint[n] < 27)) {
348 										srcletters.y = 112;
349 										srcletters.x = (passint[i] - 21) * 16;
350 									}
351 									if ((passint[i] > 26) && (passint[i] <  37)) {
352 										srcletters.y = 64;
353 										srcletters.x = (passint[i] - 27) * 16;
354 									}
355 									destletters.x = 64 + (i * 16);
356 									destletters.y = 64;
357 									SDL_BlitSurface(fonts,&srcletters,window,&destletters);
358 								}
359 							}
360 							/* Showing character position */
361 							srcletters.x = 96;
362 							srcletters.y = 112;
363 							destletters.x = 64 + (n * 16);
364 							destletters.y = 80;
365 							SDL_BlitSurface(fonts,&srcletters,window,&destletters);
366 							/* Checking if password is correct */
367 							if (validatepass == 1) {
368 								result = passwvalidate(passint);
369 								validatepass = 0;
370 								if (result > 0) {
371 									*level = result;
372 									Mix_PlayChannel(-1,ding,0);
373 									*state = 2;
374 								}
375 								else
376 									Mix_PlayChannel(-1,error,0);
377 							}
378 							break;
379 		}
380 		/* Zoom 2x */
381 		doble = zoomSurface(window,2,2,0);
382 		SDL_BlitSurface(doble,NULL,screen,NULL);
383 		SDL_Flip(screen);
384 		SDL_FreeSurface(doble);
385 		SDL_FreeSurface(screen);
386 		control_frames(2,framerate);
387 	}
388 
389 	/* Cleaning */
390 	SDL_FreeSurface(karoshi);
391 	SDL_FreeSurface(blackbox);
392 	SDL_FreeSurface(blocks);
393 	SDL_FreeSurface(startscreen);
394 	SDL_FreeSurface(startinfo);
395 	SDL_FreeSurface(passwords);
396 	SDL_FreeSurface(fonts);
397 	Mix_FreeChunk(start);
398 	Mix_FreeChunk(poff);
399 	Mix_FreeChunk(error);
400 	Mix_FreeChunk(ding);
401 
402 }
403 
passwvalidate(uint passint[])404 int passwvalidate (uint passint[]) {
405 
406 	int n = 0;
407 
408 	if ((passint[0] == 7) && (passint[1] == 5) && (passint[2] == 14) && (passint[3] == 5) && (passint[4] == 19) && (passint[5] == 9) && (passint[6] == 19) && (passint[7] == 0))
409 		n = 6;
410 
411 	if ((passint[0] == 16) && (passint[1] == 19) && (passint[2] == 1) && (passint[3] == 12) && (passint[4] == 13) && (passint[5] == 19) && (passint[6] == 0) && (passint[7] == 0))
412 		n = 11;
413 
414 	if ((passint[0] == 18) && (passint[1] == 15) && (passint[2] == 13) && (passint[3] == 1) && (passint[4] == 14) && (passint[5] == 19) && (passint[6] == 0) && (passint[7] == 0))
415 		n = 16;
416 
417 	if ((passint[0] == 10) && (passint[1] == 1) && (passint[2] == 13) && (passint[3] == 5) && (passint[4] == 19) && (passint[5] == 0) && (passint[6] == 0) && (passint[7] == 0))
418 		n = 21;
419 
420 	if ((passint[0] == 5) && (passint[1] == 3) && (passint[2] == 3) && (passint[3] == 12) && (passint[4] == 5) && (passint[5] == 9) && (passint[6] == 1) && (passint[7] == 0))
421 		n = 26;
422 
423 	if ((passint[0] == 8) && (passint[1] == 5) && (passint[2] == 2) && (passint[3] == 18) && (passint[4] == 5) && (passint[5] == 23) && (passint[6] == 19) && (passint[7] == 0))
424 		n = 31;
425 
426 	if ((passint[0] == 10) && (passint[1] == 15) && (passint[2] == 8) && (passint[3] == 14) && (passint[4] == 0) && (passint[5] == 0) && (passint[6] == 0) && (passint[7] == 0))
427 		n = 37;
428 
429 	if ((passint[0] == 16) && (passint[1] == 18) && (passint[2] == 15) && (passint[3] == 22) && (passint[4] == 5) && (passint[5] == 18) && (passint[6] == 2) && (passint[7] == 19))
430 		n = 43;
431 
432 	if ((passint[0] == 9) && (passint[1] == 19) && (passint[2] == 1) && (passint[3] == 9) && (passint[4] == 1) && (passint[5] == 8) && (passint[6] == 0) && (passint[7] == 0))
433 		n = 49;
434 
435 	if ((passint[0] == 13) && (passint[1] == 1) && (passint[2] == 20) && (passint[3] == 20) && (passint[4] == 8) && (passint[5] == 5) && (passint[6] == 23) && (passint[7] == 0))
436 		n = 55;
437 
438 	return n;
439 
440 }