0
votes

I am new to c++, and i have difficulties understannding some of it's parts, especially in memory management. This is one of my first projects, an SFML lib game. So i get an access violation error, in this code:

include "Game.h"

void Game::Start()
{
    if (_gameState == Uninitialized)
        return;
    _mainWindow.create(sf::VideoMode(1024, 768, 32), "Hi!");
    _gameState = Game::Playing;

    while (!IsExiting()){
        GameLoop();
    }

    _mainWindow.close();
}

bool Game::IsExiting(){
    if (_gameState == Game::Exiting)
    {
        return true;
    }
    else 
    {
        return false;
    }
}

void Game::GameLoop(){
    sf::Event currentEvent;
    while (_mainWindow.pollEvent(currentEvent))
        {
            switch (_gameState)
            {
            case Game::Playing:
                {
                    _mainWindow.clear(sf::Color(255, 255, 255));
                    _mainWindow.display();

                    if (currentEvent.type == sf::Event::Closed)
                    {
                        _gameState = Game::Exiting;
                    }
                    break;
                }
            }
        }
}

Game::GameState Game::_gameState = Uninitialized;
sf::RenderWindow Game::_mainWindow;

The error happens, when code gets to the very last line of code, the _mainWIndow declaration. I understand, that problem most probably happened because i do not completely understand the quirks of static. But i still dont yet understand what causes the problem, and how to solve it.

The Game class:

class Game
{
public:
    static void Start();
private:
    static bool IsExiting();
    static void GameLoop();

    enum GameState {Uninitialized, ShowingSplash, Paused, ShowingMenu, Playing, Exiting};

    static GameState _gameState;
    static sf::RenderWindow _mainWindow;
};
1
Might be related to the Static Initialization Order Fiasco. Depends on what SFML library requirements are. If it's using statics internally, then this naive approach with statics would be a really bad idea.paddy
why are all of functions static? By making them static, you don't create an instance of Game class to call the functions, for example this is how you should call Game::IsExistiting();Krypton
My guess is that the OP already knows Java.paddy
@Krypton I am mostly consulting the gamefromscratch guide on SFML. I have planned to consult only the general direction, but ultimately write my own game, however, as errors started popping out, i have switch to the guide code (that still seem not working). The reasons to static in the guide are like that: it's essentially global, it's clean, there's only one instance of this object, and it shouldn break anything.Gwiny

1 Answers

0
votes

_gameState and _mainWindow are private members of the Game class, so that is probably the cause of your access errors.

Either you need to create a static constructor method for the Game class and place the problem lines of code there, or, perhaps you don't need the last two lines at all. I don't know how the sf:RenderWindow class is defined, but maybe that private declaration is all that is needed.

Your demo should probably have a line somewhere saying

Game::Start();