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

SWTHandle.c

Go to the documentation of this file.
00001 /****************************************************************************************/
00002 /*  SWTHandle.C                                                                         */
00003 /*                                                                                      */
00004 /*  Author: Mike Sandige, John Pollard                                                  */
00005 /*  Description:  Manager for texture construction and available texture formats for    */
00006 /*                the software driver                                                   */
00007 /*                                                                                      */
00008 /*  The contents of this file are subject to the Genesis3D Public License               */
00009 /*  Version 1.01 (the "License"); you may not use this file except in                   */
00010 /*  compliance with the License. You may obtain a copy of the License at                */
00011 /*  http://www.genesis3d.com                                                            */
00012 /*                                                                                      */
00013 /*  Software distributed under the License is distributed on an "AS IS"                 */
00014 /*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See                */
00015 /*  the License for the specific language governing rights and limitations              */
00016 /*  under the License.                                                                  */
00017 /*                                                                                      */
00018 /*  The Original Code is Genesis3D, released March 25, 1999.                            */
00019 /*Genesis3D Version 1.1 released November 15, 1999                            */
00020 /*  Copyright (C) 1999 WildTangent, Inc. All Rights Reserved           */
00021 /*                                                                                      */
00022 /****************************************************************************************/
00023 
00024 #include <stdlib.h>                                     // malloc, free
00025 #include <memory.h>                                     // memset
00026 #include <Assert.h>
00027 
00028 #include "SoftDrv.h"                            // SD_Display, ClientWindow.  Could be cleaned up.
00029 #include "SWTHandle.h"
00030 
00031 #ifdef GENESIS_VERSION_2
00032 #include "errorlog.h"
00033 #else
00034 #define geErrorLog_AddString(Error,xx,yy) 
00035 #endif
00036 
00037 
00038 #define MAX_TEXTURE_HANDLES             15000
00039 
00040 geRDriver_THandle       SWTHandle_TextureHandles[MAX_TEXTURE_HANDLES];
00041 
00042 static int32 SWTHandle_SnapToPower2(int32 Width)
00043 {
00044         if (Width > 1 && Width <= 2) Width = 2;
00045         else if (Width > 2 && Width <= 4) Width = 4;
00046         else if (Width > 4 && Width <= 8) Width = 8;
00047         else if (Width > 8 && Width <= 16) Width =16;
00048         else if (Width > 16 && Width <= 32) Width = 32;
00049         else if (Width > 32 && Width <= 64) Width = 64;
00050         else if (Width > 64 && Width <= 128) Width = 128;
00051         else if (Width > 128 && Width <= 256) Width = 256;
00052 
00053         return Width;
00054 }
00055 
00056 
00057 //========================================================================================
00058 //      SWTHandle_FindTextureHandle
00059 //========================================================================================
00060 static geRDriver_THandle *SWTHandle_FindTextureHandle()
00061 {
00062         int32                           i;
00063         geRDriver_THandle       *THandle;
00064 
00065         THandle = SWTHandle_TextureHandles;
00066 
00067         for (i=0; i< MAX_TEXTURE_HANDLES; i++, THandle++)
00068         {
00069                 if (!THandle->Active)
00070                 {
00071                         memset(THandle, 0, sizeof(geRDriver_THandle));
00072 
00073                         THandle->Active = GE_TRUE;
00074 
00075                         return THandle;
00076                 }
00077         }
00078 
00079         return NULL;
00080 }
00081 
00082 //========================================================================================
00083 //      SWTHandle_FreeTextureHandle
00084 //========================================================================================
00085 static geBoolean SWTHandle_FreeTextureHandle(geRDriver_THandle *THandle)
00086 {
00087         int     k;
00088 
00089         assert(THandle);
00090         //<>    assert(THandle->Active == GE_TRUE);
00091 
00092         if( ! THandle->Active )
00093         {
00094                 return  GE_FALSE;
00095         }
00096 
00097         if(THandle->PalHandle)
00098         {
00099                 SWTHandle_FreeTextureHandle(THandle->PalHandle);
00100         }
00101 
00102         if(THandle->AlphaHandle)
00103         {
00104                 SWTHandle_FreeTextureHandle(THandle->AlphaHandle);
00105         }
00106 
00107         for(k=0;k < THandle->MipLevels;k++)
00108         {
00109                 if(THandle->BitPtr[k])
00110                         {
00111                                 free(THandle->BitPtr[k]);
00112                         }
00113                 THandle->BitPtr[k]      =NULL;
00114         }
00115 
00116         memset(THandle, 0, sizeof(geRDriver_THandle));  
00117 
00118         return  GE_TRUE;
00119 }
00120 
00121 //==================================================================================
00122 //      SWTHandle_FreeAllTextureHandles
00123 //==================================================================================
00124 geBoolean SWTHandle_FreeAllTextureHandles(void)
00125 {
00126         int32                           i;
00127         geRDriver_THandle       *pTHandle;
00128 
00129         pTHandle = SWTHandle_TextureHandles;
00130 
00131         for (i=0; i< MAX_TEXTURE_HANDLES; i++, pTHandle++)
00132         {
00133                 if (!pTHandle->Active)
00134                 {
00135                         continue;
00136                 }
00137                 
00138                 SWTHandle_FreeTextureHandle(pTHandle);
00139         }
00140 
00141         return GE_TRUE;
00142 }
00143 
00144 geRDriver_THandle *DRIVERCC SWTHandle_CreateTexture(int32                                                       Width, 
00145                                                                                                         int32                                                   Height, 
00146                                                                                                         int32                                                   NumMipLevels, 
00147                                                                                                         const geRDriver_PixelFormat             *PixelFormat)
00148 {
00149         int32                           i, SWidth, SHeight;                     // Snapped to power of 2 width/height
00150         geRDriver_THandle       *THandle;
00151 
00152         assert(PixelFormat);
00153         
00154         THandle = SWTHandle_FindTextureHandle();
00155 
00156         if (!THandle)
00157         {
00158                 geErrorLog_AddString(GE_ERR_INTERNAL_RESOURCE, "SWTHandle_CreateTexture:  No more handles left.",NULL);
00159                 goto ExitWithError;
00160         }
00161 
00162         if(PixelFormat->Flags & RDRIVER_PF_3D)
00163         {
00164                 if (Width > 256)
00165                 {
00166                         geErrorLog_AddString(GE_ERR_BAD_PARAMETER, "SWTHandle_CreateTexture: Width > 256.",NULL);
00167                         goto ExitWithError;
00168                 }
00169 
00170                 if (Height > 256)
00171                 {
00172                         geErrorLog_AddString(GE_ERR_BAD_PARAMETER, "SWTHandle_CreateTexture: Height > 256.",NULL);
00173                         goto ExitWithError;
00174                 }
00175                 SWidth = SWTHandle_SnapToPower2(Width);
00176                 SHeight = SWTHandle_SnapToPower2(Height);
00177 
00178                 if (Width != SWidth)
00179                 {
00180                         geErrorLog_AddString(GE_ERR_BAD_PARAMETER, "SWTHandle_CreateTexture: Not a power of 2.",NULL);
00181                         goto ExitWithError;
00182                 }
00183 
00184                 if (Height != SHeight)
00185                 {
00186                         geErrorLog_AddString(GE_ERR_BAD_PARAMETER, "SWTHandle_CreateTexture: Not a power of 2.",NULL);
00187                         goto ExitWithError;
00188                 }
00189         }
00190         
00191         THandle->MipLevels              =NumMipLevels;
00192         THandle->Width                  =Width;
00193         THandle->Height                 =Height;
00194         THandle->PixelFormat    =*PixelFormat;
00195 
00196         for(i=0;i < NumMipLevels;i++)
00197         {
00198                 if(PixelFormat->Flags & RDRIVER_PF_PALETTE)
00199                 {
00200                 
00201                         if( PixelFormat->PixelFormat == GE_PIXELFORMAT_32BIT_XRGB ||
00202                                 PixelFormat->PixelFormat == GE_PIXELFORMAT_32BIT_XBGR)  
00203                         {
00204                                 THandle->BitPtr[i]      =(uint16 *)malloc(sizeof(U32) * Width * Height);
00205                         }
00206                         else                                    
00207                         {
00208                                 geErrorLog_AddString(GE_ERR_BAD_PARAMETER, "SWTHandle_CreateTexture: Bad Pal format.",NULL);
00209                                 goto ExitWithError;
00210                         }
00211                 }
00212                 else
00213                 {
00214                         switch (PixelFormat->PixelFormat)
00215                         {
00216                                 case GE_PIXELFORMAT_16BIT_4444_ARGB:
00217                                 {
00218                                         THandle->BitPtr[i]      =(uint16 *)malloc(sizeof(uint16) * Width * Height);
00219                                         break;
00220                                 }
00221                                         
00222                                 case GE_PIXELFORMAT_16BIT_565_RGB:
00223                                 {
00224                                         THandle->BitPtr[i]      =(uint16 *)malloc(sizeof(uint16) * Width * Height);
00225                                         break;
00226                                 }
00227                                 
00228                                 case GE_PIXELFORMAT_16BIT_555_RGB:
00229                                 {
00230                                         THandle->BitPtr[i]      =(uint16 *)malloc(sizeof(uint16) * Width * Height);
00231                                         break;
00232                                 }
00233 
00234                                 case GE_PIXELFORMAT_8BIT:
00235                                 {
00236                                         THandle->BitPtr[i]      =(uint16 *)malloc(sizeof(uint8) * Width * Height);
00237                                         break;
00238                                 }
00239 
00240                                 case GE_PIXELFORMAT_24BIT_RGB:
00241                                 {
00242                                         THandle->BitPtr[i]      =(uint16 *)malloc((sizeof(uint8)*3) * Width * Height);
00243                                         break;
00244                                 }
00245 
00246                                 default:
00247                                 {
00248                                         geErrorLog_AddString(GE_ERR_BAD_PARAMETER, "SOFT_Create3DTexture: Invalid pixel format.",NULL);
00249                                         goto ExitWithError;
00250                                 }
00251                         }
00252                         
00253                         if ( PixelFormat->Flags & RDRIVER_PF_CAN_DO_COLORKEY )
00254                                 {
00255                                         THandle->Flags |= THANDLE_TRANS;
00256                                 }
00257 
00258                 }
00259                 Width = Width / 2;
00260                 if (Width < 1) Width = 1;
00261                 Height = Height / 2;
00262                 if (Height < 1) Height = 1;
00263         }
00264 
00265         return THandle;
00266                 
00267         ExitWithError:
00268         {
00269                 return NULL;
00270         }
00271 }
00272 
00273 geBoolean       DRIVERCC        SWTHandle_GetInfo(geRDriver_THandle *THandle, int32 MipLevel, geRDriver_THandleInfo *Info)
00274 {
00275         assert(THandle);
00276         assert(Info);
00277         if ( ! THandle->Active )
00278                 {
00279                         geErrorLog_AddString(GE_ERR_BAD_PARAMETER, "SWTHandle_GetInfo:  Bad Texture Handle.",NULL);
00280                         return GE_FALSE;
00281                 }
00282 
00283 
00284         Info->Width                     = THandle->Width  >> MipLevel;
00285         Info->Height            = THandle->Height >> MipLevel;
00286         Info->Stride            = THandle->Width  >> MipLevel;
00287         Info->ColorKey          = 1;
00288         Info->Flags                     = GE_FALSE;
00289         #pragma message ("is this right?")
00290         Info->PixelFormat       = THandle->PixelFormat;
00291         if ( THandle->Flags & THANDLE_TRANS )
00292                 {
00293         #pragma message ("remember this")
00294                         Info->Flags     = RDRIVER_THANDLE_HAS_COLORKEY;
00295                 }
00296         return  GE_TRUE;
00297 }
00298 
00299 //can be used to null out the pal (cant assert on palhandle)
00300 geBoolean DRIVERCC SWTHandle_SetPalette(geRDriver_THandle *THandle, geRDriver_THandle *PalHandle)
00301 {
00302         assert(THandle);
00303 
00304         THandle->PalHandle      =PalHandle;
00305 
00306         return  GE_TRUE;
00307 }
00308 
00309 //can be used to null out the pal (cant assert on palhandle)
00310 geRDriver_THandle *DRIVERCC SWTHandle_GetPalette(geRDriver_THandle *THandle)
00311 {
00312         assert(THandle);
00313         if ( ! THandle->Active )
00314                 {
00315                         geErrorLog_AddString(GE_ERR_BAD_PARAMETER, "SWTHandle_GetPalette: Bad Texture Handle.",NULL);
00316                         return GE_FALSE;
00317                 }
00318 
00319         return  THandle->PalHandle;
00320 }
00321 
00322 geBoolean DRIVERCC      SWTHandle_SetAlpha(geRDriver_THandle *THandle, geRDriver_THandle *AHandle)
00323 {
00324         assert(THandle);
00325         if ( ! THandle->Active )
00326                 {
00327                         geErrorLog_AddString(GE_ERR_BAD_PARAMETER, "SWTHandle_SetAlpha: Bad Texture Handle.",NULL);
00328                         return GE_FALSE;
00329                 }
00330 
00331         if(THandle->PixelFormat.Flags & RDRIVER_PF_HAS_ALPHA)
00332         {
00333                 if(AHandle->PixelFormat.Flags & RDRIVER_PF_ALPHA)
00334                 {
00335                         THandle->AlphaHandle    =AHandle;
00336                         return  GE_TRUE;
00337                 }
00338         }
00339         return  GE_FALSE;
00340 }
00341 
00342 geRDriver_THandle *DRIVERCC     SWTHandle_GetAlpha(geRDriver_THandle *THandle)
00343 {
00344         assert(THandle);
00345         if ( ! THandle->Active )
00346                 {
00347                         geErrorLog_AddString(GE_ERR_BAD_PARAMETER, "SWTHandle_GetAlpha: Bad Texture Handle.",NULL);
00348                         return GE_FALSE;
00349                 }
00350 
00351         return  THandle->AlphaHandle;
00352 }
00353 
00354 geBoolean DRIVERCC SWTHandle_DestroyTexture(geRDriver_THandle *THandle)
00355 {
00356         return SWTHandle_FreeTextureHandle(THandle);
00357 }
00358 
00359 geBoolean DRIVERCC SWTHandle_LockTextureHandle(geRDriver_THandle *THandle, int32 MipLevel, void **Data)
00360 {
00361         assert(THandle);
00362         assert(MipLevel <= THandle->MipLevels && MipLevel >= 0);
00363         if ( ! THandle->Active )
00364                 {
00365                         geErrorLog_AddString(GE_ERR_BAD_PARAMETER, "SWTHandle_LockTextureHandle: Bad Texture Handle.",NULL);
00366                         return GE_FALSE;
00367                 }
00368         if ( ! THandle->BitPtr[MipLevel] )
00369                 {
00370                         geErrorLog_AddString(GE_ERR_BAD_PARAMETER, "SWTHandle_LockTextureHandle: Bad Texture Handle in Mip.",NULL);
00371                         return GE_FALSE;
00372                 }
00373 
00374         THandle->Flags  |=(THANDLE_LOCKED << MipLevel);
00375         *Data                   =(uint16*)THandle->BitPtr[MipLevel];
00376 
00377         return  GE_TRUE;
00378 }
00379 
00380 geBoolean DRIVERCC SWTHandle_UnLockTextureHandle(geRDriver_THandle *THandle, int32 MipLevel)
00381 {
00382         assert(THandle);
00383         if (!(THandle->Flags & (THANDLE_LOCKED << MipLevel)))
00384                 {
00385                         geErrorLog_AddString(GE_ERR_BAD_PARAMETER, "SWTHandle_UnLockTextureHandle: Texture Handle is not Locked.",NULL);
00386                         return GE_FALSE;
00387                 }
00388 
00389 
00390         THandle->Flags  &=~(THANDLE_LOCKED << MipLevel);        // This mip is now unlocked
00391         THandle->Flags  |=THANDLE_UPDATE;                                       // Mark it for updating...
00392 
00393         return GE_TRUE;
00394 }
00395 
00396 
00397 
00398 
00399 geBoolean DRIVERCC SWTHandle_EnumPixelFormats(DRV_ENUM_PFORMAT_CB *Cb, void *Context)
00400 {
00401         geRDriver_PixelFormat   PixelFormat;
00402 
00403         if(!SD_Display)
00404         {
00405                 geErrorLog_AddString(GE_ERR_INTERNAL_RESOURCE, "SWTHandle_EnumPixelFormats:  No Mode Set.",NULL);
00406                 return GE_FALSE;
00407         }
00408 
00409 
00410         PixelFormat.PixelFormat = GE_PIXELFORMAT_8BIT;
00411         PixelFormat.Flags               = RDRIVER_PF_3D;
00412         if(!Cb(&PixelFormat, Context))
00413                 {
00414                         return  GE_TRUE;
00415                 }
00416         PixelFormat.PixelFormat = GE_PIXELFORMAT_8BIT;
00417         PixelFormat.Flags               = RDRIVER_PF_3D| RDRIVER_PF_COMBINE_LIGHTMAP;
00418         if(!Cb(&PixelFormat, Context))
00419                 {
00420                         return  GE_TRUE;
00421                 }
00422 
00423         PixelFormat.PixelFormat = GE_PIXELFORMAT_16BIT_4444_ARGB;
00424         PixelFormat.Flags               = RDRIVER_PF_3D;
00425         if(!Cb(&PixelFormat, Context))
00426                 {
00427                         return  GE_TRUE;
00428                 }
00429         PixelFormat.PixelFormat = GE_PIXELFORMAT_16BIT_4444_ARGB;
00430         PixelFormat.Flags               = RDRIVER_PF_3D | RDRIVER_PF_COMBINE_LIGHTMAP;
00431         if(!Cb(&PixelFormat, Context))
00432                 {
00433                         return  GE_TRUE;
00434                 }
00435 
00436         PixelFormat.PixelFormat = GE_PIXELFORMAT_32BIT_XBGR;
00437         PixelFormat.Flags               = RDRIVER_PF_PALETTE;
00438         if(!Cb(&PixelFormat, Context))
00439                 {
00440                         return  GE_TRUE;
00441                 }
00442 
00443         if(ClientWindow.G_mask == 0x3e0)        //555
00444                 {
00445                         PixelFormat.PixelFormat = GE_PIXELFORMAT_16BIT_555_RGB;
00446                         PixelFormat.Flags               = RDRIVER_PF_2D;
00447                         if(!Cb(&PixelFormat, Context))
00448                                 {
00449                                         return  GE_TRUE;
00450                                 }
00451                         PixelFormat.PixelFormat = GE_PIXELFORMAT_16BIT_555_RGB;
00452                         PixelFormat.Flags               = RDRIVER_PF_2D| RDRIVER_PF_CAN_DO_COLORKEY;
00453                         if(!Cb(&PixelFormat, Context))
00454                                 {
00455                                         return  GE_TRUE;
00456                                 }
00457                 }
00458         else
00459                 {
00460                         PixelFormat.PixelFormat = GE_PIXELFORMAT_16BIT_565_RGB;
00461                         PixelFormat.Flags               = RDRIVER_PF_2D;
00462                         if(!Cb(&PixelFormat, Context))
00463                                 {
00464                                         return  GE_TRUE;
00465                                 }
00466                         PixelFormat.PixelFormat = GE_PIXELFORMAT_16BIT_565_RGB;
00467                         PixelFormat.Flags               = RDRIVER_PF_2D| RDRIVER_PF_CAN_DO_COLORKEY;
00468                         if(!Cb(&PixelFormat, Context))
00469                                 {
00470                                         return  GE_TRUE;
00471                                 }
00472                 }
00473 
00474         PixelFormat.PixelFormat = GE_PIXELFORMAT_24BIT_RGB;
00475         PixelFormat.Flags               = RDRIVER_PF_LIGHTMAP;
00476 
00477         if(!Cb(&PixelFormat, Context))
00478         {
00479                 return  GE_TRUE;
00480         }
00481 
00482         return  GE_TRUE;
00483 }

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