1 /****************************************************************************************/
\r
4 /* Author: John Pollard */
\r
5 /* Description: This code keeps a list of shared textures. */
\r
7 /* The contents of this file are subject to the Genesis3D Public License */
\r
8 /* Version 1.01 (the "License"); you may not use this file except in */
\r
9 /* compliance with the License. You may obtain a copy of the License at */
\r
10 /* http://www.genesis3d.com */
\r
12 /* Software distributed under the License is distributed on an "AS IS" */
\r
13 /* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See */
\r
14 /* the License for the specific language governing rights and limitations */
\r
15 /* under the License. */
\r
17 /* The Original Code is Genesis3D, released March 25, 1999. */
\r
18 /*Genesis3D Version 1.1 released November 15, 1999 */
\r
19 /* Copyright (C) 1999 WildTangent, Inc. All Rights Reserved */
\r
21 /****************************************************************************************/
\r
22 #include <Windows.h>
\r
29 #include "Texture.h"
\r
37 GFX_Texture Textures[MAX_MAP_TEXTURES];
\r
39 GFX_TexInfo TexInfo[MAX_MAP_TEXINFO];
\r
41 typedef struct NamedBitmap
\r
47 static int NumBitmaps;
\r
48 static NamedBitmap * Bitmaps;
\r
50 //========================================================================================
\r
52 //========================================================================================
\r
53 int32 FindTextureIndex(char *Name, uint32 Flags)
\r
57 for (i=0; i< NumTextures; i++)
\r
59 if (!stricmp(Name, Textures[i].Name) && Textures[i].Flags == Flags)
\r
63 if (NumTextures >= MAX_MAP_TEXTURES)
\r
65 GHook.Error("FindTextureIndex: Too many Textures in map.");
\r
69 strcpy(Textures[NumTextures].Name, Name);
\r
70 Textures[NumTextures].Flags = Flags;
\r
74 //Hook.Printf("Adding Texture: %s\n", Name);
\r
76 return(NumTextures-1);
\r
79 int32 NumSurfaceLights;
\r
81 //========================================================================================
\r
83 //========================================================================================
\r
84 int32 FindTexInfo(GFX_TexInfo *Tex2)
\r
89 if ((Tex2->Flags & TEXINFO_FLAT))
\r
90 Tex2->Flags |= TEXINFO_GOURAUD;
\r
92 if (Tex2->Flags & TEXINFO_SKY) // Force sky to fullbright
\r
93 Tex2->Flags |= TEXINFO_FULLBRIGHT;
\r
95 if ((Tex2->Flags & TEXINFO_SKY) || (Tex2->Flags & TEXINFO_FULLBRIGHT))
\r
96 Tex2->Flags |= TEXINFO_NO_LIGHTMAP;
\r
98 if ((Tex2->Flags & TEXINFO_GOURAUD))
\r
99 Tex2->Flags |= TEXINFO_NO_LIGHTMAP;
\r
101 for (i=0; i< NumTexInfo; i++)
\r
103 Tex1 = &TexInfo[i];
\r
105 if (Tex1->Vecs[0].X == Tex2->Vecs[0].X)
\r
106 if (Tex1->Vecs[0].Y == Tex2->Vecs[0].Y)
\r
107 if (Tex1->Vecs[0].Z == Tex2->Vecs[0].Z)
\r
108 if (Tex1->Vecs[1].X == Tex2->Vecs[1].X)
\r
109 if (Tex1->Vecs[1].Y == Tex2->Vecs[1].Y)
\r
110 if (Tex1->Vecs[1].Z == Tex2->Vecs[1].Z)
\r
111 if (Tex1->Shift[0] == Tex2->Shift[0])
\r
112 if (Tex1->Shift[1] == Tex2->Shift[1])
\r
113 if (Tex1->DrawScale[0] == Tex2->DrawScale[0])
\r
114 if (Tex1->DrawScale[1] == Tex2->DrawScale[1])
\r
115 if (Tex1->Flags == Tex2->Flags)
\r
116 if (Tex1->FaceLight == Tex2->FaceLight)
\r
117 if (Tex1->ReflectiveScale == Tex2->ReflectiveScale)
\r
118 if (Tex1->Alpha == Tex2->Alpha)
\r
119 if (Tex1->MipMapBias == Tex2->MipMapBias)
\r
120 if (Tex1->Texture == Tex2->Texture)
\r
124 if (NumTexInfo >= MAX_MAP_TEXINFO)
\r
126 GHook.Error("FindTexInfo: Too much texture information...\n");
\r
130 TexInfo[i] = *Tex2;
\r
136 geBoolean InitTextureLib(char *FileName)
\r
138 char Buff[_MAX_PATH];
\r
140 geVFile_Finder * Finder;
\r
145 strcpy(Buff, FileName);
\r
146 StripExtension(Buff);
\r
147 DefaultExtension(Buff, ".txl");
\r
149 VFS = geVFile_OpenNewSystem(NULL,
\r
150 GE_VFILE_TYPE_VIRTUAL,
\r
153 GE_VFILE_OPEN_DIRECTORY | GE_VFILE_OPEN_READONLY);
\r
157 Finder = geVFile_CreateFinder(VFS, "*.*");
\r
162 while (geVFile_FinderGetNextFile(Finder) == GE_TRUE)
\r
167 geVFile_DestroyFinder(Finder);
\r
169 Finder = geVFile_CreateFinder(VFS, "*.*");
\r
175 Bitmaps = (NamedBitmap *)geRam_Allocate(sizeof(*Bitmaps) * NumBitmaps);
\r
180 memset(Bitmaps, 0, sizeof(*Bitmaps) * NumBitmaps);
\r
183 while (geVFile_FinderGetNextFile(Finder) == GE_TRUE)
\r
185 geVFile_Properties Properties;
\r
188 geVFile_FinderGetProperties(Finder, &Properties);
\r
189 Bitmaps[i].Name = strdup(Properties.Name);
\r
190 if (!Bitmaps[i].Name)
\r
192 // GHook.Error("InitTextures: 5 Unable to load texture library '%s'.\n", Buff);
\r
195 File = geVFile_Open(VFS, Properties.Name, GE_VFILE_OPEN_READONLY);
\r
198 GHook.Error("InitTextures: Unable to load texture file '%s'.\n", Properties.Name);
\r
201 Bitmaps[i].Bitmap = geBitmap_CreateFromFile(File);
\r
202 geVFile_Close(File);
\r
203 if (!Bitmaps[i].Bitmap)
\r
205 GHook.Error("InitTextures: Unable to load texture '%s'.\n", Properties.Name);
\r
208 // GHook.Printf("InitTextures: Loaded texture '%s'.\n", Properties.Name);
\r
212 geVFile_Close(VFS);
\r
217 geVFile_Close(VFS);
\r
220 geVFile_DestroyFinder(Finder);
\r
222 ShutdownTextureLib();
\r
224 GHook.Error("InitTextures: Unable to load texture library '%s'.\n", Buff);
\r
229 //========================================================================================
\r
230 //========================================================================================
\r
231 void ShutdownTextureLib(void)
\r
237 for (i = 0; i < NumBitmaps; i++)
\r
239 if (Bitmaps[i].Name)
\r
240 free(Bitmaps[i].Name);
\r
241 if (Bitmaps[i].Bitmap)
\r
242 geBitmap_Destroy(&Bitmaps[i].Bitmap);
\r
244 geRam_Free(Bitmaps);
\r
251 static NamedBitmap * FindBitmapByName(const char *Name)
\r
257 for (i = 0; i < NumBitmaps; i++)
\r
259 if (!stricmp(Bitmaps[i].Name, Name))
\r
261 return &Bitmaps[i];
\r
269 // added transparent textures
\r
270 geBoolean HasTextureAlpha(char *Name)
\r
273 NamedBitmap * Bitmap;
\r
275 Bitmap = FindBitmapByName(Name);
\r
277 if (Bitmap == NULL)
\r
279 GHook.Printf("Could not find texture alpha'%s' in texture library.\n", Name);
\r
282 GHook.Error("Could not find any textures in texture library.\n");
\r
286 assert(NumBitmaps > 0);
\r
288 Name = Bitmaps[0].Name;
\r
289 Image = Bitmaps[0].Bitmap;
\r
293 Image = Bitmap->Bitmap;
\r
296 return geBitmap_HasAlpha(Image);
\r
298 // end transparent textures
\r
300 //========================================================================================
\r
301 //========================================================================================
\r
302 geBoolean GetTexture(char *Name, uint8 *Data, int32 *Size, int32 *Width, int32 *Height, geVFile *f)
\r
306 NamedBitmap * Bitmap;
\r
308 Bitmap = FindBitmapByName(Name);
\r
310 if (Bitmap == NULL)
\r
312 GHook.Printf("Could not find texture '%s' in texture library.\n", Name);
\r
315 GHook.Error("Could not find any textures in texture library.\n");
\r
319 assert(NumBitmaps > 0);
\r
321 Name = Bitmaps[0].Name;
\r
322 Image = Bitmaps[0].Bitmap;
\r
326 Image = Bitmap->Bitmap;
\r
329 //GHook.Printf("%s\n", Name);
\r
331 *Width = geBitmap_Width(Image);
\r
332 *Height = geBitmap_Height(Image);
\r
334 if (*Width > 256 || *Height > 256)
\r
336 GHook.Error("Texture '%s' has a dimension bigger than 256.\n", Name);
\r
341 geBitmap_UpdateMips(Image, 0, 1);
\r
342 geBitmap_UpdateMips(Image, 1, 2);
\r
343 geBitmap_UpdateMips(Image, 2, 3);
\r
344 // added transparent textures - only 1 mip sent
\r
345 for (i = 0; i < 1; i++)
\r
349 geBitmap * LockedImage;
\r
353 // added transparent textures
\r
354 if(geBitmap_LockForRead(Image, &LockedImage, i, i, GE_PIXELFORMAT_32BIT_ARGB, GE_TRUE, 0xffff00ff) == GE_FALSE)
\r
357 GHook.Error("Could not get mip level %d for texture '%s'.\n", i, Name);
\r
361 Bits = geBitmap_GetBits(LockedImage);
\r
364 GHook.Error("Could not get mip bits for texture '%s'.\n", i, Name);
\r
368 MipWidth = *Width / (1 << i);
\r
369 MipHeight = *Height / (1 << i);
\r
371 //Start Dec2001DCS - Added * 4 since textures are now 32 bit
\r
372 // added transparent textures
\r
373 if (geVFile_Write(f, Bits, MipWidth * MipHeight * 4) != GE_TRUE)
\r
376 GHook.Error("Could not write texture data.\n");
\r
380 geBitmap_UnLock(LockedImage);
\r
382 //Start Dec2001DCS - Added * 4 since textures are now 32 bit
\r
383 // added transparent textures
\r
384 *Size += MipWidth * MipHeight * 4;
\r
391 geBoolean GetTexturePalette(const char *Name, DRV_Palette Palette)
\r
394 NamedBitmap * Bitmap;
\r
395 geBitmap_Palette * ImagePalette;
\r
398 assert(Name != NULL);
\r
399 assert(Palette != NULL);
\r
401 Bitmap = FindBitmapByName(Name);
\r
403 if (Bitmap == NULL)
\r
405 GHook.Printf("Could not find texture '%s' in texture library.\n", Name);
\r
408 GHook.Error("Could not find any textures in texture library.\n");
\r
412 Name = Bitmaps[0].Name;
\r
413 Image = Bitmaps[0].Bitmap;
\r
417 Image = Bitmap->Bitmap;
\r
420 ImagePalette = geBitmap_GetPalette(Image);
\r
424 // GHook.Error("Could not find get pallette for texture '%s'.\n", Name);
\r
425 // return GE_FALSE;
\r
426 // No palette found is not an error anymore - 24 bit color bitmaps do not have a palette
\r
431 for (i = 0; i < 255; i++)
\r
437 geBitmap_Palette_GetEntryColor(ImagePalette, i, &R, &G, &B, &A);
\r