Added state changing

This commit is contained in:
Grayson Riffe (Laptop) 2021-08-18 13:54:06 -05:00
parent c63fd0ec88
commit e8bc0903e6
11 changed files with 99 additions and 24 deletions

View File

@ -173,6 +173,10 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="src\Game.cpp" /> <ClCompile Include="src\Game.cpp" />
<ClCompile Include="src\MainState.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\include\MainState.h" />
</ItemGroup> </ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">

View File

@ -18,5 +18,13 @@
<ClCompile Include="src\Game.cpp"> <ClCompile Include="src\Game.cpp">
<Filter>Source Files</Filter> <Filter>Source Files</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="src\MainState.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="src\include\MainState.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

View File

@ -1,4 +1,5 @@
#include "NothinFancy.h" #include "NothinFancy.h"
#include "MainState.h"
using namespace nf; using namespace nf;
@ -12,6 +13,11 @@ int main(int argc, char* argv[]) {
// app.setWindowCursor(...); // app.setWindowCursor(...);
//Configure states //Configure states
// app.addDefaultState(...); // app.addDefaultState(...);
MainState* test = new MainState;
app.addState("Main State", test);
app.addDefaultState("Main State");
app.run(); app.run();
return 0; return 0;

17
Game/src/MainState.cpp Normal file
View File

@ -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!");
}

View File

@ -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:
};

View File

