Game State Machine: Abstract
Nov 17, 2014 at 10:52pm UTC
Hi guys
I'm now trying to get Things working for my state manager. But I’m stuck again ^^. I think that a normal state machine with a switch(currentstate) is a bit too ridiculous to maintain after reaching a certain point.
Problem:
I need to get access to my “MainRenderer” of type SDL_Renderer* in my MenuState.cpp file. Stupid question again but I’m thinking about it for a few hours now…
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
GameState.hpp
#ifndef _GAMESTATE_HPP_
#define _GAMESTATE_HPP_
#include "GameEngine.hpp"
class CGameState
{
public :
virtual void Init(SDL_Renderer* pRenderer) = 0;
virtual void Cleanup() = 0;
virtual void Pause() = 0;
virtual void Resume() = 0;
virtual void HandleEvents(CGameEngine* game) = 0;
virtual void Update(CGameEngine* game) = 0;
virtual void Draw(CGameEngine* game) = 0;
void ChangeState(CGameEngine* game, CGameState* state) {game->ChangeState(state);
}
protected :
CGameState() { }
};
#endif //_GAMESTATE_HPP_
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
GameEngine.hpp
#ifndef _GAMEENGINE_HPP_
#define _GAMEENGINE_HPP_
#include "SDL.h"
#include <iostream>
#include <vector>
class CGameState;
class CGameEngine
{
public :
void Init();
void Cleanup();
void ChangeState(CGameState* state);
void PushState(CGameState* state);
void PopState();
void HandleEvents();
void Update();
void Draw();
bool Running() { return m_running; }
void Quit() { m_running = false ; }
SDL_Surface* MainWindowSurface;
SDL_Window* MainWindow;
SDL_Renderer* MainRenderer;
private :
// List of all states...
std::vector<CGameState*> states;
bool m_running;
bool m_fullscreen;
};
#endif //_GAMEENGINE_HPP_
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
MenuState.cpp
#include "GameEngine.hpp"
#include "GameState.hpp"
#include "MenuState.hpp"
CMenuState CMenuState::m_MenuState;
void CMenuState::Init()
{
SDL_Surface* temp = SDL_LoadBMP("data/imgs/menu.bmp" );
bg = SDL_CreateTextureFromSurface(RENDERER,temp); //<-- I need access to the MainRenderer from GameEngine.hpp
if (bg == NULL)
{
std::cout << SDL_GetError() << "\n" ;
}
SDL_FreeSurface(temp);
std::cout << "CMenuState Init\n" ;
}
Thanks :D
Topic archived. No new replies allowed.