| | 1 | #include "system.h" |
| | 2 | #include "prototyp.h" |
| | 3 | #include <assert.h> |
| | 4 | #include "sfx.h" |
| | 5 | #include <stdio.h> |
| | 6 | |
| | 7 | int NumActiveBlocks; |
| | 8 | DISPLAYBLOCK *ActiveBlockList[maxobjects]; |
| | 9 | |
| | 10 | #define maxlightblocks 100 /* This ought to be MORE than enough */ |
| | 11 | #define maxTxAnimblocks 100 |
| | 12 | |
| | 13 | const DISPLAYBLOCK Zero_Displayblock = |
| | 14 | { |
| | 15 | 0, |
| | 16 | -1, |
| | 17 | NULL, |
| | 18 | NULL, |
| | 19 | { 0, 0, 0 }, |
| | 20 | { 0, 0, 0 }, |
| | 21 | { 0, 0, 0, 0, 0, 0, 0, 0, 0 }, |
| | 22 | 0, |
| | 23 | 0, |
| | 24 | 0, |
| | 25 | 0, |
| | 26 | {0}, |
| | 27 | NULL, |
| | 28 | { 0, 0, 0}, |
| | 29 | 0, |
| | 30 | 0, |
| | 31 | 0, |
| | 32 | 0, |
| | 33 | 0, |
| | 34 | 0, |
| | 35 | 0, |
| | 36 | NULL, |
| | 37 | NULL, |
| | 38 | NULL, |
| | 39 | NULL, |
| | 40 | 0 |
| | 41 | }; |
| | 42 | |
| | 43 | static int NumFreeBlocks; |
| | 44 | static DISPLAYBLOCK *FreeBlockList[maxobjects]; |
| | 45 | static DISPLAYBLOCK **FreeBlockListPtr = &FreeBlockList[maxobjects-1]; |
| | 46 | static DISPLAYBLOCK FreeBlockData[maxobjects]; |
| | 47 | static DISPLAYBLOCK **ActiveBlockListPtr = &ActiveBlockList[0]; |
| | 48 | |
| | 49 | /* |
| | 50 | Texture Animation Block Extensions |
| | 51 | */ |
| | 52 | |
| | 53 | static int NumFreeTxAnimBlocks; |
| | 54 | static TXACTRLBLK *FreeTxAnimBlockList[maxTxAnimblocks]; |
| | 55 | static TXACTRLBLK **FreeTxAnimBlockListPtr = &FreeTxAnimBlockList[maxTxAnimblocks-1]; |
| | 56 | static TXACTRLBLK FreeTxAnimBlockData[maxTxAnimblocks]; |
| | 57 | |
| | 58 | /* |
| | 59 | Light Block Extensions |
| | 60 | */ |
| | 61 | |
| | 62 | static int NumFreeLightBlocks; |
| | 63 | static LIGHTBLOCK *FreeLightBlockList[maxlightblocks]; |
| | 64 | static LIGHTBLOCK **FreeLightBlockListPtr = &FreeLightBlockList[maxlightblocks-1]; |
| | 65 | static LIGHTBLOCK FreeLightBlockData[maxlightblocks]; |
| | 66 | |
| | 67 | /* |
| | 68 | |
| | 69 | To create the free block list, pointers to "FreeBlockData[]" must be copied |
| | 70 | to "FreeBlockList[]". |
| | 71 | |
| | 72 | Also: |
| | 73 | |
| | 74 | "NumFreeBlocks" must be updated as "FreeBlockList[]" is created. |
| | 75 | "NumActiveBlocks" must be initialised to zero. |
| | 76 | |
| | 77 | */ |
| | 78 | |
| | 79 | void InitialiseObjectBlocks() |
| | 80 | { |
| | 81 | DISPLAYBLOCK *FreeBlkPtr = &FreeBlockData[0]; |
| | 82 | |
| | 83 | NumActiveBlocks = 0; |
| | 84 | |
| | 85 | FreeBlockListPtr = &FreeBlockList[maxobjects-1]; |
| | 86 | ActiveBlockListPtr = &ActiveBlockList[0]; |
| | 87 | |
| | 88 | for(NumFreeBlocks = 0; NumFreeBlocks < maxobjects; NumFreeBlocks++) |
| | 89 | { |
| | 90 | FreeBlockList[NumFreeBlocks] = FreeBlkPtr++; |
| | 91 | ActiveBlockList[NumFreeBlocks] = NULL; |
| | 92 | } |
| | 93 | } |
| | 94 | |
| | 95 | /* |
| | 96 | "CreateActiveObject()". An active object is |
| | 97 | passed into the view and strategy routines unless flagged otherwise |
| | 98 | |
| | 99 | WARNING! |
| | 100 | |
| | 101 | An active object must ALWAYS be deallocated by "DestroyActiveObject()". |
| | 102 | */ |
| | 103 | |
| | 104 | DISPLAYBLOCK* CreateActiveObject() |
| | 105 | { |
| | 106 | if(NumFreeBlocks) |
| | 107 | { |
| | 108 | DISPLAYBLOCK *FreeBlkPtr = *FreeBlockListPtr--; |
| | 109 | |
| | 110 | NumFreeBlocks--; /* One less free block */ |
| | 111 | |
| | 112 | *FreeBlkPtr = Zero_Displayblock; |
| | 113 | *ActiveBlockListPtr++ = FreeBlkPtr; |
| | 114 | FreeBlkPtr->index = NumActiveBlocks++; |
| | 115 | |
| | 116 | int i; |
| | 117 | for(i=0; i < MaxObjectLights; i++) |
| | 118 | FreeBlkPtr->ObLights[i] = NULL; |
| | 119 | |
| | 120 | return FreeBlkPtr; |
| | 121 | } |
| | 122 | |
| | 123 | return NULL; |
| | 124 | } |
| | 125 | |
| | 126 | static void DeallocateTxAnimBlock(TXACTRLBLK *TxAnimblockptr) |
| | 127 | { |
| | 128 | FreeTxAnimBlockListPtr++; |
| | 129 | *FreeTxAnimBlockListPtr = TxAnimblockptr; |
| | 130 | NumFreeTxAnimBlocks++; /* One more free block */ |
| | 131 | } |
| | 132 | |
| | 133 | /* |
| | 134 | To delete a light block, copy the end block to the new free slot and |
| | 135 | reduce the count by one. Make sure the end block array entry is cleared. |
| | 136 | */ |
| | 137 | |
| | 138 | static void DeleteLightBlock(LIGHTBLOCK *lptr, DISPLAYBLOCK *dptr) |
| | 139 | { |
| | 140 | /* Not all lights come from the free light list */ |
| | 141 | if(!(lptr->LightFlags & LFlag_WasNotAllocated)) |
| | 142 | { |
| | 143 | int i; |
| | 144 | |
| | 145 | /* Make sure that this light IS from the free light list */ |
| | 146 | assert( (lptr >= FreeLightBlockData) && (lptr < &FreeLightBlockData[maxlightblocks]) ); |
| | 147 | /* Ok to return the light */ |
| | 148 | FreeLightBlockListPtr++; |
| | 149 | *FreeLightBlockListPtr = lptr; |
| | 150 | NumFreeLightBlocks++; /* One more free block */ |
| | 151 | |
| | 152 | /* What is lptr's array index? */ |
| | 153 | |
| | 154 | for(i=0; i < dptr->ObNumLights; i++) |
| | 155 | { |
| | 156 | if(dptr->ObLights[i] == lptr) |
| | 157 | { |
| | 158 | /* Copy the end block to that of lptr */ |
| | 159 | |
| | 160 | dptr->ObLights[i] = dptr->ObLights[--dptr->ObNumLights]; |
| | 161 | |
| | 162 | /* Clear the end block array entry */ |
| | 163 | |
| | 164 | dptr->ObLights[dptr->ObNumLights] = NULL; |
| | 165 | |
| | 166 | /* One less light in the dptr list */ |
| | 167 | return; |
| | 168 | } |
| | 169 | } |
| | 170 | } |
| | 171 | } |
| | 172 | |
| | 173 | void LightBlockDeallocation() |
| | 174 | { |
| | 175 | int i,j; |
| | 176 | for(i=0; i < NumActiveBlocks; i++) |
| | 177 | { |
| | 178 | DISPLAYBLOCK *dptr = ActiveBlockList[i]; |
| | 179 | |
| | 180 | for(j=0; j < dptr->ObNumLights; j++) |
| | 181 | { |
| | 182 | LIGHTBLOCK *lightPtr = dptr->ObLights[j]; |
| | 183 | |
| | 184 | if(lightPtr->LightFlags & LFlag_Deallocate) |
| | 185 | DeleteLightBlock(lightPtr, dptr); |
| | 186 | } |
| | 187 | } |
| | 188 | } |
| | 189 | |
| | 190 | void DestroyActiveObject(DISPLAYBLOCK **dblockptr2) |
| | 191 | { |
| | 192 | if(*dblockptr2) |
| | 193 | { |
| | 194 | DISPLAYBLOCK *dblockptr = *dblockptr2; |
| | 195 | |
| | 196 | if( -1 != dblockptr->index) |
| | 197 | { |
| | 198 | ActiveBlockList[dblockptr->index] = ActiveBlockList[--NumActiveBlocks]; |
| | 199 | ActiveBlockList[dblockptr->index]->index = dblockptr->index; |
| | 200 | ActiveBlockListPtr--; |
| | 201 | |
| | 202 | int i; |
| | 203 | for(i=0; i < dblockptr->ObNumLights; i++) |
| | 204 | DeleteLightBlock(dblockptr->ObLights[i], dblockptr); |
| | 205 | |
| | 206 | /* If no SB, deallocate any Texture Animation Blocks */ |
| | 207 | |
| | 208 | if(!dblockptr->ObStrategyBlock && dblockptr->ObTxAnimCtrlBlks) |
| | 209 | { |
| | 210 | TXACTRLBLK *taptr = dblockptr->ObTxAnimCtrlBlks; |
| | 211 | |
| | 212 | while(taptr) |
| | 213 | { |
| | 214 | DeallocateTxAnimBlock(taptr); |
| | 215 | taptr = taptr->tac_next; |
| | 216 | } |
| | 217 | } |
| | 218 | |
| | 219 | /* KJL 16:52:43 06/01/98 - dealloc sfx block if one exists */ |
| | 220 | if(dblockptr->SfxPtr) |
| | 221 | DeallocateSfxBlock(dblockptr->SfxPtr); |
| | 222 | |
| | 223 | dblockptr->index = -1; |
| | 224 | FreeBlockListPtr++; |
| | 225 | *FreeBlockListPtr = dblockptr; |
| | 226 | NumFreeBlocks++; /* One more free block */ |
| | 227 | *dblockptr2 = NULL; |
| | 228 | } |
| | 229 | } |
| | 230 | } |
| | 231 | |
| | 232 | // Support Functions for Texture Animation Blocks |
| | 233 | |
| | 234 | void InitialiseTxAnimBlocks() |
| | 235 | { |
| | 236 | TXACTRLBLK *FreeBlkPtr = &FreeTxAnimBlockData[0]; |
| | 237 | |
| | 238 | FreeTxAnimBlockListPtr = &FreeTxAnimBlockList[maxTxAnimblocks-1]; |
| | 239 | |
| | 240 | for(NumFreeTxAnimBlocks=0; NumFreeTxAnimBlocks < maxTxAnimblocks; NumFreeTxAnimBlocks++) |
| | 241 | { |
| | 242 | FreeTxAnimBlockList[NumFreeTxAnimBlocks] = FreeBlkPtr; |
| | 243 | FreeBlkPtr++; |
| | 244 | } |
| | 245 | } |
| | 246 | |
| | 247 | void InitialiseLightBlocks() |
| | 248 | { |
| | 249 | LIGHTBLOCK *FreeBlkPtr = &FreeLightBlockData[0]; |
| | 250 | |
| | 251 | FreeLightBlockListPtr = &FreeLightBlockList[maxlightblocks-1]; |
| | 252 | |
| | 253 | for(NumFreeLightBlocks=0; NumFreeLightBlocks < maxlightblocks; NumFreeLightBlocks++) |
| | 254 | { |
| | 255 | FreeLightBlockList[NumFreeLightBlocks] = FreeBlkPtr; |
| | 256 | FreeBlkPtr++; |
| | 257 | } |
| | 258 | } |
| | 259 | |
| | 260 | static const struct lightblock Zero_Ligthblock = |
| | 261 | { |
| | 262 | 0, |
| | 263 | { 0, 0, 0 }, |
| | 264 | { 0, 0, 0 }, |
| | 265 | 0, |
| | 266 | 0, |
| | 267 | 0, |
| | 268 | 0, |
| | 269 | 0, |
| | 270 | 0, |
| | 271 | 0 |
| | 272 | }; |
| | 273 | |
| | 274 | static LIGHTBLOCK* AllocateLightBlock() |
| | 275 | { |
| | 276 | if(NumFreeLightBlocks) |
| | 277 | { |
| | 278 | LIGHTBLOCK *FreeBlkPtr = *FreeLightBlockListPtr--; |
| | 279 | |
| | 280 | NumFreeLightBlocks--; /* One less free block */ |
| | 281 | |
| | 282 | *FreeBlkPtr = Zero_Ligthblock; |
| | 283 | return FreeBlkPtr; |
| | 284 | } |
| | 285 | |
| | 286 | return NULL; |
| | 287 | } |
| | 288 | |
| | 289 | /* |
| | 290 | See if there are any free slots in the dptr light block array. |
| | 291 | If there are, allocate a light block, place it in the list and return |
| | 292 | the pointer to the caller. |
| | 293 | |
| | 294 | A late addition is the passing of a light block (from somewhere, it does |
| | 295 | not matter where). This light block is then added rather than one being |
| | 296 | allocated from the free light block list. |
| | 297 | |
| | 298 | */ |
| | 299 | |
| | 300 | LIGHTBLOCK* AddLightBlock(DISPLAYBLOCK *dptr, LIGHTBLOCK *lptr_to_add) |
| | 301 | { |
| | 302 | int i; |
| | 303 | for(i=0; i < MaxObjectLights; i++) |
| | 304 | { |
| | 305 | if(NULL == dptr->ObLights[i]) |
| | 306 | { |
| | 307 | LIGHTBLOCK *lptr = lptr_to_add ? lptr_to_add : AllocateLightBlock(); |
| | 308 | |
| | 309 | if(lptr) |
| | 310 | { |
| | 311 | dptr->ObLights[i] = lptr; |
| | 312 | dptr->ObNumLights++; |
| | 313 | } |
| | 314 | |
| | 315 | return lptr; |
| | 316 | } |
| | 317 | } |
| | 318 | |
| | 319 | return NULL; |
| | 320 | } |