I am making a game. I have two source files, game.cpp and renderHandler.cpp. I have one header file, gameState.h. The gameState.h file contains a static instance of an enumeration that represents the different game states.
I want to share this static variable with the two source files. I don't want two separate variables in each source file. If I change the value of the game state variable I want it to transfer to the other source file.
gameState.h
#pragma once
enum State {
start,
play,
stop
} static gameState;
game.cpp
#include "../inc/gameState.h"
void Game::init()
{
gameState = State::play;
}
renderHandler.cpp
#include "../inc/gameState.h"
void RenderHandler::render()
{
if (gameState == State::start) {
// code
}
else if (gameState == State::play) {
// code
}
else if (gameState == State::stop) {
// code
}
}
The value of gameState is changed in the game.cpp file. But this does not affect the value of gameState in renderHandler.cpp, it defaults to 0, which I don't want. The value change happens before any of the rendering code is executed.
How do I share a static instance of an enumeration between two source files? Is my logic wrong and should I not use headers and enumerations this way?