Skip to content

Commit

Permalink
Replace the working set save ptr with an optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Dextinfire committed Sep 18, 2024
1 parent baf9385 commit 1a6ef40
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 38 deletions.
70 changes: 32 additions & 38 deletions src/games/cclcc/savesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ using namespace Impacto::Profile::SaveSystem;
using namespace Impacto::Profile::ScriptVars;
using namespace Impacto::Profile::Vm;

SaveFileEntry* WorkingSaveEntry = 0;

SaveError SaveSystem::MountSaveFile() {
Io::Stream* stream;
IoError err = Io::PhysicalFileStream::Create(SaveFilePath, &stream);
Expand All @@ -37,8 +35,7 @@ SaveError SaveSystem::MountSaveFile() {
case IoError_OK:
break;
};

WorkingSaveEntry = new SaveFileEntry();
WorkingSaveEntry = std::optional<SaveFileEntry>(SaveFileEntry());
WorkingSaveThumbnail.Sheet =
SpriteSheet(Window->WindowWidth, Window->WindowHeight);
WorkingSaveThumbnail.Bounds =
Expand Down Expand Up @@ -209,33 +206,30 @@ void SaveSystem::FlushWorkingSaveEntry(SaveType type, int id) {
break;
}

if (WorkingSaveEntry != 0) {
if (entry != 0) {
Renderer->FreeTexture(entry->SaveThumbnail.Sheet.Texture);
*entry = *WorkingSaveEntry;
time_t rawtime;
time(&rawtime);
entry->SaveDate = *localtime(&rawtime);
auto captureBuffer = Renderer->GetImageFromTexture(
WorkingSaveThumbnail.Sheet.Texture, WorkingSaveThumbnail.Bounds);

Texture tex;
tex.Init(TexFmt_RGBA, SaveThumbnailWidth, SaveThumbnailHeight);

entry->SaveThumbnail.Sheet =
SpriteSheet(SaveThumbnailWidth, SaveThumbnailHeight);
entry->SaveThumbnail.Bounds =
RectF(0.0f, 0.0f, SaveThumbnailWidth, SaveThumbnailHeight);

int result = ResizeImage(
WorkingSaveThumbnail.Bounds, entry->SaveThumbnail.Bounds,
captureBuffer,
tcb::span{tex.Buffer, static_cast<size_t>(tex.BufferSize)}, true);
if (result < 0) {
ImpLog(LL_Error, LC_General, "Failed to resize save thumbnail\n");
}
entry->SaveThumbnail.Sheet.Texture = tex.Submit();
if (entry != 0) {
Renderer->FreeTexture(entry->SaveThumbnail.Sheet.Texture);
*entry = *WorkingSaveEntry;
time_t rawtime;
time(&rawtime);
entry->SaveDate = *localtime(&rawtime);
auto captureBuffer = Renderer->GetImageFromTexture(
WorkingSaveThumbnail.Sheet.Texture, WorkingSaveThumbnail.Bounds);

Texture tex;
tex.Init(TexFmt_RGBA, SaveThumbnailWidth, SaveThumbnailHeight);

entry->SaveThumbnail.Sheet =
SpriteSheet(SaveThumbnailWidth, SaveThumbnailHeight);
entry->SaveThumbnail.Bounds =
RectF(0.0f, 0.0f, SaveThumbnailWidth, SaveThumbnailHeight);

int result = ResizeImage(
WorkingSaveThumbnail.Bounds, entry->SaveThumbnail.Bounds, captureBuffer,
tcb::span{tex.Buffer, static_cast<size_t>(tex.BufferSize)}, true);
if (result < 0) {
ImpLog(LL_Error, LC_General, "Failed to resize save thumbnail\n");
}
entry->SaveThumbnail.Sheet.Texture = tex.Submit();
}
}

Expand Down Expand Up @@ -423,7 +417,7 @@ tm const& SaveSystem::GetSaveDate(SaveType type, int id) {
void SaveSystem::SaveMemory() {
// TODO: Sys save data

if (WorkingSaveEntry != 0) {
if (WorkingSaveEntry) {
WorkingSaveEntry->Status = 1;
WorkingSaveEntry->Checksum = 0; // CalculateChecksum(0);
time_t rawtime;
Expand Down Expand Up @@ -461,16 +455,16 @@ void SaveSystem::SaveMemory() {
}

void SaveSystem::LoadEntry(SaveType type, int id) {
if (WorkingSaveEntry != 0) {
delete WorkingSaveEntry;
WorkingSaveEntry = 0;
if (!WorkingSaveEntry) {
ImpLog(LL_Error, LC_IO, "Failed to load save memory: no working save\n");
return;
}
switch (type) {
case SaveQuick:
WorkingSaveEntry = (SaveFileEntry*)QuickSaveEntries[id];
WorkingSaveEntry = *static_cast<SaveFileEntry*>(QuickSaveEntries[id]);
break;
case SaveFull:
WorkingSaveEntry = (SaveFileEntry*)FullSaveEntries[id];
WorkingSaveEntry = *static_cast<SaveFileEntry*>(FullSaveEntries[id]);
break;
default:
ImpLog(LL_Error, LC_IO,
Expand All @@ -480,7 +474,7 @@ void SaveSystem::LoadEntry(SaveType type, int id) {
}

void SaveSystem::LoadMemoryNew(LoadProcess load) {
if (!WorkingSaveEntry->Status) {
if (!WorkingSaveEntry || WorkingSaveEntry->Status == 0) {
ImpLog(LL_Error, LC_IO, "Failed to load entry: save is empty\n");
return;
}
Expand Down Expand Up @@ -662,7 +656,7 @@ bool SaveSystem::GetEVVariationIsUnlocked(int evId, int variationIdx) {
bool SaveSystem::GetBgmFlag(int id) { return BGMFlags[id]; }

void SaveSystem::SetCheckpointId(int id) {
if (WorkingSaveEntry != nullptr) WorkingSaveEntry->MainThreadIp = id;
if (WorkingSaveEntry) WorkingSaveEntry->MainThreadIp = id;
}

Sprite const& SaveSystem::GetSaveThumbnail(SaveType type, int id) {
Expand Down
2 changes: 2 additions & 0 deletions src/games/cclcc/savesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "../../data/savesystem.h"
#include "../../texture/texture.h"
#include "../../spritesheet.h"
#include <optional>

namespace Impacto {
namespace CCLCC {
Expand Down Expand Up @@ -55,6 +56,7 @@ class SaveSystem : public SaveSystemBase {
uint8_t MessageFlags[10000];
bool EVFlags[1200];
uint8_t BGMFlags[200];
std::optional<SaveFileEntry> WorkingSaveEntry;
};

} // namespace CCLCC
Expand Down

0 comments on commit 1a6ef40

Please sign in to comment.