So I wanted to make a game with really clean code and good organization. Looking into game states I found this site: http://gamedevgeek.com/tutorials/managing-game-states-in-c/
Using his templates, I have this so far:
GameState.hpp
#ifndef Rect_Game_GameState_hpp
#define Rect_Game_GameState_hpp
#include <SFML/Graphics.hpp>
#include "GameEngine.hpp"
class GameEngine;
class GameState {
public:
virtual void Init() = 0;
virtual void Cleanup() = 0;
virtual void Pause() = 0;
virtual void Resume() = 0;
virtual void HandleEvents(GameEngine* game) = 0;
virtual void Update(GameEngine* game) = 0;
virtual void Draw(GameEngine* game) = 0;
virtual void ChangeState(GameEngine* game, GameState* state);
private:
GameState() { }
};
#endif
TitleScreenState.hpp
#ifdef Rect_Game_TitleScreenState_hpp
#def Rect_Game_TitleScreenState_hpp
#include <iostream>
#include <SFML/Graphics.hpp>
#include "GameState.hpp"
#include "GameEngine.hpp"
class TitleScreenState : public GameState {
public:
void Init();
void Cleanup();
void Pause();
void Resume();
void HandleEvents(GameEngine* engine);
void Update(GameEngine* engine);
void Draw(GameEngine* engine);
void ChangeState(GameEngine* engine, GameState* state);
static TitleScreenState* Instance();
private:
TitleScreenState() {}
static TitleScreenState* titleScreenInstance;
sf::RenderWindow* window;
int mouseX;
int mouseY;
Button* playButton;
};
#endif
And then the error “Use of undeclared identifier ‘TitleScreenState'” appears every time I try to implement the functions. It’s not auto-completing “TitleScreenState” either. Any suggestions?
TitleScreenState.cpp
#include <iostream>
#include "GameEngine.hpp"
#include "GameState.hpp"
#include "TitleScreenState.hpp"
#include "ResourcePath.hpp"
void TitleScreenState::Init()
{
// Initialize values
leftClick = false;
mouseX = 0;
mouseY = 0;
// Load title screen image
sf::Texture titleImage;
if (!titleImage.loadFromFile(resourcePath() + "TitleScreen.png"))
printf("could not load TitleScreen.png");
sf::Sprite titleScreen;
titleScreen.setTexture(titleImage);
playButton = new Button("Play", 350, 220);
}
The problem here is TitleScreenState.hpp:
First, the
#ifdefprevents the compiler from seeing the rest of the file, becauseRect_Game_TitleScreenState_hpphasn’t been defined. Therefore, it should have been#ifndef. In addition, the#defshould have been#define. That should solve the problem.