//Game.h
#pragma once
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
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;
};
#include "Game.h"
Game::GameState Game::_gameState = Uninitialized;
sf::RenderWindow Game::_mainWindow;
void Game::Start()
{
if (_gameState != Uninitialized)
return;
_mainWindow.create(sf::VideoMode(1024, 768,32), "Pang!");
_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,0,0));
_mainWindow.display();
if(currentEvent.type == sf::Event::Closed)
{
_gameState = Game::Exiting;
}
break;
}
}
}
}
I have error in this line: sf::RenderWindow Game::_mainWindow;.
How I can fix it?
Sorry for my English) it is not my native English)
So is this code below wrong when using SFML? I use this to share textures between different types of entities in my game and to load them only once at the start of the game for each type of entity.
class Monster : public Thing
{
private:
static std::vector<sf::Texture> textures;
public:
static bool loadTextures();
}
Short answer: Yes
Longer answer: There are two factors:
The issue specifically with SFML is, that SFML itself initializes stuff globally on its own and since the order of destruction in the global scope is undefined, one will run into crashes when the random order doesn't line up as imagined. So one has to make sure, that everything gets instantiated and destroyed in a local scope.
The other factor is a discussion on code design and this has comes always with some controversy. For me and many other people with orientation for modern design, global variables, which include static class member, are in most cases a bad design.