Skip to content

Commit 8d46a46

Browse files
committed
unify(imagecollection): Merge ImageCollection code (#1771)
1 parent 632e1b9 commit 8d46a46

File tree

5 files changed

+24
-105
lines changed

5 files changed

+24
-105
lines changed

Generals/Code/GameEngine/Include/GameClient/Image.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "Common/AsciiString.h"
3434
#include "Common/GameMemory.h"
3535
#include "Common/SubsystemInterface.h"
36+
#include <map>
3637

3738
struct FieldParse;
3839
class INI;
@@ -106,8 +107,6 @@ friend class ImageCollection;
106107
void *m_rawTextureData; ///< raw texture data
107108
UnsignedInt m_status; ///< status bits from ImageStatus
108109

109-
Image *m_next; ///< for maintaining lists as collections
110-
111110
static const FieldParse m_imageFieldParseTable[]; ///< the parse table for INI definition
112111

113112
};
@@ -130,25 +129,28 @@ class ImageCollection : public SubsystemInterface
130129
void load( Int textureSize ); ///< load images
131130

132131
const Image *findImageByName( const AsciiString& name ); ///< find image based on name
133-
const Image *findImageByFilename( const AsciiString& name ); ///< find image based on filename
134132

135-
Image *firstImage( void ); ///< return first image in list
136-
Image *nextImage( Image *image ); ///< return next image
133+
/// adds the given image to the collection, transfers ownership to this object
134+
void addImage(Image *image);
137135

138-
Image *newImage( void ); ///< return a new, linked image
136+
/// enumerates the list of existing images
137+
Image *Enum(unsigned index)
138+
{
139+
for (std::map<unsigned,Image *>::iterator i=m_imageMap.begin();i!=m_imageMap.end();++i)
140+
if (!index--)
141+
return i->second;
142+
return NULL;
143+
}
139144

140145
protected:
141-
142-
Image *m_imageList; ///< the image list
143-
146+
std::map<unsigned,Image *> m_imageMap; ///< maps named keys to images
144147
};
145148

146149
// INLINING ///////////////////////////////////////////////////////////////////////////////////////
147150
inline void Image::setName( AsciiString name ) { m_name = name; }
148151
inline AsciiString Image::getName( void ) const { return m_name; }
149152
inline void Image::setFilename( AsciiString name ) { m_filename = name; }
150153
inline AsciiString Image::getFilename( void ) const { return m_filename; }
151-
inline Image *ImageCollection::firstImage( void ) { return m_imageList; }
152154
inline void Image::setUV( Region2D *uv ) { if( uv ) m_UVCoords = *uv; }
153155
inline const Region2D *Image::getUV( void ) const { return &m_UVCoords; }
154156
inline void Image::setTextureWidth( Int width ) { m_textureSize.x = width; }

