Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 022510c

Browse files
committed
Fix bad sprite asset loading logic
1 parent f4d2776 commit 022510c

File tree

3 files changed

+45
-35
lines changed

3 files changed

+45
-35
lines changed

System/ContentFile.cpp

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace RTE {
77

88
const std::string ContentFile::c_ClassName = "ContentFile";
99

10-
std::unordered_map<std::string, BITMAP *> ContentFile::s_LoadedBitmaps[BitDepthCount];
10+
std::array<std::unordered_map<std::string, BITMAP *>, ContentFile::BitDepths::BitDepthCount> ContentFile::s_LoadedBitmaps;
1111
std::unordered_map<std::string, FMOD::Sound *> ContentFile::s_LoadedSamples;
1212
std::unordered_map<size_t, std::string> ContentFile::s_PathHashes;
1313

@@ -46,7 +46,7 @@ namespace RTE {
4646

4747
void ContentFile::FreeAllLoaded() {
4848
for (int depth = BitDepths::Eight; depth < BitDepths::BitDepthCount; ++depth) {
49-
for (const std::pair<std::string, BITMAP *> &bitmap : s_LoadedBitmaps[depth]) {
49+
for (const std::pair<std::string, BITMAP *> &bitmap : s_LoadedBitmaps.at(depth)) {
5050
destroy_bitmap(bitmap.second);
5151
}
5252
}
@@ -101,22 +101,36 @@ namespace RTE {
101101

102102
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
103103

104-
BITMAP * ContentFile::GetAsBitmap(int conversionMode) {
104+
BITMAP * ContentFile::GetAsBitmap(int conversionMode, const std::string &dataPathToSpecificFrame) {
105105
if (m_DataPath.empty()) {
106106
return nullptr;
107107
}
108108
BITMAP *returnBitmap = nullptr;
109-
int bitDepth = (conversionMode == COLORCONV_8_TO_32) ? BitDepths::ThirtyTwo : BitDepths::Eight;
109+
const int bitDepth = (conversionMode == COLORCONV_8_TO_32) ? BitDepths::ThirtyTwo : BitDepths::Eight;
110+
std::string dataPathToLoad = dataPathToSpecificFrame.empty() ? m_DataPath : dataPathToSpecificFrame;
111+
SetFormattedReaderPosition(GetFormattedReaderPosition());
110112

111113
// Check if the file has already been read and loaded from the disk and, if so, use that data.
112-
std::unordered_map<std::string, BITMAP *>::iterator foundBitmap = s_LoadedBitmaps[bitDepth].find(m_DataPath);
113-
if (foundBitmap != s_LoadedBitmaps[bitDepth].end()) {
114+
std::unordered_map<std::string, BITMAP *>::iterator foundBitmap = s_LoadedBitmaps.at(bitDepth).find(dataPathToLoad);
115+
if (foundBitmap != s_LoadedBitmaps.at(bitDepth).end()) {
114116
returnBitmap = (*foundBitmap).second;
115117
} else {
116-
returnBitmap = LoadAndReleaseBitmap(conversionMode); //NOTE: This takes ownership of the bitmap file
118+
if (!std::filesystem::exists(dataPathToLoad)) {
119+
const std::string dataPathWithoutExtension = dataPathToLoad.substr(0, dataPathToLoad.length() - m_DataPathExtension.length());
120+
const std::string altFileExtension = (m_DataPathExtension == ".png") ? ".bmp" : ".png";
121+
122+
if (std::filesystem::exists(dataPathWithoutExtension + altFileExtension)) {
123+
g_ConsoleMan.AddLoadWarningLogEntry(m_DataPath, m_FormattedReaderPosition, altFileExtension);
124+
SetDataPath(m_DataPathWithoutExtension + altFileExtension);
125+
dataPathToLoad = dataPathWithoutExtension + altFileExtension;
126+
} else {
127+
RTEAbort("Failed to find image file with following path and name:\n\n" + m_DataPath + " or " + altFileExtension + "\n" + m_FormattedReaderPosition);
128+
}
129+
}
130+
returnBitmap = LoadAndReleaseBitmap(conversionMode, dataPathToLoad); // NOTE: This takes ownership of the bitmap file
117131

118132
// Insert the bitmap into the map, PASSING OVER OWNERSHIP OF THE LOADED DATAFILE
119-
s_LoadedBitmaps[bitDepth].insert(std::pair<std::string, BITMAP *>(m_DataPath, returnBitmap));
133+
s_LoadedBitmaps.at(bitDepth).insert(std::pair<std::string, BITMAP *>(dataPathToLoad, returnBitmap));
120134
}
121135
return returnBitmap;
122136
}
@@ -129,56 +143,50 @@ namespace RTE {
129143
}
130144
// Create the array of as many BITMAP pointers as requested frames
131145
BITMAP **returnBitmaps = new BITMAP *[frameCount];
146+
SetFormattedReaderPosition(GetFormattedReaderPosition());
132147

133148
// Don't try to append numbers if there's only one frame
134149
if (frameCount == 1) {
150+
// Check for 000 in the file name in case it is part of an animation but the FrameCount was set to 1. Do not warn about this because it's normal operation, but warn about incorrect extension.
151+
if (!std::filesystem::exists(m_DataPath)) {
152+
const std::string altFileExtension = (m_DataPathExtension == ".png") ? ".bmp" : ".png";
153+
154+
if (std::filesystem::exists(m_DataPathWithoutExtension + "000" + m_DataPathExtension)) {
155+
SetDataPath(m_DataPathWithoutExtension + "000" + m_DataPathExtension);
156+
} else if (std::filesystem::exists(m_DataPathWithoutExtension + "000" + altFileExtension)) {
157+
g_ConsoleMan.AddLoadWarningLogEntry(m_DataPath, m_FormattedReaderPosition, altFileExtension);
158+
SetDataPath(m_DataPathWithoutExtension + "000" + altFileExtension);
159+
}
160+
}
135161
returnBitmaps[0] = GetAsBitmap(conversionMode);
136162
return returnBitmaps;
137163
}
138164
char framePath[1024];
139-
// For each frame in the animation, temporarily assign it to the datapath member var so that GetAsBitmap and then load it with GetBitmap
140165
for (int frameNum = 0; frameNum < frameCount; frameNum++) {
141166
std::snprintf(framePath, sizeof(framePath), "%s%03i%s", m_DataPathWithoutExtension.c_str(), frameNum, m_DataPathExtension.c_str());
142-
m_DataPath = framePath;
143-
returnBitmaps[frameNum] = GetAsBitmap(conversionMode);
167+
returnBitmaps[frameNum] = GetAsBitmap(conversionMode, framePath);
144168
}
145-
m_DataPath = m_DataPathWithoutExtension + m_DataPathExtension;
146169
return returnBitmaps;
147170
}
148171

149172
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
150173

151-
BITMAP * ContentFile::LoadAndReleaseBitmap(int conversionMode) {
174+
BITMAP * ContentFile::LoadAndReleaseBitmap(int conversionMode, const std::string &dataPathToSpecificFrame) {
152175
if (m_DataPath.empty()) {
153176
return nullptr;
154177
}
178+
const std::string dataPathToLoad = dataPathToSpecificFrame.empty() ? m_DataPath : dataPathToSpecificFrame;
155179
SetFormattedReaderPosition(GetFormattedReaderPosition());
156-
const std::string altFileExtension = (m_DataPathExtension == ".png") ? ".bmp" : ".png";
157180

158-
if (!std::filesystem::exists(m_DataPath)) {
159-
// Check for 000 in the file name in case it is part of an animation but the FrameCount was set to 1. Do not warn about this because it's normal operation.
160-
if (std::filesystem::exists(m_DataPathWithoutExtension + "000" + m_DataPathExtension)) {
161-
SetDataPath(m_DataPathWithoutExtension + "000" + m_DataPathExtension);
162-
} else {
163-
if (std::filesystem::exists(m_DataPathWithoutExtension + altFileExtension)) {
164-
g_ConsoleMan.AddLoadWarningLogEntry(m_DataPath, m_FormattedReaderPosition, altFileExtension);
165-
SetDataPath(m_DataPathWithoutExtension + altFileExtension);
166-
} else if (std::filesystem::exists(m_DataPathWithoutExtension + "000" + altFileExtension)) {
167-
g_ConsoleMan.AddLoadWarningLogEntry(m_DataPath, m_FormattedReaderPosition, altFileExtension);
168-
SetDataPath(m_DataPathWithoutExtension + "000" + altFileExtension);
169-
} else {
170-
RTEAbort("Failed to find image file with following path and name:\n\n" + m_DataPath + " or " + altFileExtension + "\n" + m_FormattedReaderPosition);
171-
}
172-
}
173-
}
174181
BITMAP *returnBitmap = nullptr;
175182

176183
PALETTE currentPalette;
177184
get_palette(currentPalette);
178185

179186
set_color_conversion((conversionMode == 0) ? COLORCONV_MOST : conversionMode);
180-
returnBitmap = load_bitmap(m_DataPath.c_str(), currentPalette);
187+
returnBitmap = load_bitmap(dataPathToLoad.c_str(), currentPalette);
181188
RTEAssert(returnBitmap, "Failed to load image file with following path and name:\n\n" + m_DataPathAndReaderPosition + "\nThe file may be corrupt, incorrectly converted or saved with unsupported parameters.");
189+
182190
return returnBitmap;
183191
}
184192

System/ContentFile.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ namespace RTE {
119119
/// Gets the data represented by this ContentFile object as an Allegro BITMAP, loading it into the static maps if it's not already loaded. Note that ownership of the BITMAP is NOT transferred!
120120
/// </summary>
121121
/// <param name="conversionMode">The Allegro color conversion mode to use when loading this bitmap.</param>
122+
/// <param name="dataPathToSpecificFrame">Path to a specific frame when loading an animation to avoid overwriting the original preset DataPath when loading each frame.</param>
122123
/// <returns>Pointer to the BITMAP loaded from disk.</returns>
123-
BITMAP * GetAsBitmap(int conversionMode = 0);
124+
BITMAP * GetAsBitmap(int conversionMode = 0, const std::string &dataPathToSpecificFrame = "");
124125

125126
/// <summary>
126127
/// Gets the data represented by this ContentFile object as an array of Allegro BITMAPs, each representing a frame in the animation.
@@ -136,8 +137,9 @@ namespace RTE {
136137
/// Note that this is relatively slow since it reads the data from disk each time.
137138
/// </summary>
138139
/// <param name="conversionMode">The Allegro color conversion mode to use when loading this bitmap. Only applies the first time a bitmap is loaded from the disk.</param>
140+
/// <param name="dataPathToSpecificFrame">Path to a specific frame when loading an animation to avoid overwriting the original preset DataPath when loading each frame.</param>
139141
/// <returns>Pointer to the BITMAP loaded from disk.</returns>
140-
BITMAP * LoadAndReleaseBitmap(int conversionMode = 0);
142+
BITMAP * LoadAndReleaseBitmap(int conversionMode = 0, const std::string &dataPathToSpecificFrame = "");
141143

142144
/// <summary>
143145
/// Gets the data represented by this ContentFile object as an FMOD FSOUND_SAMPLE, loading it into the static maps if it's not already loaded. Ownership of the FSOUND_SAMPLE is NOT transferred!
@@ -174,7 +176,7 @@ namespace RTE {
174176
enum BitDepths { Eight = 0, ThirtyTwo, BitDepthCount };
175177

176178
static std::unordered_map<size_t, std::string> s_PathHashes; //!< Static map containing the hash values of paths of all loaded data files.
177-
static std::unordered_map<std::string, BITMAP *> s_LoadedBitmaps[BitDepthCount]; //!< Static map containing all the already loaded BITMAPs and their paths for each bit depth.
179+
static std::array<std::unordered_map<std::string, BITMAP *>, BitDepthCount> s_LoadedBitmaps; //!< Static map containing all the already loaded BITMAPs and their paths for each bit depth.
178180
static std::unordered_map<std::string, FMOD::Sound *> s_LoadedSamples; //!< Static map containing all the already loaded FSOUND_SAMPLEs and their paths.
179181

180182
std::string m_DataPath; //!< The path to this ContentFile's data file. In the case of an animation, this filename/name will be appended with 000, 001, 002 etc.

System/System.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ namespace RTE {
6969
inputString = std::regex_replace(inputString, regexError, "\033[1;31m$&\033[0;0m");
7070

7171
// Color .rte-paths green
72-
std::regex regexPath("\\w*\\.rte\\/(\\w| |\\.|\\/)*(\\/|\\.bmp|\\.wav|\\.lua|\\.ini)");
72+
std::regex regexPath("\\w*\\.rte\\/(\\w| |\\.|\\/)*(\\/|\\.bmp|\\.png|\\.wav|\\.lua|\\.ini)");
7373
inputString = std::regex_replace(inputString, regexPath, "\033[1;32m$&\033[0;0m");
7474

7575
// Color names in quotes yellow

0 commit comments

Comments
 (0)