From cd205d241bcddc1843c2152c99e5adb089782c01 Mon Sep 17 00:00:00 2001 From: "Grayson Riffe (Laptop)" Date: Wed, 18 Aug 2021 17:25:23 -0500 Subject: [PATCH] Tidied up code and fixed an Alt-Enter bug --- Game/src/Game.cpp | 5 ----- NothinFancy/src/Application.cpp | 26 +++++++++++++++----------- NothinFancy/src/include/Application.h | 5 ++--- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/Game/src/Game.cpp b/Game/src/Game.cpp index 42d32c4..34fb7ea 100644 --- a/Game/src/Game.cpp +++ b/Game/src/Game.cpp @@ -4,15 +4,10 @@ using namespace nf; int main(int argc, char* argv[]) { - - //TODO: Argument parser - Config conf = { 1280, 720, false, "Test"}; Application app(conf); //app.setWindowIcon(...); // app.setWindowCursor(...); - //Configure states - // app.addDefaultState(...); MainState* test = new MainState; app.addState("Main State", test); diff --git a/NothinFancy/src/Application.cpp b/NothinFancy/src/Application.cpp index 0e234d8..60513e6 100644 --- a/NothinFancy/src/Application.cpp +++ b/NothinFancy/src/Application.cpp @@ -23,10 +23,7 @@ namespace nf { m_defaultWindowStyle = GetWindowLong(m_window, GWL_STYLE); SetProp(m_window, L"App", this); if (m_currentConfig.fullscreen) toggleFullscreen(); - createOpenGLContext(); - m_states.reserve(100); - m_activeStates.reserve(100); } void Application::setWindowIcon(HANDLE hIcon) { @@ -39,7 +36,12 @@ namespace nf { } void Application::addState(const char* stateName, IGamestate* state) { - m_states[stateName] = state; + if (m_states.find(stateName) == m_states.end()) { + m_states[stateName] = state; + } + else { + Error(("State \"" + (std::string)stateName + (std::string)"\" already exists!").c_str()); + } } void Application::addDefaultState(const char* stateName) { @@ -53,7 +55,7 @@ namespace nf { } } else { - Error("More than one default state defined"); //TODO: Test this + Error("More than one default state defined"); } } @@ -61,6 +63,7 @@ namespace nf { if (m_states.find(stateName) != m_states.end()) { m_currentState->onExit(); m_currentState = m_states[stateName]; + m_currentState->onEnter(this); } else { Error(("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!").c_str()); @@ -68,9 +71,8 @@ namespace nf { } void Application::run() { - showWindow(true); addIntroState(); - m_currentState = m_sIntro; + showWindow(true); m_running = true; MSG msg = { }; while (m_running) { @@ -82,7 +84,7 @@ namespace nf { DispatchMessage(&msg); if (msg.message == WM_QUIT) m_running = false; - goto FrameEnd; + goto FrameEnd; } glClear(GL_COLOR_BUFFER_BIT); m_currentState->update(); @@ -92,7 +94,7 @@ namespace nf { m_frameClock = std::chrono::steady_clock::now(); //TODO: Update and render current state } - FrameEnd: + FrameEnd: m_fpsClock2 = std::chrono::steady_clock::now(); m_fpsDuration = m_fpsClock2 - m_fpsClock1; if (m_fpsDuration.count() >= 1.0) { @@ -121,7 +123,7 @@ namespace nf { void Application::addIntroState() { m_sIntro = new IntroGamestate; m_sIntro->onEnter(this); - m_activeStates.push_back(m_sIntro); + m_currentState = m_sIntro; } void Application::registerWindowClass() { @@ -150,9 +152,9 @@ namespace nf { SetWindowPos(m_window, HWND_TOP, mi.rcMonitor.left, mi.rcMonitor.top, mi.rcMonitor.right - mi.rcMonitor.left, mi.rcMonitor.bottom - mi.rcMonitor.top, SWP_NOOWNERZORDER | SWP_FRAMECHANGED); } else { + SetWindowLong(m_window, GWL_STYLE, m_defaultWindowStyle); SetWindowPlacement(m_window, &m_wndPlacement); SetWindowPos(m_window, NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED); - SetWindowLong(m_window, GWL_STYLE, m_defaultWindowStyle); } } @@ -191,6 +193,7 @@ namespace nf { } case WM_DESTROY: { app->m_currentState->onExit(); + app->m_currentState = nullptr; //Unload anything else PostQuitMessage(0); return 0; @@ -244,6 +247,7 @@ namespace nf { } Application::~Application() { + Log("Exiting NF application"); //TODO: Iterate through m_activeStates and m_states and exit and unload them } } diff --git a/NothinFancy/src/include/Application.h b/NothinFancy/src/include/Application.h index 244dfd8..21e260b 100644 --- a/NothinFancy/src/include/Application.h +++ b/NothinFancy/src/include/Application.h @@ -55,10 +55,9 @@ namespace nf { const double m_minFrametime = 1.0 / m_targetFPS; int m_FPS; - //Inactive states states to potentially be loaded during the Application's lifetime + //Inactive states states to potentially be active during the Application's lifetime + //Mapped to const char* to be referenced later in the frontend 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;