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

Commit d4689ae

Browse files
committed
Allowed saving to be done synchronously and swapped metagame to do synchronous saving to hopefully avoid problems with this stupid mess of code
1 parent da095e7 commit d4689ae

File tree

8 files changed

+31
-19
lines changed

8 files changed

+31
-19
lines changed

Entities/SLTerrain.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,13 @@ namespace RTE {
257257

258258
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
259259

260-
int SLTerrain::SaveData(const std::string &pathBase) {
260+
int SLTerrain::SaveData(const std::string &pathBase, bool doAsyncSaves) {
261261
if (pathBase.empty()) {
262262
return -1;
263263
}
264-
SceneLayer::SaveData(pathBase + " Mat.png");
265-
m_FGColorLayer->SaveData(pathBase + " FG.png");
266-
m_BGColorLayer->SaveData(pathBase + " BG.png");
264+
SceneLayer::SaveData(pathBase + " Mat.png", doAsyncSaves);
265+
m_FGColorLayer->SaveData(pathBase + " FG.png", doAsyncSaves);
266+
m_BGColorLayer->SaveData(pathBase + " BG.png", doAsyncSaves);
267267
return 0;
268268
}
269269

Entities/SLTerrain.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,9 @@ namespace RTE {
7777
/// Saves bitmap data currently in memory to disk.
7878
/// </summary>
7979
/// <param name="pathBase">The filepath base to the where to save the Bitmap data. This means everything up to the extension. "FG" and "Mat" etc will be added.</param>
80+
/// <param name="doAsyncSaves">Whether or not to save asynchronously.</param>
8081
/// <returns>An error return value signaling success or any particular failure. Anything below 0 is an error signal.</returns>
81-
int SaveData(const std::string &pathBase) override;
82+
int SaveData(const std::string &pathBase, bool doAsyncSaves = true) override;
8283

8384
/// <summary>
8485
/// Clears out any previously loaded bitmap data from memory.

Entities/Scene.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,7 +1029,7 @@ int Scene::ExpandAIPlanAssemblySchemes()
10291029
//////////////////////////////////////////////////////////////////////////////////////////
10301030
// Description: Saves currently loaded bitmap data in memory to disk.
10311031

1032-
int Scene::SaveData(std::string pathBase)
1032+
int Scene::SaveData(std::string pathBase, bool doAsyncSaves)
10331033
{
10341034
const std::string fullPathBase = g_PresetMan.GetFullModulePath(pathBase);
10351035
if (fullPathBase.empty())
@@ -1039,7 +1039,7 @@ int Scene::SaveData(std::string pathBase)
10391039
return 0;
10401040

10411041
// Save Terrain's data
1042-
if (m_pTerrain->SaveData(fullPathBase) < 0)
1042+
if (m_pTerrain->SaveData(fullPathBase, doAsyncSaves) < 0)
10431043
{
10441044
RTEAbort("Saving Terrain " + m_pTerrain->GetPresetName() + "\'s data failed!");
10451045
return -1;
@@ -1055,7 +1055,7 @@ int Scene::SaveData(std::string pathBase)
10551055
{
10561056
std::snprintf(str, sizeof(str), "T%d", team);
10571057
// Save unseen layer data to disk
1058-
if (m_apUnseenLayer[team]->SaveData(fullPathBase + " US" + str + ".png") < 0)
1058+
if (m_apUnseenLayer[team]->SaveData(fullPathBase + " US" + str + ".png", doAsyncSaves) < 0)
10591059
{
10601060
g_ConsoleMan.PrintString("ERROR: Saving unseen layer " + m_apUnseenLayer[team]->GetPresetName() + "\'s data failed!");
10611061
return -1;

Entities/Scene.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,10 +391,11 @@ EntityAllocation(Scene)
391391
// Description: Saves data currently in memory to disk.
392392
// Arguments: The filepath base to the where to save the Bitmap data. This means
393393
// everything up to the extension. "FG" and "Mat" etc will be added.
394+
// Whether or not to save asynchronously.
394395
// Return value: An error return value signaling success or any particular failure.
395396
// Anything below 0 is an error signal.
396397

397-
int SaveData(std::string pathBase);
398+
int SaveData(std::string pathBase, bool doAsyncSaves = true);
398399

399400

400401
//////////////////////////////////////////////////////////////////////////////////////////

Entities/SceneLayer.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,28 +200,37 @@ namespace RTE {
200200
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
201201

202202
template <bool TRACK_DRAWINGS>
203-
int SceneLayerImpl<TRACK_DRAWINGS>::SaveData(const std::string &bitmapPath) {
203+
int SceneLayerImpl<TRACK_DRAWINGS>::SaveData(const std::string &bitmapPath, bool doAsyncSaves) {
204204
if (bitmapPath.empty()) {
205205
return -1;
206206
}
207-
g_ActivityMan.IncrementSavingThreadCount();
207+
if (doAsyncSaves) {
208+
g_ActivityMan.IncrementSavingThreadCount();
209+
}
208210
if (m_MainBitmap) {
209211
// Make a copy of the bitmap to pass to the thread because the bitmap may be offloaded mid thread and everything will be on fire.
210212
BITMAP *outputBitmap = create_bitmap_ex(bitmap_color_depth(m_MainBitmap), m_MainBitmap->w, m_MainBitmap->h);
211213
blit(m_MainBitmap, outputBitmap, 0, 0, 0, 0, m_MainBitmap->w, m_MainBitmap->h);
212214

213-
auto saveLayerBitmap = [bitmapPath](BITMAP *bitmapToSave) {
215+
auto saveLayerBitmap = [bitmapPath, doAsyncSaves](BITMAP *bitmapToSave) {
214216
PALETTE palette;
215217
get_palette(palette);
216218
if (save_png(bitmapPath.c_str(), bitmapToSave, palette) != 0) {
217219
RTEAbort(std::string("Failed to save SceneLayerImpl bitmap to path and name: " + bitmapPath));
218220
}
219221
destroy_bitmap(bitmapToSave);
220-
g_ActivityMan.DecrementSavingThreadCount();
222+
if (doAsyncSaves) {
223+
g_ActivityMan.DecrementSavingThreadCount();
224+
}
221225
};
222-
std::thread saveThread(saveLayerBitmap, outputBitmap);
226+
223227
m_BitmapFile.SetDataPath(bitmapPath);
224-
saveThread.detach();
228+
if (doAsyncSaves) {
229+
std::thread saveThread(saveLayerBitmap, outputBitmap);
230+
saveThread.detach();
231+
} else {
232+
saveLayerBitmap(outputBitmap);
233+
}
225234
}
226235
return 0;
227236
}

Entities/SceneLayer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,9 @@ namespace RTE {
103103
/// Saves data currently in memory to disk.
104104
/// </summary>
105105
/// <param name="bitmapPath">The filepath to the where to save the bitmap data.</param>
106+
/// <param name="doAsyncSaves">Whether or not to save asynchronously.</param>
106107
/// <returns>An error return value signaling success or any particular failure. Anything below 0 is an error signal.</returns>
107-
virtual int SaveData(const std::string &bitmapPath);
108+
virtual int SaveData(const std::string &bitmapPath, bool doAsyncSaves = true);
108109

109110
/// <summary>
110111
/// Clears out any previously loaded bitmap data from memory.

Managers/MetaMan.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ int MetaMan::Save(Writer &writer) const {
370370
for (const Scene *metaScene : m_Scenes) {
371371
// Save the scene data to a good unique prefix for the Scene's layers' bitmap files as they are saved
372372
// This should be handled separately from any .ini writing
373-
//(*sItr)->SaveData(writer.GetFolderPath() + m_GameName + " - " + (*sItr)->GetPresetName());
373+
//(*sItr)->SaveData(writer.GetFolderPath() + m_GameName + " - " + (*sItr)->GetPresetName(), false);
374374
writer.NewPropertyWithValue("AddScene", metaScene);
375375
}
376376

@@ -403,7 +403,7 @@ int MetaMan::SaveSceneData(std::string pathBase)
403403
if ((*sItr)->IsRevealed() && (*sItr)->GetTerrain() && (*sItr)->GetTerrain()->IsLoadedFromDisk())
404404
{
405405
// Save the scene data to a good unique prefix for the Scene's layers' bitmap files as they are saved
406-
if ((*sItr)->SaveData(pathBase + " - " + (*sItr)->GetPresetName()) < 0)
406+
if ((*sItr)->SaveData(pathBase + " - " + (*sItr)->GetPresetName(), false) < 0)
407407
return -1;
408408
}
409409
}

Menus/MetagameGUI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2963,7 +2963,7 @@ void MetagameGUI::CompletedActivity()
29632963
// However, don't suck up actors of any non-winning team, and don't save the brains if we autoresolved, because that took care of placing the resident brains already
29642964
pAlteredScene->RetrieveSceneObjects(true, winningTeam, autoResolved);
29652965
// Save out the altered scene before clearing out its data from memory
2966-
pAlteredScene->SaveData(METASAVEPATH + std::string(AUTOSAVENAME) + " - " + pAlteredScene->GetPresetName());
2966+
pAlteredScene->SaveData(METASAVEPATH + std::string(AUTOSAVENAME) + " - " + pAlteredScene->GetPresetName(), false);
29672967
// Clear the bitmap data etc of the altered scene, we don't need to copy that over
29682968
pAlteredScene->ClearData();
29692969

0 commit comments

Comments
 (0)