-
Notifications
You must be signed in to change notification settings - Fork 13
/
ads.c
961 lines (728 loc) · 27.4 KB
/
ads.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
/*
* This file is part of 'Johnny Reborn'
*
* An open-source engine for the classic
* 'Johnny Castaway' screensaver by Sierra.
*
* Copyright (C) 2019 Jeremie GUILLAUME
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <SDL2/SDL.h>
#include "mytypes.h"
#include "utils.h"
#include "resource.h"
#include "graphics.h"
#include "ttm.h"
#include "island.h"
#include "walk.h"
#include "bench.h"
#include "ads.h"
#define MAX_RANDOM_OPS 10
#define MAX_ADS_CHUNKS 100
#define MAX_ADS_CHUNKS_LOCAL 1
#define OP_ADD_SCENE 0
#define OP_STOP_SCENE 1
#define OP_NOP 2
struct TAdsChunk { // TODO should not be here
struct TAdsScene scene;
uint32 offset;
};
struct TAdsRandOp { // TODO should not be here
int type;
uint16 slot;
uint16 tag;
uint16 numPlays;
uint16 weight;
};
static struct TAdsChunk adsChunks[MAX_ADS_CHUNKS];
static int numAdsChunks;
static struct TAdsChunk adsChunksLocal[MAX_ADS_CHUNKS_LOCAL];
static int numAdsChunksLocal;
static struct TTtmSlot ttmBackgroundSlot;
static struct TTtmSlot ttmHolidaySlot;
static struct TTtmSlot ttmSlots[MAX_TTM_SLOTS];
static struct TTtmThread ttmBackgroundThread;
static struct TTtmThread ttmHolidayThread;
static struct TTtmThread ttmThreads[MAX_TTM_THREADS];
static struct TAdsChunk adsChunks[MAX_ADS_CHUNKS];
static struct TTtmTag *adsTags;
static int adsNumTags = 0;
static struct TAdsRandOp adsRandOps[MAX_RANDOM_OPS];
static int adsNumRandOps = 0;
static int numThreads = 0;
static int adsStopRequested = 0;
static void adsLoad(uint8 *data, uint32 dataSize, uint16 numTags, uint16 tag, uint32 *tagOffset)
{
uint32 offset = 0;
uint16 args[10];
int bookmarkingChunks = 0;
int bookmarkingIfNotRunnings = 0;
numAdsChunks = 0;
numAdsChunksLocal = 0;
*tagOffset = 0;
adsNumTags = 0;
adsTags = safe_malloc(numTags * sizeof(struct TTtmTag));
while (offset < dataSize) {
uint16 opcode = peekUint16(data, &offset);
switch (opcode) {
case 0x1350: // IF_LASTPLAYED
if (bookmarkingChunks) {
bookmarkingIfNotRunnings = 0;
peekUint16Block(data, &offset, args, 2);
adsChunks[numAdsChunks].scene.slot = args[0];
adsChunks[numAdsChunks].scene.tag = args[1];
adsChunks[numAdsChunks].offset = offset;
numAdsChunks++;
}
else {
offset += 2<<1;
}
break;
case 0x1360: // IF_NOT_RUNNING
// We only bookmark the IF_NOT_RUNNINGs
// preceding the first IF_LAST_PLAYED or IF_IS_RUNNING
if (bookmarkingChunks && bookmarkingIfNotRunnings) {
peekUint16Block(data, &offset, args, 2);
adsChunks[numAdsChunks].scene.slot = args[0];
adsChunks[numAdsChunks].scene.tag = args[1];
adsChunks[numAdsChunks].offset = offset;
numAdsChunks++;
}
else {
offset += 2<<1;
}
break;
case 0x1370: // IF_IS_RUNNING
bookmarkingIfNotRunnings = 0;
offset += 2<<1;
break;
case 0x1070: offset += 2<<1; break;
case 0x1330: offset += 2<<1; break;
case 0x1420: offset += 0<<1; break;
case 0x1430: offset += 0<<1; break; // OR // TODO : manage here if_lastplayed OK tags ?
case 0x1510: offset += 0<<1; break;
case 0x1520: offset += 5<<1; break;
case 0x2005: offset += 4<<1; break;
case 0x2010: offset += 3<<1; break;
case 0x2014: offset += 0<<1; break;
case 0x3010: offset += 0<<1; break;
case 0x3020: offset += 1<<1; break;
case 0x30ff: offset += 0<<1; break;
case 0x4000: offset += 3<<1; break;
case 0xf010: offset += 0<<1; break;
case 0xf200: offset += 1<<1; break;
case 0xffff: offset += 0<<1; break;
case 0xfff0: offset += 0<<1; break;
default:
adsTags[adsNumTags].id = opcode;
adsTags[adsNumTags].offset = offset;
adsNumTags++;
if (opcode == tag) {
*tagOffset = offset;
bookmarkingChunks = 1;
bookmarkingIfNotRunnings = 1;
}
else {
bookmarkingChunks = 0;
bookmarkingIfNotRunnings = 0;
}
break;
}
}
if (adsNumTags != numTags)
debugMsg("Warning : didn't find every tag in ADS data");
if (*tagOffset == 0)
debugMsg("Warning : ADS tag #%d not found, starting from offset 0", tag);
}
static void adsReleaseAds()
{
free(adsTags);
}
static uint32 adsFindTag(uint16 reqdTag)
{
uint32 result = 0;
int i = 0;
while (result == 0 && i < adsNumTags) {
if (adsTags[i].id == reqdTag)
result = adsTags[i].offset;
else
i++;
}
if (result == 0)
fprintf(stderr, "Warning : ADS tag #%d not found, returning offset 0000\n", reqdTag);
return result;
}
static void adsAddScene(uint16 ttmSlotNo, uint16 ttmTag, uint16 arg3)
{
for (int i=0; i < MAX_TTM_THREADS; i++) {
struct TTtmThread *ttmThread = &ttmThreads[i];
if (ttmThread->isRunning == 1) {
if (ttmThread->sceneSlot == ttmSlotNo && ttmThread->sceneTag == ttmTag) {
debugMsg("(%d,%d) thread is already running - didn't add extra one\n", ttmSlotNo, ttmTag);
return;
}
}
}
int i=0;
while (ttmThreads[i].isRunning)
i++;
struct TTtmThread *ttmThread = &ttmThreads[i];
ttmThread->ttmSlot = &ttmSlots[ttmSlotNo];
ttmThread->isRunning = 1;
ttmThread->sceneSlot = ttmSlotNo;
ttmThread->sceneTag = ttmTag;
ttmThread->sceneTimer = 0;
ttmThread->sceneIterations = 0;
ttmThread->delay = 4;
ttmThread->timer = 0;
ttmThread->nextGotoOffset = 0;
ttmThread->selectedBmpSlot = 0;
ttmThread->fgColor = 0x0f;
ttmThread->bgColor = 0x0f;
if (ttmSlotNo)
ttmThread->ip = ttmFindTag(&ttmSlots[ttmSlotNo], ttmTag);
else
ttmThread->ip = 0;
if (((short)arg3) < 0) {
ttmThread->sceneTimer = -((short)arg3);
}
else if (((short)arg3) > 0) {
ttmThread->sceneIterations = arg3 - 1;
}
ttmThread->ttmLayer = grNewLayer();
numThreads++;
}
static void adsStopScene(int sceneNo)
{
grFreeLayer(ttmThreads[sceneNo].ttmLayer);
ttmThreads[sceneNo].isRunning = 0;
numThreads--;
}
static void adsStopSceneByTtmTag(uint16 ttmSlotNo, uint16 ttmTag)
{
struct TTtmThread *ttmThread;
for (int i=0; i < MAX_TTM_THREADS; i++) {
ttmThread = &ttmThreads[i];
if (ttmThread->isRunning) {
if (ttmThread->sceneSlot == ttmSlotNo && ttmThread->sceneTag == ttmTag)
adsStopScene(i);
}
}
}
static int isSceneRunning(uint16 ttmSlotNo, uint16 ttmTag)
{
for (int i=0; i < MAX_TTM_THREADS; i++) {
struct TTtmThread thread = ttmThreads[i];
if ( thread.isRunning == 1
&& thread.sceneSlot == ttmSlotNo
&& thread.sceneTag == ttmTag ) {
return 1;
}
}
return 0;
}
static struct TAdsRandOp *adsRandomPickOp()
{
int totalWeight = 0;
int partialWeight = 0;
int res;
// Pick in a list of weighted elements
for (int i=0; i < adsNumRandOps; i++)
totalWeight += adsRandOps[i].weight;
int a = rand() % totalWeight;
for (res=0; res < adsNumRandOps; res++) {
partialWeight += adsRandOps[res].weight;
if (a < partialWeight)
break;
}
return &adsRandOps[res];
}
static void adsRandomStart()
{
adsNumRandOps = 0;
}
static void adsRandomAddScene(uint16 ttmSlotNo, uint16 ttmTag, uint16 numPlays,
uint16 weight)
{
adsRandOps[adsNumRandOps].type = OP_ADD_SCENE;
adsRandOps[adsNumRandOps].slot = ttmSlotNo;
adsRandOps[adsNumRandOps].tag = ttmTag;
adsRandOps[adsNumRandOps].numPlays = numPlays;
adsRandOps[adsNumRandOps].weight = weight;
adsNumRandOps++;
}
static void adsRandomStopSceneByTtmTag(uint16 ttmSlotNo, uint16 ttmTag,
uint16 weight)
{
adsRandOps[adsNumRandOps].type = OP_STOP_SCENE;
adsRandOps[adsNumRandOps].slot = ttmSlotNo;
adsRandOps[adsNumRandOps].tag = ttmTag;
adsRandOps[adsNumRandOps].numPlays = 0;
adsRandOps[adsNumRandOps].weight = weight;
adsNumRandOps++;
}
static void adsRandomNop(uint16 weight)
{
adsRandOps[adsNumRandOps].type = OP_NOP;
adsRandOps[adsNumRandOps].slot = 0;
adsRandOps[adsNumRandOps].tag = 0;
adsRandOps[adsNumRandOps].numPlays = 0;
adsRandOps[adsNumRandOps].weight = weight;
adsNumRandOps++;
}
static void adsRandomEnd()
{
if (adsNumRandOps) {
struct TAdsRandOp *op = adsRandomPickOp();
switch (op->type) {
case OP_ADD_SCENE:
debugMsg("RANDOM: chose ADD_SCENE %d %d", op->slot, op->tag);
adsAddScene(op->slot, op->tag, op->numPlays);
break;
case OP_STOP_SCENE:
debugMsg("RANDOM: chose STOP_SCENE %d %d", op->slot, op->tag);
adsStopSceneByTtmTag(op->slot, op->tag);
break;
default:
debugMsg("RANDOM: chose NOP");
break;
}
}
else {
debugMsg("RANDOM : no operation to choose from");
}
}
void adsInit() // Init slots and threads for TTM scripts // TODO : rename
{
for (int i=0; i < MAX_TTM_SLOTS; i++)
ttmInitSlot(&ttmSlots[i]);
for (int i=0; i < MAX_TTM_THREADS; i++) {
ttmThreads[i].isRunning = 0;
ttmThreads[i].timer = 0;
}
grUpdateDelay = 0;
ttmBackgroundThread.isRunning = 0;
ttmHolidayThread.isRunning = 0;
numThreads = 0;
adsStopRequested = 0;
}
void adsPlaySingleTtm(char *ttmName) // TODO - tempo
{
adsInit();
ttmLoadTtm(ttmSlots, ttmName);
adsAddScene(0,0,0);
ttmThreads[0].ip = 0;
while (ttmThreads[0].ip < ttmSlots[0].dataSize) {
ttmPlay(ttmThreads);
ttmThreads[0].isRunning = 1;
grUpdateDisplay(NULL, ttmThreads, NULL);
grUpdateDelay = ttmThreads[0].delay;
}
adsStopScene(0);
ttmResetSlot(&ttmSlots[0]);
}
static void adsPlayChunk(uint8 *data, uint32 dataSize, uint32 offset)
{
uint16 opcode;
uint16 args[10];
int inRandBlock = 0;
int inOrBlock = 0;
int inSkipBlock = 0;
int inIfLastplayedLocal = 0;
int continueLoop = 1;
while (continueLoop && offset < dataSize) {
opcode = peekUint16(data, &offset);
switch (opcode) {
case 0x1070:
// Inside an IF_LASTPLAYED chunk, local IF_LASTPLAYED
// which overrides the global IF_LASTPLAYEDs.
peekUint16Block(data, &offset, args, 2);
debugMsg("IF_LASTPLAYED_LOCAL");
inIfLastplayedLocal = 1;
adsChunksLocal[numAdsChunksLocal].scene.slot = args[0];
adsChunksLocal[numAdsChunksLocal].scene.tag = args[1];
adsChunksLocal[numAdsChunksLocal].offset = offset;
numAdsChunksLocal++;
break;
case 0x1330:
// Always just before a call to ADD_SCENE with same (ttm,tag)
// references tags which init commands : LOAD_IMAGE LOAD_SCREEN etc.
// - one exception: FISHING.ADS tag 3
// - seems to be a synonym of "IF_NOT_RUNNING"
// - if so, our implementation works fine anyway by ignoring this one...
peekUint16Block(data, &offset, args, 2);
debugMsg("IF_UNKNOWN_1 %d %d", args[0], args[1]);
break;
case 0x1350:
peekUint16Block(data, &offset, args, 2);
debugMsg("IF_LASTPLAYED %d %d", args[0], args[1]);
if (!inOrBlock)
continueLoop = 0;
inOrBlock = 0;
break;
case 0x1360:
peekUint16Block(data, &offset, args, 2);
debugMsg("IF_NOT_RUNNING %d %d", args[0], args[1]);
if (isSceneRunning(args[0], args[1]))
inSkipBlock = 1;
break;
case 0x1370:
peekUint16Block(data, &offset, args, 2);
debugMsg("IF_IS_RUNNING %d %d", args[0], args[1]);
inSkipBlock = !isSceneRunning(args[0], args[1]);
break;
case 0x1420:
debugMsg("AND");
break;
case 0x1430:
debugMsg("OR");
inOrBlock = 1;
break;
case 0x1510:
// PLAY_SCENE : in fact, sort of a 'closing brace' for a
// statement block (several types possible).
// TODO : implement that in a cleaner way.
// For now, works quite well like that though...
debugMsg("PLAY_SCENE");
if (inSkipBlock)
inSkipBlock = 0;
else
continueLoop = 0;
break;
case 0x1520:
// Only in ACTIVITY.ADS tag 7, after IF_LASTPLAYED_LOCAL
peekUint16Block(data, &offset, args, 5);
debugMsg("ADD_SCENE_LOCAL");
if (inIfLastplayedLocal) {
// First pass : the scene was queued by IF_LASTPLAYED_LOCAL,
// nothing more to do for now
inIfLastplayedLocal = 0;
}
else {
// Second pass (we were called directly from the scheduler)
// --> we launch the execution of the scene
adsAddScene(args[1],args[2],args[3]);
}
break;
case 0x2005:
peekUint16Block(data, &offset, args, 4);
debugMsg("ADD_SCENE %d %d %d %d", args[0], args[1], args[2], args[3]);
if (!inSkipBlock) { // TODO - TEMPO
if (inRandBlock)
adsRandomAddScene(args[0],args[1],args[2], args[3]);
else
adsAddScene(args[0],args[1],args[2]);
}
break;
case 0x2010:
peekUint16Block(data, &offset, args, 3);
debugMsg("STOP_SCENE %d %d %d", args[0], args[1], args[2]);
if (!inSkipBlock) { // TODO - TEMPO
if (inRandBlock)
adsRandomStopSceneByTtmTag(args[0], args[1], args[2]);
else
adsStopSceneByTtmTag(args[0], args[1]);
}
break;
case 0x3010:
debugMsg("RANDOM_START");
adsRandomStart();
inRandBlock = 1;
break;
case 0x3020:
peekUint16Block(data, &offset, args, 1);
debugMsg("NOP");
if (inRandBlock)
adsRandomNop(args[0]);
break;
case 0x30ff:
debugMsg("RANDOM_END");
adsRandomEnd();
inRandBlock = 0;
break;
case 0x4000:
peekUint16Block(data, &offset, args, 3);
debugMsg("UNKNOWN_6"); // only in BUILDING.ADS tag 7
break;
case 0xf010:
debugMsg("FADE_OUT");
break;
case 0xf200:
peekUint16Block(data, &offset, args, 1);
debugMsg("GOSUB_TAG %d", args[0]); // ex UNKNOWN_8
// "quick and dirty" implementation, sufficient for
// JCastaway : only encountered in STAND.ADS to tag 14
// which only contains 1 scene
adsPlayChunk(data, dataSize, adsFindTag(args[0]));
break;
case 0xffff:
debugMsg("END");
if (inSkipBlock) // TODO - no doubt this is q&d
inSkipBlock = 0;
else
adsStopRequested = 1;
break;
case 0xfff0:
debugMsg("END_IF");
break;
default :
debugMsg(":TAG %d\n", opcode);
break;
}
}
}
static void adsPlayTriggeredChunks(uint8 *data, uint32 dataSize, uint16 ttmSlotNo, uint16 ttmTag)
{
// First we deal with the case where a local trigger was declared
// (only one occurence of this, in ACTIVITY.ADS tag #7)
if (numAdsChunksLocal) {
for (int i=0; i < numAdsChunksLocal; i++)
if (adsChunksLocal[i].scene.slot == ttmSlotNo && adsChunksLocal[i].scene.tag == ttmTag) {
adsPlayChunk(data, dataSize, adsChunksLocal[i].offset);
numAdsChunksLocal--;
}
}
// Then, the general case
else {
// Note : in a few rare cases (eg BUILDING.ADS tag #2), the ADS script
// contains several 'IF_LASTPLAYED' commands for one given scene.
for (int i=0; i < numAdsChunks; i++)
if (adsChunks[i].scene.slot == ttmSlotNo && adsChunks[i].scene.tag == ttmTag)
adsPlayChunk(data, dataSize, adsChunks[i].offset);
}
}
void adsPlay(char *adsName, uint16 adsTag)
{
uint32 offset;
uint8 *data;
uint32 dataSize;
struct TAdsResource *adsResource = findAdsResource(adsName);
debugMsg("\n\n========== Playing ADS: %s:%d ==========\n", adsResource->resName, adsTag);
data = adsResource->uncompressedData;
dataSize = adsResource->uncompressedSize;
for (int i=0; i < adsResource->numRes; i++)
ttmLoadTtm(&ttmSlots[adsResource->res[i].id], adsResource->res[i].name);
adsLoad(data, dataSize, adsResource->numTags, adsTag, &offset);
adsStopRequested = 0;
grUpdateDelay = 0;
// Play the first ADS chunk of the sequence
adsPlayChunk(data, dataSize, offset);
// Main ADS loop
while (numThreads) {
if (ttmBackgroundThread.isRunning && ttmBackgroundThread.timer == 0) {
debugMsg(" ------> Animate bg");
ttmBackgroundThread.timer = ttmBackgroundThread.delay;
islandAnimate(&ttmBackgroundThread);
}
for (int i=0; i < MAX_TTM_THREADS; i++) {
// Call ttmPlay() for each thread which timer reaches 0
if (ttmThreads[i].isRunning && ttmThreads[i].timer == 0) {
debugMsg(" ------> Thread #%d", i);
ttmThreads[i].timer = ttmThreads[i].delay;
ttmPlay(&ttmThreads[i]);
}
}
if (debugMode) {
debugMsg("\n +------ THREADS: %d -------", numThreads);
if (ttmBackgroundThread.isRunning) {
debugMsg(" |");
debugMsg(" | #bg:");
debugMsg(" | delay........... %d" , ttmBackgroundThread.delay );
debugMsg(" | timer........... %d" , ttmBackgroundThread.timer );
}
for (int i=0; i < MAX_TTM_THREADS; i++) {
if (ttmThreads[i].isRunning) {
debugMsg(" |");
debugMsg(" | #%d: (%d,%d)", i, ttmThreads[i].sceneSlot, ttmThreads[i].sceneTag);
debugMsg(" | sceneTimer...... %d" , ttmThreads[i].sceneTimer );
debugMsg(" | isRunning....... %d" , ttmThreads[i].isRunning );
debugMsg(" | offset.......... %ld", ttmThreads[i].ip );
debugMsg(" | nextGotoOffset.. %ld", ttmThreads[i].nextGotoOffset );
debugMsg(" | delay........... %d" , ttmThreads[i].delay );
debugMsg(" | timer........... %d" , ttmThreads[i].timer );
}
}
debugMsg(" +-------------------------\n");
}
// Refresh display
grUpdateDisplay(&ttmBackgroundThread, ttmThreads, &ttmHolidayThread);
// Determine min timer through all threads
uint16 mini = 300;
if (ttmBackgroundThread.isRunning)
mini = ttmBackgroundThread.timer;
for (int i=0; i < MAX_TTM_THREADS; i++) {
if (ttmThreads[i].isRunning) {
if (ttmThreads[i].delay < mini)
mini = ttmThreads[i].delay;
if (ttmThreads[i].timer < mini)
mini = ttmThreads[i].timer;
}
}
// Decrease all timers by the shortest one, and wait that amount of time
ttmBackgroundThread.timer -= mini;
for (int i=0; i < MAX_TTM_THREADS; i++)
if (ttmThreads[i].isRunning)
ttmThreads[i].timer -= mini;
debugMsg(" ******* WAIT: %d ticks *******\n", mini);
grUpdateDelay = mini;
// Various threads processes
for (int i=0; i < MAX_TTM_THREADS; i++) {
if (ttmThreads[i].isRunning && ttmThreads[i].timer == 0) {
// Process jumps
if (ttmThreads[i].nextGotoOffset) {
ttmThreads[i].ip = ttmThreads[i].nextGotoOffset;
ttmThreads[i].nextGotoOffset = 0;
}
// Managing the timer which was indicated in ADD_SCENE arg3 (neg. value)
if (ttmThreads[i].sceneTimer > 0) {
ttmThreads[i].sceneTimer -= ttmThreads[i].delay;
if (ttmThreads[i].sceneTimer <= 0)
ttmThreads[i].isRunning = 2;
}
// Free terminated threads
if (ttmThreads[i].isRunning == 2) {
// Managing the numPlays which was indicated in ADD_SCENE arg3 (postive value)
if (ttmThreads[i].sceneIterations) {
ttmThreads[i].sceneIterations--;
ttmThreads[i].isRunning = 1;
ttmThreads[i].ip = ttmFindTag(&ttmSlots[ttmThreads[i].sceneSlot], ttmThreads[i].sceneTag);
}
// Is there one (or more) IF_LASTPLAYED matching the terminated thread ?
else {
adsStopScene(i);
if (!adsStopRequested)
adsPlayTriggeredChunks(data, dataSize, ttmThreads[i].sceneSlot, ttmThreads[i].sceneTag);
}
}
}
}
}
for (int i=0; i < MAX_TTM_SLOTS; i++)
ttmResetSlot(&ttmSlots[i]);
grRestoreZone(NULL, 0, 0, 0, 0);
adsReleaseAds();
}
void adsPlayBench() // TODO - tempo
{
int numsLayers[] = { 1, 4, 8 };
uint32 startTicks, counter;
adsInit();
for (int i=0; i < 8; i++) {
ttmThreads[i].ttmSlot = &ttmSlots[0];;
ttmThreads[i].isRunning = 1;
ttmThreads[i].selectedBmpSlot = 0;
ttmThreads[i].ttmLayer = grNewLayer();
}
benchInit(ttmSlots);
grUpdateDelay = 0;
for (int j=0; j < 3; j++) {
int numLayers = numsLayers[j];
for (int i=0; i < MAX_TTM_THREADS; i++)
ttmThreads[i].isRunning = (i<numLayers ? 1 : 0);
startTicks = SDL_GetTicks();
counter = 0;
while ((SDL_GetTicks() - startTicks) <= 3000) {
for (int i=0; i < numLayers; i++)
benchPlay(&ttmThreads[i], i);
grUpdateDisplay(NULL, ttmThreads, NULL);
counter++;
}
printf(" %d-layers test --> %d fps\n", numLayers, counter/3);
}
for (int i=0; i < 8; i++)
adsStopScene(i);
ttmResetSlot(&ttmSlots[0]);
}
void adsPlayIntro()
{
grLoadScreen("INTRO.SCR");
grUpdateDelay = 100;
grUpdateDisplay(NULL, ttmThreads, NULL);
grFadeOut();
ttmResetSlot(&ttmSlots[0]);
}
void adsInitIsland()
{
// Init the background thread (animated waves)
// and call islandInit() to draw the background
ttmInitSlot(&ttmBackgroundSlot);
ttmBackgroundThread.ttmSlot = &ttmBackgroundSlot;
ttmBackgroundThread.isRunning = 3;
ttmBackgroundThread.delay = 40; // TODO
ttmBackgroundThread.timer = 0;
islandInit(&ttmBackgroundThread);
// Init the "holiday" layer and thread
ttmInitSlot(&ttmHolidaySlot);
ttmHolidayThread.ttmSlot = &ttmHolidaySlot;
ttmHolidayThread.isRunning = 0;
ttmHolidayThread.delay = 0;
ttmHolidayThread.timer = 0;
islandInitHoliday(&ttmHolidayThread);
}
void adsReleaseIsland()
{
ttmBackgroundThread.isRunning = 0;
ttmResetSlot(&ttmBackgroundSlot);
if (ttmHolidayThread.isRunning) {
ttmHolidayThread.isRunning = 0;
grFreeLayer(ttmHolidayThread.ttmLayer);
}
}
void adsNoIsland()
{
grDx = grDy = 0;
grInitEmptyBackground();
}
void adsPlayWalk(int fromSpot, int fromHdg, int toSpot, int toHdg)
{
adsAddScene(0,0,0);
grLoadBmp(ttmSlots, 0, "JOHNWALK.BMP");
grDx = islandState.xPos;
grDy = islandState.yPos;
ttmThreads[0].timer = ttmThreads[0].delay = 6; // 12 ?
walkInit(fromSpot, fromHdg, toSpot, toHdg);
ttmThreads[0].delay = walkAnimate(&ttmThreads[0], ttmBackgroundThread.ttmSlot);
while (ttmThreads[0].delay) {
// Call each thread which timer reaches 0
if (!ttmBackgroundThread.timer) {
debugMsg(" ------> Animate bg");
ttmBackgroundThread.timer = ttmBackgroundThread.delay;
islandAnimate(&ttmBackgroundThread);
}
if (!ttmThreads[0].timer) {
debugMsg(" ------> Animate walking");
ttmThreads[0].timer = ttmThreads[0].delay =
walkAnimate(&ttmThreads[0], ttmBackgroundThread.ttmSlot);
}
// Refresh display
grUpdateDisplay(&ttmBackgroundThread, ttmThreads, &ttmHolidayThread);
// Determine min timer from the two threads
uint16 mini = 300;
if (ttmBackgroundThread.timer < ttmThreads[0].timer)
mini = ttmBackgroundThread.timer;
else
mini = ttmThreads[0].timer;
// Decrease all timers by the shortest one, and wait that amount of time
ttmBackgroundThread.timer -= mini;
ttmThreads[0].timer -= mini;
debugMsg(" ******* WAIT: %d ticks *******\n", mini);
grUpdateDelay = mini;
}
adsStopScene(0);
}