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

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

@ -18,5 +18,13 @@
<ClCompile Include="src\Game.cpp">
<Filter>Source Files</Filter>
</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>
</Project>

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

17
Game/src/MainState.cpp Normal 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!");
}

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

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

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

@ -1,8 +1,10 @@
#pragma once
#include "Config.h"
#include "Utility.h"
#include "IntroGamestate.h"
#include <Windows.h>
#include <chrono>
#include <unordered_map>
#include <vector>
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<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
std::vector<IGamestate*> m_activeStates;
IntroGamestate* m_sIntro;
IGamestate* m_DefaultState;
bool m_defaultStateAdded = false;
IGamestate* m_currentState;
};
}

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

@ -1,5 +1,3 @@
//Master engine include (Is this even useful?)
#include "Application.h"
#include "Config.h"
#include "Utility.h"
#include "Application.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 {