Rewrote Gamestate to be a class instead of an interface; Added layers

This commit is contained in:
Grayson Riffe (Laptop) 2021-08-24 15:03:55 -05:00
parent 2f4e0ebfac
commit e35688373e
15 changed files with 155 additions and 70 deletions

View File

@ -9,7 +9,7 @@ int main(int argc, char* argv[]) {
//app.setWindowIcon(...);
// app.setWindowCursor(...);
MainState* test = new MainState;
MainState* test = new MainState(&app);
app.addState(test, "Main State");
app.addDefaultState("Main State");

View File

@ -1,18 +1,22 @@
#include "MainState.h"
void MainState::onEnter(Application* app) {
MainState::MainState(Application* app) :
Gamestate(app)
{
}
void MainState::onEnter() {
Log("MainState onEnter!");
m_app = app;
}
void MainState::update(double deltaTime) {
}
void MainState::render() {
}
void MainState::onExit() {
Log("MainState onExit!");
}
void MainState::update() {
Log("MainState update!");
}
void MainState::render() {
Log("MainState render!");
}

View File

@ -1,13 +1,16 @@
#pragma once
#include "NothinFancy.h"
class MainState : public nf::IGamestate {
class MainState : public nf::Gamestate {
public:
void onEnter(Application* app) override;
void onExit() override;
MainState(Application* app);
void update() override;
void onEnter() override;
void update(double deltaTime) override;
void render() override;
void onExit() override;
private:
};

View File

@ -192,6 +192,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="src\Application.cpp" />
<ClCompile Include="src\Gamestate.cpp" />
<ClCompile Include="src\IntroGamestate.cpp" />
<ClCompile Include="src\Renderer\Drawable.cpp" />
<ClCompile Include="src\Renderer\IndexBuffer.cpp" />
@ -205,7 +206,7 @@
<ClInclude Include="src\include\Application.h" />
<ClInclude Include="src\include\Config.h" />
<ClInclude Include="src\include\Drawable.h" />
<ClInclude Include="src\include\IGamestate.h" />
<ClInclude Include="src\include\Gamestate.h" />
<ClInclude Include="src\include\IndexBuffer.h" />
<ClInclude Include="src\include\IntroGamestate.h" />
<ClInclude Include="src\include\Input.h" />

View File

@ -42,6 +42,9 @@
<ClCompile Include="src\Renderer\Drawable.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="src\Gamestate.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\include\Config.h">
@ -56,7 +59,7 @@
<ClInclude Include="src\include\Application.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\include\IGamestate.h">
<ClInclude Include="src\include\Gamestate.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="src\include\IntroGamestate.h">

View File

@ -40,7 +40,11 @@ namespace nf {
SetClassLongPtr(m_window, GCLP_HCURSOR, (LONG_PTR)hCursor);
}
void Application::addState(IGamestate* state,const std::string& stateName) {
Renderer* Application::getRenderer() const {
return m_renderer;
}
void Application::addState(Gamestate* state,const std::string& stateName) {
if (m_states.find(stateName) == m_states.end()) {
m_states[stateName] = state;
}
@ -188,9 +192,10 @@ namespace nf {
std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();
const std::chrono::duration<double> wait_time = std::chrono::nanoseconds(1000000000 / 60);
auto next_time = start_time + wait_time;
m_deltaTime = 0.0167;
while (m_running) {
start_time = std::chrono::steady_clock::now();
m_currentState->update();
m_currentState->update(m_deltaTime);
m_currentState->render();
m_renderer->doFrame();
m_frames++;
@ -214,8 +219,8 @@ namespace nf {
}
void Application::startIntroState() {
m_sIntro = new IntroGamestate;
m_sIntro->onEnter(this);
m_sIntro = new IntroGamestate(this);
m_sIntro->onEnter();
m_currentState = m_sIntro;
}
@ -223,7 +228,7 @@ namespace nf {
m_stateChange = false;
m_currentState->onExit();
m_currentState = m_states[m_nextState];
m_currentState->onEnter(this);
m_currentState->onEnter();
}
LRESULT CALLBACK Application::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
@ -297,8 +302,8 @@ namespace nf {
Application::~Application() {
Log("Exiting NF application");
for (std::pair<std::string, IGamestate*> state : m_states) {
IGamestate* curr = state.second;
for (std::pair<std::string, Gamestate*> state : m_states) {
Gamestate* curr = state.second;
delete curr;
}
}

View File

@ -0,0 +1,27 @@
#include "Gamestate.h"
#include "Application.h"
#include "Utility.h"
namespace nf {
Gamestate::Gamestate(Application* app) {
m_app = app;
m_renderer = m_app->getRenderer();
}
void Gamestate::onEnter() {
}
void Gamestate::update(double deltaTime) {
}
void Gamestate::render() {
}
void Gamestate::onExit() {
}
}

View File

@ -4,25 +4,29 @@
#include "Utility.h"
namespace nf {
void IntroGamestate::onEnter(Application* app) {
IntroGamestate::IntroGamestate(Application* app) :
Gamestate(app),
m_counter(0)
{
}
void IntroGamestate::onEnter() {
Log("Intro onEnter!");
m_app = app;
counter = 0;
m_counter = 0;
}
void IntroGamestate::update(double deltaTime) {
if (m_counter >= 120) {
m_app->changeState("Main State");
}
m_counter++;
}
void IntroGamestate::render() {
}
void IntroGamestate::onExit() {
Log("Intro onExit!");
}
void IntroGamestate::update() {
Log("Intro update!");
if (counter >= 120) {
m_app->changeState("Main State");
}
counter++;
}
void IntroGamestate::render() {
Log("Intro render!");
}
}

View File

@ -46,13 +46,31 @@ namespace nf {
wglMakeCurrent(m_hdc, m_hglrc);
wglSwapIntervalEXT(0);
Log("OpenGL version: " + std::string((char*)glGetString(GL_VERSION)));
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}
void Renderer::render(const Drawable& in) {
//TODO: Check identity
}
void Renderer::doFrame() {
glViewport(0, 0, m_app->getConfig().width, m_app->getConfig().height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
for (Drawable* draw : m_lGame) {
Drawable& curr = *draw;
}
for (Drawable* draw : m_lUI) {
Drawable& curr = *draw;
}
SwapBuffers(m_hdc);
GLenum err = glGetError();

View File

@ -17,7 +17,8 @@ namespace nf {
void setWindowIcon(HANDLE hIcon);
void setWindowCursor(HCURSOR hCursor);
void addState(IGamestate* state, const std::string& stateName);
Renderer* getRenderer() const;
void addState(Gamestate* state, const std::string& stateName);
void addDefaultState(const std::string& stateName);
void run();
void changeState(const std::string& stateName);
@ -59,11 +60,11 @@ namespace nf {
//Inactive states to potentially be active during the Application's lifetime
//Mapped to const char* to be referenced later in the frontend
std::unordered_map<std::string, IGamestate*> m_states;
IntroGamestate* m_sIntro;
IGamestate* m_DefaultState;
std::unordered_map<std::string, Gamestate*> m_states;
Gamestate* m_sIntro;
Gamestate* m_DefaultState;
bool m_defaultStateAdded;
IGamestate* m_currentState;
Gamestate* m_currentState;
bool m_stateChange;
std::string m_nextState;

View File

@ -1,12 +1,20 @@
#pragma once
#include "VertexArray.h"
#include "Shader.h"
#include "IndexBuffer.h"
namespace nf {
class Drawable {
enum class DrawableType {
NF_GAME, NF_UI
};
public:
Drawable();
~Drawable();
private:
virtual DrawableType identity();
~Drawable();
protected:
//TODO: Add VAO, Shader, index buffer, etc.
};
}

View File

@ -0,0 +1,24 @@
#pragma once
namespace nf {
class Application;
class Renderer;
class Gamestate {
public:
Gamestate(Application* app);
Gamestate() = delete;
Gamestate(const Gamestate& other) = delete;
virtual void onEnter();
virtual void update(double deltaTime);
virtual void render();
virtual void onExit();
protected:
Application* m_app;
Renderer* m_renderer;
//Resource identifier?
};
}

View File

@ -1,18 +0,0 @@
#pragma once
namespace nf {
class Application;
class IGamestate {
public:
virtual void onEnter(Application* app) = 0;
virtual void onExit() = 0;
virtual void update() = 0;
virtual void render() = 0;
Application* m_app;
private:
//Resource identifier?
};
}

View File

@ -1,16 +1,19 @@
#pragma once
#include "IGamestate.h"
#include "Gamestate.h"
namespace nf {
class IntroGamestate : public IGamestate {
class IntroGamestate : public Gamestate {
public:
void onEnter(Application* app) override;
void onExit() override;
IntroGamestate(Application* app);
void update() override;
void onEnter() override;
void update(double deltaTime) override;
void render() override;
void onExit() override;
private:
int counter;
int m_counter;
//TODO: Flesh out intro gamestate
};
}

View File

@ -11,6 +11,8 @@ namespace nf {
public:
Renderer(Application* app);
void render(const Drawable& in);
void doFrame();
~Renderer();