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

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> #include <iostream>
void CGameEngine::Init()
void Init()
{ {
cout << "Game Engine: Initializing" << endl; cout << "Game Engine: Initializing" << endl;
m_running = true; m_running = true;
} }
void Cleanup() void CGameEngine::Cleanup()
{ {
cout << "Game Engine: Cleanup" << endl; cout << "Game Engine: Cleanup" << endl;
...@@ -22,7 +22,7 @@ void Cleanup() ...@@ -22,7 +22,7 @@ void Cleanup()
} }
// Operations with the gamestate stack // Operations with the gamestate stack
void ChangeState(CGameState* state) void CGameEngine::ChangeState(CGameState* state)
{ {
cout << "Game Engine: Changing to state " << state->GetName() << endl; cout << "Game Engine: Changing to state " << state->GetName() << endl;
...@@ -38,7 +38,7 @@ void ChangeState(CGameState* state) ...@@ -38,7 +38,7 @@ void ChangeState(CGameState* state)
states.back()->Init(); states.back()->Init();
} }
void PushState(CGameState* state) void CGameEngine::PushState(CGameState* state)
{ {
cout << "Game Engine: Pausing " << states.back()->GetName() << endl; cout << "Game Engine: Pausing " << states.back()->GetName() << endl;
...@@ -55,14 +55,14 @@ void PushState(CGameState* state) ...@@ -55,14 +55,14 @@ void PushState(CGameState* state)
states.back()->Init(); states.back()->Init();
} }
void PopState() void CGameEngine::PopState()
{ {
cout << "Game Engine: Popping " << states.back()->GetName() << endl; cout << "Game Engine: Popping " << states.back()->GetName() << endl;
// Pause the current state // Pause the current state
if (!states.empty()) if (!states.empty())
{ {
states.back()->CleanUp(); states.back()->Cleanup();
states.pop_back(); states.pop_back();
} }
...@@ -76,19 +76,19 @@ void PopState() ...@@ -76,19 +76,19 @@ void PopState()
// Primary Loop // Primary Loop
// Send game engine to the state to handle events // Send game engine to the state to handle events
void HandleEvents() void CGameEngine::HandleEvents()
{ {
states.back()->HandleEvents(this); states.back()->HandleEvents(this);
} }
// Send game engine to update the game // Send game engine to update the game
void Update() void CGameEngine::Update()
{ {
states.back()->Update(this); states.back()->Update(this);
} }
// Send game engine to draw to the console // Send game engine to draw to the console
void Draw() void CGameEngine::Draw()
{ {
states.back()->Draw(this); states.back()->Draw(this);
} }
\ No newline at end of file
#ifndef GAMEENGINE_H #ifndef GAMEENGINE_H
#define GAMEENGINE_H #define GAMEENGINE_H
#include "../GameStateManager/gamestate.h"
#include <vector> #include <vector>
using namespace std;
class CGameState;
class CGameEngine class CGameEngine
{ {
...@@ -31,6 +33,6 @@ class CGameEngine ...@@ -31,6 +33,6 @@ class CGameEngine
// Currently running bool (false will initiate cleanup) // Currently running bool (false will initiate cleanup)
bool m_running; bool m_running;
} };
#endif #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 // Parent gamestate class gamestates will inherit from
class CGameState class CGameState
...@@ -21,17 +28,17 @@ class CGameState ...@@ -21,17 +28,17 @@ class CGameState
virtual void Resume() = 0; virtual void Resume() = 0;
// Process input // Process input
virtual void HandleEvents() = 0; virtual void HandleEvents(CGameEngine* game) = 0;
// React to input // React to input
virtual void Update() = 0; virtual void Update(CGameEngine* game) = 0;
// Display result to screen // Display result to screen
virtual void Draw() = 0; virtual void Draw(CGameEngine* game) = 0;
string GetName() { return stateName; } string GetName() { return stateName; }
// States will change to other states on their own when exiting // States will change to other states on their own when exiting
// This way the game engine doesn't need to know when to change // 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); game->ChangeState(state);
} }
...@@ -43,4 +50,6 @@ class CGameState ...@@ -43,4 +50,6 @@ class CGameState
private: private:
string stateName; 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 #ifndef TESTSTATE_H
#define INTROSTATE_H #define TESTSTATE_H
#include "gamestate.h" #include "GameState.h"
#include <iostream> #include <iostream>
class CIntroState : public CGameState class CTestState : public CGameState
{ {
public: public:
void Init(); void Init();
...@@ -17,14 +17,15 @@ class CIntroState : public CGameState ...@@ -17,14 +17,15 @@ class CIntroState : public CGameState
void Update(CGameEngine* game); void Update(CGameEngine* game);
void Draw(CGameEngine* game); void Draw(CGameEngine* game);
static CIntroState* Instance() { return &m_Inrostate; } static CTestState* Instance() { return &m_TestState; }
protected: protected:
CIntroState() {} CTestState() {}
private: private:
static CIntroState m_Inrostate; static CTestState m_TestState;
string stateName = "IntroState"; string stateName = "TestState";
int runtimes = 0;
}; };
#endif #endif
\ No newline at end of file
#include "gameengine.h" #include "GameEngine.h"
#include "introstate.h" #include "TestState.h"
int main() int main()
{ {
...@@ -7,9 +7,9 @@ int main() ...@@ -7,9 +7,9 @@ int main()
game.Init(); game.Init();
game.ChangeState(CIntroState::Instance()); game.ChangeState(CTestState::Instance());
while (isRunning()) while (game.isRunning())
{ {
game.HandleEvents(); game.HandleEvents();
game.Update(); 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;
}
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