@ -1,5 +1,4 @@
#include "Application.h" #include "Application.h"
#include "Utility.h"
#ifdef NFENGINE #ifdef NFENGINE
#include "GL\glew.h" #include "GL\glew.h"
#include "GL\wglew.h" #include "GL\wglew.h"
@ -28,8 +27,6 @@ namespace nf {
createOpenGLContext(); createOpenGLContext();
m_states.reserve(100); m_states.reserve(100);
m_activeStates.reserve(100); m_activeStates.reserve(100);
m_sIntro = new IntroGamestate;
addState(m_sIntro);
} }
void Application::setWindowIcon(HANDLE hIcon) { void Application::setWindowIcon(HANDLE hIcon) {
@ -41,24 +38,39 @@ namespace nf {
SetClassLongPtr(m_window, GCLP_HCURSOR, (LONG_PTR)hCursor); SetClassLongPtr(m_window, GCLP_HCURSOR, (LONG_PTR)hCursor);
} }
void Application::addState(IGamestate* in) { void Application::addState(const char* stateName, IGamestate* state) {
(*in).onEnter(); m_states[stateName] = state;
m_states.push_back(in);
} }
void Application::addDefaultState(IGamestate* in) { void Application::addDefaultState(const char* stateName) {
if (!m_defaultStateAdded) { if (!m_defaultStateAdded) {
(*in).onEnter(); if (m_states.find(stateName) != m_states.end()) {
m_activeStates.push_back(in); m_DefaultState = m_states[stateName];
m_defaultStateAdded = true; m_defaultStateAdded = true;
}
else {
Error(("State \"" + (std::string)stateName + (std::string)"\" doesn't exist!").c_str());
}
} }
else { else {
Error("More than one default state defined"); //TODO: Test this 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() { void Application::run() {
showWindow(true); showWindow(true);
addIntroState();
m_currentState = m_sIntro;
m_running = true; m_running = true;
MSG msg = { }; MSG msg = { };
while (m_running) { while (m_running) {
@ -70,13 +82,17 @@ namespace nf {
DispatchMessage(&msg); DispatchMessage(&msg);
if (msg.message == WM_QUIT) if (msg.message == WM_QUIT)
m_running = false; m_running = false;
goto FrameEnd;
} }
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
m_currentState->update();
m_currentState->render();
SwapBuffers(m_hdc); SwapBuffers(m_hdc);
m_frames++; m_frames++;
m_frameClock = std::chrono::steady_clock::now(); m_frameClock = std::chrono::steady_clock::now();
//TODO: Update and render current state //TODO: Update and render current state
} }
FrameEnd:
m_fpsClock2 = std::chrono::steady_clock::now(); m_fpsClock2 = std::chrono::steady_clock::now();
m_fpsDuration = m_fpsClock2 - m_fpsClock1; m_fpsDuration = m_fpsClock2 - m_fpsClock1;
if (m_fpsDuration.count() >= 1.0) { if (m_fpsDuration.count() >= 1.0) {
@ -102,6 +118,12 @@ namespace nf {
return m_FPS; return m_FPS;
} }
void Application::addIntroState() {
m_sIntro = new IntroGamestate;
m_sIntro->onEnter();
m_activeStates.push_back(m_sIntro);
}
void Application::registerWindowClass() { void Application::registerWindowClass() {
if (!FindWindow(L"NFClass", NULL)) { if (!FindWindow(L"NFClass", NULL)) {
m_wclassName = L"NFClass"; m_wclassName = L"NFClass";
@ -164,12 +186,12 @@ namespace nf {
return MNC_CLOSE << 16; return MNC_CLOSE << 16;
} }
case WM_CLOSE: { case WM_CLOSE: {
//State onExit() in order
//unload anything else
DestroyWindow(hWnd); DestroyWindow(hWnd);
return 0; return 0;
} }
case WM_DESTROY: { case WM_DESTROY: {
app->m_currentState->onExit();
//Unload anything else
PostQuitMessage(0); PostQuitMessage(0);
return 0; return 0;
} }

View File

@ -2,18 +2,18 @@
namespace nf { namespace nf {
void IntroGamestate::onEnter() { void IntroGamestate::onEnter() {
Log("Intro onEnter!");
} }
void IntroGamestate::onExit() { void IntroGamestate::onExit() {
Log("Intro onExit!");
} }
void IntroGamestate::update() { void IntroGamestate::update() {
Log("Intro update!");
} }
void IntroGamestate::render() { void IntroGamestate::render() {
Log("Intro render!");
} }
} }

View File

@ -1,8 +1,10 @@
#pragma once #pragma once
#include "Config.h" #include "Config.h"
#include "Utility.h"
#include "IntroGamestate.h" #include "IntroGamestate.h"
#include <Windows.h> #include <Windows.h>
#include <chrono> #include <chrono>
#include <unordered_map>
#include <vector> #include <vector>
namespace nf { namespace nf {
@ -14,8 +16,9 @@ namespace nf {
void setWindowIcon(HANDLE hIcon); void setWindowIcon(HANDLE hIcon);
void setWindowCursor(HCURSOR hCursor); void setWindowCursor(HCURSOR hCursor);
void addState(IGamestate* in); void addState(const char* stateName, IGamestate* state);
void addDefaultState(IGamestate* in); void addDefaultState(const char* stateName);
void changeState(const char* stateName);
void run(); void run();
void showWindow(bool show); void showWindow(bool show);
const Config& getConfig() const; const Config& getConfig() const;
@ -23,6 +26,7 @@ namespace nf {
~Application(); ~Application();
private: private:
void addIntroState();
void registerWindowClass(); void registerWindowClass();
void toggleFullscreen(); void toggleFullscreen();
RECT getWindowRect() const; RECT getWindowRect() const;
@ -52,10 +56,12 @@ namespace nf {
int m_FPS; int m_FPS;
//Inactive states states to potentially be loaded during the Application's lifetime //Inactive states states to potentially be loaded during the Application's lifetime
std::vector<IGamestate*> m_states; std::unordered_map<const char*, IGamestate*> m_states;
//The currently active and loaded states where the top-most is the current one //The currently active and loaded states where the top-most is the current one
std::vector<IGamestate*> m_activeStates; std::vector<IGamestate*> m_activeStates;
IntroGamestate* m_sIntro; IntroGamestate* m_sIntro;
IGamestate* m_DefaultState;
bool m_defaultStateAdded = false; bool m_defaultStateAdded = false;
IGamestate* m_currentState;
}; };
} }

View File

@ -1,4 +1,5 @@
#include "IGamestate.h" #include "IGamestate.h"
#include "Utility.h"
namespace nf { namespace nf {
class IntroGamestate : public IGamestate { class IntroGamestate : public IGamestate {

View File

@ -1,5 +1,3 @@
//Master engine include (Is this even useful?) //Master engine include (Is this even useful?)
#include "Application.h" #include "Application.h"
#include "Config.h"
#include "Utility.h"

View File

@ -10,8 +10,8 @@ namespace nf {
#define DEBUGINIT std::chrono::steady_clock::time_point Debug::m_initTime = std::chrono::high_resolution_clock::now(); #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 SleepS(x) std::this_thread::sleep_for(std::chrono::seconds(x))
#define SleepMS(x) std::this_thread::sleep_for(std::chrono::milliseconds(x)) #define SleepMS(x) std::this_thread::sleep_for(std::chrono::milliseconds(x))
#define Log(x) Debug::LogImp(x) #define Log(x) nf::Debug::LogImp(x)
#define Error(x) Debug::ErrorImp(x,__FILENAME__, __LINE__);\ #define Error(x) nf::Debug::ErrorImp(x,__FILENAME__, __LINE__);\
DebugBreak(); DebugBreak();
class Debug { class Debug {