From e8bc0903e699eb75a1b401643cf5cdce6dd55dd4 Mon Sep 17 00:00:00 2001 From: "Grayson Riffe (Laptop)" Date: Wed, 18 Aug 2021 13:54:06 -0500 Subject: [PATCH] Added state changing --- Game/Game.vcxproj | 4 +++ Game/Game.vcxproj.filters | 8 +++++ Game/src/Game.cpp | 6 ++++ Game/src/MainState.cpp | 17 +++++++++ Game/src/include/MainState.h | 13 +++++++ NothinFancy/src/Application.cpp | 46 +++++++++++++++++------- NothinFancy/src/IntroGamestate.cpp | 8 ++--- NothinFancy/src/include/Application.h | 12 +++++-- NothinFancy/src/include/IntroGamestate.h | 1 + NothinFancy/src/include/NothinFancy.h | 4 +-- NothinFancy/src/include/Utility.h | 4 +-- 11 files changed, 99 insertions(+), 24 deletions(-) create mode 100644 Game/src/MainState.cpp create mode 100644 Game/src/include/MainState.h diff --git a/Game/Game.vcxproj b/Game/Game.vcxproj index 3073f0f..622e1f0 100644 --- a/Game/Game.vcxproj +++ b/Game/Game.vcxproj @@ -173,6 +173,10 @@ + + + + diff --git a/Game/Game.vcxproj.filters b/Game/Game.vcxproj.filters index 08a7b41..204f178 100644 --- a/Game/Game.vcxproj.filters +++ b/Game/Game.vcxproj.filters @@ -18,5 +18,13 @@ Source Files + + Source Files + + + + + Header Files + \ No newline at end of file diff --git a/Game/src/Game.cpp b/Game/src/Game.cpp index 9c1e9a0..42d32c4 100644 --- a/Game/src/Game.cpp +++ b/Game/src/Game.cpp @@ -1,4 +1,5 @@ #include "NothinFancy.h" +#include "MainState.h" using namespace nf; @@ -12,6 +13,11 @@ int main(int argc, char* argv[]) { // app.setWindowCursor(...); //Configure states // app.addDefaultState(...); + + MainState* test = new MainState; + app.addState("Main State", test); + app.addDefaultState("Main State"); + app.run(); return 0; diff --git a/Game/src/MainState.cpp b/Game/src/MainState.cpp new file mode 100644 index 0000000..237f889 --- /dev/null +++ b/Game/src/MainState.cpp @@ -0,0 +1,17 @@ +#include "MainState.h" + +void MainState::onEnter() { + Log("MainState update!"); +} + +void MainState::onExit() { + Log("MainState update!"); +} + +void MainState::update() { + Log("MainState update!"); +} + +void MainState::render() { + Log("MainState render!"); +} \ No newline at end of file diff --git a/Game/src/include/MainState.h b/Game/src/include/MainState.h new file mode 100644 index 0000000..aa2b6d8 --- /dev/null +++ b/Game/src/include/MainState.h @@ -0,0 +1,13 @@ +#pragma once +#include "NothinFancy.h" + +class MainState : public nf::IGamestate { +public: + void onEnter() override; + void onExit() override; + + void update() override; + void render() override; +private: + +}; \ No newline at end of file diff --git a/NothinFancy/src/Application.cpp b/NothinFancy/src/Application.cpp index cad4030..523a0e7 100644 --- a/NothinFancy/src/Application.cpp +++ b/NothinFancy/src/Application.cpp @@ -1,5 +1,4 @@ #include "Application.h" -#include "Utility.h" #ifdef NFENGINE #include "GL\glew.h" #include "GL\wglew.h" @@ -28,8 +27,6 @@ namespace nf { createOpenGLContext(); m_states.reserve(100); m_activeStates.reserve(100); - m_sIntro = new IntroGamestate; - addState(m_sIntro); } void Application::setWindowIcon(HANDLE hIcon) { @@ -41,24 +38,39 @@ namespace nf { SetClassLongPtr(m_window, GCLP_HCURSOR, (LONG_PTR)hCursor); } - void Application::addState(IGamestate* in) { - (*in).onEnter(); - m_states.push_back(in); + void Application::addState(const char* stateName, IGamestate* state) { + m_states[stateName] = state; } - void Application::addDefaultState(IGamestate* in) { + void Application::addDefaultState(const char* stateName) { if (!m_defaultStateAdded) { - (*in).onEnter(); - m_activeStates.push_back(in); - m_defaultStateAdded = true; + if (m_states.find(stateName) != m_states.end()) { + m_DefaultState = m_states[stateName]; + m_defaultStateAdded = true; + } + else { + Error(("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!").c_str()); + } } else { Error("More than one default state defined"); //TODO: Test this } } + void Application::changeState(const char* stateName) { + if (m_states.find(stateName) != m_states.end()) { + m_currentState->onExit(); + m_currentState = m_states[stateName]; + } + else { + Error(("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!").c_str()); + } + } + void Application::run() { showWindow(true); + addIntroState(); + m_currentState = m_sIntro; m_running = true; MSG msg = { }; while (m_running) { @@ -70,13 +82,17 @@ namespace nf { DispatchMessage(&msg); if (msg.message == WM_QUIT) m_running = false; + goto FrameEnd; } glClear(GL_COLOR_BUFFER_BIT); + m_currentState->update(); + m_currentState->render(); SwapBuffers(m_hdc); m_frames++; m_frameClock = std::chrono::steady_clock::now(); //TODO: Update and render current state } + FrameEnd: m_fpsClock2 = std::chrono::steady_clock::now(); m_fpsDuration = m_fpsClock2 - m_fpsClock1; if (m_fpsDuration.count() >= 1.0) { @@ -102,6 +118,12 @@ namespace nf { return m_FPS; } + void Application::addIntroState() { + m_sIntro = new IntroGamestate; + m_sIntro->onEnter(); + m_activeStates.push_back(m_sIntro); + } + void Application::registerWindowClass() { if (!FindWindow(L"NFClass", NULL)) { m_wclassName = L"NFClass"; @@ -164,12 +186,12 @@ namespace nf { return MNC_CLOSE << 16; } case WM_CLOSE: { - //State onExit() in order - //unload anything else DestroyWindow(hWnd); return 0; } case WM_DESTROY: { + app->m_currentState->onExit(); + //Unload anything else PostQuitMessage(0); return 0; } diff --git a/NothinFancy/src/IntroGamestate.cpp b/NothinFancy/src/IntroGamestate.cpp index 12488c9..eb7ed47 100644 --- a/NothinFancy/src/IntroGamestate.cpp +++ b/NothinFancy/src/IntroGamestate.cpp @@ -2,18 +2,18 @@ namespace nf { void IntroGamestate::onEnter() { - + Log("Intro onEnter!"); } void IntroGamestate::onExit() { - + Log("Intro onExit!"); } void IntroGamestate::update() { - + Log("Intro update!"); } void IntroGamestate::render() { - + Log("Intro render!"); } } \ No newline at end of file diff --git a/NothinFancy/src/include/Application.h b/NothinFancy/src/include/Application.h index 5fdfc96..244dfd8 100644 --- a/NothinFancy/src/include/Application.h +++ b/NothinFancy/src/include/Application.h @@ -1,8 +1,10 @@ #pragma once #include "Config.h" +#include "Utility.h" #include "IntroGamestate.h" #include #include +#include #include namespace nf { @@ -14,8 +16,9 @@ namespace nf { void setWindowIcon(HANDLE hIcon); void setWindowCursor(HCURSOR hCursor); - void addState(IGamestate* in); - void addDefaultState(IGamestate* in); + void addState(const char* stateName, IGamestate* state); + void addDefaultState(const char* stateName); + void changeState(const char* stateName); void run(); void showWindow(bool show); const Config& getConfig() const; @@ -23,6 +26,7 @@ namespace nf { ~Application(); private: + void addIntroState(); void registerWindowClass(); void toggleFullscreen(); RECT getWindowRect() const; @@ -52,10 +56,12 @@ namespace nf { int m_FPS; //Inactive states states to potentially be loaded during the Application's lifetime - std::vector m_states; + std::unordered_map m_states; //The currently active and loaded states where the top-most is the current one std::vector m_activeStates; IntroGamestate* m_sIntro; + IGamestate* m_DefaultState; bool m_defaultStateAdded = false; + IGamestate* m_currentState; }; } \ No newline at end of file diff --git a/NothinFancy/src/include/IntroGamestate.h b/NothinFancy/src/include/IntroGamestate.h index bc7eef0..9c3926d 100644 --- a/NothinFancy/src/include/IntroGamestate.h +++ b/NothinFancy/src/include/IntroGamestate.h @@ -1,4 +1,5 @@ #include "IGamestate.h" +#include "Utility.h" namespace nf { class IntroGamestate : public IGamestate { diff --git a/NothinFancy/src/include/NothinFancy.h b/NothinFancy/src/include/NothinFancy.h index b32f466..406a481 100644 --- a/NothinFancy/src/include/NothinFancy.h +++ b/NothinFancy/src/include/NothinFancy.h @@ -1,5 +1,3 @@ //Master engine include (Is this even useful?) -#include "Application.h" -#include "Config.h" -#include "Utility.h" \ No newline at end of file +#include "Application.h" \ No newline at end of file diff --git a/NothinFancy/src/include/Utility.h b/NothinFancy/src/include/Utility.h index cffb2c9..343042c 100644 --- a/NothinFancy/src/include/Utility.h +++ b/NothinFancy/src/include/Utility.h @@ -10,8 +10,8 @@ namespace nf { #define DEBUGINIT std::chrono::steady_clock::time_point Debug::m_initTime = std::chrono::high_resolution_clock::now(); #define SleepS(x) std::this_thread::sleep_for(std::chrono::seconds(x)) #define SleepMS(x) std::this_thread::sleep_for(std::chrono::milliseconds(x)) -#define Log(x) Debug::LogImp(x) -#define Error(x) Debug::ErrorImp(x,__FILENAME__, __LINE__);\ +#define Log(x) nf::Debug::LogImp(x) +#define Error(x) nf::Debug::ErrorImp(x,__FILENAME__, __LINE__);\ DebugBreak(); class Debug {