Simplified DawnGAme a lot
This commit is contained in:
@ -26,6 +26,7 @@ target_sources(${DAWN_TARGET_NAME}
|
||||
# Subdirs
|
||||
add_subdirectory(asset)
|
||||
add_subdirectory(display)
|
||||
add_subdirectory(game)
|
||||
add_subdirectory(games)
|
||||
add_subdirectory(input)
|
||||
add_subdirectory(locale)
|
||||
|
10
src/dawn/game/CMakeLists.txt
Normal file
10
src/dawn/game/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
# Copyright (c) 2023 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Sources
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
DawnGame.cpp
|
||||
)
|
42
src/dawn/game/DawnGame.cpp
Normal file
42
src/dawn/game/DawnGame.cpp
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "DawnGame.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
DawnGame::DawnGame(DawnHost *host) :
|
||||
host(host),
|
||||
renderManager(this),
|
||||
inputManager(this),
|
||||
saveManager(this)
|
||||
{
|
||||
}
|
||||
|
||||
int32_t DawnGame::init() {
|
||||
this->assetManager.init();
|
||||
this->renderManager.init();
|
||||
|
||||
this->scene = dawnGameGetInitialScene(this);
|
||||
|
||||
return DAWN_GAME_INIT_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
int32_t DawnGame::update(float_t delta) {
|
||||
this->assetManager.update();
|
||||
this->inputManager.update();
|
||||
this->timeManager.update(delta);
|
||||
|
||||
if(this->scene != nullptr) this->scene->update();
|
||||
|
||||
this->renderManager.update();
|
||||
|
||||
return DAWN_GAME_UPDATE_RESULT_SUCCESS;
|
||||
}
|
||||
|
||||
void DawnGame::sceneCutover(Scene *scene) {
|
||||
if(scene == nullptr) scene = this->scene;
|
||||
this->sceneToCutTo = scene;
|
||||
}
|
@ -22,12 +22,25 @@
|
||||
namespace Dawn {
|
||||
class DawnHost;
|
||||
|
||||
class IDawnGame {
|
||||
protected:
|
||||
// SceneItemComponentList componentList;
|
||||
class DawnGame {
|
||||
private:
|
||||
Scene *sceneToCutTo = nullptr;
|
||||
|
||||
public:
|
||||
DawnHost *host;
|
||||
Scene *scene = nullptr;
|
||||
AssetManager assetManager;
|
||||
TimeManager timeManager;
|
||||
RenderManager renderManager;
|
||||
InputManager inputManager;
|
||||
SaveManager saveManager;
|
||||
|
||||
/**
|
||||
* Construct a new game instance.
|
||||
*
|
||||
* @param host Host that executed this game instantiation.
|
||||
*/
|
||||
DawnGame(DawnHost *host);
|
||||
|
||||
/**
|
||||
* Initialize the game. This is performed by the host at a time that is
|
||||
@ -38,7 +51,7 @@ namespace Dawn {
|
||||
* @param host Pointer to the host that is running this game.
|
||||
* @return The game initialize result.
|
||||
*/
|
||||
virtual int32_t init() = 0;
|
||||
int32_t init();
|
||||
|
||||
/**
|
||||
* Performs a game update operation. This operation should occur exactly
|
||||
@ -52,7 +65,7 @@ namespace Dawn {
|
||||
* @param delta Time delta to tick the game by.
|
||||
* @return The game update result.
|
||||
*/
|
||||
virtual int32_t update(float_t delta) = 0;
|
||||
int32_t update(float_t delta);
|
||||
|
||||
/**
|
||||
* Changes to a new scene, will dispose the currently active scene as part
|
||||
@ -61,13 +74,15 @@ namespace Dawn {
|
||||
*
|
||||
* @param scene Scene to cut over to.
|
||||
*/
|
||||
virtual void sceneCutover(Scene *scene) = 0;
|
||||
|
||||
/**
|
||||
* Cleanup the memory of the DawnGame instance.
|
||||
*/
|
||||
virtual ~IDawnGame() {
|
||||
|
||||
}
|
||||
void sceneCutover(Scene *scene);
|
||||
};
|
||||
|
||||
/**
|
||||
* Unimplemented by default, required by the game as the basic entry point
|
||||
* for which scene should be used by default.
|
||||
*
|
||||
* @param game Game that is requesting this scene.
|
||||
* @return Pointer to a scene that you wish to have as the default scene.
|
||||
*/
|
||||
Scene * dawnGameGetInitialScene(DawnGame *game);
|
||||
}
|
@ -1,217 +1,217 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "SaveManager.hpp"
|
||||
#include "game/DawnGame.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
SaveManager::SaveManager(DawnGame *game) {
|
||||
this->game = game;
|
||||
}
|
||||
|
||||
void SaveManager::saveFile() {
|
||||
savedata_t value;
|
||||
|
||||
assertTrue(this->currentSaveSlot >= 0);
|
||||
|
||||
// Update metadata
|
||||
auto timestamp = this->game->timeManager.getTimestamp();
|
||||
value.i64 = timestamp;
|
||||
this->currentSave.set(SAVE_KEY_TIMESTAMP, value);
|
||||
value.i16 = this->currentSaveSlot;
|
||||
this->currentSave.set(SAVE_KEY_SLOT, value);
|
||||
|
||||
// Now open output buffer.
|
||||
char filename[SAVE_MANAGER_FILENAME_LENGTH];
|
||||
sprintf(filename, "savefile_%u.save", this->currentSaveSlot);
|
||||
FILE *fptr = fopen(filename, "wb");
|
||||
if(fptr == NULL) {
|
||||
printf("Error opening %s\n", filename);
|
||||
assertUnreachable();
|
||||
return;
|
||||
}
|
||||
|
||||
// Now begin buffering data. First we start with the common phrase, the engine
|
||||
// version, the timestamp. In future we will store the MD5 here too.
|
||||
char buffer[SAVE_MANAGER_BUFFER_LENGTH];
|
||||
auto len = sprintf(buffer, "DE_SAVE|%s|%lld|", "1.00", timestamp);
|
||||
fwrite(buffer, sizeof(char), len, fptr);
|
||||
|
||||
// Now buffer out the data, this is almost certain to change over time.
|
||||
auto it = this->currentSave.values.begin();
|
||||
while(it != this->currentSave.values.end()) {
|
||||
uint8_t *buffer = (uint8_t *)&it->second;
|
||||
char delim = '|';
|
||||
fwrite(it->first.c_str(), sizeof(char), strlen(it->first.c_str()), fptr);
|
||||
fwrite(&delim, sizeof(char), 1, fptr);
|
||||
fwrite(buffer, sizeof(uint8_t), sizeof(savedata_t) / sizeof(uint8_t), fptr);
|
||||
fwrite(&delim, sizeof(char), 1, fptr);
|
||||
++it;
|
||||
}
|
||||
|
||||
// Buffering done, close the file buffer.
|
||||
fclose(fptr);
|
||||
this->currentSave.hasChanges = false;
|
||||
}
|
||||
|
||||
enum SaveLoadResult SaveManager::loadFile() {
|
||||
this->currentSave.reset();
|
||||
|
||||
assertTrue(this->currentSaveSlot >= 0);
|
||||
|
||||
// Load file
|
||||
struct SaveFile temporaryFile;
|
||||
|
||||
char filename[SAVE_MANAGER_FILENAME_LENGTH];
|
||||
sprintf(filename, "savefile_%u.save", this->currentSaveSlot);
|
||||
FILE *fptr = fopen(filename, "rb");
|
||||
if(fptr == NULL) {
|
||||
return SAVE_LOAD_RESULT_FILE_NOT_PRESENT;
|
||||
}
|
||||
|
||||
// Let's read how long the file is first.
|
||||
fseek(fptr, 0, SEEK_END);
|
||||
auto len = ftell(fptr);
|
||||
|
||||
// Now let's just ensure the file isn't too small, this starts by us making
|
||||
// sure we have at least "DE_SAVE|%s|%lld|", the string is unknown length,
|
||||
// but we can be 100% sure of the rest of the file size. So we take
|
||||
// 7+1+1+1+19+1=30
|
||||
if(len < 30) {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_TOO_SMALL;
|
||||
}
|
||||
|
||||
// Rewind.
|
||||
fseek(fptr, 0, SEEK_SET);
|
||||
|
||||
// Ok let's buffer the first 8 characters to validate "DE_SAVE|"
|
||||
char buffer[SAVE_MANAGER_BUFFER_LENGTH];
|
||||
auto read = fread(buffer, sizeof(char), SAVE_MANAGER_BUFFER_LENGTH, fptr);
|
||||
if(
|
||||
read < 8 ||
|
||||
buffer[0] != 'D' ||
|
||||
buffer[1] != 'E' ||
|
||||
buffer[2] != '_' ||
|
||||
buffer[3] != 'S' ||
|
||||
buffer[4] != 'A' ||
|
||||
buffer[5] != 'V' ||
|
||||
buffer[6] != 'E' ||
|
||||
buffer[7] != '|'
|
||||
) {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_CORRUPTED_DE_SAVE;
|
||||
}
|
||||
|
||||
// Now read ahead to the next vertical bar, that will give us the engine
|
||||
// version
|
||||
char *bufferCurrent = buffer + 8;
|
||||
char *p = stringFindNext(bufferCurrent, '|', 10);
|
||||
if(p == NULL) {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_CORRUPTED_VERSION;
|
||||
}
|
||||
*p = '\0';
|
||||
char *version = bufferCurrent;
|
||||
bufferCurrent = p + 1;
|
||||
|
||||
// Now read the timestamp string.
|
||||
p = stringFindNext(bufferCurrent, '|', 64);
|
||||
if(p == NULL) {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_CORRUPTED_TIMESTAMP;
|
||||
}
|
||||
*p = '\0';
|
||||
char *strTimestamp = bufferCurrent;
|
||||
|
||||
// Parse timestamp
|
||||
int64_t timestamp = strtoll(strTimestamp, NULL, 10);
|
||||
if(timestamp == 0) {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_CORRUPTED_TIMESTAMP;
|
||||
}
|
||||
|
||||
// Now begin parsing the data in the save file. We start here, at the end of
|
||||
// the previous string, so it will INCLUDE "|" at the first char.
|
||||
read = p - buffer;
|
||||
while(read < (len-1)) {
|
||||
// Rewind and then buffer the next set of bytes
|
||||
fseek(fptr, (long)read, SEEK_SET);
|
||||
if(fread(buffer, sizeof(char), SAVE_MANAGER_BUFFER_LENGTH, fptr) <= 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Because we finish the last string INCLUDING "|", then we skip it here.
|
||||
bufferCurrent = buffer + 1;
|
||||
|
||||
// Now, read the key
|
||||
p = stringFindNext(bufferCurrent, '|', SAVE_MANAGER_BUFFER_LENGTH);
|
||||
if(p == NULL) {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_CORRUPTED_DATA_KEY;
|
||||
}
|
||||
|
||||
*p = '\0';
|
||||
char *key = bufferCurrent;
|
||||
|
||||
// Validate the string lengths
|
||||
if(strlen(key) <= 0) {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_CORRUPTED_DATA_KEY;
|
||||
}
|
||||
|
||||
// Now read the value
|
||||
bufferCurrent = p + 1;
|
||||
savedata_t destination;
|
||||
uint8_t *ptrValue = (uint8_t*)bufferCurrent;
|
||||
memoryCopy(ptrValue, &destination, sizeof(savedata_t));
|
||||
|
||||
// Now advance the pointer...
|
||||
p = bufferCurrent + sizeof(savedata_t);
|
||||
if(*p != '|') {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_CORRUPTED_DATA_VALUE;
|
||||
}
|
||||
|
||||
// Set the value.
|
||||
temporaryFile.set(std::string(key), destination);
|
||||
read += (p - buffer);
|
||||
}
|
||||
|
||||
// Close file
|
||||
fclose(fptr);
|
||||
|
||||
// OK Let's validate that everything was read OK
|
||||
if(temporaryFile.get(SAVE_KEY_TIMESTAMP).i64 != timestamp) {
|
||||
return SAVE_LOAD_RESULT_MISMATCH_TIMESTAMP;
|
||||
}
|
||||
|
||||
// Now pass off to the loader
|
||||
if(this->validateSave(temporaryFile) != false) {
|
||||
return SAVE_LOAD_RESULT_ERROR;
|
||||
}
|
||||
|
||||
// Unmark changes. In future I may need to do more complex testing, e.g. if
|
||||
// the game has a newer version that updates save files some how.
|
||||
this->currentSave.hasChanges = false;
|
||||
return SAVE_LOAD_SUCCESS;
|
||||
}
|
||||
|
||||
void SaveManager::deleteFile(int16_t slot) {
|
||||
assertTrue(slot >= 0);
|
||||
}
|
||||
|
||||
void SaveManager::useSlot(int16_t slot) {
|
||||
assertTrue(slot >= 0);
|
||||
this->currentSaveSlot = slot;
|
||||
this->currentSave.reset();
|
||||
}
|
||||
|
||||
std::vector<int16_t> SaveManager::getUsedSlots() {
|
||||
std::vector<int16_t> slots;
|
||||
|
||||
return slots;
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "SaveManager.hpp"
|
||||
#include "game/DawnGame.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
SaveManager::SaveManager(DawnGame *game) {
|
||||
this->game = game;
|
||||
}
|
||||
|
||||
void SaveManager::saveFile() {
|
||||
savedata_t value;
|
||||
|
||||
assertTrue(this->currentSaveSlot >= 0);
|
||||
|
||||
// Update metadata
|
||||
auto timestamp = this->game->timeManager.getTimestamp();
|
||||
value.i64 = timestamp;
|
||||
this->currentSave.set(SAVE_KEY_TIMESTAMP, value);
|
||||
value.i16 = this->currentSaveSlot;
|
||||
this->currentSave.set(SAVE_KEY_SLOT, value);
|
||||
|
||||
// Now open output buffer.
|
||||
char filename[SAVE_MANAGER_FILENAME_LENGTH];
|
||||
sprintf(filename, "savefile_%u.save", this->currentSaveSlot);
|
||||
FILE *fptr = fopen(filename, "wb");
|
||||
if(fptr == NULL) {
|
||||
printf("Error opening %s\n", filename);
|
||||
assertUnreachable();
|
||||
return;
|
||||
}
|
||||
|
||||
// Now begin buffering data. First we start with the common phrase, the engine
|
||||
// version, the timestamp. In future we will store the MD5 here too.
|
||||
char buffer[SAVE_MANAGER_BUFFER_LENGTH];
|
||||
auto len = sprintf(buffer, "DE_SAVE|%s|%lld|", "1.00", timestamp);
|
||||
fwrite(buffer, sizeof(char), len, fptr);
|
||||
|
||||
// Now buffer out the data, this is almost certain to change over time.
|
||||
auto it = this->currentSave.values.begin();
|
||||
while(it != this->currentSave.values.end()) {
|
||||
uint8_t *buffer = (uint8_t *)&it->second;
|
||||
char delim = '|';
|
||||
fwrite(it->first.c_str(), sizeof(char), strlen(it->first.c_str()), fptr);
|
||||
fwrite(&delim, sizeof(char), 1, fptr);
|
||||
fwrite(buffer, sizeof(uint8_t), sizeof(savedata_t) / sizeof(uint8_t), fptr);
|
||||
fwrite(&delim, sizeof(char), 1, fptr);
|
||||
++it;
|
||||
}
|
||||
|
||||
// Buffering done, close the file buffer.
|
||||
fclose(fptr);
|
||||
this->currentSave.hasChanges = false;
|
||||
}
|
||||
|
||||
enum SaveLoadResult SaveManager::loadFile() {
|
||||
this->currentSave.reset();
|
||||
|
||||
assertTrue(this->currentSaveSlot >= 0);
|
||||
|
||||
// Load file
|
||||
struct SaveFile temporaryFile;
|
||||
|
||||
char filename[SAVE_MANAGER_FILENAME_LENGTH];
|
||||
sprintf(filename, "savefile_%u.save", this->currentSaveSlot);
|
||||
FILE *fptr = fopen(filename, "rb");
|
||||
if(fptr == NULL) {
|
||||
return SAVE_LOAD_RESULT_FILE_NOT_PRESENT;
|
||||
}
|
||||
|
||||
// Let's read how long the file is first.
|
||||
fseek(fptr, 0, SEEK_END);
|
||||
auto len = ftell(fptr);
|
||||
|
||||
// Now let's just ensure the file isn't too small, this starts by us making
|
||||
// sure we have at least "DE_SAVE|%s|%lld|", the string is unknown length,
|
||||
// but we can be 100% sure of the rest of the file size. So we take
|
||||
// 7+1+1+1+19+1=30
|
||||
if(len < 30) {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_TOO_SMALL;
|
||||
}
|
||||
|
||||
// Rewind.
|
||||
fseek(fptr, 0, SEEK_SET);
|
||||
|
||||
// Ok let's buffer the first 8 characters to validate "DE_SAVE|"
|
||||
char buffer[SAVE_MANAGER_BUFFER_LENGTH];
|
||||
auto read = fread(buffer, sizeof(char), SAVE_MANAGER_BUFFER_LENGTH, fptr);
|
||||
if(
|
||||
read < 8 ||
|
||||
buffer[0] != 'D' ||
|
||||
buffer[1] != 'E' ||
|
||||
buffer[2] != '_' ||
|
||||
buffer[3] != 'S' ||
|
||||
buffer[4] != 'A' ||
|
||||
buffer[5] != 'V' ||
|
||||
buffer[6] != 'E' ||
|
||||
buffer[7] != '|'
|
||||
) {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_CORRUPTED_DE_SAVE;
|
||||
}
|
||||
|
||||
// Now read ahead to the next vertical bar, that will give us the engine
|
||||
// version
|
||||
char *bufferCurrent = buffer + 8;
|
||||
char *p = stringFindNext(bufferCurrent, '|', 10);
|
||||
if(p == NULL) {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_CORRUPTED_VERSION;
|
||||
}
|
||||
*p = '\0';
|
||||
char *version = bufferCurrent;
|
||||
bufferCurrent = p + 1;
|
||||
|
||||
// Now read the timestamp string.
|
||||
p = stringFindNext(bufferCurrent, '|', 64);
|
||||
if(p == NULL) {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_CORRUPTED_TIMESTAMP;
|
||||
}
|
||||
*p = '\0';
|
||||
char *strTimestamp = bufferCurrent;
|
||||
|
||||
// Parse timestamp
|
||||
int64_t timestamp = strtoll(strTimestamp, NULL, 10);
|
||||
if(timestamp == 0) {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_CORRUPTED_TIMESTAMP;
|
||||
}
|
||||
|
||||
// Now begin parsing the data in the save file. We start here, at the end of
|
||||
// the previous string, so it will INCLUDE "|" at the first char.
|
||||
read = p - buffer;
|
||||
while(read < (len-1)) {
|
||||
// Rewind and then buffer the next set of bytes
|
||||
fseek(fptr, (long)read, SEEK_SET);
|
||||
if(fread(buffer, sizeof(char), SAVE_MANAGER_BUFFER_LENGTH, fptr) <= 1) {
|
||||
break;
|
||||
}
|
||||
|
||||
// Because we finish the last string INCLUDING "|", then we skip it here.
|
||||
bufferCurrent = buffer + 1;
|
||||
|
||||
// Now, read the key
|
||||
p = stringFindNext(bufferCurrent, '|', SAVE_MANAGER_BUFFER_LENGTH);
|
||||
if(p == NULL) {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_CORRUPTED_DATA_KEY;
|
||||
}
|
||||
|
||||
*p = '\0';
|
||||
char *key = bufferCurrent;
|
||||
|
||||
// Validate the string lengths
|
||||
if(strlen(key) <= 0) {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_CORRUPTED_DATA_KEY;
|
||||
}
|
||||
|
||||
// Now read the value
|
||||
bufferCurrent = p + 1;
|
||||
savedata_t destination;
|
||||
uint8_t *ptrValue = (uint8_t*)bufferCurrent;
|
||||
memoryCopy(ptrValue, &destination, sizeof(savedata_t));
|
||||
|
||||
// Now advance the pointer...
|
||||
p = bufferCurrent + sizeof(savedata_t);
|
||||
if(*p != '|') {
|
||||
fclose(fptr);
|
||||
return SAVE_LOAD_RESULT_CORRUPTED_DATA_VALUE;
|
||||
}
|
||||
|
||||
// Set the value.
|
||||
temporaryFile.set(std::string(key), destination);
|
||||
read += (p - buffer);
|
||||
}
|
||||
|
||||
// Close file
|
||||
fclose(fptr);
|
||||
|
||||
// OK Let's validate that everything was read OK
|
||||
if(temporaryFile.get(SAVE_KEY_TIMESTAMP).i64 != timestamp) {
|
||||
return SAVE_LOAD_RESULT_MISMATCH_TIMESTAMP;
|
||||
}
|
||||
|
||||
// Now pass off to the loader
|
||||
if(saveValidateFile(temporaryFile) != false) {
|
||||
return SAVE_LOAD_RESULT_ERROR;
|
||||
}
|
||||
|
||||
// Unmark changes. In future I may need to do more complex testing, e.g. if
|
||||
// the game has a newer version that updates save files some how.
|
||||
this->currentSave.hasChanges = false;
|
||||
return SAVE_LOAD_SUCCESS;
|
||||
}
|
||||
|
||||
void SaveManager::deleteFile(int16_t slot) {
|
||||
assertTrue(slot >= 0);
|
||||
}
|
||||
|
||||
void SaveManager::useSlot(int16_t slot) {
|
||||
assertTrue(slot >= 0);
|
||||
this->currentSaveSlot = slot;
|
||||
this->currentSave.reset();
|
||||
}
|
||||
|
||||
std::vector<int16_t> SaveManager::getUsedSlots() {
|
||||
std::vector<int16_t> slots;
|
||||
|
||||
return slots;
|
||||
}
|
@ -1,91 +1,91 @@
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "SaveFile.hpp"
|
||||
#include "util/string.hpp"
|
||||
|
||||
#define SAVE_KEY_TIMESTAMP "meta.timestamp"
|
||||
#define SAVE_KEY_SLOT "meta.slot"
|
||||
#define SAVE_MANAGER_BUFFER_LENGTH 128
|
||||
#define SAVE_MANAGER_FILENAME_LENGTH 512
|
||||
|
||||
namespace Dawn {
|
||||
class DawnGame;
|
||||
|
||||
enum SaveLoadResult {
|
||||
SAVE_LOAD_SUCCESS,
|
||||
SAVE_LOAD_RESULT_FILE_NOT_PRESENT,
|
||||
SAVE_LOAD_RESULT_TOO_SMALL,
|
||||
SAVE_LOAD_RESULT_CORRUPTED_DE_SAVE,
|
||||
SAVE_LOAD_RESULT_CORRUPTED_VERSION,
|
||||
SAVE_LOAD_RESULT_CORRUPTED_TIMESTAMP,
|
||||
SAVE_LOAD_RESULT_CORRUPTED_DATA_KEY,
|
||||
SAVE_LOAD_RESULT_CORRUPTED_DATA_VALUE,
|
||||
SAVE_LOAD_RESULT_MISMATCH_TIMESTAMP,
|
||||
SAVE_LOAD_RESULT_ERROR
|
||||
};
|
||||
|
||||
class SaveManager {
|
||||
protected:
|
||||
int16_t currentSaveSlot = -1;
|
||||
struct SaveFile currentSave;
|
||||
|
||||
/**
|
||||
* Reads a save file and then validates all of the values. If a value is
|
||||
* not validated then it will NOT be retained.
|
||||
*
|
||||
* @param file Save file to read from.
|
||||
* @return Whether or not the save file is corrupt, false for not corrupt.
|
||||
*/
|
||||
virtual bool_t validateSave(struct SaveFile raw) = 0;
|
||||
|
||||
public:
|
||||
DawnGame *game;
|
||||
|
||||
/**
|
||||
* Create a new Save Manager. Used by the game instance to decide when/how
|
||||
* to perform saving.
|
||||
*
|
||||
* @param game Game that this save manager belongs to.
|
||||
*/
|
||||
SaveManager(DawnGame *game);
|
||||
|
||||
/**
|
||||
* Immediately save the game to the given file. This will invoke the
|
||||
* game's custom writer to write the output data to the save file.
|
||||
*/
|
||||
void saveFile();
|
||||
|
||||
/**
|
||||
* Loads the current slotted save file. Invokes the internal managers read
|
||||
* function.
|
||||
*/
|
||||
enum SaveLoadResult loadFile();
|
||||
|
||||
/**
|
||||
* Deletes the given save file slot. We use slots here because we need to
|
||||
* be able to delete a file without reading it (in case of corruption).
|
||||
*
|
||||
* @param slot Slot to delete.
|
||||
*/
|
||||
void deleteFile(int16_t slot);
|
||||
|
||||
/**
|
||||
* Determines which save slot to use.
|
||||
*
|
||||
* @param slot Save slot to use.
|
||||
*/
|
||||
void useSlot(int16_t slot);
|
||||
|
||||
/**
|
||||
* Returns a list of used save slots, does not confirm if they are corrupt
|
||||
* or not, just whether they are in existance or not.
|
||||
*
|
||||
* @return List of used save slots.
|
||||
*/
|
||||
std::vector<int16_t> getUsedSlots();
|
||||
};
|
||||
// Copyright (c) 2022 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "SaveFile.hpp"
|
||||
#include "util/string.hpp"
|
||||
|
||||
#define SAVE_KEY_TIMESTAMP "meta.timestamp"
|
||||
#define SAVE_KEY_SLOT "meta.slot"
|
||||
#define SAVE_MANAGER_BUFFER_LENGTH 128
|
||||
#define SAVE_MANAGER_FILENAME_LENGTH 512
|
||||
|
||||
namespace Dawn {
|
||||
class DawnGame;
|
||||
|
||||
enum SaveLoadResult {
|
||||
SAVE_LOAD_SUCCESS,
|
||||
SAVE_LOAD_RESULT_FILE_NOT_PRESENT,
|
||||
SAVE_LOAD_RESULT_TOO_SMALL,
|
||||
SAVE_LOAD_RESULT_CORRUPTED_DE_SAVE,
|
||||
SAVE_LOAD_RESULT_CORRUPTED_VERSION,
|
||||
SAVE_LOAD_RESULT_CORRUPTED_TIMESTAMP,
|
||||
SAVE_LOAD_RESULT_CORRUPTED_DATA_KEY,
|
||||
SAVE_LOAD_RESULT_CORRUPTED_DATA_VALUE,
|
||||
SAVE_LOAD_RESULT_MISMATCH_TIMESTAMP,
|
||||
SAVE_LOAD_RESULT_ERROR
|
||||
};
|
||||
|
||||
/**
|
||||
* Reads a save file and then validates all of the values. If a value is
|
||||
* not validated then it will NOT be retained.
|
||||
*
|
||||
* @param file Save file to read from.
|
||||
* @return Whether or not the save file is corrupt, false for not corrupt.
|
||||
*/
|
||||
bool_t saveValidateFile(struct SaveFile raw);
|
||||
|
||||
class SaveManager {
|
||||
protected:
|
||||
int16_t currentSaveSlot = -1;
|
||||
struct SaveFile currentSave;
|
||||
|
||||
public:
|
||||
DawnGame *game;
|
||||
|
||||
/**
|
||||
* Create a new Save Manager. Used by the game instance to decide when/how
|
||||
* to perform saving.
|
||||
*
|
||||
* @param game Game that this save manager belongs to.
|
||||
*/
|
||||
SaveManager(DawnGame *game);
|
||||
|
||||
/**
|
||||
* Immediately save the game to the given file. This will invoke the
|
||||
* game's custom writer to write the output data to the save file.
|
||||
*/
|
||||
void saveFile();
|
||||
|
||||
/**
|
||||
* Loads the current slotted save file. Invokes the internal managers read
|
||||
* function.
|
||||
*/
|
||||
enum SaveLoadResult loadFile();
|
||||
|
||||
/**
|
||||
* Deletes the given save file slot. We use slots here because we need to
|
||||
* be able to delete a file without reading it (in case of corruption).
|
||||
*
|
||||
* @param slot Slot to delete.
|
||||
*/
|
||||
void deleteFile(int16_t slot);
|
||||
|
||||
/**
|
||||
* Determines which save slot to use.
|
||||
*
|
||||
* @param slot Save slot to use.
|
||||
*/
|
||||
void useSlot(int16_t slot);
|
||||
|
||||
/**
|
||||
* Returns a list of used save slots, does not confirm if they are corrupt
|
||||
* or not, just whether they are in existance or not.
|
||||
*
|
||||
* @return List of used save slots.
|
||||
*/
|
||||
std::vector<int16_t> getUsedSlots();
|
||||
};
|
||||
}
|
@ -9,7 +9,6 @@ target_sources(${DAWN_TARGET_NAME}
|
||||
Scene.cpp
|
||||
SceneItem.cpp
|
||||
SceneItemComponent.cpp
|
||||
SceneItemComponentList.cpp
|
||||
)
|
||||
|
||||
# Subdirs
|
||||
|
@ -1,8 +0,0 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "SceneItemComponentList.hpp"
|
||||
|
||||
using namespace Dawn;
|
@ -1,38 +0,0 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "dawnlibs.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
struct SceneItemComponentRegister {
|
||||
uint8_t i;
|
||||
};
|
||||
|
||||
class SceneItemComponentList {
|
||||
private:
|
||||
std::vector<struct SceneItemComponentRegister> components;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* Request the list to append a scene item component of type T
|
||||
* to the registry.
|
||||
*
|
||||
* @tparam T Parameter type of the SceneItemComponent
|
||||
*/
|
||||
template<class T>
|
||||
void append() {
|
||||
struct SceneItemComponentRegister r;
|
||||
this->components.push_back(r);
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
* Initialies the scene item component list. This constructor is generated
|
||||
* at build time from the scene item component registry tool.
|
||||
*/
|
||||
SceneItemComponentList();
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user