Mercurial > hg > demos > krapula
comparison krapula.c @ 21:ea93b1d5c894
Use the dmsimple demo engine "framework".
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 29 Sep 2012 17:00:01 +0300 |
parents | 64017da3aa2f |
children | af6fad875a28 |
comparison
equal
deleted
inserted
replaced
20:67ee1ae6185e | 21:ea93b1d5c894 |
---|---|
1 #include "dmlib.h" | 1 #include "dmsimple.h" |
2 #include "dmargs.h" | |
3 #include "dmvecmat.h" | 2 #include "dmvecmat.h" |
4 #include "dmres.h" | |
5 #include "dmimage.h" | |
6 | |
7 #include "jss.h" | |
8 #include "jssmod.h" | |
9 #include "jssmix.h" | |
10 #include "jssplr.h" | |
11 | 3 |
12 #include <math.h> | 4 #include <math.h> |
13 | 5 |
6 | |
14 #define DM_COLORS (256) | 7 #define DM_COLORS (256) |
15 | 8 |
16 | 9 |
17 typedef struct | 10 DMOptArg optList[] = |
18 { | 11 { |
19 int x, y; | |
20 } DMCoords; | |
21 | |
22 | |
23 typedef struct | |
24 { | |
25 int x, y; | |
26 char *filename; | |
27 SDL_Surface *img; | |
28 } DMCredits; | |
29 | |
30 | |
31 typedef struct | |
32 { | |
33 int currFrame, endTime, startTime, totalFrameTime; | |
34 BOOL pauseFlag, exitFlag; | |
35 SDL_Surface *screen; | |
36 SDL_Event event; | |
37 } DMEngineData; | |
38 | |
39 | |
40 typedef struct | |
41 { | |
42 int currFrame, endTime, startTime; | |
43 } DMFrameData; | |
44 | |
45 | |
46 | |
47 | |
48 int optVFlags = SDL_SWSURFACE | SDL_HWPALETTE; | |
49 int optScrWidth = 640, optScrHeight = 480; | |
50 int optBenchmarkLen = 20; | |
51 | |
52 | |
53 DMOptArg optList[] = { | |
54 { 0, '?', "help", "Show this help", OPT_NONE }, | 12 { 0, '?', "help", "Show this help", OPT_NONE }, |
55 { 2, 'v', "verbose", "Be more verbose", OPT_NONE }, | 13 { 2, 'v', "verbose", "Be more verbose", OPT_NONE }, |
56 { 3, 'f', "fs", "Fullscreen", OPT_NONE }, | 14 { 3, 'f', "fs", "Fullscreen", OPT_NONE }, |
57 // { 5, 's', "size", "Screen resolution -s 640x480", OPT_ARGREQ }, | |
58 }; | 15 }; |
59 | 16 |
60 const int optListN = sizeof(optList) / sizeof(optList[0]); | 17 const int optListN = sizeof(optList) / sizeof(optList[0]); |
61 | 18 |
62 | 19 |
79 case 2: | 36 case 2: |
80 dmVerbosity++; | 37 dmVerbosity++; |
81 break; | 38 break; |
82 | 39 |
83 case 3: | 40 case 3: |
84 optVFlags |= SDL_FULLSCREEN; | 41 engine.optVFlags |= SDL_FULLSCREEN; |
85 break; | |
86 | |
87 case 5: | |
88 { | |
89 int w, h; | |
90 if (sscanf(optArg, "%dx%d", &w, &h) == 2) | |
91 { | |
92 if (w < 320 || h < 200 || w > 1024 || h > 768) | |
93 { | |
94 dmError("Invalid width or height: %d x %d\n", w, h); | |
95 return FALSE; | |
96 } | |
97 optScrWidth = w; | |
98 optScrHeight = h; | |
99 } | |
100 else | |
101 { | |
102 dmError("Invalid size argument '%s'.\n", optArg); | |
103 return FALSE; | |
104 } | |
105 } | |
106 break; | 42 break; |
107 | 43 |
108 default: | 44 default: |
109 dmError("Unknown option '%s'.\n", currArg); | 45 dmError("Unknown option '%s'.\n", currArg); |
110 return FALSE; | 46 return FALSE; |
111 } | 47 } |
112 | 48 |
113 return TRUE; | 49 return TRUE; |
114 } | 50 } |
51 | |
52 | |
115 | 53 |
116 | 54 |
117 void dmMakePalette(SDL_Surface *scr) | 55 void dmMakePalette(SDL_Surface *scr) |
118 { | 56 { |
119 SDL_Color pal[DM_COLORS]; | 57 SDL_Color pal[DM_COLORS]; |
398 lightMap[j][i] = vrayLen; | 336 lightMap[j][i] = vrayLen; |
399 } | 337 } |
400 } | 338 } |
401 | 339 |
402 | 340 |
403 void engineAudioCallback(void *userdata, Uint8 *stream, int len) | |
404 { | |
405 JSSMixer *d = (JSSMixer *) userdata; | |
406 | |
407 if (d != NULL) | |
408 { | |
409 jvmRenderAudio(d, stream, len / jvmGetSampleSize(d)); | |
410 } | |
411 } | |
412 | |
413 #define DEBUG 0 | |
414 | |
415 #define CREDITS_SPEED 1000 | 341 #define CREDITS_SPEED 1000 |
416 #define CREDITS_RAND 4 | 342 #define CREDITS_RAND 4 |
417 | 343 |
418 #define NOSFE_MIN 1 | 344 typedef struct |
419 #define NOSFE_MAX 269 | 345 { |
346 int x, y; | |
347 } DMCoords; | |
348 | |
349 | |
350 typedef struct | |
351 { | |
352 int x, y; | |
353 char *filename; | |
354 SDL_Surface *img; | |
355 } DMCredits; | |
356 | |
357 | |
420 | 358 |
421 static const DMCoords randomCoords[] = | 359 static const DMCoords randomCoords[] = |
422 { | 360 { |
423 { -300, -430 }, | 361 { -300, -430 }, |
424 { 700, -550 }, | 362 { 700, -550 }, |
445 }; | 383 }; |
446 | 384 |
447 const int ncredits = sizeof(credits) / sizeof(credits[0]); | 385 const int ncredits = sizeof(credits) / sizeof(credits[0]); |
448 | 386 |
449 | 387 |
450 | 388 #define NOSFE_MIN 1 |
451 | 389 #define NOSFE_MAX 269 |
452 DMEngineData engine; | 390 |
453 DMFrameData frame; | 391 SDL_Surface *bmap; |
454 | 392 SDL_Surface *nosfe[NOSFE_MAX - NOSFE_MIN + 1]; |
455 int engineGetTick() | 393 |
456 { | 394 |
457 return (frame.startTime - engine.startTime) + DEBUG * 1000; | 395 int demoPreInit(int argc, char *argv[]) |
458 } | 396 { |
459 | |
460 float engineGetTimeDT() | |
461 { | |
462 return (float) engineGetTick() / 1000.0f; | |
463 } | |
464 | |
465 | |
466 int engineGetTimeDTi() | |
467 { | |
468 return (float) engineGetTick() / 1000; | |
469 } | |
470 | |
471 | |
472 int engineGetTime(int t) | |
473 { | |
474 return engineGetTick() - (1000 * t);; | |
475 } | |
476 | |
477 | |
478 int engineGetDT(int t) | |
479 { | |
480 return engineGetTime(t) / 1000; | |
481 } | |
482 | |
483 | |
484 int dmScaledBlitSurface32to32TransparentX(SDL_Surface *src, const int x0, const int y0, const int dwidth, const int dheight, SDL_Surface *dst); | |
485 | |
486 | |
487 int engineResImageLoad(DMResource *res) | |
488 { | |
489 SDL_Surface *img = dmLoadImage(res); | |
490 if (res != NULL) | |
491 { | |
492 res->rdata = img; | |
493 return DMERR_OK; | |
494 } | |
495 else | |
496 return dmferror(res); | |
497 } | |
498 | |
499 void engineResImageFree(DMResource *res) | |
500 { | |
501 SDL_FreeSurface((SDL_Surface *)res->rdata); | |
502 } | |
503 | |
504 int engineResModuleLoad(DMResource *res) | |
505 { | |
506 return jssLoadXM(res, (JSSModule **) &(res->rdata)); | |
507 } | |
508 | |
509 void engineResModuleFree(DMResource *res) | |
510 { | |
511 jssFreeModule((JSSModule *) res->rdata); | |
512 } | |
513 | |
514 | |
515 static DMResourceDataOps engineResImage = | |
516 { | |
517 engineResImageLoad, | |
518 engineResImageFree | |
519 }; | |
520 | |
521 static DMResourceDataOps engineResModule = | |
522 { | |
523 engineResModuleLoad, | |
524 engineResModuleFree | |
525 }; | |
526 | |
527 | |
528 int engineClassifier(DMResource *res) | |
529 { | |
530 DMResourceDataOps *rops = NULL; | |
531 char *fext; | |
532 | |
533 if (res == NULL) | |
534 return DMERR_NULLPTR; | |
535 | |
536 if ((fext = strrchr(res->filename, '.')) != NULL) | |
537 { | |
538 if (strcasecmp(fext, ".png") == 0 || strcasecmp(fext, ".jpg") == 0) | |
539 rops = &engineResImage; | |
540 else | |
541 if (strcasecmp(fext, ".xm") == 0 || strcasecmp(fext, ".jmod") == 0) | |
542 rops = &engineResModule; | |
543 } | |
544 | |
545 res->rops = rops; | |
546 | |
547 return DMERR_OK; | |
548 } | |
549 | |
550 | |
551 void *engineGetResource(const char *name) | |
552 { | |
553 DMResource *res = dmres_find(name); | |
554 if (res != NULL && res->rdata != NULL) | |
555 return res->rdata; | |
556 else | |
557 { | |
558 dmError("Could not find resource '%s'.\n", name); | |
559 return NULL; | |
560 } | |
561 } | |
562 | |
563 | |
564 #define engineGetResImage(name) (SDL_Surface *) engineGetResource(name) | |
565 #define engineGetResModule(name) (JSSModule *) engineGetResource(name) | |
566 | |
567 | |
568 int engineLoadResources() | |
569 { | |
570 int err, loaded, total; | |
571 | |
572 err = dmres_preload(TRUE, &loaded, &total); | |
573 | |
574 while ((err = dmres_preload(FALSE, &loaded, &total)) == DMERR_PROGRESS) | |
575 { | |
576 // Show a nice progress bar while loading | |
577 if (total > 0 && (loaded % 2) == 0) | |
578 { | |
579 int dx = 60, | |
580 dh = 20, | |
581 dw = engine.screen->w - (2 * dx), | |
582 dy = (engine.screen->h - dh) / 2; | |
583 | |
584 if (SDL_MUSTLOCK(engine.screen) != 0 && SDL_LockSurface(engine.screen) != 0) | |
585 return DMERR_INIT_FAIL; | |
586 | |
587 // Draw the progress bar | |
588 dmClearSurface(engine.screen, dmMapRGBA(engine.screen, 0,0,0,0)); | |
589 dmFillRect(engine.screen, dx, dy, dx+dw, dy+dh, dmMapRGB(engine.screen, 255,255,255)); | |
590 dmFillRect(engine.screen, dx+1, dy+1, dx+dw-1, dy+dh-1, dmMapRGB(engine.screen, 0,0,0)); | |
591 | |
592 if (total > 0) | |
593 { | |
594 dmFillRect(engine.screen, | |
595 dx+3, dy+3, | |
596 dx + 3 + ((dw - 3) * loaded) / total, | |
597 dy + dh - 3, | |
598 dmMapRGB(engine.screen, 200,200,200)); | |
599 } | |
600 | |
601 // Flip screen | |
602 if (SDL_MUSTLOCK(engine.screen) != 0) | |
603 SDL_UnlockSurface(engine.screen); | |
604 | |
605 SDL_Flip(engine.screen); | |
606 } | |
607 } | |
608 | |
609 return err; | |
610 } | |
611 | |
612 | |
613 | |
614 int main(int argc, char *argv[]) | |
615 { | |
616 BOOL initSDL = FALSE; | |
617 JSSModule *mod = NULL; | |
618 JSSMixer *dev = NULL; | |
619 JSSPlayer *plr = NULL; | |
620 int err, i; | |
621 SDL_AudioSpec afmt; | |
622 | |
623 memset(&afmt, 0, sizeof(afmt)); | |
624 memset(&frame, 0, sizeof(frame)); | |
625 memset(&engine, 0, sizeof(engine)); | |
626 | |
627 dmInitProg("krapula", "Lauantai Aamun Krapula", "0.2", "(c) 2012 Anciat Prodz & TNSP", "PENIS."); | 397 dmInitProg("krapula", "Lauantai Aamun Krapula", "0.2", "(c) 2012 Anciat Prodz & TNSP", "PENIS."); |
398 | |
399 engine.optScrWidth = 640; | |
400 engine.optScrHeight = 480; | |
401 engine.optBitDepth = 32; | |
402 engine.optVFlags = SDL_SWSURFACE | SDL_HWPALETTE; | |
403 | |
404 | |
628 if (!dmArgsProcess(argc, argv, optList, optListN, | 405 if (!dmArgsProcess(argc, argv, optList, optListN, |
629 argHandleOpt, NULL, FALSE)) | 406 argHandleOpt, NULL, FALSE)) |
630 exit(1); | 407 return DMERR_INIT_FAIL; |
631 | 408 |
632 dmPrint(0, "%s\n", dmProgDesc); | 409 dmPrint(0, "%s\n", dmProgDesc); |
633 dmPrint(0, "%s\n", dmProgAuthor); | 410 dmPrint(0, "%s\n", dmProgAuthor); |
634 dmPrint(0, "TNSP PIERUPASKA engine 2012 'passeli professional' loading.\n"); | 411 dmPrint(0, "TNSP PIERUPASKA engine 2012 'passeli professional' loading.\n"); |
635 | 412 |
636 // Initialize resource subsystem | 413 return DMERR_OK; |
637 dmPrint(1, "Initializing resources subsystem.\n"); | 414 } |
638 if ((err = dmres_init("orvellys.dat", NULL, DRF_USE_PACK | DRF_PRELOAD_RES, engineClassifier)) != DMERR_OK) | 415 |
639 { | 416 |
640 dmError("Could not initialize resource manager: %d, %s.\n", err, dmErrorStr(err)); | 417 int demoGlobalInit() |
641 goto error_exit; | 418 { |
642 } | 419 int i; |
643 | |
644 | |
645 // Initialize SDL components | |
646 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) != 0) | |
647 { | |
648 dmError("Could not initialize SDL: %s\n", SDL_GetError()); | |
649 goto error_exit; | |
650 } | |
651 initSDL = TRUE; | |
652 | |
653 | |
654 // Initialize JSS | |
655 jssInit(); | |
656 | |
657 afmt.freq = 44100; | |
658 afmt.format = AUDIO_S16SYS; | |
659 afmt.channels = 2; | |
660 afmt.samples = 16*1024; | |
661 | |
662 dmPrint(1, "Initializing miniJSS mixer with: %d, %d, %d\n", | |
663 JSS_AUDIO_S16, afmt.channels, afmt.freq); | |
664 | |
665 if ((dev = jvmInit(JSS_AUDIO_S16, afmt.channels, afmt.freq, JMIX_AUTO)) == NULL) | |
666 { | |
667 dmError("jvmInit() returned NULL, voi perkele.\n"); | |
668 goto error_exit; | |
669 } | |
670 | |
671 if ((plr = jmpInit(dev)) == NULL) | |
672 { | |
673 dmError("jmpInit() returned NULL\n"); | |
674 goto error_exit; | |
675 } | |
676 | |
677 // Initialize SDL audio | |
678 dmPrint(1, "Trying to init SDL audio with: %d, %d, %d\n", | |
679 afmt.format, afmt.channels, afmt.freq); | |
680 | |
681 afmt.callback = engineAudioCallback; | |
682 afmt.userdata = (void *) dev; | |
683 | |
684 if (SDL_OpenAudio(&afmt, NULL) < 0) | |
685 { | |
686 dmError("Couldn't open audio: %s\n", SDL_GetError()); | |
687 goto error_exit; | |
688 } | |
689 | |
690 // Initialize SDL video | |
691 dmPrint(1, "Initializing SDL video %d x %d x %dbpp - %08x flags\n", | |
692 optScrWidth, optScrHeight, 32, optVFlags); | |
693 | |
694 engine.screen = SDL_SetVideoMode(optScrWidth, optScrHeight, 32, optVFlags); | |
695 if (engine.screen == NULL) | |
696 { | |
697 dmError("Can't SDL_SetVideoMode(): %s\n", SDL_GetError()); | |
698 goto error_exit; | |
699 } | |
700 | |
701 SDL_ShowCursor(SDL_DISABLE); | |
702 SDL_WM_SetCaption(dmProgDesc, dmProgName); | |
703 | |
704 | |
705 // Load resources | |
706 err = engineLoadResources(); | |
707 if (err != DMERR_OK) | |
708 { | |
709 dmError("Error loading resources, %d: %s.\n", | |
710 err, dmErrorStr(err)); | |
711 goto error_exit; | |
712 } | |
713 | 420 |
714 // Initialize effect stuff | 421 // Initialize effect stuff |
715 dmPerlinInit(); | 422 dmPerlinInit(); |
716 SDL_Surface *nosfe[NOSFE_MAX - NOSFE_MIN + 1]; | 423 |
717 for (i = 0; i < NOSFE_MAX; i++) | 424 for (i = 0; i < NOSFE_MAX; i++) |
718 { | 425 { |
719 char fname[64]; | 426 char fname[32]; |
720 snprintf(fname, sizeof(fname), "%08d.jpg", NOSFE_MIN + i); | 427 snprintf(fname, sizeof(fname), "%08d.jpg", NOSFE_MIN + i); |
721 nosfe[i] = engineGetResImage(fname); | 428 engineGetResImage(nosfe[i], fname); |
722 } | 429 } |
723 | 430 |
724 for (i = 0; i < ncredits; i++) | 431 for (i = 0; i < ncredits; i++) |
725 credits[i].img = engineGetResImage(credits[i].filename); | 432 engineGetResImage(credits[i].img, credits[i].filename); |
726 | 433 |
727 SDL_Surface *bmap = SDL_CreateRGBSurface(SDL_SWSURFACE, QWIDTH, QHEIGHT, 8, 0, 0, 0, 0); | 434 bmap = SDL_CreateRGBSurface(SDL_SWSURFACE, QWIDTH, QHEIGHT, 8, 0, 0, 0, 0); |
728 | 435 |
729 | 436 |
730 // Initialize music player | 437 // Initialize music player |
731 jvmSetCallback(dev, jmpExec, plr); | 438 JSSModule *mod = NULL; |
732 jmpSetModule(plr, engineGetResModule("krapula.xm")); | 439 engineGetResModule(mod, "krapula.xm"); |
733 jmpPlayOrder(plr, 0); | 440 jvmSetCallback(engine.dev, jmpExec, engine.plr); |
734 jvmSetGlobalVol(dev, 55); | 441 jmpSetModule(engine.plr, mod); |
442 jmpPlayOrder(engine.plr, 0); | |
443 jvmSetGlobalVol(engine.dev, 55); | |
735 SDL_PauseAudio(0); | 444 SDL_PauseAudio(0); |
736 | 445 |
737 | 446 return DMERR_OK; |
738 engine.startTime = SDL_GetTicks(); | 447 } |
739 | 448 |
740 while (!engine.exitFlag) | 449 |
741 { | 450 int demoMainTick() |
742 while (SDL_PollEvent(&engine.event)) | 451 { |
743 switch (engine.event.type) | 452 float t = engineGetTimeDT(); |
744 { | 453 |
745 case SDL_KEYDOWN: | 454 if (t < 5) |
746 switch (engine.event.key.keysym.sym) | 455 { |
456 int dt = engineGetTime(0); | |
457 static SDL_Surface *anciat; | |
458 static DMLerpContext lerpX, lerpY, lerpD; | |
459 static DMScaledBlitFunc nblit; | |
460 DMVector light; | |
461 static BOOL nollattu = FALSE; | |
462 if (!nollattu) | |
463 { | |
464 engineGetResImage(anciat, "anciat.png"); | |
465 nblit = dmGetScaledBlitFunc(bmap->format, engine.screen->format, DMD_NONE); | |
466 dmMakePalette(bmap); | |
467 dmLerpInit(&lerpX, 0, QWIDTH, 5000); | |
468 dmLerpInit(&lerpY, QHEIGHT * 0.25, QHEIGHT * 0.75, 5000); | |
469 dmLerpInit(&lerpD, 0.04, 0.08, 5000); | |
470 nollattu = TRUE; | |
471 } | |
472 | |
473 light.x = dmLerpSCurve(&lerpX, dt); | |
474 light.y = dmLerp1(&lerpY, dt); | |
475 light.z = 128; | |
476 | |
477 dmShadowTraceHeightMap2(bmap->pixels, anciat->pixels, &light); | |
478 nblit(bmap, 0, 0, engine.screen->w, engine.screen->h, engine.screen); | |
479 } | |
480 else | |
481 if (t < 10) | |
482 { | |
483 int dt = engineGetTime(5); | |
484 static SDL_Surface *logobg, *logolayer1, *logolayer2; | |
485 static DMScaledBlitFunc nblit, kblit; | |
486 static DMLerpContext lerpD; | |
487 static BOOL nollattu = FALSE; | |
488 | |
489 if (!nollattu) | |
490 { | |
491 engineGetResImage(logobg, "logobg.png"); | |
492 engineGetResImage(logolayer1, "logolayer1.png"); | |
493 engineGetResImage(logolayer2, "logolayer2.png"); | |
494 | |
495 nblit = dmGetScaledBlitFunc(logobg->format, engine.screen->format, DMD_TRANSPARENT); | |
496 kblit = dmGetScaledBlitFunc(logobg->format, engine.screen->format, DMD_NONE); | |
497 dmLerpInit(&lerpD, 0.01, 500, 10000); | |
498 nollattu = TRUE; | |
499 } | |
500 | |
501 float q = dmLerpSCurve(&lerpD, dt); | |
502 float t = sin((float) dt / 150.0f); | |
503 int x = t * 25.0f + q, y = t * 35.0f + q*2.0f, | |
504 w = t * 70.0f + q, h = t * 40.0f + q*2.0f; | |
505 | |
506 float t2 = sin((float) dt / 150.0f + 0.2f); | |
507 int x2 = t2 * 25.0f + q, y2 = t * 35.0f + q*2.0f, | |
508 w2 = t2 * 70.0f + q, h2 = t * 40.0f + q*2.0f; | |
509 | |
510 kblit(logobg, 0, 0, engine.screen->w, engine.screen->h, engine.screen); | |
511 nblit(logolayer1, -x, -y, engine.screen->w+w, engine.screen->h+h, engine.screen); | |
512 nblit(logolayer2, -x2, -y2, engine.screen->w+w2, engine.screen->h+h2, engine.screen); | |
513 } | |
514 else | |
515 if (t < 20) | |
516 { | |
517 int dt = engineGetTime(10); | |
518 static SDL_Surface *gay, *logobg; | |
519 static DMLerpContext lerpX, lerpY, lerpD; | |
520 static DMScaledBlitFunc nblit, kblit; | |
521 static BOOL nollattu = FALSE; | |
522 DMVector light; | |
523 DMBlockMap heightMap; | |
524 | |
525 if (!nollattu) | |
526 { | |
527 engineGetResImage(gay, "gay.png"); | |
528 engineGetResImage(logobg, "logobg.png"); | |
529 nblit = dmGetScaledBlitFunc(bmap->format, engine.screen->format, DMD_NONE); | |
530 kblit = dmGetScaledBlitFunc(logobg->format, engine.screen->format, DMD_TRANSPARENT); | |
531 dmMakePalette(bmap); | |
532 dmLerpInit(&lerpX, QWIDTH, 0, 10000); | |
533 dmLerpInit(&lerpY, QHEIGHT * 0.25, QHEIGHT * 0.75, 10000); | |
534 dmLerpInit(&lerpD, 0.04, 0.08, 10000); | |
535 nollattu = TRUE; | |
536 } | |
537 | |
538 light.x = dmLerpSCurve(&lerpX, dt); | |
539 light.y = QHEIGHT * 0.5 + sin(dmLerp1(&lerpY, dt)) * 0.5; | |
540 light.z = 128; | |
541 | |
542 dmMakeBumpMap(heightMap, dmLerpSCurve(&lerpD, dt), 254); | |
543 | |
544 dmShadowTraceHeightMap(bmap->pixels, heightMap, &light); | |
545 | |
546 nblit(bmap, 0, 0, engine.screen->w, engine.screen->h, engine.screen); | |
547 | |
548 if ((dt / 100) % 10 < 5) | |
549 { | |
550 kblit(gay, 0, 0, engine.screen->w, engine.screen->h, engine.screen); | |
551 } | |
552 } | |
553 else | |
554 if (t < 45) | |
555 { | |
556 static SDL_Surface *ruutu; | |
557 static int currState, currCredit, creditStartTime; | |
558 static DMLerpContext lerpX, lerpY, lerpZ; | |
559 static DMScaledBlitFunc nblit, kblit; | |
560 static BOOL stateChange, nollattu = FALSE; | |
561 int currFrame = engineGetTime(20) * 15 / 1000; | |
562 if (!nollattu) | |
563 { | |
564 engineGetResImage(ruutu, "ruutu.png"); | |
565 dmClearSurface(ruutu, dmMapRGBA(ruutu, 0,0,0,0)); | |
566 nblit = dmGetScaledBlitFunc(nosfe[0]->format, engine.screen->format, DMD_NONE); | |
567 kblit = dmGetScaledBlitFunc(credits[0].img->format, engine.screen->format, DMD_TRANSPARENT); | |
568 currCredit = -1; | |
569 currState = -1; | |
570 stateChange = TRUE; | |
571 nollattu = TRUE; | |
572 } | |
573 | |
574 float gt = 1.0f + sin(engineGetTime(0) / 250.0f); | |
575 int g1 = gt * 25.0f, g2 = gt * 50.0f; | |
576 | |
577 nblit(nosfe[currFrame % NOSFE_MAX], -g1, -g1, engine.screen->w+g2, engine.screen->h+g2, engine.screen); | |
578 | |
579 if (t >= 30) | |
580 { | |
581 int qtime = engineGetTime(30); | |
582 int creditTime = (engineGetTime(0) - creditStartTime); | |
583 float zscale; | |
584 if ( ( (qtime / (CREDITS_SPEED + 500)) % 2) == 0 && currState == -1) | |
585 stateChange = TRUE; | |
586 | |
587 if (stateChange && currCredit < ncredits) | |
588 { | |
589 stateChange = FALSE; | |
590 switch (currState) | |
747 { | 591 { |
748 case SDLK_ESCAPE: | 592 case 0: |
749 engine.exitFlag = TRUE; | 593 { |
594 int qt = (qtime / 100) % nrandomCoords; | |
595 creditStartTime = engineGetTime(0); | |
596 creditTime = 0; | |
597 dmLerpInit(&lerpX, randomCoords[qt].x, credits[currCredit].x - 50, CREDITS_SPEED); | |
598 dmLerpInit(&lerpY, randomCoords[qt].y, credits[currCredit].y - 50, CREDITS_SPEED); | |
599 dmLerpInit(&lerpZ, 5.0f, 0.0f, CREDITS_SPEED); | |
600 currState = 1; | |
601 } | |
602 break; | |
603 | |
604 case 2: | |
605 if (creditTime >= CREDITS_SPEED) | |
606 creditTime = CREDITS_SPEED - 1; | |
607 | |
608 zscale = dmLerpSCurve(&lerpZ, creditTime); | |
609 dmScaledBlitSurface32to32TransparentX( | |
610 credits[currCredit].img, | |
611 dmLerpSCurve(&lerpX, creditTime) - (zscale * credits[currCredit].img->w), | |
612 dmLerpSCurve(&lerpY, creditTime) - (zscale * credits[currCredit].img->h), | |
613 credits[currCredit].img->w * (1.0f + zscale), | |
614 credits[currCredit].img->h * (1.0f + zscale), | |
615 ruutu); | |
616 | |
617 currState = -1; | |
750 break; | 618 break; |
751 | 619 |
752 case SDLK_SPACE: | |
753 engine.pauseFlag = !engine.pauseFlag; | |
754 break; | |
755 | |
756 default: | 620 default: |
621 currCredit++; | |
622 currState = 0; | |
623 stateChange = TRUE; | |
757 break; | 624 break; |
758 } | 625 } |
759 | 626 } |
760 break; | |
761 | |
762 case SDL_VIDEOEXPOSE: | |
763 break; | |
764 | |
765 case SDL_QUIT: | |
766 engine.exitFlag = TRUE; | |
767 break; | |
768 } | |
769 | |
770 // Draw frame | |
771 frame.startTime = SDL_GetTicks(); | |
772 | |
773 if (SDL_MUSTLOCK(engine.screen) != 0 && SDL_LockSurface(engine.screen) != 0) | |
774 { | |
775 dmError("Can't lock surface.\n"); | |
776 goto error_exit; | |
777 } | |
778 | |
779 float t = engineGetTimeDT(); | |
780 | |
781 if (t < 5) | |
782 { | |
783 SDL_Surface *anciat; | |
784 int dt = engineGetTime(0); | |
785 static DMLerpContext lerpX, lerpY, lerpD; | |
786 static DMScaledBlitFunc nblit; | |
787 DMVector light; | |
788 static BOOL nollattu = FALSE; | |
789 if (!nollattu) | |
790 { | |
791 anciat = engineGetResImage("anciat.png"); | |
792 nblit = dmGetScaledBlitFunc(bmap->format, engine.screen->format, DMD_NONE); | |
793 dmMakePalette(bmap); | |
794 dmLerpInit(&lerpX, 0, QWIDTH, 5000); | |
795 dmLerpInit(&lerpY, QHEIGHT * 0.25, QHEIGHT * 0.75, 5000); | |
796 dmLerpInit(&lerpD, 0.04, 0.08, 5000); | |
797 nollattu = TRUE; | |
798 } | |
799 | |
800 light.x = dmLerpSCurve(&lerpX, dt); | |
801 light.y = dmLerp1(&lerpY, dt); | |
802 light.z = 128; | |
803 | |
804 dmShadowTraceHeightMap2(bmap->pixels, anciat->pixels, &light); | |
805 nblit(bmap, 0, 0, engine.screen->w, engine.screen->h, engine.screen); | |
806 } | |
807 else | |
808 if (t < 10) | |
809 { | |
810 SDL_Surface *logobg, *logolayer1, *logolayer2; | |
811 int dt = engineGetTime(5); | |
812 static DMScaledBlitFunc nblit, kblit; | |
813 static DMLerpContext lerpD; | |
814 static BOOL nollattu = FALSE; | |
815 | |
816 if (!nollattu) | |
817 { | |
818 logobg = engineGetResImage("logobg.png"); | |
819 logolayer1 = engineGetResImage("logolayer1.png"); | |
820 logolayer2 = engineGetResImage("logolayer2.png"); | |
821 | |
822 nblit = dmGetScaledBlitFunc(logobg->format, engine.screen->format, DMD_TRANSPARENT); | |
823 kblit = dmGetScaledBlitFunc(logobg->format, engine.screen->format, DMD_NONE); | |
824 dmLerpInit(&lerpD, 0.01, 500, 10000); | |
825 nollattu = TRUE; | |
826 } | |
827 | |
828 float q = dmLerpSCurve(&lerpD, dt); | |
829 float t = sin((float) dt / 150.0f); | |
830 int x = t * 25.0f + q, y = t * 35.0f + q*2.0f, | |
831 w = t * 70.0f + q, h = t * 40.0f + q*2.0f; | |
832 | |
833 float t2 = sin((float) dt / 150.0f + 0.2f); | |
834 int x2 = t2 * 25.0f + q, y2 = t * 35.0f + q*2.0f, | |
835 w2 = t2 * 70.0f + q, h2 = t * 40.0f + q*2.0f; | |
836 | |
837 kblit(logobg, 0, 0, engine.screen->w, engine.screen->h, engine.screen); | |
838 nblit(logolayer1, -x, -y, engine.screen->w+w, engine.screen->h+h, engine.screen); | |
839 nblit(logolayer2, -x2, -y2, engine.screen->w+w2, engine.screen->h+h2, engine.screen); | |
840 } | |
841 else | |
842 if (t < 20) | |
843 { | |
844 SDL_Surface *gay, *logobg; | |
845 int dt = engineGetTime(10); | |
846 static DMLerpContext lerpX, lerpY, lerpD; | |
847 static DMScaledBlitFunc nblit, kblit; | |
848 static BOOL nollattu = FALSE; | |
849 DMVector light; | |
850 DMBlockMap heightMap; | |
851 | |
852 if (!nollattu) | |
853 { | |
854 gay = engineGetResImage("gay.png"); | |
855 logobg = engineGetResImage("logobg.png"); | |
856 nblit = dmGetScaledBlitFunc(bmap->format, engine.screen->format, DMD_NONE); | |
857 kblit = dmGetScaledBlitFunc(logobg->format, engine.screen->format, DMD_TRANSPARENT); | |
858 dmMakePalette(bmap); | |
859 dmLerpInit(&lerpX, QWIDTH, 0, 10000); | |
860 dmLerpInit(&lerpY, QHEIGHT * 0.25, QHEIGHT * 0.75, 10000); | |
861 dmLerpInit(&lerpD, 0.04, 0.08, 10000); | |
862 nollattu = TRUE; | |
863 } | |
864 | |
865 light.x = dmLerpSCurve(&lerpX, dt); | |
866 light.y = QHEIGHT * 0.5 + sin(dmLerp1(&lerpY, dt)) * 0.5; | |
867 light.z = 128; | |
868 | |
869 dmMakeBumpMap(heightMap, dmLerpSCurve(&lerpD, dt), 254); | |
870 | |
871 dmShadowTraceHeightMap(bmap->pixels, heightMap, &light); | |
872 | |
873 nblit(bmap, 0, 0, engine.screen->w, engine.screen->h, engine.screen); | |
874 | |
875 if ((dt / 100) % 10 < 5) | |
876 { | |
877 kblit(gay, 0, 0, engine.screen->w, engine.screen->h, engine.screen); | |
878 } | |
879 } | |
880 else | |
881 if (t < 45) | |
882 { | |
883 static SDL_Surface *ruutu; | |
884 static int currState, currCredit, creditStartTime; | |
885 static DMLerpContext lerpX, lerpY, lerpZ; | |
886 static DMScaledBlitFunc nblit, kblit; | |
887 static BOOL stateChange, nollattu = FALSE; | |
888 int currFrame = engineGetTime(20) * 15 / 1000; | |
889 if (!nollattu) | |
890 { | |
891 ruutu = engineGetResImage("ruutu.png"); | |
892 dmClearSurface(ruutu, dmMapRGBA(ruutu, 0,0,0,0)); | |
893 nblit = dmGetScaledBlitFunc(nosfe[0]->format, engine.screen->format, DMD_NONE); | |
894 kblit = dmGetScaledBlitFunc(credits[0].img->format, engine.screen->format, DMD_TRANSPARENT); | |
895 currCredit = -1; | |
896 currState = -1; | |
897 stateChange = TRUE; | |
898 nollattu = TRUE; | |
899 } | |
900 | |
901 float gt = 1.0f + sin(engineGetTime(0) / 250.0f); | |
902 int g1 = gt * 25.0f, g2 = gt * 50.0f; | |
903 | 627 |
904 nblit(nosfe[currFrame % NOSFE_MAX], -g1, -g1, engine.screen->w+g2, engine.screen->h+g2, engine.screen); | 628 |
905 | 629 if (currCredit > 0) |
906 if (t >= 30) | 630 { |
907 { | 631 kblit(ruutu, 0, 0, engine.screen->w, engine.screen->h, engine.screen); |
908 int qtime = engineGetTime(30); | 632 } |
909 int creditTime = (engineGetTime(0) - creditStartTime); | 633 |
910 float zscale; | 634 if (currState == 1) |
911 if ( ( (qtime / (CREDITS_SPEED + 500)) % 2) == 0 && currState == -1) | 635 { |
636 if (creditTime >= CREDITS_SPEED) | |
637 { | |
638 creditTime = CREDITS_SPEED; | |
912 stateChange = TRUE; | 639 stateChange = TRUE; |
913 | 640 currState = 2; |
914 if (stateChange && currCredit < ncredits) | |
915 { | |
916 // fprintf(stderr, "[%6d] stateChange: st=%d, credit=%d\n", creditTime, currState, currCredit); | |
917 stateChange = FALSE; | |
918 switch (currState) | |
919 { | |
920 case 0: | |
921 { | |
922 int qt = (qtime / 100) % nrandomCoords; | |
923 creditStartTime = engineGetTime(0); | |
924 creditTime = 0; | |
925 dmLerpInit(&lerpX, randomCoords[qt].x, credits[currCredit].x - 50, CREDITS_SPEED); | |
926 dmLerpInit(&lerpY, randomCoords[qt].y, credits[currCredit].y - 50, CREDITS_SPEED); | |
927 dmLerpInit(&lerpZ, 5.0f, 0.0f, CREDITS_SPEED); | |
928 currState = 1; | |
929 } | |
930 break; | |
931 | |
932 case 2: | |
933 if (creditTime >= CREDITS_SPEED) | |
934 creditTime = CREDITS_SPEED - 1; | |
935 | |
936 zscale = dmLerpSCurve(&lerpZ, creditTime); | |
937 dmScaledBlitSurface32to32TransparentX( | |
938 credits[currCredit].img, | |
939 dmLerpSCurve(&lerpX, creditTime) - (zscale * credits[currCredit].img->w), | |
940 dmLerpSCurve(&lerpY, creditTime) - (zscale * credits[currCredit].img->h), | |
941 credits[currCredit].img->w * (1.0f + zscale), | |
942 credits[currCredit].img->h * (1.0f + zscale), | |
943 ruutu); | |
944 | |
945 currState = -1; | |
946 break; | |
947 | |
948 default: | |
949 currCredit++; | |
950 currState = 0; | |
951 stateChange = TRUE; | |
952 break; | |
953 } | |
954 | |
955 // fprintf(stderr, "[%6d] changed: st=%d, credit=%d, chg=%d\n", creditTime, currState, currCredit, stateChange); | |
956 } | 641 } |
957 | 642 |
958 | 643 zscale = dmLerpSCurve(&lerpZ, creditTime); |
959 if (currCredit > 0) | 644 kblit(credits[currCredit].img, |
960 { | 645 dmLerpSCurve(&lerpX, creditTime) - (zscale * credits[currCredit].img->w), |
961 kblit(ruutu, 0, 0, engine.screen->w, engine.screen->h, engine.screen); | 646 dmLerpSCurve(&lerpY, creditTime) - (zscale * credits[currCredit].img->h), |
962 } | 647 credits[currCredit].img->w * (1.0f + zscale), |
963 | 648 credits[currCredit].img->h * (1.0f + zscale), |
964 if (currState == 1) | 649 engine.screen); |
965 { | 650 } |
966 if (creditTime >= CREDITS_SPEED) | 651 } |
967 { | 652 |
968 creditTime = CREDITS_SPEED; | 653 } |
969 stateChange = TRUE; | 654 else |
970 currState = 2; | 655 if (t < 60) |
971 } | 656 { |
972 | 657 int dt = engineGetTime(45); |
973 zscale = dmLerpSCurve(&lerpZ, creditTime); | 658 static SDL_Surface *logobg, *greets; |
974 kblit(credits[currCredit].img, | 659 static DMScaledBlitFunc nblit, kblit; |
975 dmLerpSCurve(&lerpX, creditTime) - (zscale * credits[currCredit].img->w), | 660 static DMLerpContext lerpD; |
976 dmLerpSCurve(&lerpY, creditTime) - (zscale * credits[currCredit].img->h), | 661 static BOOL nollattu = FALSE; |
977 credits[currCredit].img->w * (1.0f + zscale), | 662 |
978 credits[currCredit].img->h * (1.0f + zscale), | 663 if (!nollattu) |
979 engine.screen); | 664 { |
980 } | 665 engineGetResImage(logobg, "logobg.png"); |
981 } | 666 engineGetResImage(greets, "greetings.png"); |
982 | 667 nblit = dmGetScaledBlitFunc(logobg->format, engine.screen->format, DMD_TRANSPARENT); |
983 } | 668 kblit = dmGetScaledBlitFunc(logobg->format, engine.screen->format, DMD_NONE); |
984 else | 669 dmLerpInit(&lerpD, 0.01, 500, 10000); |
985 if (t < 60) | 670 nollattu = TRUE; |
986 { | 671 } |
987 SDL_Surface *logobg, *greets; | 672 |
988 int dt = engineGetTime(45); | 673 float q = dmLerpSCurve(&lerpD, dt); |
989 static DMScaledBlitFunc nblit, kblit; | 674 float t = sin((float) dt / 150.0f), |
990 static DMLerpContext lerpD; | 675 j = (1.0 + t) * 15; |
991 static BOOL nollattu = FALSE; | 676 int x = t * 25.0f + q, y = t * 35.0f + q, |
992 | 677 w = t * 70.0f + q*2.0f, h = t * 40.0f + q*2.0f; |
993 if (!nollattu) | 678 |
994 { | 679 kblit(logobg, -j, -j, engine.screen->w+j*2.0f, engine.screen->h+j*2.0f, engine.screen); |
995 logobg = engineGetResImage("logobg.png"); | 680 nblit(greets, -x, -y, engine.screen->w+w, engine.screen->h+h, engine.screen); |
996 greets = engineGetResImage("greetings.png"); | 681 } |
997 nblit = dmGetScaledBlitFunc(logobg->format, engine.screen->format, DMD_TRANSPARENT); | 682 else |
998 kblit = dmGetScaledBlitFunc(logobg->format, engine.screen->format, DMD_NONE); | 683 engine.exitFlag = TRUE; |
999 dmLerpInit(&lerpD, 0.01, 500, 10000); | 684 |
1000 nollattu = TRUE; | 685 |
1001 } | 686 { |
1002 | 687 static SDL_Surface *feidi; |
1003 float q = dmLerpSCurve(&lerpD, dt); | 688 static int fadeStartTime; |
1004 float t = sin((float) dt / 150.0f), | 689 static BOOL fadeActive, nollattu = FALSE; |
1005 j = (1.0 + t) * 15; | 690 static DMLerpContext fadeLerp; |
1006 int x = t * 25.0f + q, y = t * 35.0f + q, | 691 BOOL hit; |
1007 w = t * 70.0f + q*2.0f, h = t * 40.0f + q*2.0f; | 692 int ch; |
1008 | 693 |
1009 kblit(logobg, -j, -j, engine.screen->w+j*2.0f, engine.screen->h+j*2.0f, engine.screen); | 694 if (!nollattu) |
1010 nblit(greets, -x, -y, engine.screen->w+w, engine.screen->h+h, engine.screen); | 695 { |
1011 } | 696 engineGetResImage(feidi, "feidi.png"); |
1012 else | 697 dmLerpInit(&fadeLerp, 255, 0, 250); |
1013 engine.exitFlag = TRUE; | 698 nollattu = TRUE; |
1014 | 699 } |
1015 { | 700 |
1016 static SDL_Surface *feidi; | 701 JSS_LOCK(engine.plr); |
1017 static int fadeStartTime; | 702 for (hit = FALSE, ch = 0; ch < 6; ch++) |
1018 static BOOL fadeActive, nollattu = FALSE; | 703 if (engine.plr->iCExtInstrumentN[ch] == 0) |
1019 static DMLerpContext fadeLerp; | 704 { |
1020 BOOL hit; | 705 hit = TRUE; |
1021 int ch; | 706 break; |
1022 | 707 } |
1023 if (!nollattu) | 708 JSS_UNLOCK(engine.plr); |
1024 { | 709 |
1025 feidi = engineGetResImage("feidi.png"); | 710 if (hit && !fadeActive) |
1026 dmLerpInit(&fadeLerp, 255, 0, 250); | 711 { |
1027 nollattu = TRUE; | 712 fadeActive = TRUE; |
1028 } | 713 fadeStartTime = engineGetTime(0); |
1029 | 714 } |
1030 JSS_LOCK(plr); | 715 if (fadeActive) |
1031 for (hit = FALSE, ch = 0; ch < 6; ch++) | 716 { |
1032 if (plr->iCExtInstrumentN[ch] == 0) | 717 int fadeTime = engineGetTime(0) - fadeStartTime; |
1033 { | 718 if (fadeTime < 250) |
1034 hit = TRUE; | 719 { |
1035 break; | 720 dmScaledBlitSurface32to32TransparentGA(feidi, |
1036 } | 721 0, 0, engine.screen->w, engine.screen->h, engine.screen, |
1037 JSS_UNLOCK(plr); | 722 dmLerpSCurve(&fadeLerp, fadeTime)); |
1038 | 723 } |
1039 if (hit && !fadeActive) | 724 else |
1040 { | 725 fadeActive = FALSE; |
1041 fadeActive = TRUE; | 726 } |
1042 fadeStartTime = engineGetTime(0); | 727 } |
1043 } | 728 |
1044 if (fadeActive) | 729 return DMERR_OK; |
1045 { | 730 } |
1046 int fadeTime = engineGetTime(0) - fadeStartTime; | 731 |
1047 if (fadeTime < 250) | 732 |
1048 { | 733 void demoFinish() |
1049 dmScaledBlitSurface32to32TransparentGA(feidi, | 734 { |
1050 0, 0, engine.screen->w, engine.screen->h, engine.screen, | 735 dmPrint(0, "Krapulassa on kivaa.\n"); |
1051 dmLerpSCurve(&fadeLerp, fadeTime)); | 736 } |
1052 } | |
1053 else | |
1054 fadeActive = FALSE; | |
1055 } | |
1056 } | |
1057 | |
1058 // Flip screen | |
1059 if (SDL_MUSTLOCK(engine.screen) != 0) | |
1060 SDL_UnlockSurface(engine.screen); | |
1061 | |
1062 SDL_Flip(engine.screen); | |
1063 SDL_Delay(20); | |
1064 | |
1065 // Get frame time, etc | |
1066 frame.endTime = SDL_GetTicks(); | |
1067 engine.currFrame++; | |
1068 engine.totalFrameTime += frame.endTime - frame.startTime; | |
1069 } | |
1070 | |
1071 // Print benchmark results | |
1072 engine.endTime = SDL_GetTicks(); | |
1073 dmPrint(1, "%d frames in %d ms, fps = %1.3f\n", | |
1074 engine.currFrame, engine.endTime - engine.startTime, | |
1075 (float) (engine.currFrame * 1000.0f) / (float) engine.totalFrameTime); | |
1076 | |
1077 | |
1078 error_exit: | |
1079 dmPrint(1, "Shutting down.\n"); | |
1080 SDL_ShowCursor(SDL_ENABLE); | |
1081 SDL_PauseAudio(1); | |
1082 | |
1083 if (engine.screen) | |
1084 SDL_FreeSurface(engine.screen); | |
1085 | |
1086 SDL_LockAudio(); | |
1087 jmpClose(plr); | |
1088 jvmClose(dev); | |
1089 jssFreeModule(mod); | |
1090 jssClose(); | |
1091 SDL_UnlockAudio(); | |
1092 | |
1093 dmres_close(); | |
1094 | |
1095 if (initSDL) | |
1096 SDL_Quit(); | |
1097 | |
1098 dmPrint(0, "SDL on muuten melko paska kirjasto.\n"); | |
1099 | |
1100 return 0; | |
1101 } |