Mercurial > hg > demos > bussi
comparison demo.c @ 0:f339a3903bc9
Initial import.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 21 May 2015 22:52:30 +0300 |
parents | |
children | 496b9ab9238c |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:f339a3903bc9 |
---|---|
1 #include "dmengine.h" | |
2 #include "dmtext.h" | |
3 #include "dmfft.h" | |
4 #include "dmvecmat.h" | |
5 #include <math.h> | |
6 | |
7 static int demoInit(DMEngineData *engine); | |
8 static void demoShutdown(DMEngineData *engine); | |
9 static int demoRender(DMEngineData *engine); | |
10 | |
11 | |
12 int demoPreInit(DMEngineData *engine) | |
13 { | |
14 dmInitProg("bussi", | |
15 "Bussi by AnCiat ProDz", | |
16 "0", "ENGINE INITTIALISSSIZING!!!!!", NULL); | |
17 | |
18 engine->optPackFilename = "bussi.dat"; | |
19 engine->optDataPath = NULL; | |
20 engine->optResFlags = DRF_USE_PACK | DRF_PRELOAD_RES | DRF_USE_STDIO; | |
21 | |
22 engine->optAudioSetup = DM_ASETUP_JSS; | |
23 | |
24 engine->optVidSetup = DM_VSETUP_ASPECT; | |
25 engine->optVidWidth = 640; | |
26 engine->optVidHeight = 480; | |
27 engine->optVidDepth = 32; | |
28 engine->optVFlags = SDL_SWSURFACE; | |
29 | |
30 engine->demoInit = demoInit; | |
31 engine->demoRender = demoRender; | |
32 engine->demoShutdown = demoShutdown; | |
33 | |
34 return DMERR_OK; | |
35 } | |
36 | |
37 | |
38 #define FFT_SIZE 128 | |
39 DMFFTContext fft; | |
40 | |
41 | |
42 static int demoInit(DMEngineData *engine) | |
43 { | |
44 int i; | |
45 JSSModule *mod = NULL; | |
46 | |
47 dmInitializeFFT(&fft, FFT_SIZE); | |
48 | |
49 engineGetResModule(engine, mod, "pas2.xm"); | |
50 | |
51 if ((i = jssConvertModuleForPlaying(mod)) != DMERR_OK) | |
52 { | |
53 dmErrorMsg("Could not convert module for playing, %d: %s\n", | |
54 i, dmErrorStr(i)); | |
55 return DMERR_INIT_FAIL; | |
56 } | |
57 | |
58 jvmSetCallback(engine->jssDev, jmpExec, engine->jssPlr); | |
59 jmpSetModule(engine->jssPlr, mod); | |
60 jmpPlayOrder(engine->jssPlr, 0); | |
61 jvmSetGlobalVol(engine->jssDev, 55); | |
62 | |
63 // Jne | |
64 srand(15); | |
65 | |
66 return DMERR_OK; | |
67 } | |
68 | |
69 | |
70 static void demoShutdown(DMEngineData *engine) | |
71 { | |
72 (void) engine; | |
73 dmEndFFT(&fft); | |
74 } | |
75 | |
76 | |
77 static inline float dmCX(DMEngineData *engine, const float x) | |
78 { | |
79 return (x * engine->screen->w); | |
80 } | |
81 | |
82 | |
83 static inline float dmCY(DMEngineData *engine, const float y) | |
84 { | |
85 return (y * engine->screen->h); | |
86 } | |
87 | |
88 | |
89 static inline float dmQX(DMEngineData *engine, SDL_Surface *img, const float x) | |
90 { | |
91 return engine->optVidNative ? (img->w * x) : (img->w * engine->screen->w * x) / 640.0f; | |
92 } | |
93 | |
94 | |
95 static inline float dmQY(DMEngineData *engine, SDL_Surface *img, const float y) | |
96 { | |
97 return engine->optVidNative ? (img->h * y) : (img->h * engine->screen->h * y) / 480.0f; | |
98 } | |
99 | |
100 | |
101 typedef struct | |
102 { | |
103 char *filename; | |
104 float xc, yc; | |
105 SDL_Surface *img; | |
106 } DMTextItem; | |
107 | |
108 | |
109 static DMTextItem textItems[] = | |
110 { | |
111 { "text01.png", 0.05, 0.10, NULL }, | |
112 { "text02.png", 0.10, 0.30, NULL }, | |
113 { "text03.png", 0.60, 0.30, NULL }, | |
114 { "text04.png", 0.20, 0.60, NULL }, | |
115 { "text05.png", 0.30, 0.70, NULL }, | |
116 }; | |
117 | |
118 static const int ntextItems = sizeof(textItems) / sizeof(textItems[0]); | |
119 | |
120 | |
121 static DMTextItem textItems2[] = | |
122 { | |
123 { "text11.png", 0.05, 0.10, NULL }, | |
124 { "text02.png", 0.30, 0.25, NULL }, | |
125 { "text12.png", 0.10, 0.40, NULL }, | |
126 { "text13.png", 0.20, 0.60, NULL }, | |
127 { "text14.png", 0.30, 0.70, NULL }, | |
128 }; | |
129 | |
130 static const int ntextItems2 = sizeof(textItems2) / sizeof(textItems2[0]); | |
131 | |
132 | |
133 static DMTextItem textItems3[] = | |
134 { | |
135 { "teki.png" , 0.25, 0.20, NULL }, | |
136 { "mitvit.png" , 0.05, 0.40, NULL }, | |
137 { "ja.png" , 0.05, 0.65, NULL }, | |
138 { "kemisti.png", 0.15, 0.65, NULL }, | |
139 }; | |
140 | |
141 static const int ntextItems3 = sizeof(textItems3) / sizeof(textItems3[0]); | |
142 | |
143 | |
144 | |
145 | |
146 #define DM_RADIAL_BLUR(YC, XC) \ | |
147 DMVector p1 = { xc, yc, 0, 0 }, p2 = { cx, cy, 0, 0 }, v; \ | |
148 dm_vector_sub_r(&v, &p2, &p1); \ | |
149 dm_vector_scale(&v, scale); \ | |
150 dm_vector_add(&v, &p1); \ | |
151 if (v.y YC || v.x XC) continue; \ | |
152 DMColor *dp = pix + xc, \ | |
153 *q = ((DMColor *)img->pixels) + ((int)(v.y) * pitch) + (int)v.x; \ | |
154 dp->r = (q->r + dp->r) / 2; \ | |
155 dp->g = (q->g + dp->g) / 2; \ | |
156 dp->b = (q->b + dp->b) / 2; | |
157 | |
158 | |
159 | |
160 void dmRadialBlur(SDL_Surface *img, const int cx, const int cy, const DMFloat scale) | |
161 { | |
162 const int pitch = img->pitch / sizeof(DMColor); | |
163 int xc, yc; | |
164 | |
165 #pragma omp parallel private(yc, xc) shared(img) | |
166 { | |
167 #pragma omp sections nowait | |
168 { | |
169 #pragma omp section | |
170 for (yc = cy; yc >= 0; yc--) | |
171 { | |
172 DMColor *pix = ((DMColor *)img->pixels) + yc * pitch; | |
173 for (xc = cx + 1; xc < img->w; xc++) | |
174 { | |
175 DM_RADIAL_BLUR(< 0, >= img->w) | |
176 } | |
177 } | |
178 | |
179 #pragma omp section | |
180 for (yc = cy; yc >= 0; yc--) | |
181 { | |
182 DMColor *pix = ((DMColor *)img->pixels) + yc * pitch; | |
183 for (xc = cx; xc > 0; xc--) | |
184 { | |
185 DM_RADIAL_BLUR(< 0, < 0) | |
186 } | |
187 } | |
188 | |
189 #pragma omp section | |
190 for (yc = cy + 1; yc < img->h; yc++) | |
191 { | |
192 DMColor *pix = ((DMColor *)img->pixels) + yc * pitch; | |
193 for (xc = cx; xc > 0; xc--) | |
194 { | |
195 DM_RADIAL_BLUR(>= img->h, < 0) | |
196 } | |
197 } | |
198 | |
199 #pragma omp section | |
200 for (yc = cy + 1; yc < img->h; yc++) | |
201 { | |
202 DMColor *pix = ((DMColor *)img->pixels) + yc * pitch; | |
203 for (xc = cx + 1; xc < img->w; xc++) | |
204 { | |
205 DM_RADIAL_BLUR(>= img->h, >= img->w) | |
206 } | |
207 } | |
208 } | |
209 } | |
210 } | |
211 | |
212 | |
213 static int hitlerText(DMEngineData *engine, const int dt, float fftPow, DMTextItem *items, const int nitems, BOOL *nollattu) | |
214 { | |
215 int i, vis; | |
216 if (!(*nollattu)) | |
217 { | |
218 for (i = 0; i < nitems; i++) | |
219 engineGetResImage(engine, items[i].img, items[i].filename); | |
220 | |
221 *nollattu = TRUE; | |
222 } | |
223 | |
224 float q = fftPow * 0.05f; | |
225 vis = dt / 1000; | |
226 for (i = 0; i < nitems; i++) | |
227 { | |
228 DMTextItem *item = &items[i]; | |
229 if (i < vis) | |
230 { | |
231 dmScaledBlitSurface32to32TransparentGA( | |
232 item->img, | |
233 dmCX(engine, item->xc - q), | |
234 dmCY(engine, item->yc - q), | |
235 | |
236 dmQX(engine, item->img, 1.0f + q * 2.0f), | |
237 dmQY(engine, item->img, 1.0f + q * 2.0f), | |
238 | |
239 engine->screen, | |
240 | |
241 100 + 80 * fftPow); | |
242 } | |
243 } | |
244 | |
245 return DMERR_OK; | |
246 } | |
247 | |
248 | |
249 static int demoRender(DMEngineData *engine) | |
250 { | |
251 float t = engineGetTimeDT(engine); | |
252 static DMScaledBlitFunc cblit = NULL; | |
253 | |
254 // Do FFT | |
255 DMFFTType fftPow = 0; | |
256 BOOL fftOK = FALSE; | |
257 static DMFFTType fftAmp[FFT_SIZE / 2]; | |
258 static DMFFTType fftData[FFT_SIZE]; | |
259 | |
260 dmMutexLock(engine->audioStreamMutex); | |
261 if (engine->audioStreamBuf != 0 && engine->audioStreamLen > FFT_SIZE) | |
262 { | |
263 int i; | |
264 Sint16 *buf = (Sint16 *) engine->audioStreamBuf; | |
265 for (i = 0; i < FFT_SIZE; i++) | |
266 { | |
267 fftData[i] = *buf; | |
268 buf += 2; | |
269 } | |
270 fftOK = TRUE; | |
271 } | |
272 dmMutexUnlock(engine->audioStreamMutex); | |
273 | |
274 if (fftOK) | |
275 { | |
276 dmRealFFT(&fft, fftData); | |
277 dmConvertFFTtoPowerAndSum(&fft, fftData, fftAmp, 1.0, &fftPow, 0.00004f); | |
278 } | |
279 | |
280 | |
281 #if 1 | |
282 // Demokoodi | |
283 if (t < 5) | |
284 { | |
285 // Alkufeidi sisään | |
286 int dt = engineGetTime(engine, 0); | |
287 static DMLerpContext fadeLerp; | |
288 static SDL_Surface *tausta = NULL; | |
289 static BOOL nollattu = FALSE; | |
290 if (!nollattu) | |
291 { | |
292 engineGetResImage(engine, tausta, "tausta.jpg"); | |
293 cblit = dmGetScaledBlitFunc(tausta->format, engine->screen->format, DMD_NONE); | |
294 | |
295 dmLerpInit(&fadeLerp, 0, 255, 5000); | |
296 nollattu = TRUE; | |
297 } | |
298 | |
299 dmClearSurface(engine->screen, dmMapRGB(engine->screen, 0,0,0)); | |
300 | |
301 dmScaledBlitSurface32to32TransparentGA(tausta, | |
302 0, | |
303 0, | |
304 | |
305 dmQX(engine, tausta, 1.0f), | |
306 dmQX(engine, tausta, 1.0f), | |
307 | |
308 engine->screen, | |
309 dmLerpSCurveClamp(&fadeLerp, dt + 150500 * fftPow)); | |
310 } | |
311 #endif | |
312 #if 0 | |
313 else | |
314 if (t < 25) | |
315 { | |
316 static BOOL nollattu = FALSE; | |
317 static SDL_Surface *tausta = NULL; | |
318 if (!nollattu) | |
319 { | |
320 engineGetResImage(engine, tausta, "tausta.jpg"); | |
321 } | |
322 | |
323 cblit(tausta, | |
324 0, | |
325 0, | |
326 | |
327 dmQX(engine, tausta, 1.0f), | |
328 dmQX(engine, tausta, 1.0f), | |
329 | |
330 engine->screen); | |
331 } | |
332 | |
333 if (t <= 7) | |
334 { | |
335 int dt = engineGetTime(engine, 2); | |
336 static DMLerpContext fadeLerp; | |
337 static SDL_Surface *logo1 = NULL, *logo2 = NULL; | |
338 static BOOL nollattu = FALSE; | |
339 if (!nollattu) | |
340 { | |
341 engineGetResImage(engine, logo1, "iso.png"); | |
342 engineGetResImage(engine, logo2, "forevisar.png"); | |
343 | |
344 dmLerpInit(&fadeLerp, 0, 255, 2500); | |
345 nollattu = TRUE; | |
346 } | |
347 | |
348 int fade; | |
349 if (dt < 2500) | |
350 { | |
351 fade = dmLerpSCurveClamp(&fadeLerp, dt + fftPow * 1000); | |
352 } | |
353 else | |
354 if (dt > 4500) | |
355 { | |
356 fade = 255 - dmLerpSCurveClamp(&fadeLerp, dt - 4500 + fftPow * 1000); | |
357 } | |
358 else | |
359 { | |
360 fade = 255; | |
361 } | |
362 | |
363 float d = fftPow * 0.1f; | |
364 | |
365 dmScaledBlitSurface32to32TransparentGA(logo1, | |
366 dmCX(engine, 0.1f), | |
367 dmCY(engine, 0.22f), | |
368 | |
369 dmQX(engine, logo1, 1.0f), | |
370 dmQY(engine, logo1, 1.0f), | |
371 | |
372 engine->screen, | |
373 fade); | |
374 | |
375 dmScaledBlitSurface32to32TransparentGA(logo2, | |
376 dmCX(engine, 0.2f - d), | |
377 dmCY(engine, 0.72f - d), | |
378 | |
379 dmQX(engine, logo2, 1.0f + d * 2.0f), | |
380 dmQY(engine, logo2, 1.0f + d * 2.0f), | |
381 | |
382 engine->screen, | |
383 fade); | |
384 } | |
385 else | |
386 if (t <= 15) | |
387 { | |
388 int dt = engineGetTime(engine, 7); | |
389 static DMLerpContext fadeLerp; | |
390 static SDL_Surface *logo1 = NULL, *logo2 = NULL; | |
391 static BOOL nollattu = FALSE; | |
392 if (!nollattu) | |
393 { | |
394 engineGetResImage(engine, logo1, "hitler_on_kiva.png"); | |
395 engineGetResImage(engine, logo2, "hitler.png"); | |
396 | |
397 dmLerpInit(&fadeLerp, 0, 255, 2500); | |
398 nollattu = TRUE; | |
399 } | |
400 | |
401 int fade; | |
402 if (dt < 2500) | |
403 { | |
404 fade = dmLerpSCurveClamp(&fadeLerp, dt + fftPow * 1000); | |
405 } | |
406 else | |
407 if (dt > 4500) | |
408 { | |
409 fade = 255 - dmLerpSCurveClamp(&fadeLerp, dt - 4500 + fftPow * 1000); | |
410 } | |
411 else | |
412 { | |
413 fade = 255; | |
414 } | |
415 | |
416 dmScaledBlitSurface32to32TransparentGA(logo1, | |
417 dmCX(engine, 0.1f), | |
418 dmCY(engine, 0.22f), | |
419 | |
420 dmQX(engine, logo1, 1.0f), | |
421 dmQY(engine, logo1, 1.0f), | |
422 | |
423 engine->screen, | |
424 fade); | |
425 | |
426 float q = fftPow * 0.2; | |
427 dmScaledBlitSurface32to32TransparentGA(logo2, | |
428 dmCX(engine, 0.15f), | |
429 dmCY(engine, 0.47f - q), | |
430 | |
431 dmQX(engine, logo2, 1.0f + q), | |
432 dmQY(engine, logo2, 1.0f + q), | |
433 | |
434 engine->screen, | |
435 fade); | |
436 } | |
437 | |
438 if (t <= 15) | |
439 { | |
440 int dt = engineGetTime(engine, 0); | |
441 static DMLerpContext posLerp, posLerp2; | |
442 static SDL_Surface *mainos = NULL; | |
443 static BOOL nollattu = FALSE; | |
444 if (!nollattu) | |
445 { | |
446 engineGetResImage(engine, mainos, "radio.png"); | |
447 | |
448 dmLerpInit(&posLerp, 1, -1, 15000); | |
449 dmLerpInit(&posLerp2, 0.3, 0.7, 15000); | |
450 nollattu = TRUE; | |
451 } | |
452 | |
453 dmScaledBlitSurface32to32TransparentGA(mainos, | |
454 dmCX(engine, dmLerpSCurveClamp(&posLerp2, dt)), | |
455 dmCY(engine, dmLerpSCurveClamp(&posLerp, dt)), | |
456 | |
457 dmQX(engine, mainos, 1.0f), | |
458 dmQY(engine, mainos, 1.0f), | |
459 | |
460 engine->screen, | |
461 50); | |
462 } | |
463 else | |
464 if (t <= 25) | |
465 { | |
466 int dt = engineGetTime(engine, 15); | |
467 static DMLerpContext posLerp, posLerp2; | |
468 static DMScaledBlitFunc qblit = NULL; | |
469 static SDL_Surface *mainos = NULL, *mainos2 = NULL, *hitler = NULL, *sydan = NULL; | |
470 static BOOL nollattu = FALSE; | |
471 if (!nollattu) | |
472 { | |
473 engineGetResImage(engine, mainos, "lasi.jpg"); | |
474 engineGetResImage(engine, mainos2, "nigrolin.jpg"); | |
475 engineGetResImage(engine, hitler, "hitler2.png"); | |
476 engineGetResImage(engine, sydan, "sydan.png"); | |
477 | |
478 qblit = dmGetScaledBlitFunc(hitler->format, engine->screen->format, DMD_TRANSPARENT); | |
479 | |
480 dmLerpInit(&posLerp, 1, -1, 20000); | |
481 dmLerpInit(&posLerp2, -0.5, -0.2, 20000); | |
482 | |
483 nollattu = TRUE; | |
484 } | |
485 | |
486 dmScaledBlitSurface32to32TransparentGA(mainos, | |
487 dmCX(engine, dmLerpSCurveClamp(&posLerp2, dt)), | |
488 dmCY(engine, dmLerpSCurveClamp(&posLerp, dt + fftPow * 250)), | |
489 | |
490 dmQX(engine, mainos, 1.0f), | |
491 dmQY(engine, mainos, 1.0f), | |
492 | |
493 engine->screen, | |
494 50); | |
495 | |
496 dmScaledBlitSurface32to32TransparentGA(mainos2, | |
497 dmCX(engine, dmLerpSCurveClamp(&posLerp, dt)), | |
498 dmCY(engine, dmLerpSCurveClamp(&posLerp2, dt + fftPow * 250)), | |
499 | |
500 dmQX(engine, mainos2, 1.0f), | |
501 dmQY(engine, mainos2, 1.0f), | |
502 | |
503 engine->screen, | |
504 50); | |
505 | |
506 float d = fftPow * 0.1f; | |
507 | |
508 qblit(hitler, | |
509 dmCX(engine, 0.4 - d), | |
510 dmCY(engine, 0.3 - d), | |
511 | |
512 dmQX(engine, hitler, 1.0f + d * 2.0f), | |
513 dmQY(engine, hitler, 1.0f + d * 2.0f), | |
514 | |
515 engine->screen); | |
516 | |
517 | |
518 | |
519 if (t > 17) | |
520 { | |
521 static BOOL xnollattu = FALSE; | |
522 | |
523 if (t > 19) | |
524 { | |
525 float z = sin(t * 2.0f + fftPow) * 0.1f; | |
526 qblit(sydan, | |
527 dmCX(engine, 0.7) + dmQX(engine, sydan, 0.5f - z), | |
528 dmCY(engine, 0.55) + dmQX(engine, sydan, 0.5f - z), | |
529 | |
530 dmQX(engine, sydan, 1.0f + z * 2.0f), | |
531 dmQY(engine, sydan, 1.0f + z * 2.0f), | |
532 | |
533 engine->screen); | |
534 } | |
535 | |
536 hitlerText(engine, engineGetTime(engine, 17), fftPow, textItems, ntextItems, &xnollattu); | |
537 } | |
538 } | |
539 else | |
540 if (t <= 60) | |
541 { | |
542 int dt = engineGetTime(engine, 25); | |
543 static SDL_Surface *tekstur, *poni1, *poni2; | |
544 static BOOL nollattu = FALSE; | |
545 static DMScaledBlitFunc qblit = NULL; | |
546 if (!nollattu) | |
547 { | |
548 engineGetResImage(engine, tekstur, "disco.jpg"); | |
549 engineGetResImage(engine, poni1, "poni1.png"); | |
550 engineGetResImage(engine, poni2, "poni2.png"); | |
551 | |
552 qblit = dmGetScaledBlitFunc(poni1->format, engine->screen->format, DMD_TRANSPARENT); | |
553 | |
554 nollattu = TRUE; | |
555 } | |
556 | |
557 float m = t*2.0f; | |
558 float q = sin(m) * 0.3f, | |
559 q2 = sin(m + 0.2) * 0.4f, | |
560 z = sin(m) * 0.3f + 2.0f, | |
561 z2 = sin(m + 0.1f) * 0.3f + 2.0f; | |
562 | |
563 dmScaledBlitSurface32to32TransparentGA(tekstur, | |
564 dmCX(engine, sin(q) * 0.1f - 0.2f), | |
565 dmCY(engine, cos(q) * 0.1f - 0.2f), | |
566 | |
567 dmQX(engine, tekstur, z), | |
568 dmQY(engine, tekstur, z), | |
569 | |
570 engine->screen, | |
571 130); | |
572 | |
573 dmScaledBlitSurface32to32TransparentGA(tekstur, | |
574 dmCX(engine, sin(q2) * 0.1f - 0.2f), | |
575 dmCY(engine, cos(q2) * 0.1f - 0.2f), | |
576 | |
577 dmQX(engine, tekstur, z2), | |
578 dmQY(engine, tekstur, z2), | |
579 | |
580 engine->screen, | |
581 130); | |
582 | |
583 | |
584 qblit(poni1, | |
585 dmCX(engine, 0.2f), | |
586 dmCY(engine, 0.07f), | |
587 | |
588 dmQX(engine, poni1, 1.0f), | |
589 dmQY(engine, poni1, 1.0f), | |
590 | |
591 engine->screen); | |
592 | |
593 | |
594 dmScaledBlitSurface32to32TransparentGA(poni2, | |
595 dmCX(engine, 0.2f), | |
596 dmCY(engine, 0.07f), | |
597 | |
598 dmQX(engine, poni2, 1.0f), | |
599 dmQY(engine, poni2, 1.0f), | |
600 | |
601 engine->screen, | |
602 50 + fftPow * 400); | |
603 | |
604 dmRadialBlur(engine->screen, | |
605 dmCX(engine, 0.5f + sin(m) * 0.1f), | |
606 dmCY(engine, 0.5f + cos(m) * 0.1f), | |
607 0.035f); | |
608 | |
609 if (t >= 30 && t <= 37) | |
610 { | |
611 static BOOL xnollattu = FALSE; | |
612 hitlerText(engine, engineGetTime(engine, 30), fftPow + 0.2, textItems2, ntextItems2, &xnollattu); | |
613 } | |
614 else | |
615 if (t >= 38 && t <= 47) | |
616 { | |
617 static BOOL xnollattu = FALSE; | |
618 hitlerText(engine, engineGetTime(engine, 38), fftPow + 0.2, textItems3, ntextItems3, &xnollattu); | |
619 } | |
620 else | |
621 if (t > 50) | |
622 return 1; | |
623 } | |
624 | |
625 #endif | |
626 | |
627 return DMERR_OK; | |
628 } |