Generals/Code/GameEngine/Source/Common/INI/INIMappedImage.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ void INI::parseMappedImageDefinition( INI* ini )
6666
{
6767

6868
// image not found, create a new one
69-
image = TheMappedImageCollection->newImage();
69+
image = newInstance(Image);
7070
image->setName( name );
71+
TheMappedImageCollection->addImage(image);
7172
DEBUG_ASSERTCRASH( image, ("parseMappedImage: unable to allocate image for '%s'",
7273
name.str()) );
7374

Generals/Code/GameEngine/Source/GameClient/MapUtil.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1104,7 +1104,7 @@ Image *getMapPreviewImage( AsciiString mapName )
11041104

11051105
if (success)
11061106
{
1107-
image = TheMappedImageCollection->newImage();
1107+
image = newInstance(Image);
11081108
image->setName(tempName);
11091109
//image->setFullPath("mission.tga");
11101110
image->setFilename(name);
@@ -1117,6 +1117,7 @@ Image *getMapPreviewImage( AsciiString mapName )
11171117
image->setUV(&uv);
11181118
image->setTextureHeight(128);
11191119
image->setTextureWidth(128);
1120+
TheMappedImageCollection->addImage(image);
11201121
}
11211122
else
11221123
{

Generals/Code/GameEngine/Source/GameClient/System/Image.cpp

Lines changed: 7 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
#include "Common/INI.h"
4040
#include "Common/GlobalData.h"
4141
#include "GameClient/Image.h"
42-
42+
#include "Common/NameKeyGenerator.h"
4343

4444
// PRIVATE DATA ///////////////////////////////////////////////////////////////////////////////////
4545
const FieldParse Image::m_imageFieldParseTable[] =
@@ -154,7 +154,6 @@ Image::Image( void )
154154
m_imageSize.y = 0;
155155
m_rawTextureData = NULL;
156156
m_status = IMAGE_STATUS_NONE;
157-
m_next = NULL;
158157

159158
}
160159

@@ -199,113 +198,31 @@ UnsignedInt Image::clearStatus( UnsignedInt bit )
199198
//-------------------------------------------------------------------------------------------------
200199
ImageCollection::ImageCollection( void )
201200
{
202-
203-
m_imageList = NULL;
204-
205201
}
206202

207203
//-------------------------------------------------------------------------------------------------
208204
//-------------------------------------------------------------------------------------------------
209205
ImageCollection::~ImageCollection( void )
210206
{
211-
Image *image, *next;
212-
213-
// delete the images
214-
image = m_imageList;
215-
while( image )
216-
{
217-
218-
next = image->m_next;
219-
deleteInstance(image);
220-
image = next;
221-
222-
}
223-
m_imageList = NULL;
224-
207+
for (std::map<unsigned,Image *>::iterator i=m_imageMap.begin();i!=m_imageMap.end();++i)
208+
deleteInstance(i->second);
225209
}
226210

227211
//-------------------------------------------------------------------------------------------------
228212
/** Return the next image in the collection */
229213
//-------------------------------------------------------------------------------------------------
230-
Image *ImageCollection::nextImage( Image *image )
214+
void ImageCollection::addImage( Image *image )
231215
{
232-
233-
if( image )
234-
return image->m_next;
235-
236-
return NULL;
237-
238-
}
239-
240-
//-------------------------------------------------------------------------------------------------
241-
/** Allocate a new image, tie to the image list and return it */
242-
//-------------------------------------------------------------------------------------------------
243-
Image *ImageCollection::newImage( void )
244-
{
245-
Image *image = newInstance(Image);
246-
247-
// attach to collection list
248-
image->m_next = m_imageList;
249-
m_imageList = image;
250-
251-
return image;
252-
216+
m_imageMap[TheNameKeyGenerator->nameToLowercaseKey(image->getName())]=image;
253217
}
254218

255219
//-------------------------------------------------------------------------------------------------
256220
/** Find an image given the image name */
257221
//-------------------------------------------------------------------------------------------------
258222
const Image *ImageCollection::findImageByName( const AsciiString& name )
259223
{
260-
Image *image;
261-
262-
/** @todo this needs to be more intelligent if this image collection
263-
becomes a real system we use a lot */
264-
265-
// search the images
266-
image = m_imageList;
267-
while( image )
268-
{
269-
270-
//
271-
// want to do a case insensitive compare here cause image INI files are
272-
// autogenerated from filenames using the image packer tool
273-
//
274-
if( image->getName().compareNoCase( name.str() ) == 0 )
275-
return image;
276-
image = image->m_next;
277-
278-
}
279-
280-
// not found
281-
return NULL;
282-
283-
}
284-
285-
//-------------------------------------------------------------------------------------------------
286-
/** Find image given image filename */
287-
//-------------------------------------------------------------------------------------------------
288-
const Image *ImageCollection::findImageByFilename( const AsciiString& filename )
289-
{
290-
Image *image;
291-
292-
/** @todo this needs to be more intelligent if this image collection
293-
becomes a real system we use a lot */
294-
295-
// search the images
296-
image = m_imageList;
297-
while( image )
298-
{
299-
300-
if( image->getFilename() == filename )
301-
return image;
302-
image = image->m_next;
303-
304-
}
305-
306-
// not found
307-
return NULL;
308-
224+
std::map<unsigned,Image *>::iterator i=m_imageMap.find(TheNameKeyGenerator->nameToLowercaseKey(name));
225+
return i==m_imageMap.end()?NULL:i->second;
309226
}
310227

311228
//-------------------------------------------------------------------------------------------------

Generals/Code/Tools/GUIEdit/Source/Properties.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,9 +1176,7 @@ void LoadImageListComboBox( HWND comboBox )
11761176
SendMessage( comboBox, CB_RESETCONTENT, 0, 0 );
11771177

11781178
// load the combo box with string names from the GUI image collection
1179-
for( image = TheMappedImageCollection->firstImage();
1180-
image;
1181-
image = TheMappedImageCollection->nextImage( image ) )
1179+
for (unsigned index=0;(image=TheMappedImageCollection->Enum(index))!=NULL;index++)
11821180
{
11831181

11841182
SendMessage( comboBox, CB_ADDSTRING, 0, (LPARAM)image->getName().str() );

0 commit comments

Comments
 (0)