diff --git a/NothinFancy/src/Application.cpp b/NothinFancy/src/Application.cpp index 1bd0122..c6d9255 100644 --- a/NothinFancy/src/Application.cpp +++ b/NothinFancy/src/Application.cpp @@ -87,7 +87,7 @@ namespace nf { break; } } - updateInput(); + updateMouse(); std::this_thread::sleep_for(std::chrono::milliseconds(5)); } mainThread.join(); @@ -137,13 +137,21 @@ namespace nf { return m_FPS; } - bool Application::isInput(unsigned int code) { + bool Application::isKeyHeld(unsigned int code) { if (code < 164) { - return m_input[code]; + return m_keys[code].first; } - else { - return false; + return false; + } + + bool Application::isKeyPressed(unsigned int code) { + if (code < 164) { + if (m_keys[code].second) { + m_keys[code].second = false; + return true; + } } + return false; } void Application::trackMouse(bool track) { @@ -216,13 +224,7 @@ namespace nf { } } - void Application::updateInput() { - for (unsigned int i = 0; i < 164; i++) { - if (GetFocus() == m_window) - m_input[i] = GetKeyState(i) & 0x8000; - else - m_input[i] = false; - } + void Application::updateMouse() { POINT mouse; GetCursorPos(&mouse); ScreenToClient(m_window, &mouse); @@ -321,45 +323,57 @@ namespace nf { LRESULT CALLBACK Application::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { Application* app = (Application*)GetProp(hWnd, L"App"); switch (uMsg) { - case WM_CREATE: { - return 0; - } - case WM_SYSKEYDOWN: { - if (GetKeyState(VK_RETURN) & 0x8000) { - if (!(lParam & (1 << 30))) { - if (!app->m_currentConfig.fullscreen) { - app->m_altWidth = app->m_currentConfig.width; - app->m_altHeight = app->m_currentConfig.height; - } - app->changeConfig({ app->m_currentConfig.width, app->m_currentConfig.height, !app->m_currentConfig.fullscreen, app->m_currentConfig.title }); - if (!app->m_currentConfig.fullscreen) { - app->changeConfig({ app->m_altWidth, app->m_altHeight, app->m_currentConfig.fullscreen, app->m_currentConfig.title }); + case WM_CREATE: { + return 0; + } + case WM_SYSKEYDOWN: { + if (GetKeyState(VK_RETURN) & 0x8000) { + if (!(lParam & (1 << 30))) { + if (!app->m_currentConfig.fullscreen) { + app->m_altWidth = app->m_currentConfig.width; + app->m_altHeight = app->m_currentConfig.height; + } + app->changeConfig({ app->m_currentConfig.width, app->m_currentConfig.height, !app->m_currentConfig.fullscreen, app->m_currentConfig.title }); + if (!app->m_currentConfig.fullscreen) { + app->changeConfig({ app->m_altWidth, app->m_altHeight, app->m_currentConfig.fullscreen, app->m_currentConfig.title }); + } } + return 0; } - return 0; - } - break; - } - case WM_MENUCHAR: { - return MNC_CLOSE << 16; - } - case WM_SETCURSOR: { - if (LOWORD(lParam) != HTCLIENT) break; - if (app->m_trackingMouse && LOWORD(lParam) == HTCLIENT && GetFocus() == hWnd) { - SetCursor(NULL); + } + case WM_MENUCHAR: { + return MNC_CLOSE << 16; + } + case WM_KEYDOWN: { + if (wParam < 164 && !(lParam & (1 << 30))) + app->m_keys[wParam].first = app->m_keys[wParam].second = true; + + return 0; + } + case WM_KEYUP: { + if (wParam < 164) + app->m_keys[wParam].first = app->m_keys[wParam].second = false; + + return 0; + } + case WM_SETCURSOR: { + if (LOWORD(lParam) != HTCLIENT) + break; + if (app->m_trackingMouse && LOWORD(lParam) == HTCLIENT && GetFocus() == hWnd) { + SetCursor(NULL); + return 0; + } + break; + } + case WM_CLOSE: { + DestroyWindow(hWnd); + return 0; + } + case WM_DESTROY: { + PostQuitMessage(0); return 0; } - break; - } - case WM_CLOSE: { - DestroyWindow(hWnd); - return 0; - } - case WM_DESTROY: { - PostQuitMessage(0); - return 0; - } } return DefWindowProc(hWnd, uMsg, wParam, lParam); } diff --git a/NothinFancy/src/IntroGamestate.cpp b/NothinFancy/src/IntroGamestate.cpp index 933535c..2f8a146 100644 --- a/NothinFancy/src/IntroGamestate.cpp +++ b/NothinFancy/src/IntroGamestate.cpp @@ -24,10 +24,10 @@ namespace nf { scale += 0.12 * deltaTime; } - if (dur.count() > 3.5 || app->isInput(NFI_SPACE)) { + if (dur.count() > 3.5 || app->isKeyPressed(NFI_SPACE)) { app->changeState(app->getDefaultState()); } - if (app->isInput(NFI_ESCAPE)) + if (app->isKeyPressed(NFI_ESCAPE)) app->quit(); } diff --git a/NothinFancy/src/include/Application.h b/NothinFancy/src/include/Application.h index 94b44ac..e884ddc 100644 --- a/NothinFancy/src/include/Application.h +++ b/NothinFancy/src/include/Application.h @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include #include "Config.h" @@ -29,7 +30,8 @@ namespace nf { void changeConfig(const Config& in); const Config& getConfig() const; int getFPS() const; - bool isInput(unsigned int code); + bool isKeyHeld(unsigned int code); + bool isKeyPressed(unsigned int code); void trackMouse(bool track); void getMouseDiff(int& x, int& y); static Application* getApp(); @@ -41,7 +43,7 @@ namespace nf { RECT getWindowRect() const; void calculateNewWindowPos(int& x, int& y); void toggleFullscreen(); - void updateInput(); + void updateMouse(); void runMainGameThread(); void doStateChange(); static void setApp(Application* app); @@ -77,7 +79,7 @@ namespace nf { std::string m_nextState; //Array of booleans that represent keyboard and mouse input minus the scrollwheel - bool m_input[164]; + std::array, 164> m_keys; unsigned int m_mouseX, m_mouseY; bool m_trackingMouse; bool m_mouseTrackFirst; diff --git a/NothinFancy/src/include/NothinFancy.h b/NothinFancy/src/include/NothinFancy.h index bfe6ffb..ad752bd 100644 --- a/NothinFancy/src/include/NothinFancy.h +++ b/NothinFancy/src/include/NothinFancy.h @@ -3,6 +3,7 @@ #pragma once #include #include +#include #include #include "Config.h" @@ -82,7 +83,8 @@ namespace nf { void changeConfig(const Config& in); const Config& getConfig() const; int getFPS() const; - bool isInput(unsigned int code); + bool isKeyHeld(unsigned int code); + bool isKeyPressed(unsigned int code); void trackMouse(bool track); void getMouseDiff(int& x, int& y); static Application* getApp(); @@ -94,7 +96,7 @@ namespace nf { RECT getWindowRect() const; void calculateNewWindowPos(int& x, int& y); void toggleFullscreen(); - void updateInput(); + void updateMouse(); void runMainGameThread(); void doStateChange(); static void setApp(Application* app); @@ -130,7 +132,7 @@ namespace nf { std::string m_nextState; //Array of booleans that represent keyboard and mouse input minus the scrollwheel - bool m_input[164]; + std::array, 164> m_keys; unsigned int m_mouseX, m_mouseY; bool m_trackingMouse; bool m_mouseTrackFirst;