forked from TheSuperHackers/GeneralsGameCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.cpp
More file actions
273 lines (226 loc) · 10.5 KB
/
Image.cpp
File metadata and controls
273 lines (226 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
** Command & Conquer Generals Zero Hour(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
////////////////////////////////////////////////////////////////////////////////
// //
// (c) 2001-2003 Electronic Arts Inc. //
// //
////////////////////////////////////////////////////////////////////////////////
// FILE: Image.cpp ////////////////////////////////////////////////////////////////////////////////
// Created: Colin Day, June 2001
// Desc: High level representation of images, this is currently being
// written so we have a way to refer to images in the windows
// GUI, this system should be replaced with something that can
// handle real image management or written to accommodate
// all parts of the engine that need images.
///////////////////////////////////////////////////////////////////////////////////////////////////
#include "PreRTS.h" // This must go first in EVERY cpp file in the GameEngine
#define DEFINE_IMAGE_STATUS_NAMES
#include "Lib/BaseType.h"
#include "Common/Debug.h"
#include "Common/INI.h"
#include "Common/GlobalData.h"
#include "GameClient/Image.h"
#include "Common/NameKeyGenerator.h"
// PRIVATE DATA ///////////////////////////////////////////////////////////////////////////////////
const FieldParse Image::m_imageFieldParseTable[] =
{
{ "Texture", INI::parseAsciiString, nullptr, offsetof( Image, m_filename ) },
{ "TextureWidth", INI::parseInt, nullptr, offsetof( Image, m_textureSize.x ) },
{ "TextureHeight", INI::parseInt, nullptr, offsetof( Image, m_textureSize.y ) },
{ "Coords", Image::parseImageCoords, nullptr, offsetof( Image, m_UVCoords ) },
{ "Status", Image::parseImageStatus, nullptr, offsetof( Image, m_status ) },
{ nullptr, nullptr, nullptr, 0 }
};
// PRIVATE FUNCTIONS //////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------------------------
/** Parse an image coordinates in the form of
*
* COORDS = Left:AAA Top:BBB Right:CCC Bottom:DDD */
//-------------------------------------------------------------------------------------------------
void Image::parseImageCoords( INI* ini, void *instance, void *store, const void* /*userData*/ )
{
Int left = INI::scanInt(ini->getNextSubToken("Left"));
Int top = INI::scanInt(ini->getNextSubToken("Top"));
Int right = INI::scanInt(ini->getNextSubToken("Right"));
Int bottom = INI::scanInt(ini->getNextSubToken("Bottom"));
// get the image we're storing in
Image *theImage = (Image *)instance;
//
// store the UV coords based on what we've read in and the texture size
// defined for this image
//
Region2D uvCoords;
uvCoords.lo.x = (Real)left;
uvCoords.lo.y = (Real)top;
uvCoords.hi.x = (Real)right;
uvCoords.hi.y = (Real)bottom;
// adjust the coords by texture size
const ICoord2D *textureSize = theImage->getTextureSize();
if( textureSize->x )
{
uvCoords.lo.x /= (Real)textureSize->x;
uvCoords.hi.x /= (Real)textureSize->x;
}
if( textureSize->y )
{
uvCoords.lo.y /= (Real)textureSize->y;
uvCoords.hi.y /= (Real)textureSize->y;
}
// store the uv coords
theImage->setUV( &uvCoords );
// compute the image size based on the coords we read and store
ICoord2D imageSize;
imageSize.x = right - left;
imageSize.y = bottom - top;
theImage->setImageSize( &imageSize );
}
//-------------------------------------------------------------------------------------------------
/** Parse the image status line */
//-------------------------------------------------------------------------------------------------
void Image::parseImageStatus( INI* ini, void *instance, void *store, const void* /*userData*/)
{
// use existing INI parsing for the bit strings
INI::parseBitString32(ini, instance, store, imageStatusNames);
//
// if we are rotated 90 degrees clockwise we need to swap our width and height as
// they were computed from the page location rect, which was for the rotated image
// (see ImagePacker tool for more details)
//
UnsignedInt *theStatusBits = (UnsignedInt *)store;
if( BitIsSet( *theStatusBits, IMAGE_STATUS_ROTATED_90_CLOCKWISE ) )
{
Image *theImage = (Image *)instance;
ICoord2D imageSize;
imageSize.x = theImage->getImageHeight(); // note it's height not width
imageSize.y = theImage->getImageWidth(); // note it's width not height
theImage->setImageSize( &imageSize );
}
}
// PUBLIC DATA ////////////////////////////////////////////////////////////////////////////////////
ImageCollection *TheMappedImageCollection = nullptr; ///< mapped images
// PUBLIC FUNCTIONS////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
Image::Image()
{
m_name.clear();
m_filename.clear();
m_textureSize.x = 0;
m_textureSize.y = 0;
m_UVCoords.lo.x = 0.0f;
m_UVCoords.lo.y = 0.0f;
m_UVCoords.hi.x = 1.0f;
m_UVCoords.hi.y = 1.0f;
m_imageSize.x = 0;
m_imageSize.y = 0;
m_rawTextureData = nullptr;
m_status = IMAGE_STATUS_NONE;
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
Image::~Image()
{
}
//-------------------------------------------------------------------------------------------------
/** Set a status bit into the existing status, return the previous status
* bit collection from before the set */
//-------------------------------------------------------------------------------------------------
UnsignedInt Image::setStatus( UnsignedInt bit )
{
UnsignedInt prevStatus = m_status;
BitSet( m_status, bit );
return prevStatus;
}
//-------------------------------------------------------------------------------------------------
/** Clear a status bit from the existing status, return the previous
* status bit collection from before the clear */
//-------------------------------------------------------------------------------------------------
UnsignedInt Image::clearStatus( UnsignedInt bit )
{
UnsignedInt prevStatus = m_status;
BitClear( m_status, bit );
return prevStatus;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
ImageCollection::ImageCollection()
{
}
//-------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------
ImageCollection::~ImageCollection()
{
for (ImageMap::iterator i=m_imageMap.begin();i!=m_imageMap.end();++i)
deleteInstance(i->second);
}
//-------------------------------------------------------------------------------------------------
/** adds the given image to the collection, transfers ownership to this object */
//-------------------------------------------------------------------------------------------------
void ImageCollection::addImage( Image *image )
{
m_imageMap[TheNameKeyGenerator->nameToLowercaseKey(image->getName())]=image;
}
//-------------------------------------------------------------------------------------------------
const Image *ImageCollection::findImage( NameKeyType namekey ) const
{
ImageMap::const_iterator i = m_imageMap.find(namekey);
return i == m_imageMap.end() ? nullptr : i->second;
}
//-------------------------------------------------------------------------------------------------
/** Find an image given the image name */
//-------------------------------------------------------------------------------------------------
const Image *ImageCollection::findImageByName( const AsciiString& name ) const
{
return findImage(TheNameKeyGenerator->nameToLowercaseKey(name));
}
//-------------------------------------------------------------------------------------------------
/** Find an image given the image name */
//-------------------------------------------------------------------------------------------------
const Image *ImageCollection::findImageByName( const char* name ) const
{
return findImage(TheNameKeyGenerator->nameToLowercaseKey(name));
}
//-------------------------------------------------------------------------------------------------
/** Load this image collection with all the images specified in the INI files
* for the proper texture size directory */
//-------------------------------------------------------------------------------------------------
void ImageCollection::load( Int textureSize )
{
char buffer[ _MAX_PATH ];
INI ini;
// first load in the user created mapped image files if we have them.
WIN32_FIND_DATA findData;
AsciiString userDataPath;
if(TheGlobalData)
{
userDataPath.format("%sINI\\MappedImages\\*.ini",TheGlobalData->getPath_UserData().str());
if(FindFirstFile(userDataPath.str(), &findData) !=INVALID_HANDLE_VALUE)
{
userDataPath.format("%sINI\\MappedImages",TheGlobalData->getPath_UserData().str());
ini.loadDirectory(userDataPath, INI_LOAD_OVERWRITE, nullptr );
}
}
// construct path to the mapped images folder of the correct texture size
sprintf( buffer, "Data\\INI\\MappedImages\\TextureSize_%d", textureSize );
// load all the ine files in that directory
ini.loadDirectory( AsciiString( buffer ), INI_LOAD_OVERWRITE, nullptr );
ini.loadDirectory("Data\\INI\\MappedImages\\HandCreated", INI_LOAD_OVERWRITE, nullptr );
}