Commit e7bf0466 authored by Konrad McClure's avatar Konrad McClure
Browse files

Added functionality to GameEngine and the testing gamestate Introstate, almost...

Added functionality to GameEngine and the testing gamestate Introstate, almost ready to test the engine and game flow
parent a10176eb
#include "gameengine.h"
#include "introstate.h"
int main()
{
CGameEngine game;
game.Init();
game.ChangeState(CIntroState::Instance());
while (isRunning())
{
game.HandleEvents();
game.Update();
game.Draw();
}
game.Cleanup();
return 0;
}
\ No newline at end of file
#include "gemeengine.h"
#include <iostream>
void Init()
{
cout << "Game Engine: Initializing" << endl;
m_running = true;
}
void Cleanup()
{
cout << "Game Engine: Cleanup" << endl;
// Cleanup the states
while (!states.empty())
{
states.back()->Cleanup();
states.pop_back();
}
}
// Operations with the gamestate stack
void ChangeState(CGameState* state)
{
cout << "Game Engine: Changing to state " << state->GetName() << endl;
// Pop and cleanup the current state
if (!states.empty())
{
states.back()->Cleanup();
states.pop_back();
}
// Push and init the new state
states.push_back(state);
states.back()->Init();
}
void PushState(CGameState* state)
{
cout << "Game Engine: Pausing " << states.back()->GetName() << endl;
// Pause the current state
if (!states.empty())
{
states.back()->Pause();
}
cout << "Game Engine: Pushing " << state->GetName() << endl;
// Push new state to stack and initialize it
states.push_back(state);
states.back()->Init();
}
void PopState()
{
cout << "Game Engine: Popping " << states.back()->GetName() << endl;
// Pause the current state
if (!states.empty())
{
states.back()->CleanUp();
states.pop_back();
}
// Check if there was a paused state and if so, resume
if (!states.empty())
{
cout << "Game Engine: Resuming " << states.back()->GetName() << endl;
states.back()->Resume();
}
}
// Primary Loop
// Send game engine to the state to handle events
void HandleEvents()
{
states.back()->HandleEvents(this);
}
// Send game engine to update the game
void Update()
{
states.back()->Update(this);
}
// Send game engine to draw to the console
void Draw()
{
states.back()->Draw(this);
}
\ No newline at end of file
......@@ -27,6 +27,8 @@ class CGameState
// Display result to screen
virtual void Draw() = 0;
string GetName() { return stateName; }
// States will change to other states on their own when exiting
// This way the game engine doesn't need to know when to change
void ChangeState(CGameEngine* game, CGameSatate* state)
......@@ -36,5 +38,9 @@ class CGameState
// Singleton Pattern, protecting the gamestate constructor makes sure there's
// only ever one copy of a gviven state
protected: CGameState() {}
protected:
CGameState() {}
private:
string stateName;
};
\ No newline at end of file
......@@ -6,6 +6,16 @@ void CIntroState::Init()
cout << "IntroState Init" << endl;
}
void CIntroState::Pause()
{
cout << "InstroState Pause" << endl;
}
void CIntroState::Resume()
{
cout << "IntroState Resume" << endl;
}
void CIntroState::Cleanup()
{
cout << "IntroState Cleanup" << endl;
......@@ -19,4 +29,10 @@ void CIntroState::HandleEvents()
void CIntroState::Update()
{
cout << "IntroState Update" << endl;
}
\ No newline at end of file
}
void CIntroState::Draw()
{
cout << "IntroState Draw" << endl;
}
......@@ -2,6 +2,7 @@
#define INTROSTATE_H
#include "gamestate.h"
#include <iostream>
class CIntroState : public CGameState
{
......@@ -23,6 +24,7 @@ class CIntroState : public CGameState
private:
static CIntroState m_Inrostate;
string stateName = "IntroState";
};
#endif
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment