From a21c24040d640091a1768652abd137cceabc784f Mon Sep 17 00:00:00 2001 From: "Grayson Riffe (Desktop)" Date: Thu, 19 Aug 2021 00:48:33 -0500 Subject: [PATCH] Added keyboard and mouse input --- Game/src/MainState.cpp | 5 +- Game/src/include/MainState.h | 2 +- NothinFancy/NothinFancy.vcxproj | 1 + NothinFancy/NothinFancy.vcxproj.filters | 3 ++ NothinFancy/src/Application.cpp | 48 ++++++++++++++++++- NothinFancy/src/Utility.cpp | 3 +- NothinFancy/src/include/Application.h | 11 +++-- NothinFancy/src/include/Input.h | 63 +++++++++++++++++++++++++ NothinFancy/src/include/NothinFancy.h | 1 + NothinFancy/src/include/Utility.h | 8 +++- 10 files changed, 136 insertions(+), 9 deletions(-) create mode 100644 NothinFancy/src/include/Input.h diff --git a/Game/src/MainState.cpp b/Game/src/MainState.cpp index 43ac4a5..21b8950 100644 --- a/Game/src/MainState.cpp +++ b/Game/src/MainState.cpp @@ -1,11 +1,12 @@ #include "MainState.h" void MainState::onEnter(Application* app) { - Log("MainState update!"); + Log("MainState onEnter!"); + m_app = app; } void MainState::onExit() { - Log("MainState update!"); + Log("MainState onExit!"); } void MainState::update() { diff --git a/Game/src/include/MainState.h b/Game/src/include/MainState.h index bc41824..9257cdb 100644 --- a/Game/src/include/MainState.h +++ b/Game/src/include/MainState.h @@ -9,5 +9,5 @@ public: void update() override; void render() override; private: - + Application* m_app; }; \ No newline at end of file diff --git a/NothinFancy/NothinFancy.vcxproj b/NothinFancy/NothinFancy.vcxproj index e6453c0..2b54896 100644 --- a/NothinFancy/NothinFancy.vcxproj +++ b/NothinFancy/NothinFancy.vcxproj @@ -200,6 +200,7 @@ + diff --git a/NothinFancy/NothinFancy.vcxproj.filters b/NothinFancy/NothinFancy.vcxproj.filters index 6c2d8b0..d07be53 100644 --- a/NothinFancy/NothinFancy.vcxproj.filters +++ b/NothinFancy/NothinFancy.vcxproj.filters @@ -44,6 +44,9 @@ Header Files + + Header Files + diff --git a/NothinFancy/src/Application.cpp b/NothinFancy/src/Application.cpp index e10787f..b59469d 100644 --- a/NothinFancy/src/Application.cpp +++ b/NothinFancy/src/Application.cpp @@ -103,6 +103,15 @@ namespace nf { return m_FPS; } + bool Application::isInput(unsigned int code) { + if (code < 164) { + return m_input[code]; + } + else { + return false; + } + } + void Application::addIntroState() { m_sIntro = new IntroGamestate; m_sIntro->onEnter(this); @@ -191,11 +200,47 @@ namespace nf { app->toggleFullscreen(); return 0; } - break; + return 0; } case WM_MENUCHAR: { return MNC_CLOSE << 16; } + case WM_LBUTTONDOWN: { + app->m_input[1] = true; + return 0; + } + case WM_LBUTTONUP: { + app->m_input[1] = false; + return 0; + } + case WM_RBUTTONDOWN: { + app->m_input[2] = true; + return 0; + } + case WM_RBUTTONUP: { + app->m_input[2] = false; + return 0; + } + case WM_MBUTTONDOWN: { + app->m_input[4] = true; + return 0; + } + case WM_MBUTTONUP: { + app->m_input[4] = false; + return 0; + } + case WM_KEYDOWN: { + if (wParam < 164 && !(lParam & (1 << 30))) { + app->m_input[wParam] = true; + } + break; + } + case WM_KEYUP: { + if (wParam < 164) { + app->m_input[wParam] = false; + } + break; + } case WM_CLOSE: { DestroyWindow(hWnd); return 0; @@ -245,6 +290,7 @@ namespace nf { wglDeleteContext(m_hglrc); m_hglrc = wglCreateContextAttribsARB(m_hdc, NULL, attrib); wglMakeCurrent(m_hdc, m_hglrc); + wglSwapIntervalEXT(0); Log("OpenGL version: " + std::string((char*)glGetString(GL_VERSION))); GLuint vao; glGenVertexArrays(1, &vao); diff --git a/NothinFancy/src/Utility.cpp b/NothinFancy/src/Utility.cpp index 3d440cb..10205c0 100644 --- a/NothinFancy/src/Utility.cpp +++ b/NothinFancy/src/Utility.cpp @@ -1,8 +1,9 @@ //TODO: Debug logger //TODO: File IO functions +#include + #include "Utility.h" #include "Config.h" -#include namespace nf { #ifdef _DEBUG diff --git a/NothinFancy/src/include/Application.h b/NothinFancy/src/include/Application.h index a06a929..26a4246 100644 --- a/NothinFancy/src/include/Application.h +++ b/NothinFancy/src/include/Application.h @@ -1,12 +1,13 @@ #pragma once -#include "Config.h" -#include "Utility.h" -#include "IntroGamestate.h" #include #include #include #include +#include "Config.h" +#include "Utility.h" +#include "IntroGamestate.h" + namespace nf { class Application { public: @@ -23,6 +24,7 @@ namespace nf { void showWindow(bool show); const Config& getConfig() const; int getFPS() const; + bool isInput(unsigned int code); ~Application(); private: @@ -63,5 +65,8 @@ namespace nf { IGamestate* m_DefaultState; bool m_defaultStateAdded = false; IGamestate* m_currentState; + + //Array of booleans that represent keyboard and mouse input minus the scrollwheel + bool m_input[164]; }; } \ No newline at end of file diff --git a/NothinFancy/src/include/Input.h b/NothinFancy/src/include/Input.h new file mode 100644 index 0000000..41b707c --- /dev/null +++ b/NothinFancy/src/include/Input.h @@ -0,0 +1,63 @@ +#pragma once + +#define NFI_LEFTMOUSE 1 +#define NFI_RIGHTMOUSE 2 +#define NFI_MIDLEMOUSE 4 +#define NFI_TAB 9 +#define NFI_ENTER 13 +#define NFI_SHIFT 16 +#define NFI_CONTROL 17 +#define NFI_ESCAPE 27 +#define NFI_SPACE 32 +#define NFI_LEFT 37 +#define NFI_UP 38 +#define NFI_RIGHT 39 +#define NFI_DOWN 40 +#define NFI_0 48 +#define NFI_1 49 +#define NFI_2 50 +#define NFI_3 51 +#define NFI_4 52 +#define NFI_5 53 +#define NFI_6 54 +#define NFI_7 55 +#define NFI_8 56 +#define NFI_9 57 +#define NFI_A 65 +#define NFI_B 66 +#define NFI_C 67 +#define NFI_D 68 +#define NFI_E 69 +#define NFI_F 70 +#define NFI_G 71 +#define NFI_H 72 +#define NFI_I 73 +#define NFI_J 74 +#define NFI_K 75 +#define NFI_L 76 +#define NFI_M 77 +#define NFI_N 78 +#define NFI_O 79 +#define NFI_P 80 +#define NFI_Q 81 +#define NFI_R 82 +#define NFI_S 83 +#define NFI_T 84 +#define NFI_U 85 +#define NFI_V 86 +#define NFI_W 87 +#define NFI_X 88 +#define NFI_Y 89 +#define NFI_Z 90 +#define NFI_F1 112 +#define NFI_F2 113 +#define NFI_F3 114 +#define NFI_F4 115 +#define NFI_F5 116 +#define NFI_F6 117 +#define NFI_F7 118 +#define NFI_F8 119 +#define NFI_F9 120 +#define NFI_F10 121 +#define NFI_F11 122 +#define NFI_F12 123 \ No newline at end of file diff --git a/NothinFancy/src/include/NothinFancy.h b/NothinFancy/src/include/NothinFancy.h index 0eb00c9..45b68f1 100644 --- a/NothinFancy/src/include/NothinFancy.h +++ b/NothinFancy/src/include/NothinFancy.h @@ -1,5 +1,6 @@ //Master engine include (Is this even useful?) #include "Application.h" +#include "Input.h" using namespace nf; \ No newline at end of file diff --git a/NothinFancy/src/include/Utility.h b/NothinFancy/src/include/Utility.h index 343042c..9f4ef5a 100644 --- a/NothinFancy/src/include/Utility.h +++ b/NothinFancy/src/include/Utility.h @@ -6,13 +6,19 @@ namespace nf { #ifdef _DEBUG +//Strips __FILE__ down to only the name of the file #define __FILENAME__ strrchr(__FILE__, '\\') + 1 +//Initializes static variables needed for debugging #define DEBUGINIT std::chrono::steady_clock::time_point Debug::m_initTime = std::chrono::high_resolution_clock::now(); +//Sleep for an amount of seconds #define SleepS(x) std::this_thread::sleep_for(std::chrono::seconds(x)) +//Sleep for an amount of milliseconds #define SleepMS(x) std::this_thread::sleep_for(std::chrono::milliseconds(x)) +//Prints a nicely-formatted message complete with a timestamp #define Log(x) nf::Debug::LogImp(x) +//Prints error message and breaks the debugger #define Error(x) nf::Debug::ErrorImp(x,__FILENAME__, __LINE__);\ -DebugBreak(); +__debugbreak(); class Debug { private: