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

BitmapList.c

Go to the documentation of this file.
00001 /****************************************************************************************/
00002 /*  BitmapList.c                                                                        */
00003 /*                                                                                      */
00004 /*  Author: Charles Bloom                                                               */
00005 /*  Description: Maintains a pool of bitmap pointers.                                   */
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 #ifdef _DEBUG
00025 #include <windows.h>
00026 #endif
00027 
00028 #include <stdio.h>
00029 #include <memory.h>
00030 
00031 
00032 #include "BitmapList.h"
00033 #include "DCommon.h"
00034 #include "Bitmap.h"
00035 #include "Bitmap._h"
00036 #include "list.h"
00037 #include "mempool.h"
00038 #include "errorlog.h"
00039 #include "ram.h"
00040 //#include "tsc.h"
00041 
00042 struct BitmapList
00043 {
00044 #ifdef _DEBUG
00045         BitmapList * MySelf;
00046 #endif
00047         Hash * Hash;
00048         int Members,Adds;
00049 };
00050 
00051 
00052 geBoolean BitmapList_IsValid(BitmapList *pList);
00053 
00054 //================================================================================
00055 //      BitmapList_Create
00056 //================================================================================
00057 BitmapList *BitmapList_Create(void)
00058 {
00059 BitmapList * pList;
00060         pList = geRam_Allocate(sizeof(*pList));
00061         if (! pList )
00062                 return NULL;
00063         memset(pList,0,sizeof(*pList));
00064         pList->Hash = Hash_Create();
00065         if ( ! pList->Hash )
00066         {
00067                 geRam_Free(pList);
00068                 return NULL;
00069         }
00070         #ifdef _DEBUG
00071         pList->MySelf = pList;
00072         #endif
00073 return pList;
00074 }
00075 
00076 //================================================================================
00077 //      BitmapList_Destroy
00078 //================================================================================
00079 geBoolean BitmapList_Destroy(BitmapList *pList)
00080 {
00081 geBoolean       Ret = GE_TRUE;
00082 
00083         if ( ! pList )
00084                 return GE_TRUE;
00085 
00086         if ( pList->Hash )
00087         {
00088         HashNode        *pNode;
00089                 pNode = NULL;
00090                 
00091                 while( (pNode = Hash_WalkNext(pList->Hash,pNode)) != NULL )
00092                 {
00093                 geBitmap *Bmp;
00094                 uint32 TimesAdded;
00095 
00096                         HashNode_GetData(pNode,(uint32 *)&Bmp,&TimesAdded);
00097 
00098                         if (!geBitmap_DetachDriver(Bmp, GE_TRUE))
00099                                 Ret = GE_FALSE;
00100 
00101                         assert( pList->Members >= 1 && pList->Adds >= (int)TimesAdded );
00102 
00103                         pList->Members --;
00104 
00105                         assert( TimesAdded >= 1 );
00106 
00107                         while(TimesAdded --)
00108                         {
00109                                 assert(Bmp);
00110                                 geBitmap_Destroy(&Bmp);
00111                                 pList->Adds --;
00112                         }
00113                 }
00114 
00115                 // Finally, destroy the entire hash table
00116                 Hash_Destroy(pList->Hash);
00117         }
00118 
00119         geRam_Free(pList);
00120 
00121         return Ret;
00122 }
00123 
00124 //================================================================================
00125 //      BitmapList_SetGamma
00126 //================================================================================
00127 geBoolean BitmapList_SetGamma(BitmapList *pList, geFloat Gamma)
00128 {
00129 HashNode *pNode;
00130 
00131         assert(BitmapList_IsValid(pList));
00132 
00133 #ifdef _DEBUG
00134         //pushTSC();
00135 #endif
00136 
00137         pNode = NULL;
00138         while( (pNode = Hash_WalkNext(pList->Hash,pNode)) != NULL )
00139         {
00140         geBitmap *Bmp;
00141                 Bmp = (geBitmap *)HashNode_Key(pNode);
00142 
00143                 if (!geBitmap_SetGammaCorrection(Bmp, Gamma, GE_TRUE) )
00144                 {
00145                         geErrorLog_AddString(-1,"BitmapList_SetGamma : SetGamma failed.", NULL);
00146                         return GE_FALSE;
00147                 }
00148         }
00149         
00150 #ifdef _DEBUG
00151         //showPopTSCper("BitmapList_SetGamma",pList->MembersAttached,"bitmap");
00152 #endif
00153 
00154 return GE_TRUE;
00155 }
00156 
00157 //================================================================================
00158 //      BitmapList_AttachAll
00159 //================================================================================
00160 geBoolean BitmapList_AttachAll(BitmapList *pList, DRV_Driver *Driver, geFloat Gamma)
00161 {
00162 HashNode *pNode;
00163 int MembersAttached;
00164 
00165         assert(BitmapList_IsValid(pList));
00166 
00167         //pushTSC();
00168 
00169         pNode = NULL;
00170         MembersAttached = 0;
00171         while( (pNode = Hash_WalkNext(pList->Hash,pNode)) != NULL )
00172         {
00173         geBitmap *Bmp;
00174 
00175                 Bmp = (geBitmap *)HashNode_Key(pNode);
00176 
00177                 if (!geBitmap_SetGammaCorrection_DontChange(Bmp, Gamma) )
00178                 {
00179                         geErrorLog_AddString(-1,"BitmapList_AttachAll : SetGamma failed", NULL);
00180                         return GE_FALSE;
00181                 }
00182 
00183                 if (!geBitmap_AttachToDriver(Bmp, Driver, 0) )
00184                 {
00185                         geErrorLog_AddString(-1,"BitmapList_AttachAll : AttachToDriver failed", NULL);
00186                         return GE_FALSE;
00187                 }
00188 
00189                 MembersAttached ++;
00190         }
00191 
00192         //showPopTSC("BitmapList_AttachAll");
00193 
00194         assert( MembersAttached == pList->Members );
00195 
00196         return GE_TRUE;
00197 }
00198 
00199 //================================================================================
00200 //      BitmapList_DetachAll
00201 //================================================================================
00202 geBoolean BitmapList_DetachAll(BitmapList *pList)
00203 {
00204 HashNode        *pNode;
00205 geBoolean       Ret = GE_TRUE;
00206 int MembersAttached;
00207 
00208         assert(BitmapList_IsValid(pList));
00209 
00210         pNode = NULL;
00211         while( (pNode = Hash_WalkNext(pList->Hash,pNode)) != NULL )
00212         {
00213         geBitmap *Bmp;
00214         uint32 TimesAdded;
00215 
00216                 HashNode_GetData(pNode,(uint32 *)&Bmp,&TimesAdded);
00217 
00218                 if (!geBitmap_DetachDriver(Bmp, GE_TRUE))
00219                         Ret = GE_FALSE;
00220         }
00221 
00222         MembersAttached = 0;
00223 
00224         return Ret;
00225 }
00226 
00227 //================================================================================
00228 //      BitmapList_CountMembers
00229 //================================================================================
00230 int BitmapList_CountMembers(BitmapList *pList)
00231 {
00232 #ifdef NDEBUG
00233         return pList->Members;
00234 #else
00235 HashNode *pNode;
00236 int Count;
00237 
00238         Count = 0;
00239         pNode = NULL;
00240         while( (pNode = Hash_WalkNext(pList->Hash,pNode)) != NULL )
00241         {
00242                 Count ++;
00243         }
00244 
00245         assert( Count == pList->Members );
00246         assert( pList->Adds >= pList->Members );
00247 
00248 return Count;
00249 #endif
00250 }
00251 int BitmapList_CountMembersAttached(BitmapList *pList)
00252 {
00253 HashNode *pNode;
00254 int Count;
00255 
00256         Count = 0;
00257         pNode = NULL;
00258         while( (pNode = Hash_WalkNext(pList->Hash,pNode)) != NULL )
00259         {
00260         geBitmap *Bmp;
00261         uint32 TimesAdded;
00262 
00263                 HashNode_GetData(pNode,(uint32 *)&Bmp,&TimesAdded);
00264 
00265                 if ( geBitmap_GetTHandle(Bmp) )
00266                         Count ++;
00267         }
00268 
00269         assert( pList->Adds >= pList->Members && pList->Members >= Count );
00270 
00271 return Count;
00272 }
00273 
00274 //================================================================================
00275 //      BitmapList_Has
00276 //================================================================================
00277 geBoolean BitmapList_Has(BitmapList *pList, geBitmap *Bitmap)
00278 {
00279 HashNode *pNode;
00280 uint32 TimesAdded;
00281 
00282         assert(pList && Bitmap);
00283 
00284         pNode = Hash_Get(pList->Hash,(uint32)Bitmap,&TimesAdded);
00285 
00286         assert( pList->Adds >= (int)TimesAdded );
00287 
00288 return (pNode && TimesAdded) ? GE_TRUE : GE_FALSE;
00289 }
00290 
00291 //================================================================================
00292 //      BitmapList_Add
00293 //================================================================================
00294 geBoolean BitmapList_Add(BitmapList *pList, geBitmap *Bitmap)
00295 {       
00296 HashNode *pNode;
00297 uint32 TimesAdded;
00298 
00299         assert(BitmapList_IsValid(pList));
00300         assert(Bitmap);
00301 
00302         // Increase reference count on this Bitmap
00303         geBitmap_CreateRef(Bitmap);
00304 
00305         pList->Adds ++;
00306 
00307         if ( (pNode = Hash_Get(pList->Hash, (uint32)Bitmap, &TimesAdded)) != NULL )
00308         {
00309                 HashNode_SetData(pNode,TimesAdded+1);
00310                 return GE_FALSE;
00311         }
00312         else
00313         {
00314                 pList->Members ++;
00315                 Hash_Add(pList->Hash,(uint32)Bitmap,1);
00316                 return GE_TRUE;
00317         }
00318 }
00319 
00320 //================================================================================
00321 //      BitmapList_Remove
00322 //================================================================================
00323 geBoolean BitmapList_Remove(BitmapList *pList,geBitmap *Bitmap)
00324 {
00325 HashNode *pNode;
00326 uint32 TimesAdded;
00327 uint32 Key;
00328 
00329         assert(BitmapList_IsValid(pList));
00330         assert(Bitmap);
00331 
00332         Key = (uint32) Bitmap;
00333         pNode = Hash_Get(pList->Hash,Key,&TimesAdded);
00334 
00335         assert(pNode);
00336 
00337         pList->Adds --;
00338         TimesAdded --;
00339 
00340         if ( TimesAdded <= 0 )
00341         {
00342                 if ( ! geBitmap_DetachDriver(Bitmap, GE_TRUE) )
00343                 {
00344                         geErrorLog_AddString(-1, "BitmapList_Remove:  geBitmap_DetachDriver failed.", NULL);
00345                         return GE_FALSE;
00346                 }
00347         }
00348 
00349         geBitmap_Destroy(&Bitmap);
00350 
00351         if ( TimesAdded <= 0 )
00352         {
00353                 pList->Members --;
00354                 Hash_DeleteNode(pList->Hash,pNode);
00355                 return GE_TRUE;
00356         }
00357         else
00358         {
00359                 HashNode_SetData(pNode,TimesAdded);
00360                 return GE_FALSE;
00361         }
00362 }
00363 
00364 
00365 geBoolean BitmapList_IsValid(BitmapList *pList)
00366 {
00367         if ( ! pList ) 
00368                 return GE_FALSE;
00369                 
00370         if ( pList->Adds < pList->Members )
00371                 return GE_FALSE;
00372 
00373 #ifdef _DEBUG
00374         if ( pList->MySelf != pList )
00375                 return GE_FALSE;
00376 #endif
00377 
00378         if ( pList->Members != BitmapList_CountMembers(pList) )
00379                 return GE_FALSE;
00380 
00381 return GE_TRUE;
00382 }

Generated on Tue Sep 30 12:35:19 2003 for GTestAndEngine by doxygen 1.3.2