Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

WBitmap.c

Go to the documentation of this file.
00001 /****************************************************************************************/
00002 /*  WBitmap.c                                                                           */
00003 /*                                                                                      */
00004 /*  Author: John Pollard                                                                */
00005 /*  Description: Creates geBitmaps from the data in the BSP, that are used to render    */
00006 /*                                                                                      */
00007 /*  The contents of this file are subject to the Genesis3D Public License               */
00008 /*  Version 1.01 (the "License"); you may not use this file except in                   */
00009 /*  compliance with the License. You may obtain a copy of the License at                */
00010 /*  http://www.genesis3d.com                                                            */
00011 /*                                                                                      */
00012 /*  Software distributed under the License is distributed on an "AS IS"                 */
00013 /*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See                */
00014 /*  the License for the specific language governing rights and limitations              */
00015 /*  under the License.                                                                  */
00016 /*                                                                                      */
00017 /*  The Original Code is Genesis3D, released March 25, 1999.                            */
00018 /*Genesis3D Version 1.1 released November 15, 1999                            */
00019 /*  Copyright (C) 1999 WildTangent, Inc. All Rights Reserved           */
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 //      NOTES -
00034 //      WBitmap is the original owner of all the bitmaps in the .BSP file.  They are kind of a hack right now.
00035 //      Right now, the textures are raw data in the .BSP.  This module, takes that data, and creates geBitmaps
00036 //      for each texture in the.BSP file.  The faces are then referred to these geBitmaps, and NOT the texture data.
00037 //      The texture data can then be freed.
00038 //      The way it really should eventually work, is that the bitmaps will be in the .BSP upon load time!!!
00039 //              JP
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;                              // Linear array of WBitmaps created when the Pool was created
00058 } geWBitmap_Pool;
00059 
00060 //=====================================================================================
00061 //      geWBitmap_Pool_Create
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         // Clear out the memory for the Pool
00078         ZeroMem(Pool);                          
00079 
00080         // Create all the bitmaps from the bspdata... (this should eventually just stream right out of the file...)
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;                                    // Done, good to go
00088 
00089         // Error
00090         ExitWithError:
00091         {
00092                 if (Pool)
00093                         geWBitmap_Pool_Destroy(Pool);
00094 
00095                 return NULL;
00096         }
00097 }
00098 
00099 //=====================================================================================
00100 //      geWBitmap_Pool_Destroy
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 //      geWBitmap_PoolGetWBitmapCount
00113 //=====================================================================================
00114 int32 geWBitmap_Pool_GetWBitmapCount(geWBitmap_Pool *Pool)
00115 {
00116         assert(Pool);
00117 
00118         return Pool->NumWBitmaps;
00119 }
00120 
00121 //=====================================================================================
00122 //      geWBitmap_Pool_GetBitmapByIndex
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 //      geWBitmap_Pool_GetWBitmapByBitmap
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 //      geWBitmap_Pool_GetBitmapByIndex
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 //      geWBitmap_Pool_GetBitmapByName
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;                                // Found it
00184         }
00185 
00186         return NULL;
00187 }
00188 
00189 #define MAX_MIPS_ALLOWED        4
00190 
00191 //=====================================================================================
00192 //      geWBitmap_Pool_CreateAllWBitmaps
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         // BitmapIsTransparent is a temporary array, that is filled in with a 1, if any face that uses it, has the
00215         //      TEXINFO_TRANS flag set.  If they expect to "see" thru the surface, then they should have set this flag 
00216         //      in the editor.  If this flag is not set, then we won't allow a color key on the surface...
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         // Now, go mark all the bitmaps that have transparent set in the texinfo
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;             // This face has the transparent value set, so mark the bitmap it references
00240                 }
00241         }
00242 
00243         // Create the bitmap pool.
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         // Clear the bitmap array
00253         memset(Pool->WBitmaps,0,sizeof(geWBitmap)*BSPData->NumGFXTextures);
00254         Pool->NumWBitmaps = BSPData->NumGFXTextures;            // Store the number of slots
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          //Start Dec2001DCS - ColorKey = 24 bit version of bright magenta.  NOTE: Blue value is 254 (0xfe)
00269          //                                                                       because I couldn't make a 24 bit bitmap
00270          //                                                                       with MSPaint using the color 
00271          //                                                                       255 0 255.  Even though Paint said it
00272          //                                                                       was the right value, by the time the 
00273          //                                                                       bitmap got to Genesis it was read as
00274          //                                                                       255 0 254 ???
00275                         ColorKey = 0xffff00fe;
00276          //End Dec2001DCS
00277                 }
00278                 else
00279                 {
00280                         UseColorKey = GE_FALSE;
00281                         ColorKey = 0;
00282                 }
00283 
00284                 strcpy(pWBitmap->Name, pGFXTexture->Name);
00285 
00286                 // <> CB this is the main loop where GFX BSP textures are made into bitmaps
00287 
00288                 // Get width and height
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                 // Create the bitmap
00300 //Start Dec2001DCS - Changed format to 32 bit
00301 // added transparent textures
00302            pWBitmap->Bitmap = geBitmap_Create(Width, Height, NumMips, GE_PIXELFORMAT_32BIT_ARGB);
00303 //End Dec2001DCS
00304 
00305                 if (!pWBitmap->Bitmap)
00306                 {
00307                         geErrorLog_AddString(-1, "geWBitmap_Pool_CreateAllWBitmaps:  Could not create geBitmap.", NULL);
00308                         goto ExitWithError;
00309                 }
00310 
00311                 // Set the driver flags
00312                 if (pGFXTexture->Flags & TEXTURE_SKYBOX)
00313                 {
00314                         geBitmap_SetDriverFlags(pWBitmap->Bitmap, RDRIVER_PF_3D);       // Skybox textures do not need to combine with lightmaps
00315                 }
00316                 else
00317                 {
00318                         geBitmap_SetDriverFlags(pWBitmap->Bitmap, RDRIVER_PF_3D | RDRIVER_PF_COMBINE_LIGHTMAP);
00319                 }
00320 
00321                 //<> 
00322                 NumMips = 1;
00323                 // Lock all the miplevels
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                 // Get the src from the .bsp texture data
00331                 pSrc = &BSPData->GFXTexData[pGFXTexture->Offset];
00332 
00333                 // Fill all the mip levels
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             //Start Dec2001DCS - Added * 4 since textures are now 32 bit
00357                         // added transparent textures
00358                            memcpy(pDest,pSrc,Width*Height*4);
00359             //End Dec2001DCS
00360                         }
00361                         else
00362                         {
00363                         int h;
00364                                 for (h=Height;h--;)
00365                                 {
00366                //Start Dec2001DCS - Added * 4 since textures are now 32 bit
00367                                 // added transparent textures
00368                memcpy(pDest,pSrc,Width*4);
00369                //End Dec2001DCS
00370                                         pSrc += Width*4;
00371                                         pDest += Stride*4;
00372                                 }
00373                         }
00374 
00375                         // Unlock this mip level, it is filled with the data
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                 // Create the palette...
00384                 {
00385                         geBitmap_Palette                *Pal;
00386                         int32                                   PalSize,cnt;
00387                         gePixelFormat                   Format;
00388                         uint32                                  *DstPalPtr,*DstPalData;
00389                         DRV_RGB                                 *SrcPalPtr;
00390 
00391                         //Pal = geBitmap_Palette_Create(GE_PIXELFORMAT_32BIT_ARGB, 256);
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                         //cnt = sizeof(DRV_Palette); 
00407                         // <> this doesn't seem to respect the pragma pack in dcommon !
00408                                 // I get size == 768
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                         //debug to see if anyone is using entry 255 :
00418                         // DstPalData[255] = 255;       // color key (it's bright blue & transparent)
00419                         //or black it : DstPalData[255] = 0;
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                 } //done making the palette
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         // added to stop a leak         
00445         if (BitmapIsTransparent)
00446         {
00447                 geRam_Free(BitmapIsTransparent);
00448                 BitmapIsTransparent = NULL;
00449         }
00450 
00451         return GE_TRUE;
00452 
00453         // Error
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 //      geWBitmap_Pool_DestroyAllWBitmaps
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                         // Destroy the geBitmap 
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;  // Just to be sure
00500 }
00501 
00502 //=====================================================================================
00503 //      geWBitmap_GetFlags
00504 //=====================================================================================
00505 uint32 geWBitmap_GetFlags(geWBitmap *WBitmap)
00506 {
00507         assert(WBitmap);
00508 
00509         return WBitmap->Flags;
00510 }
00511 
00512 //=====================================================================================
00513 //      geWBitmap_GetBitmap
00514 //=====================================================================================
00515 geBitmap *geWBitmap_GetBitmap(geWBitmap *WBitmap)
00516 {
00517         assert(WBitmap);
00518 
00519         return WBitmap->Bitmap;
00520 }
00521 
00522 //=====================================================================================
00523 //      geWBitmap_GetVisFrame
00524 //=====================================================================================
00525 int32 geWBitmap_GetVisFrame(geWBitmap *WBitmap)
00526 {
00527         assert(WBitmap);
00528 
00529         return WBitmap->VisFrame;
00530 }
00531 
00532 //=====================================================================================
00533 //      geWBitmap_SetVisFrame
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 // changed texture name
00545 
00546 //=====================================================================================
00547 //      geWBitmap_Pool_GetWNameByBitmap
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 // end change texture name

Generated on Tue Sep 30 12:36:38 2003 for GTestAndEngine by doxygen 1.3.2