Fixed issue with double initalization

This commit is contained in:
Timothy O'Barr
2026-08-28 14:54:45 -05:00
parent 3e50d46284
commit ef54fa6609
3 changed files with 23 additions and 9 deletions
+15 -3
View File
@@ -11,7 +11,6 @@
static int curX, curY; static int curX, curY;
extern bool isActive;
extern std::vector<std::vector<Room>> rooms; extern std::vector<std::vector<Room>> rooms;
#define MAP_HEIGHT 5 #define MAP_HEIGHT 5
@@ -26,6 +25,8 @@ private:
nlohmann::json json; nlohmann::json json;
fs::path filePath = "Resources/Rooms.json"; fs::path filePath = "Resources/Rooms.json";
std::ifstream stream; std::ifstream stream;
bool isActive;
inline static Game *game_; inline static Game *game_;
@@ -36,7 +37,7 @@ private:
public: public:
Game(); Game();
static bool GetIsActive() { return isActive; } inline bool GetIsActive() const { return isActive; }
bool Init(); bool Init();
void Run(); void Run();
bool Save(); bool Save();
@@ -44,7 +45,18 @@ public:
void ChangeRoom(Room r); void ChangeRoom(Room r);
Room GetCurrentRoom() { return currentRoom; } Room GetCurrentRoom() { return currentRoom; }
static void SetIsActive(const bool active) { isActive = active; } inline bool operator!() const {
if (isActive) {
return false;
}
return true;
}
void SetIsActive(const bool active) { isActive = active; }
static void SetGameInstance(Game * g) {
game_ = g;
}
static Game &Instance() { static Game &Instance() {
if (game_ == nullptr) { if (game_ == nullptr) {
+2 -4
View File
@@ -3,15 +3,13 @@
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
bool isActive;
std::vector<std::vector<Room>> rooms; std::vector<std::vector<Room>> rooms;
Game::Game() { Game::Game() {
curX = 0; curX = 0;
curY = 0; curY = 0;
isActive = false;
progress = 0; progress = 0;
Init();
} }
bool Game::Init() { bool Game::Init() {
@@ -72,7 +70,7 @@ void Game::ChangeRoom(Room r) {
} }
int Quit(const std::string &n) { int Quit(const std::string &n) {
isActive = false; Game::Instance().SetIsActive(false);
return 0; return 0;
} }
+5 -1
View File
@@ -3,7 +3,11 @@
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
Game game; // Calls default constructor which Initializes GAME Game game; // Calls default constructor which Initializes GAME
while (Game::GetIsActive()) { if (!game) {
game.Init();
Game::SetGameInstance(&game);
}
while (game.GetIsActive()) {
game.Run(); game.Run();
} }