Image.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. ////////////////////////////////////////////////////////////////////////////////
  19. // //
  20. // (c) 2001-2003 Electronic Arts Inc. //
  21. // //
  22. ////////////////////////////////////////////////////////////////////////////////
  23. // FILE: Image.cpp ////////////////////////////////////////////////////////////////////////////////
  24. // Created: Colin Day, June 2001
  25. // Desc: High level representation of images, this is currently being
  26. // written so we have a way to refer to images in the windows
  27. // GUI, this system should be replaced with something that can
  28. // handle real image management or written to accomodate
  29. // all parts of the engine that need images.
  30. ///////////////////////////////////////////////////////////////////////////////////////////////////
  31. #include "PreRTS.h" // This must go first in EVERY cpp file int the GameEngine
  32. #define DEFINE_IMAGE_STATUS_NAMES
  33. #include "Lib/BaseType.h"
  34. #include "Common/Debug.h"
  35. #include "Common/INI.h"
  36. #include "Common/GlobalData.h"
  37. #include "GameClient/Image.h"
  38. #include "Common/NameKeyGenerator.h"
  39. // PRIVATE DATA ///////////////////////////////////////////////////////////////////////////////////
  40. const FieldParse Image::m_imageFieldParseTable[] =
  41. {
  42. { "Texture", INI::parseAsciiString, NULL, offsetof( Image, m_filename ) },
  43. { "TextureWidth", INI::parseInt, NULL, offsetof( Image, m_textureSize.x ) },
  44. { "TextureHeight", INI::parseInt, NULL, offsetof( Image, m_textureSize.y ) },
  45. { "Coords", Image::parseImageCoords, NULL, offsetof( Image, m_UVCoords ) },
  46. { "Status", Image::parseImageStatus, NULL, offsetof( Image, m_status ) },
  47. { NULL, NULL, NULL, 0 }
  48. };
  49. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////////////////////////
  50. //-------------------------------------------------------------------------------------------------
  51. /** Parse an image coordinates in the form of
  52. *
  53. * COORDS = Left:AAA Top:BBB Right:CCC Bottom:DDD */
  54. //-------------------------------------------------------------------------------------------------
  55. void Image::parseImageCoords( INI* ini, void *instance, void *store, const void* /*userData*/ )
  56. {
  57. Int left = INI::scanInt(ini->getNextSubToken("Left"));
  58. Int top = INI::scanInt(ini->getNextSubToken("Top"));
  59. Int right = INI::scanInt(ini->getNextSubToken("Right"));
  60. Int bottom = INI::scanInt(ini->getNextSubToken("Bottom"));
  61. // get the image we're storing in
  62. Image *theImage = (Image *)instance;
  63. //
  64. // store the UV coords based on what we've read in and the texture size
  65. // defined for this image
  66. //
  67. Region2D uvCoords;
  68. uvCoords.lo.x = (Real)left;
  69. uvCoords.lo.y = (Real)top;
  70. uvCoords.hi.x = (Real)right;
  71. uvCoords.hi.y = (Real)bottom;
  72. // adjust the coords by texture size
  73. const ICoord2D *textureSize = theImage->getTextureSize();
  74. if( textureSize->x )
  75. {
  76. uvCoords.lo.x /= (Real)textureSize->x;
  77. uvCoords.hi.x /= (Real)textureSize->x;
  78. } // end if
  79. if( textureSize->y )
  80. {
  81. uvCoords.lo.y /= (Real)textureSize->y;
  82. uvCoords.hi.y /= (Real)textureSize->y;
  83. } // end if
  84. // store the uv coords
  85. theImage->setUV( &uvCoords );
  86. // compute the image size based on the coords we read and store
  87. ICoord2D imageSize;
  88. imageSize.x = right - left;
  89. imageSize.y = bottom - top;
  90. theImage->setImageSize( &imageSize );
  91. } // end parseImageCoord
  92. //-------------------------------------------------------------------------------------------------
  93. /** Parse the image status line */
  94. //-------------------------------------------------------------------------------------------------
  95. void Image::parseImageStatus( INI* ini, void *instance, void *store, const void* /*userData*/)
  96. {
  97. // use existing INI parsing for the bit strings
  98. INI::parseBitString32(ini, instance, store, imageStatusNames);
  99. //
  100. // if we are rotated 90 degrees clockwise we need to swap our width and height as
  101. // they were computed from the page location rect, which was for the rotated image
  102. // (see ImagePacker tool for more details)
  103. //
  104. UnsignedInt *theStatusBits = (UnsignedInt *)store;
  105. if( BitTest( *theStatusBits, IMAGE_STATUS_ROTATED_90_CLOCKWISE ) )
  106. {
  107. Image *theImage = (Image *)instance;
  108. ICoord2D imageSize;
  109. imageSize.x = theImage->getImageHeight(); // note it's height not width
  110. imageSize.y = theImage->getImageWidth(); // note it's width not height
  111. theImage->setImageSize( &imageSize );
  112. } // end if
  113. } // end parseImageStatus
  114. // PUBLIC DATA ////////////////////////////////////////////////////////////////////////////////////
  115. ImageCollection *TheMappedImageCollection = NULL; ///< mapped images
  116. // PUBLIC FUNCTIONS////////////////////////////////////////////////////////////////////////////////
  117. //-------------------------------------------------------------------------------------------------
  118. //-------------------------------------------------------------------------------------------------
  119. Image::Image( void )
  120. {
  121. m_name.clear();
  122. m_filename.clear();
  123. m_textureSize.x = 0;
  124. m_textureSize.y = 0;
  125. m_UVCoords.lo.x = 0.0f;
  126. m_UVCoords.lo.y = 0.0f;
  127. m_UVCoords.hi.x = 1.0f;
  128. m_UVCoords.hi.y = 1.0f;
  129. m_imageSize.x = 0;
  130. m_imageSize.y = 0;
  131. m_rawTextureData = NULL;
  132. m_status = IMAGE_STATUS_NONE;
  133. } // end Image
  134. //-------------------------------------------------------------------------------------------------
  135. //-------------------------------------------------------------------------------------------------
  136. Image::~Image( void )
  137. {
  138. } // end ~Image
  139. //-------------------------------------------------------------------------------------------------
  140. /** Set a status bit into the existing status, return the previous status
  141. * bit collection from before the set */
  142. //-------------------------------------------------------------------------------------------------
  143. UnsignedInt Image::setStatus( UnsignedInt bit )
  144. {
  145. UnsignedInt prevStatus = m_status;
  146. BitSet( m_status, bit );
  147. return prevStatus;
  148. } // end setStatus
  149. //-------------------------------------------------------------------------------------------------
  150. /** Clear a status bit from the existing status, return the previous
  151. * status bit collection from before the clear */
  152. //-------------------------------------------------------------------------------------------------
  153. UnsignedInt Image::clearStatus( UnsignedInt bit )
  154. {
  155. UnsignedInt prevStatus = m_status;
  156. BitClear( m_status, bit );
  157. return prevStatus;
  158. } // end clearStatus
  159. ///////////////////////////////////////////////////////////////////////////////////////////////////
  160. ///////////////////////////////////////////////////////////////////////////////////////////////////
  161. ///////////////////////////////////////////////////////////////////////////////////////////////////
  162. //-------------------------------------------------------------------------------------------------
  163. //-------------------------------------------------------------------------------------------------
  164. ImageCollection::ImageCollection( void )
  165. {
  166. } // end ImageCollection
  167. //-------------------------------------------------------------------------------------------------
  168. //-------------------------------------------------------------------------------------------------
  169. ImageCollection::~ImageCollection( void )
  170. {
  171. for (std::map<unsigned,Image *>::iterator i=m_imageMap.begin();i!=m_imageMap.end();++i)
  172. i->second->deleteInstance();
  173. } // end ~ImageCollection
  174. //-------------------------------------------------------------------------------------------------
  175. /** adds the given image to the collection, transfers ownership to this object */
  176. //-------------------------------------------------------------------------------------------------
  177. void ImageCollection::addImage( Image *image )
  178. {
  179. m_imageMap[TheNameKeyGenerator->nameToLowercaseKey(image->getName())]=image;
  180. } // end newImage
  181. //-------------------------------------------------------------------------------------------------
  182. /** Find an image given the image name */
  183. //-------------------------------------------------------------------------------------------------
  184. const Image *ImageCollection::findImageByName( const AsciiString& name )
  185. {
  186. std::map<unsigned,Image *>::iterator i=m_imageMap.find(TheNameKeyGenerator->nameToLowercaseKey(name));
  187. return i==m_imageMap.end()?NULL:i->second;
  188. } // end findImageByName
  189. //-------------------------------------------------------------------------------------------------
  190. /** Load this image collection with all the images specified in the INI files
  191. * for the proper texture size directory */
  192. //-------------------------------------------------------------------------------------------------
  193. void ImageCollection::load( Int textureSize )
  194. {
  195. char buffer[ _MAX_PATH ];
  196. INI ini;
  197. // first load in the user created mapped image files if we have them.
  198. WIN32_FIND_DATA findData;
  199. AsciiString userDataPath;
  200. if(TheGlobalData)
  201. {
  202. userDataPath.format("%sINI\\MappedImages\\*.ini",TheGlobalData->getPath_UserData().str());
  203. if(FindFirstFile(userDataPath.str(), &findData) !=INVALID_HANDLE_VALUE)
  204. {
  205. userDataPath.format("%sINI\\MappedImages",TheGlobalData->getPath_UserData().str());
  206. ini.loadDirectory(userDataPath, TRUE, INI_LOAD_OVERWRITE, NULL );
  207. }
  208. }
  209. // construct path to the mapped images folder of the correct texture size
  210. sprintf( buffer, "Data\\INI\\MappedImages\\TextureSize_%d", textureSize );
  211. // load all the ine files in that directory
  212. ini.loadDirectory( AsciiString( buffer ), TRUE, INI_LOAD_OVERWRITE, NULL );
  213. ini.loadDirectory("Data\\INI\\MappedImages\\HandCreated", TRUE, INI_LOAD_OVERWRITE, NULL );
  214. } // end load