texfcach.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /* $Header: /Commando/Code/ww3d2/texfcach.h 3 3/26/01 10:45a Jani_p $ */
  19. /***********************************************************************************************
  20. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : WW3D *
  24. * *
  25. * $Archive:: /Commando/Code/ww3d2/texfcach.h $*
  26. * *
  27. * $Author:: Jani_p $*
  28. * *
  29. * $Modtime:: 3/23/01 11:15a $*
  30. * *
  31. * $Revision:: 3 $*
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  36. #if defined(_MSC_VER)
  37. #pragma once
  38. #endif
  39. #ifndef TEXTFCACH_H
  40. #define TEXTFCACH_H
  41. #include "always.h"
  42. #include <assert.h>
  43. #include <tagblock.h>
  44. #ifdef WW3D_DX8
  45. //#include <srTextureIFace.hpp>
  46. class FileClass;
  47. //class srColorSurfaceIFace;
  48. class TextureFileCache
  49. {
  50. public:
  51. TextureFileCache(const char *fileprefix);
  52. virtual ~TextureFileCache();
  53. virtual void Reset_File();
  54. // Find texture in the cache. Returns TRUE if texture found inside of cache.
  55. int Texture_Exists(const char *fname);
  56. // Create the initial surface that is based off of the original texture.
  57. // The surface is not filled in with texels since we don't convert this.
  58. // This will also set TextureHandle up.
  59. srColorSurfaceIFace *Load_Original_Texture_Surface(const char *texturename);
  60. // Given a texture that has been loaded, save it in our file cache.
  61. bool Save_Texture(const char *texturename, srTextureIFace::MultiRequest& mreq, srColorSurfaceIFace& origsurface);
  62. // Load texture data from cache into a multirequest structure.
  63. bool Load_Texture(const char *texturename, srTextureIFace::MultiRequest& mreq);
  64. //bool TextureFileCache::Load_Texture(const char *texturename, srTextureIFace::MultiRequest& mreq);
  65. // Check that file is in cache, is not - load and save all mipmap levels
  66. bool Validate_Texture(const char* texturename);
  67. // Load a surface from the file cache, null if does not exist.
  68. srColorSurfaceIFace *Get_Surface(const char *texturename, unsigned int reduce_factor);
  69. protected:
  70. struct FileHeader {
  71. enum {
  72. // Put in date when format is changed.
  73. TCF_VERSION = 20000814,
  74. };
  75. // Will change whenever a new file version is made.
  76. int Version;
  77. };
  78. // The file format for each texture is as follows:
  79. // _TextureBlockHeader {...}
  80. // int MipMap[0].Offset
  81. // ...
  82. // int MipMap[_TextureBlockHeader.NumMipMaps - 1].Offset
  83. // int Offset of end of block to get size of last MipMap.
  84. // rawdata MipMap[0]
  85. // ...
  86. // rawdata MipMap[_TextureBlockHeader.NumMipMaps - 1]
  87. // End of BLock
  88. struct TextureBlockHeader
  89. {
  90. // Time data stamp of file.
  91. unsigned long FileTime;
  92. // Number of mip maps in texture (including first one).
  93. int NumMipMaps;
  94. // Dimensions of first mip map level saved. This is normally the same as
  95. // Source* but can be different when certain texture creation methods are done.
  96. int LargestWidth;
  97. int LargestHeight;
  98. // Dimensions of original surface/art work.
  99. int SourceWidth;
  100. int SourceHeight;
  101. // This is the pixel format used to create the sources original surface.
  102. srColorSurfaceIFace::PixelFormat SourcePixelFormat;
  103. // Pixel format that we have saved in.
  104. srColorSurfaceIFace::PixelFormat PixelFormat;
  105. TextureBlockHeader():NumMipMaps(-1),SourceWidth(-1),SourceHeight(-1),LargestWidth(-1),LargestHeight(-1) {}
  106. };
  107. // Each texture has an offset into the file and it's size when uncompressed.
  108. struct OffsetTableType
  109. {
  110. OffsetTableType() : Offset(0), Size (0) {}
  111. // Offset of texture in file.
  112. int Offset;
  113. // Size (uncompressed) of texture. The size of the compressed texture
  114. // is caclcuated by Texture_Size() below.
  115. int Size;
  116. };
  117. protected:
  118. enum {
  119. MAX_CACHED_SURFACES = srTextureIFace::MAX_LOD,
  120. };
  121. // Pointer to the low level file managment. TagBlockFile handles the seperation
  122. // of the different 'texture files'. This class deals with each seperate 'texture file'.
  123. TagBlockFile File;
  124. // Name of last texture loaded so we know if things need to be thrown away or not.
  125. char *CurrentTexture;
  126. // Handle for current texture in cache. This is created by Open_Texture_Handle().
  127. TagBlockHandle *TextureHandle;
  128. // Header that has been loaded by Open_Texture_Handle().
  129. TextureBlockHeader Header;
  130. // The offset table is loaded into memory when the file is opened.
  131. OffsetTableType *Offsets;
  132. // Cache pointers to data that has already been loaded for this texture.
  133. srColorSurface * CachedSurfaces[MAX_CACHED_SURFACES];
  134. // Number of cached textures we have.
  135. int NumCachedTextures;
  136. protected:
  137. // Buffer to compress into and decompress out of.
  138. // static char *CompressionBuffer;
  139. // static char *EOCompressionBuffer;
  140. // This keeps track of number of instances of the TextureFileCache.
  141. // This way static variables are only freed when all instances have been deleted.
  142. // static int Instances;
  143. // Access to previously cached textures so they do not have to be read off of disk.
  144. void Add_Cached_Surface(srColorSurface *surface);
  145. srColorSurface *Find_Cached_Surface(int size);
  146. srColorSurface *Find_Smallest_Cached_Surface();
  147. int Texture_Size(int lod) {
  148. assert(Offsets);
  149. assert(lod < Header.NumMipMaps);
  150. return(Offsets[lod].Size);
  151. }
  152. int Compressed_Texture_Size(int lod) {
  153. assert(Offsets);
  154. assert(lod < Header.NumMipMaps);
  155. return(Offsets[lod + 1].Offset - Offsets[lod].Offset);
  156. }
  157. bool Open_Texture_Handle(const char *texturename);
  158. void Close_Texture_Handle();
  159. void Read_Texture(int offsetidx, srColorSurface *surface);
  160. srColorSurfaceIFace *Create_First_Texture_As_Surface(srColorSurfaceIFace *surftype);
  161. static char *_Create_File_Name(const char *fileprefix);
  162. static char *_FileNamePtr;
  163. };
  164. #endif //WW3D_DX8
  165. #endif //TEXTFCACH_H