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
|
#include "3dc.h"
#include "module.h"
#include "stratdef.h"
#include "sfx.h"
#define UseLocalAssert Yes
#include "ourasert.h"
/* globals for export */
int NumActiveBlocks;
DISPLAYBLOCK *ActiveBlockList[maxobjects];
/*
Object Block Lists et al
*/
static int NumFreeBlocks;
static DISPLAYBLOCK *FreeBlockList[maxobjects];
static DISPLAYBLOCK **FreeBlockListPtr = &FreeBlockList[maxobjects-1];
static DISPLAYBLOCK FreeBlockData[maxobjects];
static DISPLAYBLOCK **ActiveBlockListPtr = &ActiveBlockList[0];
/*
Texture Animation Block Extensions
*/
static int NumFreeTxAnimBlocks;
static TXACTRLBLK *FreeTxAnimBlockList[maxTxAnimblocks];
static TXACTRLBLK **FreeTxAnimBlockListPtr = &FreeTxAnimBlockList[maxTxAnimblocks-1];
static TXACTRLBLK FreeTxAnimBlockData[maxTxAnimblocks];
/*
Light Block Extensions
*/
static int NumFreeLightBlocks;
static LIGHTBLOCK *FreeLightBlockList[maxlightblocks];
static LIGHTBLOCK **FreeLightBlockListPtr = &FreeLightBlockList[maxlightblocks-1];
static LIGHTBLOCK FreeLightBlockData[maxlightblocks];
/*
To create the free block list, pointers to "FreeBlockData[]" must be copied
to "FreeBlockList[]".
Also:
"NumFreeBlocks" must be updated as "FreeBlockList[]" is created.
"NumActiveBlocks" must be initialised to zero.
*/
void InitialiseObjectBlocks(void)
{
DISPLAYBLOCK *FreeBlkPtr = &FreeBlockData[0];
NumActiveBlocks = 0;
FreeBlockListPtr = &FreeBlockList[maxobjects-1];
ActiveBlockListPtr = &ActiveBlockList[0];
for(NumFreeBlocks = 0; NumFreeBlocks<maxobjects; NumFreeBlocks++) {
FreeBlockList[NumFreeBlocks] = FreeBlkPtr;
FreeBlkPtr++;
}
}
/*
"AllocateObjectBlock()" is identical to the routine "GetBlock"
*/
DISPLAYBLOCK* AllocateObjectBlock(void)
{
DISPLAYBLOCK *FreeBlkPtr = 0; /* Default to null ptr */
int *sptr;
int i;
if(NumFreeBlocks) {
FreeBlkPtr = *FreeBlockListPtr--;
NumFreeBlocks--; /* One less free block */
/* Clear the block */
sptr = (int *)FreeBlkPtr;
for(i = sizeof(DISPLAYBLOCK)/4; i!=0; i--)
*sptr++ = 0;
}
return(FreeBlkPtr);
}
/*
"DeallocateObjectBlock()" is identical to the routine "ReturnBlock"
*/
void DeallocateObjectBlock(DISPLAYBLOCK *dblockptr)
{
/* Deallocate the Display Block */
FreeBlockListPtr++;
*FreeBlockListPtr = dblockptr;
NumFreeBlocks++; /* One more free block */
}
/*
"CreateActiveObject()" calls "AllocateObjectBlock()". An active object is
passed into the view and strategy routines unless flagged otherwise
WARNING!
An active object must ALWAYS be deallocated by "DestroyActiveObject()".
*/
DISPLAYBLOCK* CreateActiveObject(void)
{
DISPLAYBLOCK *dblockptr;
dblockptr = AllocateObjectBlock();
if(dblockptr) {
*ActiveBlockListPtr++ = dblockptr;
NumActiveBlocks++;
}
return dblockptr;
}
/*
DestroyActiveObject()
Remove the block from "ActiveBlockList".
Use the array model because it's clearer.
This function returns 0 if successful, -1 if not
*/
int DestroyActiveObject(DISPLAYBLOCK *dblockptr)
{
int i, light;
TXACTRLBLK *taptr;
/* If the block ptr is OK, search the Active Blocks List */
if(dblockptr) {
for(i = 0; i < NumActiveBlocks; i++) {
if(ActiveBlockList[i] == dblockptr) {
ActiveBlockList[i] = ActiveBlockList[NumActiveBlocks-1];
NumActiveBlocks--;
ActiveBlockListPtr--;
DestroyActiveVDB(dblockptr->ObVDBPtr); /* Checks for null */
if(dblockptr->ObNumLights) {
for(light = dblockptr->ObNumLights - 1; light != -1; light--)
DeleteLightBlock(dblockptr->ObLights[light], dblockptr);
}
/* If no SB, deallocate any Texture Animation Blocks */
if(dblockptr->ObStrategyBlock == 0) {
if(dblockptr->ObTxAnimCtrlBlks) {
taptr = dblockptr->ObTxAnimCtrlBlks;
while(taptr) {
DeallocateTxAnimBlock(taptr);
taptr = taptr->tac_next;
}
}
}
/* Deallocate the Lazy Morphed Points Array Pointer */
#if (SupportMorphing && LazyEvaluationForMorphing)
if(dblockptr->ObMorphedPts) {
DeallocateMem(dblockptr->ObMorphedPts);
dblockptr->ObMorphedPts = 0;
}
#endif
/* KJL 16:52:43 06/01/98 - dealloc sfx block if one exists */
if(dblockptr->SfxPtr)
{
DeallocateSfxBlock(dblockptr->SfxPtr);
}
DeallocateObjectBlock(dblockptr); /* Back to Free List */
/* If this is the current landscape, clear the pointer */
return 0;
}
}
}
return -1;
}
/*
Support Functions for Texture Animation Blocks
*/
void InitialiseTxAnimBlocks(void)
{
TXACTRLBLK *FreeBlkPtr = &FreeTxAnimBlockData[0];
FreeTxAnimBlockListPtr = &FreeTxAnimBlockList[maxTxAnimblocks-1];
for(NumFreeTxAnimBlocks=0; NumFreeTxAnimBlocks < maxTxAnimblocks; NumFreeTxAnimBlocks++) {
FreeTxAnimBlockList[NumFreeTxAnimBlocks] = FreeBlkPtr;
FreeBlkPtr++;
}
}
/*
Allocate a Texture Animation Block
*/
TXACTRLBLK* AllocateTxAnimBlock(void)
{
TXACTRLBLK *FreeBlkPtr = 0; /* Default to null ptr */
int *sptr;
int i;
if(NumFreeTxAnimBlocks) {
FreeBlkPtr = *FreeTxAnimBlockListPtr--;
NumFreeTxAnimBlocks--; /* One less free block */
/* Clear the block */
sptr = (int *)FreeBlkPtr;
for(i = sizeof(TXACTRLBLK)/4; i!=0; i--)
*sptr++ = 0;
}
return FreeBlkPtr;
}
/*
Deallocate a Texture Animation Block
*/
void DeallocateTxAnimBlock(TXACTRLBLK *TxAnimblockptr)
{
FreeTxAnimBlockListPtr++;
*FreeTxAnimBlockListPtr = TxAnimblockptr;
NumFreeTxAnimBlocks++; /* One more free block */
}
/*
Add a Texture Animation Block to a Display Block
*/
void AddTxAnimBlock(DISPLAYBLOCK *dptr, TXACTRLBLK *taptr)
{
TXACTRLBLK *taptr_tmp;
if(dptr->ObTxAnimCtrlBlks) {
taptr_tmp = dptr->ObTxAnimCtrlBlks;
while(taptr_tmp->tac_next)
taptr_tmp = taptr_tmp->tac_next;
taptr_tmp->tac_next = taptr;
}
else dptr->ObTxAnimCtrlBlks = taptr;
}
/*
Support functions for Light Blocks
*/
void InitialiseLightBlocks(void)
{
LIGHTBLOCK *FreeBlkPtr = &FreeLightBlockData[0];
FreeLightBlockListPtr = &FreeLightBlockList[maxlightblocks-1];
for(NumFreeLightBlocks=0; NumFreeLightBlocks < maxlightblocks; NumFreeLightBlocks++) {
FreeLightBlockList[NumFreeLightBlocks] = FreeBlkPtr;
FreeBlkPtr++;
}
}
LIGHTBLOCK* AllocateLightBlock(void)
{
LIGHTBLOCK *FreeBlkPtr = 0; /* Default to null ptr */
int *lptr;
int i;
if(NumFreeLightBlocks) {
FreeBlkPtr = *FreeLightBlockListPtr--;
NumFreeLightBlocks--; /* One less free block */
/* Clear the block */
lptr = (int *)FreeBlkPtr;
for(i = sizeof(LIGHTBLOCK)/4; i!=0; i--)
*lptr++ = 0;
}
return(FreeBlkPtr);
}
void DeallocateLightBlock(LIGHTBLOCK *lptr)
{
/* Not all lights come from the free light list */
if(lptr->LightFlags & LFlag_WasNotAllocated) return;
/* Make sure that this light IS from the free light list */
GLOBALASSERT(
(lptr >= FreeLightBlockData) &&
(lptr < &FreeLightBlockData[maxlightblocks])
);
/* Ok to return the light */
FreeLightBlockListPtr++;
*FreeLightBlockListPtr = lptr;
NumFreeLightBlocks++; /* One more free block */
}
/*
See if there are any free slots in the dptr light block array.
If there are, allocate a light block, place it in the list and return
the pointer to the caller.
A late addition is the passing of a light block (from somewhere, it does
not matter where). This light block is then added rather than one being
allocated from the free light block list.
*/
LIGHTBLOCK* AddLightBlock(DISPLAYBLOCK *dptr, LIGHTBLOCK *lptr_to_add)
{
LIGHTBLOCK **larrayptr;
LIGHTBLOCK **freelarrayptr;
LIGHTBLOCK *lptr = 0;
int i, lfree;
/* Are there any free slots? */
lfree = No;
larrayptr = &dptr->ObLights[0];
freelarrayptr = NULL;
for(i = MaxObjectLights; i!=0 && lfree == No; i--) {
if(*larrayptr == 0) {
freelarrayptr = larrayptr;
lfree = Yes;
}
larrayptr++;
}
if(lfree) {
if(lptr_to_add) {
lptr = lptr_to_add;
}
else {
lptr = AllocateLightBlock();
}
if(lptr)
{
*freelarrayptr = lptr;
dptr->ObNumLights++;
}
}
return lptr;
}
/*
To delete a light block, copy the end block to the new free slot and
reduce the count by one. Make sure the end block array entry is cleared.
*/
void DeleteLightBlock(LIGHTBLOCK *lptr, DISPLAYBLOCK *dptr)
{
int i, larrayi;
DeallocateLightBlock(lptr);
/* What is lptr's array index? */
larrayi = -1; /* null value */
for(i = 0; i < dptr->ObNumLights; i++)
if(dptr->ObLights[i] == lptr) larrayi = i;
/* Proceed only if lptr has been found in the array */
if(larrayi != -1) {
/* Copy the end block to that of lptr */
dptr->ObLights[larrayi] = dptr->ObLights[dptr->ObNumLights - 1];
/* Clear the end block array entry */
dptr->ObLights[dptr->ObNumLights - 1] = 0;
/* One less light in the dptr list */
dptr->ObNumLights--;
}
}
/*
When running the parallel strategies, display and light block deallocation
must only be done at the end of the frame, AFTER processor synchronisation
and BEFORE the shadow copy.
*/
int DisplayAndLightBlockDeallocation(void)
{
DISPLAYBLOCK **activeblocksptr;
DISPLAYBLOCK *dptr;
int i, j;
LIGHTBLOCK *lptr;
if(NumActiveBlocks) {
activeblocksptr = &ActiveBlockList[NumActiveBlocks - 1];
for(i = NumActiveBlocks; i!=0; i--) {
dptr = *activeblocksptr--;
/* Deallocate Object? */
if(dptr->ObFlags2 & ObFlag2_Deallocate) {
DestroyActiveObject(dptr);
}
/* Deallocate any Lights? */
else {
if(dptr->ObNumLights) {
for(j = dptr->ObNumLights - 1; j > -1; j--) {
lptr = dptr->ObLights[j];
if(lptr->LightFlags & LFlag_Deallocate) {
DeleteLightBlock(dptr->ObLights[j], dptr);
}
}
}
}
}
}
return 0;
}
|