00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include <Assert.h>
00023
00024
00025
00026 #include "WBitmap.h"
00027 #include "GBSPFile.h"
00028 #include "Ram.h"
00029 #include "Bitmap.h"
00030 #include "Errorlog.h"
00031 #include "Bitmap._h"
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #define ZeroMem(Ptr) memset(Ptr,0,sizeof(*Ptr))
00042
00043 typedef struct geWBitmap
00044 {
00045 char Name[64];
00046 geBitmap *Bitmap;
00047
00048 int32 VisFrame;
00049
00050 uint32 Flags;
00051 } geWBitmap;
00052
00053 typedef struct geWBitmap_Pool
00054 {
00055 int32 NumWBitmaps;
00056
00057 geWBitmap *WBitmaps;
00058 } geWBitmap_Pool;
00059
00060
00061
00062
00063 geWBitmap_Pool *geWBitmap_Pool_Create(GBSP_BSPData *BSPData)
00064 {
00065 geWBitmap_Pool *Pool;
00066
00067 assert(BSPData);
00068
00069 Pool = GE_RAM_ALLOCATE_STRUCT(geWBitmap_Pool);
00070
00071 if (!Pool)
00072 {
00073 geErrorLog_AddString(-1, "geWBitmap_Pool_Create: Could not create the Pool. Out of memory.", NULL);
00074 goto ExitWithError;
00075 }
00076
00077
00078 ZeroMem(Pool);
00079
00080
00081 if (!geWBitmap_Pool_CreateAllWBitmaps(Pool, BSPData))
00082 {
00083 geErrorLog_AddString(-1, "geWBitmap_Pool_Create: geWBitmap_Pool_CreateAllWBitmaps failed.", NULL);
00084 goto ExitWithError;
00085 }
00086
00087 return Pool;
00088
00089
00090 ExitWithError:
00091 {
00092 if (Pool)
00093 geWBitmap_Pool_Destroy(Pool);
00094
00095 return NULL;
00096 }
00097 }
00098
00099
00100
00101
00102 void geWBitmap_Pool_Destroy(geWBitmap_Pool *Pool)
00103 {
00104 assert(Pool);
00105
00106 geWBitmap_Pool_DestroyAllWBitmaps(Pool);
00107
00108 geRam_Free(Pool);
00109 }
00110
00111
00112
00113
00114 int32 geWBitmap_Pool_GetWBitmapCount(geWBitmap_Pool *Pool)
00115 {
00116 assert(Pool);
00117
00118 return Pool->NumWBitmaps;
00119 }
00120
00121
00122
00123
00124 geWBitmap *geWBitmap_Pool_GetWBitmapByIndex(geWBitmap_Pool *Pool, int32 Index)
00125 {
00126 assert(Pool);
00127 assert(Index >= 0);
00128 assert(Index < Pool->NumWBitmaps);
00129
00130 return &Pool->WBitmaps[Index];
00131 }
00132
00133
00134
00135
00136 geWBitmap *geWBitmap_Pool_GetWBitmapByBitmap(geWBitmap_Pool *Pool, const geBitmap *Bitmap)
00137 {
00138 geWBitmap *pWBitmap;
00139 int32 i;
00140
00141 assert(Pool);
00142 assert(Bitmap);
00143
00144 pWBitmap = Pool->WBitmaps;
00145
00146 for (i=0; i< Pool->NumWBitmaps; i++, pWBitmap++)
00147 {
00148 if (pWBitmap->Bitmap == Bitmap)
00149 return pWBitmap;
00150 }
00151
00152 return NULL;
00153 }
00154
00155
00156
00157
00158 geBitmap *geWBitmap_Pool_GetBitmapByIndex(geWBitmap_Pool *Pool, int32 Index)
00159 {
00160 assert(Pool);
00161 assert(Index >= 0);
00162 assert(Index < Pool->NumWBitmaps);
00163 assert(Pool->WBitmaps[Index].Bitmap);
00164
00165 return Pool->WBitmaps[Index].Bitmap;
00166 }
00167
00168
00169
00170
00171 geBitmap *geWBitmap_Pool_GetBitmapByName(geWBitmap_Pool *Pool, const char *BitmapName)
00172 {
00173 geWBitmap *pWBitmap;
00174 int32 i;
00175
00176 assert(Pool);
00177 assert(BitmapName);
00178
00179 pWBitmap = Pool->WBitmaps;
00180 for (i=0; i< Pool->NumWBitmaps; i++, pWBitmap++)
00181 {
00182 if (!stricmp(pWBitmap->Name, BitmapName))
00183 return pWBitmap->Bitmap;
00184 }
00185
00186 return NULL;
00187 }
00188
00189 #define MAX_MIPS_ALLOWED 4
00190
00191
00192
00193
00194 geBoolean geWBitmap_Pool_CreateAllWBitmaps(geWBitmap_Pool *Pool, GBSP_BSPData *BSPData)
00195 {
00196 int32 i;
00197 geWBitmap *pWBitmap;
00198 GFX_Texture *pGFXTexture;
00199 uint8 *BitmapIsTransparent;
00200 GFX_Face *pFace;
00201 geBoolean UseColorKey;
00202 uint32 ColorKey;
00203
00204 assert(Pool);
00205 assert(BSPData);
00206 assert(Pool->NumWBitmaps == 0);
00207 assert(Pool->WBitmaps == NULL);
00208
00209 if (!BSPData->NumGFXTextures)
00210 return GE_TRUE;
00211
00212 assert(BSPData->GFXTextures);
00213
00214
00215
00216
00217 BitmapIsTransparent = GE_RAM_ALLOCATE_ARRAY(uint8, BSPData->NumGFXTextures);
00218
00219 if (!BitmapIsTransparent)
00220 {
00221 geErrorLog_AddString(-1, "geWBitmap_Pool_CreateAllWBitmaps: Could not create the BitmapIsTransparent array. Out of memory.", NULL);
00222 goto ExitWithError;
00223 }
00224
00225 memset(BitmapIsTransparent,0,BSPData->NumGFXTextures);
00226
00227
00228 pFace = BSPData->GFXFaces;
00229 for (i=0; i< BSPData->NumGFXFaces; i++, pFace++)
00230 {
00231 GFX_TexInfo *pTexInfo;
00232
00233 pTexInfo = &BSPData->GFXTexInfo[pFace->TexInfo];
00234
00235 assert(pTexInfo->Texture >= 0 || pTexInfo->Texture < BSPData->NumGFXTextures);
00236
00237 if (pTexInfo->Flags & TEXINFO_TRANS)
00238 {
00239 BitmapIsTransparent[pTexInfo->Texture] = 1;
00240 }
00241 }
00242
00243
00244 Pool->WBitmaps = GE_RAM_ALLOCATE_ARRAY(geWBitmap, BSPData->NumGFXTextures);
00245
00246 if (!Pool->WBitmaps)
00247 {
00248 geErrorLog_AddString(-1, "geWBitmap_Pool_CreateAllWBitmaps: Could not create the Pool bitmaps. Out of memory.", NULL);
00249 goto ExitWithError;
00250 }
00251
00252
00253 memset(Pool->WBitmaps,0,sizeof(geWBitmap)*BSPData->NumGFXTextures);
00254 Pool->NumWBitmaps = BSPData->NumGFXTextures;
00255
00256 pWBitmap = Pool->WBitmaps;
00257 pGFXTexture = BSPData->GFXTextures;
00258
00259 for (i=0; i< BSPData->NumGFXTextures; i++, pGFXTexture++, pWBitmap++)
00260 {
00261 geBitmap *Mips[MAX_MIPS_ALLOWED];
00262 int32 NumMips, m, Width, Height, Stride;
00263 uint8 *pSrc;
00264
00265 if (BitmapIsTransparent[i])
00266 {
00267 UseColorKey = GE_TRUE;
00268
00269
00270
00271
00272
00273
00274
00275 ColorKey = 0xffff00fe;
00276
00277 }
00278 else
00279 {
00280 UseColorKey = GE_FALSE;
00281 ColorKey = 0;
00282 }
00283
00284 strcpy(pWBitmap->Name, pGFXTexture->Name);
00285
00286
00287
00288
00289 Width = pGFXTexture->Width;
00290 Height = pGFXTexture->Height;
00291
00292 if (pGFXTexture->Flags & TEXTURE_SKYBOX)
00293 NumMips = 1;
00294 else
00295 NumMips = 4;
00296
00297 assert(NumMips <= MAX_MIPS_ALLOWED);
00298
00299
00300
00301
00302 pWBitmap->Bitmap = geBitmap_Create(Width, Height, NumMips, GE_PIXELFORMAT_32BIT_ARGB);
00303
00304
00305 if (!pWBitmap->Bitmap)
00306 {
00307 geErrorLog_AddString(-1, "geWBitmap_Pool_CreateAllWBitmaps: Could not create geBitmap.", NULL);
00308 goto ExitWithError;
00309 }
00310
00311
00312 if (pGFXTexture->Flags & TEXTURE_SKYBOX)
00313 {
00314 geBitmap_SetDriverFlags(pWBitmap->Bitmap, RDRIVER_PF_3D);
00315 }
00316 else
00317 {
00318 geBitmap_SetDriverFlags(pWBitmap->Bitmap, RDRIVER_PF_3D | RDRIVER_PF_COMBINE_LIGHTMAP);
00319 }
00320
00321
00322 NumMips = 1;
00323
00324 if (!geBitmap_LockForWrite(pWBitmap->Bitmap, Mips, 0, NumMips-1))
00325 {
00326 geErrorLog_AddString(-1, "geWBitmap_Pool_CreateAllWBitmaps: geBitmap_LockForWrite failed.", NULL);
00327 goto ExitWithError;
00328 }
00329
00330
00331 pSrc = &BSPData->GFXTexData[pGFXTexture->Offset];
00332
00333
00334 for (m=0; m< NumMips; m++)
00335 {
00336 uint8 *pDest;
00337 geBitmap_Info Info;
00338
00339 if (!geBitmap_GetInfo(Mips[m], &Info, NULL))
00340 {
00341 geErrorLog_AddString(-1, "geWBitmap_Pool_CreateAllWBitmaps: geBitmap_GetInfo failed.", NULL);
00342 goto ExitWithError;
00343 }
00344
00345 pDest = geBitmap_GetBits(Mips[m]);
00346 assert( pDest );
00347
00348 Stride = Info.Stride;
00349 Width = Info.Width;
00350 Height = Info.Height;
00351
00352 assert(Stride >= Width);
00353
00354 if ( Stride == Width )
00355 {
00356
00357
00358 memcpy(pDest,pSrc,Width*Height*4);
00359
00360 }
00361 else
00362 {
00363 int h;
00364 for (h=Height;h--;)
00365 {
00366
00367
00368 memcpy(pDest,pSrc,Width*4);
00369
00370 pSrc += Width*4;
00371 pDest += Stride*4;
00372 }
00373 }
00374
00375
00376 if (!geBitmap_UnLock(Mips[m]))
00377 {
00378 geErrorLog_AddString(-1, "geWBitmap_Pool_CreateAllWBitmaps: geBitmap_Unlock failed.", NULL);
00379 goto ExitWithError;
00380 }
00381 }
00382
00383
00384 {
00385 geBitmap_Palette *Pal;
00386 int32 PalSize,cnt;
00387 gePixelFormat Format;
00388 uint32 *DstPalPtr,*DstPalData;
00389 DRV_RGB *SrcPalPtr;
00390
00391
00392 Pal = geBitmap_Palette_Create(GE_PIXELFORMAT_32BIT_XRGB, 256);
00393
00394 if (!Pal)
00395 {
00396 geErrorLog_AddString(-1, "geWBitmap_Pool_CreateAllWBitmaps: geBitmap_Palette_Create failed.", NULL);
00397 return GE_FALSE;
00398 }
00399
00400 if (!geBitmap_Palette_Lock(Pal, &DstPalData, &Format, &PalSize))
00401 {
00402 geErrorLog_AddString(-1, "geWBitmap_Pool_CreateAllWBitmaps: geBitmap_Palette_Lock failed.", NULL);
00403 return GE_FALSE;
00404 }
00405
00406
00407
00408
00409
00410 SrcPalPtr = BSPData->GFXPalettes[pGFXTexture->PaletteIndex];
00411 DstPalPtr = DstPalData;
00412 for(cnt=PalSize;cnt--;)
00413 {
00414 *DstPalPtr++ = ((uint32)0xFF000000) + ((SrcPalPtr->r)<<16) + ((SrcPalPtr->g)<<8) + (SrcPalPtr->b);
00415 SrcPalPtr ++;
00416 }
00417
00418
00419
00420
00421 if (!geBitmap_Palette_UnLock(Pal))
00422 {
00423 geErrorLog_AddString(-1, "geWBitmap_Pool_CreateAllWBitmaps: geBitmap_Palette_UnLock failed.", NULL);
00424 return GE_FALSE;
00425 }
00426
00427 if (!geBitmap_SetPalette(pWBitmap->Bitmap, Pal))
00428 {
00429 geErrorLog_AddString(-1, "geWBitmap_Pool_CreateAllWBitmaps: geBitmap_SetPalette failed.", NULL);
00430 return GE_FALSE;
00431 }
00432
00433 geBitmap_Palette_Destroy(&Pal);
00434 }
00435
00436 if (!geBitmap_SetColorKey(pWBitmap->Bitmap, UseColorKey, ColorKey, GE_TRUE))
00437 {
00438 geErrorLog_AddString(-1, "geWBitmap_Pool_CreateAllWBitmaps: geBitmap_SetColorKey failed.", NULL);
00439 goto ExitWithError;
00440 }
00441
00442 }
00443
00444
00445 if (BitmapIsTransparent)
00446 {
00447 geRam_Free(BitmapIsTransparent);
00448 BitmapIsTransparent = NULL;
00449 }
00450
00451 return GE_TRUE;
00452
00453
00454 ExitWithError:
00455 {
00456 if (Pool->WBitmaps)
00457 {
00458 geWBitmap_Pool_DestroyAllWBitmaps(Pool);
00459 Pool->WBitmaps = NULL;
00460 }
00461
00462 if (BitmapIsTransparent)
00463 {
00464 geRam_Free(BitmapIsTransparent);
00465 BitmapIsTransparent = NULL;
00466 }
00467
00468 return GE_FALSE;
00469 }
00470 }
00471
00472
00473
00474
00475 void geWBitmap_Pool_DestroyAllWBitmaps(geWBitmap_Pool *Pool)
00476 {
00477 assert(Pool);
00478
00479 if (Pool->WBitmaps)
00480 {
00481 int32 i;
00482 geWBitmap *pWBitmap;
00483
00484 pWBitmap = Pool->WBitmaps;
00485
00486 for (i=0; i< Pool->NumWBitmaps; i++, pWBitmap++)
00487 {
00488
00489 if (pWBitmap->Bitmap)
00490 {
00491 geBitmap_Destroy(&pWBitmap->Bitmap);
00492 pWBitmap->Bitmap = NULL;
00493 }
00494 }
00495
00496 geRam_Free(Pool->WBitmaps);
00497 }
00498
00499 Pool->WBitmaps = NULL;
00500 }
00501
00502
00503
00504
00505 uint32 geWBitmap_GetFlags(geWBitmap *WBitmap)
00506 {
00507 assert(WBitmap);
00508
00509 return WBitmap->Flags;
00510 }
00511
00512
00513
00514
00515 geBitmap *geWBitmap_GetBitmap(geWBitmap *WBitmap)
00516 {
00517 assert(WBitmap);
00518
00519 return WBitmap->Bitmap;
00520 }
00521
00522
00523
00524
00525 int32 geWBitmap_GetVisFrame(geWBitmap *WBitmap)
00526 {
00527 assert(WBitmap);
00528
00529 return WBitmap->VisFrame;
00530 }
00531
00532
00533
00534
00535 geBoolean geWBitmap_SetVisFrame(geWBitmap *WBitmap, int32 VisFrame)
00536 {
00537 assert(WBitmap);
00538
00539 WBitmap->VisFrame = VisFrame;
00540
00541 return GE_TRUE;
00542 }
00543
00544
00545
00546
00547
00548
00549 char *geWBitmap_Pool_GetWNameByBitmap(geWBitmap_Pool *Pool, const geBitmap *Bitmap)
00550 {
00551 geWBitmap *pWBitmap;
00552 int32 i;
00553
00554 assert(Pool);
00555 assert(Bitmap);
00556
00557 pWBitmap = Pool->WBitmaps;
00558
00559 for (i=0; i< Pool->NumWBitmaps; i++, pWBitmap++)
00560 {
00561 char str[100];
00562 sprintf(str,"WBitmap : %x\n",pWBitmap->Bitmap);
00563 OutputDebugString(str);
00564 if (pWBitmap->Bitmap == Bitmap)
00565 return pWBitmap->Name;
00566 }
00567
00568 return NULL;
00569 }
00570