Skip to content

Commit 51eeba3

Browse files
committed
Progress towards loading, need to fix up unseen layers
1 parent 18313b2 commit 51eeba3

File tree

6 files changed

+98
-16
lines changed

6 files changed

+98
-16
lines changed

Source/Entities/SLTerrain.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,14 @@ namespace RTE {
9090
/// @param layerToDraw The layer that should be drawn. See LayerType enumeration.
9191
void SetLayerToDraw(LayerType layerToDraw) { m_LayerToDraw = layerToDraw; }
9292

93+
/// Gets the foreground scenelayer of this SLTerrain.
94+
/// @return A pointer to the foreground scenelayer.
95+
SceneLayer* GetFGSceneLayer() { return m_FGColorLayer.get(); }
96+
97+
/// Gets the background scenelayer of this SLTerrain.
98+
/// @return A pointer to the background scenelayer.
99+
SceneLayer* GetBGSceneLayer() { return m_BGColorLayer.get(); }
100+
93101
/// Gets the foreground color bitmap of this SLTerrain.
94102
/// @return A pointer to the foreground color bitmap.
95103
BITMAP* GetFGColorBitmap() { return m_FGColorLayer->GetBitmap(); m_FGColorLayer->SetUpdated(); }

Source/Entities/Scene.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,23 @@ int Scene::SaveData(std::string pathBase, bool doAsyncSaves) {
847847
return 0;
848848
}
849849

850-
std::vector<SceneLayerInfo> Scene::GetCopiedSceneLayerBitmaps() {
850+
void Scene::ConstructSceneLayersFromBitmaps(std::vector<SceneLayerInfo>&& layerInfos) {
851+
for (SceneLayerInfo& sceneLayerInfo : layerInfos) {
852+
if (sceneLayerInfo.name == "Mat") {
853+
m_pTerrain->LoadDataFromBitmap(sceneLayerInfo.bitmap.release());
854+
} else if (sceneLayerInfo.name == "FG") {
855+
m_pTerrain->GetFGSceneLayer()->LoadDataFromBitmap(sceneLayerInfo.bitmap.release());
856+
} else if (sceneLayerInfo.name == "BG") {
857+
m_pTerrain->GetBGSceneLayer()->LoadDataFromBitmap(sceneLayerInfo.bitmap.release());
858+
} else {
859+
char endCh = sceneLayerInfo.name.back();
860+
int team = endCh - '0';
861+
m_apUnseenLayer[team]->LoadDataFromBitmap(sceneLayerInfo.bitmap.release());
862+
}
863+
}
864+
}
865+
866+
std::vector<SceneLayerInfo> Scene::GetCopiedSceneLayerBitmaps() const {
851867
std::vector<SceneLayerInfo> layerInfos;
852868

853869
// Save Terrain's data

Source/Entities/Scene.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,9 @@ namespace RTE {
251251
/// Anything below 0 is an error signal.
252252
int SaveData(std::string pathBase, bool doAsyncSaves = true);
253253

254-
std::vector<SceneLayerInfo> GetCopiedSceneLayerBitmaps();
254+
void ConstructSceneLayersFromBitmaps(std::vector<SceneLayerInfo>&& layerInfos);
255+
256+
std::vector<SceneLayerInfo> GetCopiedSceneLayerBitmaps() const;
255257

256258
/// Saves preview bitmap for this scene.
257259
/// @param bitmapPath The full filepath the where to save the Bitmap data.

Source/Entities/SceneLayer.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,25 @@ void SceneLayerImpl<TRACK_DRAWINGS, STATIC_TEXTURE>::InitScrollRatios(bool initF
199199
m_ScaledDimensions.SetXY(mainBitmapWidth * m_ScaleFactor.GetX(), mainBitmapHeight * m_ScaleFactor.GetY());
200200
}
201201

202+
template <bool TRACK_DRAWINGS, bool STATIC_TEXTURE>
203+
int SceneLayerImpl<TRACK_DRAWINGS, STATIC_TEXTURE>::LoadDataFromBitmap(BITMAP* bitmap) {
204+
if (m_MainBitmapOwned) {
205+
destroy_bitmap(m_MainBitmap);
206+
}
207+
208+
m_MainBitmap = bitmap;
209+
m_MainBitmapOwned = true;
210+
211+
m_BackBitmap = create_bitmap_ex(bitmap_color_depth(m_MainBitmap), m_MainBitmap->w, m_MainBitmap->h);
212+
if constexpr (!STATIC_TEXTURE) {
213+
m_MainTexture = std::make_unique<BigTexture>(m_MainBitmap);
214+
}
215+
m_LastClearColor = ColorKeys::g_InvalidColor;
216+
217+
InitScrollRatios();
218+
return 0;
219+
}
220+
202221
template <bool TRACK_DRAWINGS, bool STATIC_TEXTURE>
203222
int SceneLayerImpl<TRACK_DRAWINGS, STATIC_TEXTURE>::LoadData() {
204223
// Load from disk and take ownership. Don't cache because the bitmap will be modified.

Source/Entities/SceneLayer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ namespace RTE {
7676
/// @return Whether this SceneLayer's bitmap data was loaded from a file or not.
7777
virtual bool IsLoadedFromDisk() const { return !m_BitmapFile.GetDataPath().empty(); }
7878

79+
/// Loads previously specified/created data into memory from an existing BITMAP. Has to be done before using this SceneLayer if the bitmap was not generated at runtime.
80+
/// @param bitmap Pointer to the bitmap to take. Takes ownership!
81+
/// @return An error return value signaling success or any particular failure. Anything below 0 is an error signal.
82+
virtual int LoadDataFromBitmap(BITMAP* bitmap);
83+
7984
/// Loads previously specified/created data into memory. Has to be done before using this SceneLayer if the bitmap was not generated at runtime.
8085
/// @return An error return value signaling success or any particular failure. Anything below 0 is an error signal.
8186
virtual int LoadData();

Source/Managers/ActivityMan.cpp

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -201,21 +201,24 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
201201
}
202202

203203
unz_file_info info;
204-
char* buffer;
204+
char* buffer = nullptr;
205205

206-
unzLocateFile(zippedSaveFile, (fileName + ".ini").c_str(), nullptr);
207-
unzOpenCurrentFile(zippedSaveFile);
208-
unzGetCurrentFileInfo(zippedSaveFile, &info, nullptr, 0, nullptr, 0, nullptr, 0);
209-
210-
buffer = (char*)malloc(info.uncompressed_size);
211-
if (!buffer) {
212-
// If this ever hits I've lost all faith in modern OSes, but alas when one is writing C, one must dance along
213-
RTEError::ShowMessageBox("Catastrophic failure! Failed to allocate memory for savegame");
214-
return false;
215-
}
206+
auto unzipFileIntoBuffer = [&](std::string fullFileName) {
207+
unzLocateFile(zippedSaveFile, fullFileName.c_str(), nullptr);
208+
unzOpenCurrentFile(zippedSaveFile);
209+
unzGetCurrentFileInfo(zippedSaveFile, &info, nullptr, 0, nullptr, 0, nullptr, 0);
216210

217-
unzReadCurrentFile(zippedSaveFile, buffer, info.uncompressed_size);
218-
unzCloseCurrentFile(zippedSaveFile);
211+
buffer = (char*)malloc(info.uncompressed_size);
212+
if (!buffer) {
213+
// If this ever hits I've lost all faith in modern OSes, but alas when one is writing C, one must dance along
214+
RTEError::ShowMessageBox("Catastrophic failure! Failed to allocate memory for savegame");
215+
}
216+
217+
unzReadCurrentFile(zippedSaveFile, buffer, info.uncompressed_size);
218+
unzCloseCurrentFile(zippedSaveFile);
219+
};
220+
221+
unzipFileIntoBuffer(fileName + ".ini");
219222

220223
Reader reader(std::make_unique<std::istringstream>(buffer), saveFilePath, true, nullptr, false);
221224

@@ -240,6 +243,10 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
240243
}
241244
}
242245

246+
free(buffer);
247+
248+
int numberOfTeams = activity->m_TeamCount;
249+
243250
// SetSceneToLoad() doesn't Clone(), but when the Activity starts, it will eventually call LoadScene(), which does a Clone() of scene internally.
244251
g_SceneMan.SetSceneToLoad(scene.get(), true, true);
245252
// Saved Scenes get their presetname set to their filename to ensure they're separate from the preset Scene they're based off of.
@@ -250,9 +257,34 @@ bool ActivityMan::LoadAndLaunchGame(const std::string& fileName) {
250257
// When this method exits, our Scene object will be destroyed, which will cause problems if you try to restart it. To avoid this, set the Scene to load to the preset object with the same name.
251258
g_SceneMan.SetSceneToLoad(originalScenePresetName, placeObjectsIfSceneIsRestarted, placeUnitsIfSceneIsRestarted);
252259

253-
g_ConsoleMan.PrintString("SYSTEM: Game \"" + fileName + "\" loaded!");
260+
// Replace our scene images with the ones from the zip
261+
std::vector<SceneLayerInfo> layerInfos;
254262

263+
PALETTE palette;
264+
get_palette(palette);
265+
266+
unzipFileIntoBuffer(fileName + " Mat.png");
267+
layerInfos.emplace_back("Mat", std::unique_ptr<BITMAP>(load_memory_png(buffer, info.uncompressed_size, palette)));
268+
free(buffer);
269+
270+
unzipFileIntoBuffer(fileName + " FG.png");
271+
layerInfos.emplace_back("FG", std::unique_ptr<BITMAP>(load_memory_png(buffer, info.uncompressed_size, palette)));
255272
free(buffer);
273+
274+
unzipFileIntoBuffer(fileName + " BG.png");
275+
layerInfos.emplace_back("BG", std::unique_ptr<BITMAP>(load_memory_png(buffer, info.uncompressed_size, palette)));
276+
free(buffer);
277+
278+
for (int i = 0; i < numberOfTeams; ++i) {
279+
unzipFileIntoBuffer(fileName + std::format(" T%i.png", i));
280+
layerInfos.emplace_back(std::format("T%i", i), std::unique_ptr<BITMAP>(load_memory_png(buffer, info.uncompressed_size, palette)));
281+
free(buffer);
282+
}
283+
284+
g_SceneMan.GetScene()->ConstructSceneLayersFromBitmaps(std::move(layerInfos));
285+
286+
g_ConsoleMan.PrintString("SYSTEM: Game \"" + fileName + "\" loaded!");
287+
256288
return true;
257289
}
258290

0 commit comments

Comments
 (0)