-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
109 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,4 @@ compile_commands.json | |
|
||
imgui.ini | ||
bave*.log | ||
/spaced |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#include <bave/persistor.hpp> | ||
#include <spaced/game/game_save.hpp> | ||
#include <array> | ||
#include <cstddef> | ||
#include <cstring> | ||
#include <span> | ||
|
||
namespace spaced { | ||
using bave::App; | ||
using bave::NotNull; | ||
using bave::Persistor; | ||
|
||
namespace { | ||
constexpr std::string_view uri_v{"spaced/game_save.bin"}; | ||
} // namespace | ||
|
||
GameSave::GameSave(NotNull<App const*> app) : m_app(app) { | ||
auto const persistor = Persistor{*m_app}; | ||
if (persistor.exists(uri_v)) { | ||
auto const blob_bytes = persistor.read_bytes(uri_v); | ||
std::memcpy(&m_blob, blob_bytes.data(), blob_bytes.size()); | ||
} | ||
} | ||
|
||
GameSave::~GameSave() { save(); } | ||
|
||
auto GameSave::save() -> bool { | ||
auto const persistor = Persistor{*m_app}; | ||
auto blob_bytes = std::array<std::byte, sizeof(Blob)>{}; | ||
std::memcpy(blob_bytes.data(), &m_blob, sizeof(Blob)); | ||
return persistor.write_bytes(uri_v, blob_bytes); | ||
} | ||
} // namespace spaced |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#pragma once | ||
#include <bave/core/not_null.hpp> | ||
#include <cstdint> | ||
|
||
namespace bave { | ||
class App; | ||
} | ||
|
||
namespace spaced { | ||
class GameSave { | ||
public: | ||
GameSave(GameSave const&) = delete; | ||
GameSave(GameSave&&) = delete; | ||
auto operator=(GameSave const&) = delete; | ||
auto operator=(GameSave&&) = delete; | ||
|
||
explicit GameSave(bave::NotNull<bave::App const*> app); | ||
~GameSave(); | ||
|
||
[[nodiscard]] auto get_hi_score() const -> std::int64_t { return m_blob.hi_score; } | ||
void set_hi_score(std::int64_t const hi_score) { m_blob.hi_score = hi_score; } | ||
|
||
auto save() -> bool; | ||
|
||
private: | ||
struct Blob { | ||
// struct must remain backwards compatible: | ||
// fields must NEVER be removed / reordered! | ||
// new fields can be added at the bottom. | ||
|
||
std::int64_t hi_score{}; | ||
}; | ||
|
||
bave::NotNull<bave::App const*> m_app; | ||
Blob m_blob{}; | ||
}; | ||
} // namespace spaced |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters