comparison demo.c @ 58:08913cea6de1

Branch merge.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 04 Oct 2016 00:15:43 +0300
parents krapula.c@51110f424dfd krapula.c@b8bbed30748b
children 7daf69b39f34
comparison
equal deleted inserted replaced
57:51110f424dfd 58:08913cea6de1
1 #include "dmengine.h"
2 #include "dmvecmat.h"
3 #include "dmperlin.h"
4 #include <math.h>
5
6 static int demoInit();
7 static void demoShutdown();
8 static void demoQuit();
9 static int demoRender();
10
11 static DMPerlinContext perlinCtx;
12
13 #define DM_COLORS (256)
14
15 void dmMakePalette(SDL_Surface *scr)
16 {
17 SDL_Color pal[DM_COLORS];
18 int n;
19
20 for (n = 0; n < 256; n++)
21 {
22 pal[n].r = n;
23 pal[n].g = n;
24 pal[n].b = n;
25 }
26
27 SDL_SetColors(scr, pal, 0, DM_COLORS);
28 }
29
30
31 #define QWIDTH 256
32 #define QHEIGHT 160
33
34 typedef Uint8 DMBlockMap[QHEIGHT][QWIDTH];
35
36
37 void dmMakeBumpMap(DMBlockMap map, DMFloat q, DMFloat m)
38 {
39 int x, y;
40 for (y = 0; y < QHEIGHT; y++)
41 {
42 for (x = 0; x < QWIDTH; x++)
43 {
44 DMFloat f = 0.40f + dmPerlinNoise2D(&perlinCtx, x, y, 1.1f, q, 2);
45 map[y][x] = (int) (dmClamp10(f) * m);
46 }
47 }
48 }
49
50
51 void dmShadowTraceHeightMap(DMBlockMap lightMap, DMBlockMap pheightMap, DMVector *light)
52 {
53 int i, j;
54
55 for (j = 0; j < QHEIGHT; j++)
56 for (i = 0; i < QWIDTH; i++)
57 {
58 DMVector vr, vl, va;
59 DMFloat vrayLen, vfactor;
60 int vlen;
61 BOOL wasHit;
62
63 /* Perform shadow occlusion via simplistic raytracing */
64 vr.x = i;
65 vr.y = j;
66 vr.z = light->z; // - 10.0;
67 // vr.z = pheightMap[j][i];
68
69 /* Calculate light vector vector */
70 dm_vector_sub_r(&vl, &vr, light);
71 vrayLen = dm_vector_length(&vl);
72
73 #if 1
74 dm_vector_copy(&va, &vl);
75 dm_vector_normalize(&va);
76 dm_vector_scale(&va, 0.6f);
77 dm_vector_copy(&vr, light);
78
79 vlen = 0;
80 wasHit = FALSE;
81 do
82 {
83 float h;
84
85 /* If ray is inside the heightmap, get value */
86 if (vr.x >= 0 && vr.y >= 0 && vr.x < QWIDTH && vr.y < QHEIGHT)
87 h = pheightMap[(int) vr.y][(int) vr.x];
88 else
89 break;
90
91 /* Check for hits */
92 if (h > vr.z)
93 wasHit = TRUE;
94 else
95 {
96 /* Move forwards */
97 dm_vector_add(&vr, &va);
98 vlen++;
99 }
100 }
101 while (!wasHit && vlen <= vrayLen);
102
103 /* Check if the ray hit something, e.g. is this point occluded? */
104 if (wasHit && vlen < vrayLen)
105 {
106 vfactor = vlen * 0.01;
107 }
108 else
109 vfactor = vlen * 0.02;
110 #endif
111
112 #if 1
113 {
114 /* Calculate light's intensity based on the angle it "hits"
115 *
116 * 1) Calculate the vectors that form the imaginary "plane"
117 * 2) Cross-product -> normal vector of the plane
118 * 2) Normalize the normal vector
119 * 3) Calculate light vector's hit angle by dot product
120 */
121 DMVector v1, v2;
122 DMFloat c;
123
124 v1.x = 2.0f;
125 v1.y = 0.0f;
126 v1.z = (DMFloat) (pheightMap[j][i] - pheightMap[j][i + 1]);
127
128 v2.x = 0.0f;
129 v2.y = 2.0f;
130 v2.z = (DMFloat) (pheightMap[j][i] - pheightMap[j + 1][i]);
131
132 dm_vector_cross(&vr, &v1, &v2);
133 dm_vector_normalize(&vr);
134 dm_vector_normalize(&vl);
135 c = dm_vector_dot(&vl, &vr);
136
137 vrayLen = 255 - (vrayLen * 0.1) * vrayLen + (c * 128.0f) + (vfactor * vfactor * 1255);
138 }
139 #else
140 vrayLen = 255 - vrayLen * vrayLen * (vfactor * vfactor);
141 if (vrayLen < 0) vrayLen = 0;
142 vrayLen += pheightMap[j][i];
143 #endif
144
145 /* Clip result */
146 if (vrayLen < 0)
147 vrayLen = 0;
148 else if (vrayLen > 255.0f)
149 vrayLen = 255.0f;
150
151 lightMap[j][i] = vrayLen;
152 }
153 }
154
155
156 void dmShadowTraceHeightMap2(DMBlockMap lightMap, DMBlockMap pheightMap, DMVector *light)
157 {
158 int i, j;
159
160 light->z = 150;
161
162 for (j = 0; j < QHEIGHT; j++)
163 for (i = 0; i < QWIDTH; i++)
164 {
165 DMVector vr, vl, va;
166 DMFloat vrayLen, vfactor;
167 int vlen;
168 BOOL wasHit;
169
170 /* Perform shadow occlusion via simplistic raytracing */
171 vr.x = i;
172 vr.y = j;
173 vr.z = 200; //light->z; // - 10.0;
174
175 /* Calculate light vector vector */
176 dm_vector_sub_r(&vl, &vr, light);
177 vrayLen = dm_vector_length(&vl);
178
179 #if 1
180 dm_vector_copy(&va, &vl);
181 dm_vector_normalize(&va);
182 dm_vector_copy(&vr, light);
183
184 vlen = 0;
185 wasHit = FALSE;
186 do
187 {
188 float h;
189
190 /* If ray is inside the heightmap, get value */
191 if (vr.x >= 0 && vr.y >= 0 && vr.x < QWIDTH && vr.y < QHEIGHT)
192 h = pheightMap[(int) vr.y][(int) vr.x];
193 else
194 break;
195
196 /* Check for hits */
197 if (h > vr.z)
198 wasHit = TRUE;
199 else
200 {
201 /* Move forwards */
202 dm_vector_add(&vr, &va);
203 vlen++;
204 }
205 }
206 while (!wasHit && vlen <= vrayLen);
207
208 /* Check if the ray hit something, e.g. is this point occluded? */
209 if (wasHit && vlen < vrayLen)
210 {
211 vfactor = vlen * 0.05;
212 }
213 else
214 vfactor = vlen * 0.001;
215 #endif
216
217 #if 0
218 {
219 /* Calculate light's intensity based on the angle it "hits"
220 *
221 * 1) Calculate the vectors that form the imaginary "plane"
222 * 2) Cross-product -> normal vector of the plane
223 * 2) Normalize the normal vector
224 * 3) Calculate light vector's hit angle by dot product
225 */
226 DMVector v1, v2;
227 DMFloat c;
228
229 v1.x = 2.0f;
230 v1.y = 0.0f;
231 v1.z = (DMFloat) (pheightMap[j][i] - pheightMap[j][i + 1]);
232
233 v2.x = 0.0f;
234 v2.y = 2.0f;
235 v2.z = (DMFloat) (pheightMap[j][i] - pheightMap[j + 1][i]);
236
237 dm_vector_cross(&vr, &v1, &v2);
238 dm_vector_normalize(&vr);
239 dm_vector_normalize(&vl);
240 c = dm_vector_dot(&vl, &vr);
241
242 vrayLen = 255 - (vrayLen * 0.1) * vrayLen + (c * 128.0f) + (vfactor * vfactor * 1255);
243 }
244 #else
245 vrayLen = 255 - vrayLen * vrayLen * (vfactor * vfactor);
246 if (vrayLen < 0) vrayLen = 0;
247 vrayLen -= pheightMap[j][i];
248 #endif
249
250 /* Clip result */
251 if (vrayLen < 0)
252 vrayLen = 0;
253 else if (vrayLen > 255.0f)
254 vrayLen = 255.0f;
255
256 lightMap[j][i] = vrayLen;
257 }
258 }
259
260
261 #define CREDITS_SPEED 1000
262 #define CREDITS_RAND 4
263
264 typedef struct
265 {
266 int x, y;
267 } DMCoords;
268
269
270 typedef struct
271 {
272 int x, y;
273 char *filename;
274 SDL_Surface *img;
275 } DMCredits;
276
277
278
279 static const DMCoords randomCoords[] =
280 {
281 { -300, -430 },
282 { 700, -550 },
283 { -200, 600 },
284 { 700, 600 }
285 };
286 const int nrandomCoords = sizeof(randomCoords) / sizeof(randomCoords[0]);
287
288
289 static DMCredits credits[] =
290 {
291 { 91, 223, "g4014.png", NULL },
292 { 151, 250, "g4026.png", NULL },
293 { 217, 227, "g4020.png", NULL },
294 { 173, 268, "g4032.png", NULL },
295 { 115, 359, "g4038.png", NULL },
296
297 { 437, 130, "g4062.png", NULL },
298 { 457, 102, "g4068.png", NULL },
299 { 450, 210, "g4056.png", NULL },
300
301 { 420, 320, "g4044.png", NULL },
302 { 486, 381, "g4050.png", NULL },
303 };
304
305 const int ncredits = sizeof(credits) / sizeof(credits[0]);
306
307
308 #define NOSFE_MIN 1
309 #define NOSFE_MAX 269
310
311 SDL_Surface *bmap;
312 SDL_Surface *nosfe[NOSFE_MAX - NOSFE_MIN + 1];
313
314
315 int demoPreInit(DMEngineData *engine)
316 {
317 dmInitProg("krapula",
318 "Lauantai Aamun Krapula",
319 "0.2", "(c) 2012 Anciat Prodz & TNSP", "PENIS.");
320
321 engine->optPackFilename = "orvellys.dat";
322 engine->optDataPath = NULL;
323 engine->optResFlags = DRF_USE_PACK | DRF_PRELOAD_RES;
324
325 engine->optAudioSetup = DM_ASETUP_JSS;
326
327 engine->optVidSetup = DM_VSETUP_ASPECT;
328 engine->optVidWidth = 640;
329 engine->optVidHeight = 480;
330 engine->optVidDepth = 32;
331 engine->optVFlags = SDL_SWSURFACE;
332
333
334 engine->demoInit = demoInit;
335 engine->demoRender = demoRender;
336 engine->demoShutdown = demoShutdown;
337 engine->demoQuit = demoQuit;
338
339 return DMERR_OK;
340 }
341
342
343 static int demoInit(DMEngineData *engine)
344 {
345 int i;
346
347 // Initialize effect stuff
348 dmPerlinInit(&perlinCtx, 1234);
349
350 for (i = 0; i < NOSFE_MAX; i++)
351 {
352 char fname[32];
353 snprintf(fname, sizeof(fname), "%08d.jpg", NOSFE_MIN + i);
354 engineGetResImage(engine, nosfe[i], fname);
355 }
356
357 for (i = 0; i < ncredits; i++)
358 engineGetResImage(engine, credits[i].img, credits[i].filename);
359
360 bmap = SDL_CreateRGBSurface(SDL_SWSURFACE, QWIDTH, QHEIGHT, 8, 0, 0, 0, 0);
361
362
363 // Initialize music player
364 JSSModule *mod = NULL;
365 engineGetResModule(engine, mod, "krapula.jss");
366
367 if ((i = jssConvertModuleForPlaying(mod)) != DMERR_OK)
368 {
369 dmErrorMsg("Could not convert module for playing, %d: %s\n",
370 i, dmErrorStr(i));
371 return DMERR_INIT_FAIL;
372 }
373
374 jvmSetCallback(engine->jssDev, jmpExec, engine->jssPlr);
375 jmpSetModule(engine->jssPlr, mod);
376 jmpPlayOrder(engine->jssPlr, 0);
377 jvmSetGlobalVol(engine->jssDev, 55);
378
379 return DMERR_OK;
380 }
381
382
383 static void demoShutdown(DMEngineData *engine)
384 {
385 (void) engine;
386 SDL_FreeSurface(bmap);
387 }
388
389
390 static void demoQuit()
391 {
392 dmPrint(0, "Krapulassa on kivaa.\n");
393 }
394
395
396 static int demoRender(DMEngineData *engine)
397 {
398 float t = engineGetTimeDT(engine);
399
400 if (t < 5)
401 {
402 //
403 // Anciat prodz logo
404 //
405 int dt = engineGetTime(engine, 0);
406 static SDL_Surface *anciat;
407 static DMLerpContext lerpX, lerpY, lerpD;
408 static DMScaledBlitFunc nblit;
409 DMVector light;
410 static BOOL nollattu = FALSE;
411 if (!nollattu)
412 {
413 engineGetResImage(engine, anciat, "anciat.png");
414 nblit = dmGetScaledBlitFunc(bmap->format, engine->screen->format, DMD_NONE);
415 dmMakePalette(bmap);
416 dmLerpInit(&lerpX, 0, QWIDTH, 5000);
417 dmLerpInit(&lerpY, QHEIGHT * 0.25, QHEIGHT * 0.75, 5000);
418 dmLerpInit(&lerpD, 0.04, 0.08, 5000);
419 nollattu = TRUE;
420 }
421
422 light.x = dmLerpSCurve(&lerpX, dt);
423 light.y = dmLerp1(&lerpY, dt);
424 light.z = 128;
425
426 dmShadowTraceHeightMap2(bmap->pixels, anciat->pixels, &light);
427 nblit(bmap, 0, 0, engine->screen->w, engine->screen->h, engine->screen);
428 }
429 else
430 if (t < 10)
431 {
432 //
433 // Demo "logo" texts
434 //
435 int dt = engineGetTime(engine, 5);
436 static SDL_Surface *logobg, *logolayer1, *logolayer2;
437 static DMScaledBlitFunc nblit, kblit;
438 static DMLerpContext lerpD;
439 static BOOL nollattu = FALSE;
440
441 if (!nollattu)
442 {
443 engineGetResImage(engine, logobg, "logobg.png");
444 engineGetResImage(engine, logolayer1, "logolayer1.png");
445 engineGetResImage(engine, logolayer2, "logolayer2.png");
446
447 nblit = dmGetScaledBlitFunc(logobg->format, engine->screen->format, DMD_TRANSPARENT);
448 kblit = dmGetScaledBlitFunc(logobg->format, engine->screen->format, DMD_NONE);
449 dmLerpInit(&lerpD, 0.01, 500, 10000);
450 nollattu = TRUE;
451 }
452
453 float q = dmLerpSCurve(&lerpD, dt);
454 float t = sin((float) dt / 150.0f);
455 int x = t * 25.0f + q, y = t * 35.0f + q*2.0f,
456 w = t * 70.0f + q, h = t * 40.0f + q*2.0f;
457
458 float t2 = sin((float) dt / 150.0f + 0.2f);
459 int x2 = t2 * 25.0f + q, y2 = t * 35.0f + q*2.0f,
460 w2 = t2 * 70.0f + q, h2 = t * 40.0f + q*2.0f;
461
462 kblit(logobg, 0, 0, engine->screen->w, engine->screen->h, engine->screen);
463 nblit(logolayer1, -x, -y, engine->screen->w+w, engine->screen->h+h, engine->screen);
464 nblit(logolayer2, -x2, -y2, engine->screen->w+w2, engine->screen->h+h2, engine->screen);
465 }
466 else
467 if (t < 20)
468 {
469 //
470 // "Gaytracing"
471 //
472 int dt = engineGetTime(engine, 10);
473 static SDL_Surface *gay, *logobg;
474 static DMLerpContext lerpX, lerpY, lerpD;
475 static DMScaledBlitFunc nblit, kblit;
476 static BOOL nollattu = FALSE;
477 DMVector light;
478 DMBlockMap heightMap;
479
480 if (!nollattu)
481 {
482 engineGetResImage(engine, gay, "gay.png");
483 engineGetResImage(engine, logobg, "logobg.png");
484 nblit = dmGetScaledBlitFunc(bmap->format, engine->screen->format, DMD_NONE);
485 kblit = dmGetScaledBlitFunc(logobg->format, engine->screen->format, DMD_TRANSPARENT);
486 dmMakePalette(bmap);
487 dmLerpInit(&lerpX, QWIDTH, 0, 10000);
488 dmLerpInit(&lerpY, QHEIGHT * 0.25, QHEIGHT * 0.75, 10000);
489 dmLerpInit(&lerpD, 0.04, 0.08, 10000);
490 nollattu = TRUE;
491 }
492
493 light.x = dmLerpSCurve(&lerpX, dt);
494 light.y = QHEIGHT * 0.5 + sin(dmLerp1(&lerpY, dt)) * 0.5;
495 light.z = 128;
496
497 dmMakeBumpMap(heightMap, dmLerpSCurve(&lerpD, dt), 254);
498
499 dmShadowTraceHeightMap(bmap->pixels, heightMap, &light);
500
501 nblit(bmap, 0, 0, engine->screen->w, engine->screen->h, engine->screen);
502
503 if ((dt / 100) % 10 < 5)
504 {
505 kblit(gay, 0, 0, engine->screen->w, engine->screen->h, engine->screen);
506 }
507 }
508 else
509 if (t < 45)
510 {
511 //
512 // Nosfe video/animation + credits
513 //
514 static SDL_Surface *ruutu;
515 static int currState, currCredit, creditStartTime;
516 static DMLerpContext lerpX, lerpY, lerpZ;
517 static DMScaledBlitFunc nblit, kblit;
518 static BOOL stateChange, nollattu = FALSE;
519 int currFrame = engineGetTime(engine, 20) * 15 / 1000;
520 if (!nollattu)
521 {
522 engineGetResImage(engine, ruutu, "ruutu.png");
523 dmClearSurface(ruutu, dmMapRGBA(ruutu, 0,0,0,0));
524 nblit = dmGetScaledBlitFunc(nosfe[0]->format, engine->screen->format, DMD_NONE);
525 kblit = dmGetScaledBlitFunc(credits[0].img->format, engine->screen->format, DMD_TRANSPARENT);
526 currCredit = -1;
527 currState = -1;
528 stateChange = TRUE;
529 nollattu = TRUE;
530 }
531
532 float gt = 1.0f + sin(engineGetTime(engine, 0) / 250.0f);
533 int g1 = gt * 25.0f, g2 = gt * 50.0f;
534
535 nblit(nosfe[currFrame % NOSFE_MAX], -g1, -g1, engine->screen->w+g2, engine->screen->h+g2, engine->screen);
536
537 if (t >= 30)
538 {
539 int qtime = engineGetTime(engine, 30);
540 int creditTime = engineGetTime(engine, 0) - creditStartTime;
541 float zscale;
542 if ( ( (qtime / (CREDITS_SPEED + 500)) % 2) == 0 && currState == -1)
543 stateChange = TRUE;
544
545 if (stateChange && currCredit < ncredits)
546 {
547 stateChange = FALSE;
548 switch (currState)
549 {
550 case 0:
551 {
552 int qt = (qtime / 100) % nrandomCoords;
553 creditStartTime = engineGetTime(engine, 0);
554 creditTime = 0;
555 dmLerpInit(&lerpX, randomCoords[qt].x, credits[currCredit].x - 50, CREDITS_SPEED);
556 dmLerpInit(&lerpY, randomCoords[qt].y, credits[currCredit].y - 50, CREDITS_SPEED);
557 dmLerpInit(&lerpZ, 5.0f, 0.0f, CREDITS_SPEED);
558 currState = 1;
559 }
560 break;
561
562 case 2:
563 if (creditTime >= CREDITS_SPEED)
564 creditTime = CREDITS_SPEED - 1;
565
566 zscale = dmLerpSCurve(&lerpZ, creditTime);
567 dmScaledBlitSurface32to32TransparentX(
568 credits[currCredit].img,
569 dmLerpSCurve(&lerpX, creditTime) - (zscale * credits[currCredit].img->w),
570 dmLerpSCurve(&lerpY, creditTime) - (zscale * credits[currCredit].img->h),
571 credits[currCredit].img->w * (1.0f + zscale),
572 credits[currCredit].img->h * (1.0f + zscale),
573 ruutu);
574
575 currState = -1;
576 break;
577
578 default:
579 currCredit++;
580 currState = 0;
581 stateChange = TRUE;
582 break;
583 }
584 }
585
586
587 if (currCredit > 0)
588 {
589 int zk = cos(engineGetTime(engine, 0) / 250.0f) * 25;
590 kblit(ruutu, -zk, -zk, engine->screen->w + zk*2, engine->screen->h + zk*2, engine->screen);
591 }
592
593 if (currState == 1)
594 {
595 if (creditTime >= CREDITS_SPEED)
596 {
597 creditTime = CREDITS_SPEED;
598 stateChange = TRUE;
599 currState = 2;
600 }
601
602 zscale = dmLerpSCurve(&lerpZ, creditTime);
603 kblit(credits[currCredit].img,
604 dmLerpSCurve(&lerpX, creditTime) - (zscale * credits[currCredit].img->w),
605 dmLerpSCurve(&lerpY, creditTime) - (zscale * credits[currCredit].img->h),
606 credits[currCredit].img->w * (1.0f + zscale),
607 credits[currCredit].img->h * (1.0f + zscale),
608 engine->screen);
609 }
610 }
611
612 }
613 else
614 if (t < 60)
615 {
616 //
617 // Greetings
618 //
619 int dt = engineGetTime(engine, 45);
620 static SDL_Surface *logobg, *greets;
621 static DMScaledBlitFunc nblit, kblit;
622 static DMLerpContext lerpD;
623 static BOOL nollattu = FALSE;
624
625 if (!nollattu)
626 {
627 engineGetResImage(engine, logobg, "logobg.png");
628 engineGetResImage(engine, greets, "greetings.png");
629 nblit = dmGetScaledBlitFunc(logobg->format, engine->screen->format, DMD_TRANSPARENT);
630 kblit = dmGetScaledBlitFunc(logobg->format, engine->screen->format, DMD_NONE);
631 dmLerpInit(&lerpD, 0.01, 500, 10000);
632 nollattu = TRUE;
633 }
634
635 float q = dmLerpSCurve(&lerpD, dt);
636 float t = sin((float) dt / 150.0f),
637 j = (1.0 + t) * 15;
638 int x = t * 25.0f + q, y = t * 35.0f + q,
639 w = t * 70.0f + q*2.0f, h = t * 40.0f + q*2.0f;
640
641 kblit(logobg, -j, -j, engine->screen->w+j*2.0f, engine->screen->h+j*2.0f, engine->screen);
642 nblit(greets, -x, -y, engine->screen->w+w, engine->screen->h+h, engine->screen);
643 }
644 else
645 engine->exitFlag = TRUE;
646
647
648 //
649 // Flash/fade thingy
650 //
651 {
652 static SDL_Surface *feidi;
653 static int fadeStartTime;
654 static BOOL fadeActive, nollattu = FALSE;
655 static DMLerpContext fadeLerp;
656 BOOL hit;
657 int ch;
658
659 if (!nollattu)
660 {
661 engineGetResImage(engine, feidi, "feidi.png");
662 dmLerpInit(&fadeLerp, 255, 0, 250);
663 nollattu = TRUE;
664 }
665
666 JSS_LOCK(engine->jssPlr);
667 for (hit = FALSE, ch = 0; ch < 6; ch++)
668 if (engine->jssPlr->channels[ch].nextInstrument == 0)
669 {
670 hit = TRUE;
671 break;
672 }
673 JSS_UNLOCK(engine->jssPlr);
674
675 if (hit && !fadeActive)
676 {
677 fadeActive = TRUE;
678 fadeStartTime = engineGetTime(engine, 0);
679 }
680 if (fadeActive)
681 {
682 int fadeTime = engineGetTime(engine, 0) - fadeStartTime;
683 if (fadeTime < 250)
684 {
685 dmScaledBlitSurface32to32TransparentGA(feidi,
686 0, 0, engine->screen->w, engine->screen->h, engine->screen,
687 dmLerpSCurve(&fadeLerp, fadeTime));
688 }
689 else
690 fadeActive = FALSE;
691 }
692 }
693
694 return DMERR_OK;
695 }