Image.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. ** Command & Conquer Generals(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. // PRIVATE DATA ///////////////////////////////////////////////////////////////////////////////////
  39. const FieldParse Image::m_imageFieldParseTable[] =
  40. {
  41. { "Texture", INI::parseAsciiString, NULL, offsetof( Image, m_filename ) },
  42. { "TextureWidth", INI::parseInt, NULL, offsetof( Image, m_textureSize.x ) },
  43. { "TextureHeight", INI::parseInt, NULL, offsetof( Image, m_textureSize.y ) },
  44. { "Coords", Image::parseImageCoords, NULL, offsetof( Image, m_UVCoords ) },
  45. { "Status", Image::parseImageStatus, NULL, offsetof( Image, m_status ) },
  46. { NULL, NULL, NULL, 0 }
  47. };
  48. // PRIVATE FUNCTIONS //////////////////////////////////////////////////////////////////////////////
  49. //-------------------------------------------------------------------------------------------------
  50. /** Parse an image coordinates in the form of
  51. *
  52. * COORDS = Left:AAA Top:BBB Right:CCC Bottom:DDD */
  53. //-------------------------------------------------------------------------------------------------
  54. void Image::parseImageCoords( INI* ini, void *instance, void *store, const void* /*userData*/ )
  55. {
  56. Int left = INI::scanInt(ini->getNextSubToken("Left"));
  57. Int top = INI::scanInt(ini->getNextSubToken("Top"));
  58. Int right = INI::scanInt(ini->getNextSubToken("Right"));
  59. Int bottom = INI::scanInt(ini->getNextSubToken("Bottom"));
  60. // get the image we're storing in
  61. Image *theImage = (Image *)instance;
  62. //
  63. // store the UV coords based on what we've read in and the texture size
  64. // defined for this image
  65. //
  66. Region2D uvCoords;
  67. uvCoords.lo.x = (Real)left;
  68. uvCoords.lo.y = (Real)top;
  69. uvCoords.hi.x = (Real)right;
  70. uvCoords.hi.y = (Real)bottom;
  71. // adjust the coords by texture size
  72. const ICoord2D *textureSize = theImage->getTextureSize();
  73. if( textureSize->x )
  74. {
  75. uvCoords.lo.x /= (Real)textureSize->x;
  76. uvCoords.hi.x /= (Real)textureSize->x;
  77. } // end if
  78. if( textureSize->y )
  79. {
  80. uvCoords.lo.y /= (Real)textureSize->y;
  81. uvCoords.hi.y /= (Real)textureSize->y;
  82. } // end if
  83. // store the uv coords
  84. theImage->setUV( &uvCoords );
  85. // compute the image size based on the coords we read and store
  86. ICoord2D imageSize;
  87. imageSize.x = right - left;
  88. imageSize.y = bottom - top;
  89. theImage->setImageSize( &imageSize );
  90. } // end parseImageCoord
  91. //-------------------------------------------------------------------------------------------------
  92. /** Parse the image status line */
  93. //-------------------------------------------------------------------------------------------------
  94. void Image::parseImageStatus( INI* ini, void *instance, void *store, const void* /*userData*/)
  95. {
  96. // use existing INI parsing for the bit strings
  97. INI::parseBitString32(ini, instance, store, imageStatusNames);
  98. //
  99. // if we are rotated 90 degrees clockwise we need to swap our width and height as
  100. // they were computed from the page location rect, which was for the rotated image
  101. // (see ImagePacker tool for more details)
  102. //
  103. UnsignedInt *theStatusBits = (UnsignedInt *)store;
  104. if( BitTest( *theStatusBits, IMAGE_STATUS_ROTATED_90_CLOCKWISE ) )
  105. {
  106. Image *theImage = (Image *)instance;
  107. ICoord2D imageSize;
  108. imageSize.x = theImage->getImageHeight(); // note it's height not width
  109. imageSize.y = theImage->getImageWidth(); // note it's width not height
  110. theImage->setImageSize( &imageSize );
  111. } // end if
  112. } // end parseImageStatus
  113. // PUBLIC DATA ////////////////////////////////////////////////////////////////////////////////////
  114. ImageCollection *TheMappedImageCollection = NULL; ///< mapped images
  115. // PUBLIC FUNCTIONS////////////////////////////////////////////////////////////////////////////////
  116. //-------------------------------------------------------------------------------------------------
  117. //-------------------------------------------------------------------------------------------------
  118. Image::Image( void )
  119. {
  120. m_name.clear();
  121. m_filename.clear();
  122. m_textureSize.x = 0;
  123. m_textureSize.y = 0;
  124. m_UVCoords.lo.x = 0.0f;
  125. m_UVCoords.lo.y = 0.0f;
  126. m_UVCoords.hi.x = 1.0f;
  127. m_UVCoords.hi.y = 1.0f;
  128. m_imageSize.x = 0;
  129. m_imageSize.y = 0;
  130. m_rawTextureData = NULL;
  131. m_status = IMAGE_STATUS_NONE;
  132. m_next = NULL;
  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. m_imageList = NULL;
  167. } // end ImageCollection
  168. //-------------------------------------------------------------------------------------------------
  169. //-------------------------------------------------------------------------------------------------
  170. ImageCollection::~ImageCollection( void )
  171. {
  172. Image *image, *next;
  173. // delete the images
  174. image = m_imageList;
  175. while( image )
  176. {
  177. next = image->m_next;
  178. image->deleteInstance();
  179. image = next;
  180. } // end while
  181. m_imageList = NULL;
  182. } // end ~ImageCollection
  183. //-------------------------------------------------------------------------------------------------
  184. /** Return the next image in the collection */
  185. //-------------------------------------------------------------------------------------------------
  186. Image *ImageCollection::nextImage( Image *image )
  187. {
  188. if( image )
  189. return image->m_next;
  190. return NULL;
  191. } // end nextImage
  192. //-------------------------------------------------------------------------------------------------
  193. /** Allocate a new image, tie to the image list and return it */
  194. //-------------------------------------------------------------------------------------------------
  195. Image *ImageCollection::newImage( void )
  196. {
  197. Image *image = newInstance(Image);
  198. // attach to collection list
  199. image->m_next = m_imageList;
  200. m_imageList = image;
  201. return image;
  202. } // end newImage
  203. //-------------------------------------------------------------------------------------------------
  204. /** Find an image given the image name */
  205. //-------------------------------------------------------------------------------------------------
  206. const Image *ImageCollection::findImageByName( const AsciiString& name )
  207. {
  208. Image *image;
  209. /** @todo this needs to be more intelligent if this image collection
  210. becomes a real system we use a lot */
  211. // search the images
  212. image = m_imageList;
  213. while( image )
  214. {
  215. //
  216. // want to do a case insensitive compare here cause image INI files are
  217. // autogenerated from filenames using the image packer tool
  218. //
  219. if( image->getName().compareNoCase( name.str() ) == 0 )
  220. return image;
  221. image = image->m_next;
  222. } // end while
  223. // not found
  224. return NULL;
  225. } // end findImageByName
  226. //-------------------------------------------------------------------------------------------------
  227. /** Find image given image filename */
  228. //-------------------------------------------------------------------------------------------------
  229. const Image *ImageCollection::findImageByFilename( const AsciiString& filename )
  230. {
  231. Image *image;
  232. /** @todo this needs to be more intelligent if this image collection
  233. becomes a real system we use a lot */
  234. // search the images
  235. image = m_imageList;
  236. while( image )
  237. {
  238. if( image->getFilename() == filename )
  239. return image;
  240. image = image->m_next;
  241. } // end while
  242. // not found
  243. return NULL;
  244. } // end findImageByFilename
  245. //-------------------------------------------------------------------------------------------------
  246. /** Load this image collection with all the images specified in the INI files
  247. * for the proper texture size directory */
  248. //-------------------------------------------------------------------------------------------------
  249. void ImageCollection::load( Int textureSize )
  250. {
  251. char buffer[ _MAX_PATH ];
  252. INI ini;
  253. // first load in the user created mapped image files if we have them.
  254. WIN32_FIND_DATA findData;
  255. AsciiString userDataPath;
  256. if(TheGlobalData)
  257. {
  258. userDataPath.format("%sINI\\MappedImages\\*.ini",TheGlobalData->getPath_UserData().str());
  259. if(FindFirstFile(userDataPath.str(), &findData) !=INVALID_HANDLE_VALUE)
  260. {
  261. userDataPath.format("%sINI\\MappedImages",TheGlobalData->getPath_UserData().str());
  262. ini.loadDirectory(userDataPath, TRUE, INI_LOAD_OVERWRITE, NULL );
  263. }
  264. }
  265. // construct path to the mapped images folder of the correct texture size
  266. sprintf( buffer, "Data\\INI\\MappedImages\\TextureSize_%d", textureSize );
  267. // load all the ine files in that directory
  268. ini.loadDirectory( AsciiString( buffer ), TRUE, INI_LOAD_OVERWRITE, NULL );
  269. ini.loadDirectory("Data\\INI\\MappedImages\\HandCreated", TRUE, INI_LOAD_OVERWRITE, NULL );
  270. } // end load