Save/load state with file parameter
Jeffrey Pfau jeffrey@endrift.com
Mon, 04 Aug 2014 22:40:11 -0700
2 files changed,
29 insertions(+),
11 deletions(-)
M
src/gba/gba-serialize.c
→
src/gba/gba-serialize.c
@@ -163,32 +163,47 @@ struct VFile* vf = _getStateVf(gba, dir, slot, true);
if (!vf) { return false; } - bool success = true; + bool success = GBASaveStateNamed(gba, vf, screenshot); + vf->close(vf); + return success; +} + +bool GBALoadState(struct GBA* gba, struct VDir* dir, int slot) { + struct VFile* vf = _getStateVf(gba, dir, slot, false); + if (!vf) { + return false; + } + bool success = GBALoadStateNamed(gba, vf); + vf->close(vf); + return success; +} + +bool GBASaveStateNamed(struct GBA* gba, struct VFile* vf, bool screenshot) { if (!screenshot) { vf->truncate(vf, sizeof(struct GBASerializedState)); struct GBASerializedState* state = vf->map(vf, sizeof(struct GBASerializedState), MAP_WRITE); + if (!state) { + return false; + } GBASerialize(gba, state); vf->unmap(vf, state, sizeof(struct GBASerializedState)); } else { - _savePNGState(gba, vf); + return _savePNGState(gba, vf); } - vf->close(vf); - return success; + return true; } -bool GBALoadState(struct GBA* gba, struct VDir* dir, int slot) { - struct VFile* vf = _getStateVf(gba, dir, slot, false); - if (!vf) { - return false; - } +bool GBALoadStateNamed(struct GBA* gba, struct VFile* vf) { if (!isPNG(vf)) { struct GBASerializedState* state = vf->map(vf, sizeof(struct GBASerializedState), MAP_READ); + if (!state) { + return false; + } GBADeserialize(gba, state); vf->unmap(vf, state, sizeof(struct GBASerializedState)); } else { - _loadPNGState(gba, vf); + return _loadPNGState(gba, vf); } - vf->close(vf); return true; }
M
src/gba/gba-serialize.h
→
src/gba/gba-serialize.h
@@ -272,6 +272,9 @@
bool GBASaveState(struct GBA* gba, struct VDir* dir, int slot, bool screenshot); bool GBALoadState(struct GBA* gba, struct VDir* dir, int slot); +bool GBASaveStateNamed(struct GBA* gba, struct VFile* vf, bool screenshot); +bool GBALoadStateNamed(struct GBA* gba, struct VFile* vf); + struct GBASerializedState* GBAAllocateState(void); void GBADeallocateState(struct GBASerializedState* state);