所以我想制作一款代码非常简洁且组织良好的游戏。调查游戏状态我发现了这个网站: http: //gamedevgeek.com/tutorials/managing-game-states-in-c/
使用他的模板,到目前为止我有这个:
游戏状态.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
然后每次我尝试实现这些功能时都会出现错误“使用未声明的标识符'TitleScreenState'”。它也不是自动完成“TitleScreenState”。有什么建议么?
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);
}