Commit e4410d1b authored by Konrad McClure's avatar Konrad McClure

Got the GameEngine, GameState, and TestState classes working and running in...

Got the GameEngine, GameState, and TestState classes working and running in enginetest.cpp. Goes through three game loops and then closes. Will add another state next
parent 45bf4c05
#include "gemeengine.h"
#include "GameEngine.h"
#include "GameState.h"
#include <iostream>
void Init()
void CGameEngine::Init()
{
cout << "Game Engine: Initializing" << endl;
m_running = true;
}
void Cleanup()
void CGameEngine::Cleanup()
{
cout << "Game Engine: Cleanup" << endl;
......@@ -22,7 +22,7 @@ void Cleanup()
}
// Operations with the gamestate stack
void ChangeState(CGameState* state)
void CGameEngine::ChangeState(CGameState* state)
{
cout << "Game Engine: Changing to state " << state->GetName() << endl;
......@@ -38,7 +38,7 @@ void ChangeState(CGameState* state)
states.back()->Init();
}
void PushState(CGameState* state)
void CGameEngine::PushState(CGameState* state)
{
cout << "Game Engine: Pausing " << states.back()->GetName() << endl;
......@@ -55,14 +55,14 @@ void PushState(CGameState* state)
states.back()->Init();
}
void PopState()
void CGameEngine::PopState()
{
cout << "Game Engine: Popping " << states.back()->GetName() << endl;
// Pause the current state
if (!states.empty())
{
states.back()->CleanUp();
states.back()->Cleanup();
states.pop_back();
}
......@@ -76,19 +76,19 @@ void PopState()
// Primary Loop
// Send game engine to the state to handle events
void HandleEvents()
void CGameEngine::HandleEvents()
{
states.back()->HandleEvents(this);
}
// Send game engine to update the game
void Update()
void CGameEngine::Update()
{
states.back()->Update(this);
}
// Send game engine to draw to the console
void Draw()
void CGameEngine::Draw()
{
states.back()->Draw(this);
}
\ No newline at end of file
#ifndef GAMEENGINE_H
#define GAMEENGINE_H
#include "../GameStateManager/gamestate.h"
#include <vector>
using namespace std;
class CGameState;
class CGameEngine
{
......@@ -31,6 +33,6 @@ class CGameEngine
// Currently running bool (false will initiate cleanup)
bool m_running;
}
};
#endif
\ No newline at end of file
#include "../GameEngine/gameengine.h"
#ifndef GAMESTATE_H
#define GAMESTATE_H
#include "GameEngine.h"
#include <string>
using namespace std;
class CGameState;
// Parent gamestate class gamestates will inherit from
class CGameState
......@@ -21,17 +28,17 @@ class CGameState
virtual void Resume() = 0;
// Process input
virtual void HandleEvents() = 0;
virtual void HandleEvents(CGameEngine* game) = 0;
// React to input
virtual void Update() = 0;
virtual void Update(CGameEngine* game) = 0;
// Display result to screen
virtual void Draw() = 0;
virtual void Draw(CGameEngine* game) = 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)
void ChangeState(CGameEngine* game, CGameState* state)
{
game->ChangeState(state);
}
......@@ -43,4 +50,6 @@ class CGameState
private:
string stateName;
};
\ No newline at end of file
};
#endif
\ No newline at end of file
#include <iostream>
#include "TestState.h"
CTestState CTestState::m_TestState;
void CTestState::Init()
{
cout << " TestState Init" << endl;
}
void CTestState::Pause()
{
cout << " TestState Pause" << endl;
}
void CTestState::Resume()
{
cout << " TestState Resume" << endl;
}
void CTestState::Cleanup()
{
cout << " TestState Cleanup" << endl;
}
void CTestState::HandleEvents(CGameEngine* game)
{
cout << " TestState Handle Events" << endl;
runtimes++;
}
void CTestState::Update(CGameEngine* game)
{
cout << " TestState Update" << endl;
if (runtimes >= 3)
{
game->Quit();
}
}
void CTestState::Draw(CGameEngine* game)
{
cout << " TestState Draw" << endl;
}
#ifndef INTROSTATE_H
#define INTROSTATE_H
#ifndef TESTSTATE_H
#define TESTSTATE_H
#include "gamestate.h"
#include "GameState.h"
#include <iostream>
class CIntroState : public CGameState
class CTestState : public CGameState
{
public:
void Init();
......@@ -17,14 +17,15 @@ class CIntroState : public CGameState
void Update(CGameEngine* game);
void Draw(CGameEngine* game);
static CIntroState* Instance() { return &m_Inrostate; }
static CTestState* Instance() { return &m_TestState; }
protected:
CIntroState() {}
CTestState() {}
private:
static CIntroState m_Inrostate;
string stateName = "IntroState";
static CTestState m_TestState;
string stateName = "TestState";
int runtimes = 0;
};
#endif
\ No newline at end of file
#include "gameengine.h"
#include "introstate.h"
#include "GameEngine.h"
#include "TestState.h"
int main()
{
......@@ -7,9 +7,9 @@ int main()
game.Init();
game.ChangeState(CIntroState::Instance());
game.ChangeState(CTestState::Instance());
while (isRunning())
while (game.isRunning())
{
game.HandleEvents();
game.Update();
......
#include <iostream>
#include "introstate.h"
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;
}
void CIntroState::HandleEvents()
{
cout << "IntroState Handle Events" << endl;
}
void CIntroState::Update()
{
cout << "IntroState Update" << endl;
}
void CIntroState::Draw()
{
cout << "IntroState Draw" << endl;
}
Markdown is supported
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