Input can now deduce pressing and holding keys

This commit is contained in:
Grayson Riffe (Laptop) 2021-09-10 12:35:19 -05:00
parent 39e0c8e808
commit 8bb59e7b6c
4 changed files with 72 additions and 54 deletions

View File

@ -87,7 +87,7 @@ namespace nf {
break; break;
} }
} }
updateInput(); updateMouse();
std::this_thread::sleep_for(std::chrono::milliseconds(5)); std::this_thread::sleep_for(std::chrono::milliseconds(5));
} }
mainThread.join(); mainThread.join();
@ -137,13 +137,21 @@ namespace nf {
return m_FPS; return m_FPS;
} }
bool Application::isInput(unsigned int code) { bool Application::isKeyHeld(unsigned int code) {
if (code < 164) { 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) { void Application::trackMouse(bool track) {
@ -216,13 +224,7 @@ namespace nf {
} }
} }
void Application::updateInput() { void Application::updateMouse() {
for (unsigned int i = 0; i < 164; i++) {
if (GetFocus() == m_window)
m_input[i] = GetKeyState(i) & 0x8000;
else
m_input[i] = false;
}
POINT mouse; POINT mouse;
GetCursorPos(&mouse); GetCursorPos(&mouse);
ScreenToClient(m_window, &mouse); ScreenToClient(m_window, &mouse);
@ -343,6 +345,18 @@ namespace nf {
case WM_MENUCHAR: { case WM_MENUCHAR: {
return MNC_CLOSE << 16; 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: { case WM_SETCURSOR: {
if (LOWORD(lParam) != HTCLIENT) if (LOWORD(lParam) != HTCLIENT)
break; break;

View File

@ -24,10 +24,10 @@ namespace nf {
scale += 0.12 * deltaTime; 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()); app->changeState(app->getDefaultState());
} }
if (app->isInput(NFI_ESCAPE)) if (app->isKeyPressed(NFI_ESCAPE))
app->quit(); app->quit();
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <chrono> #include <chrono>
#include <unordered_map> #include <unordered_map>
#include <array>
#include <Windows.h> #include <Windows.h>
#include "Config.h" #include "Config.h"
@ -29,7 +30,8 @@ namespace nf {
void changeConfig(const Config& in); void changeConfig(const Config& in);
const Config& getConfig() const; const Config& getConfig() const;
int getFPS() const; int getFPS() const;
bool isInput(unsigned int code); bool isKeyHeld(unsigned int code);
bool isKeyPressed(unsigned int code);
void trackMouse(bool track); void trackMouse(bool track);
void getMouseDiff(int& x, int& y); void getMouseDiff(int& x, int& y);
static Application* getApp(); static Application* getApp();
@ -41,7 +43,7 @@ namespace nf {
RECT getWindowRect() const; RECT getWindowRect() const;
void calculateNewWindowPos(int& x, int& y); void calculateNewWindowPos(int& x, int& y);
void toggleFullscreen(); void toggleFullscreen();
void updateInput(); void updateMouse();
void runMainGameThread(); void runMainGameThread();
void doStateChange(); void doStateChange();
static void setApp(Application* app); static void setApp(Application* app);
@ -77,7 +79,7 @@ namespace nf {
std::string m_nextState; std::string m_nextState;
//Array of booleans that represent keyboard and mouse input minus the scrollwheel //Array of booleans that represent keyboard and mouse input minus the scrollwheel
bool m_input[164]; std::array<std::pair<bool, bool>, 164> m_keys;
unsigned int m_mouseX, m_mouseY; unsigned int m_mouseX, m_mouseY;
bool m_trackingMouse; bool m_trackingMouse;
bool m_mouseTrackFirst; bool m_mouseTrackFirst;

View File

@ -3,6 +3,7 @@
#pragma once #pragma once
#include <chrono> #include <chrono>
#include <unordered_map> #include <unordered_map>
#include <array>
#include <Windows.h> #include <Windows.h>
#include "Config.h" #include "Config.h"
@ -82,7 +83,8 @@ namespace nf {
void changeConfig(const Config& in); void changeConfig(const Config& in);
const Config& getConfig() const; const Config& getConfig() const;
int getFPS() const; int getFPS() const;
bool isInput(unsigned int code); bool isKeyHeld(unsigned int code);
bool isKeyPressed(unsigned int code);
void trackMouse(bool track); void trackMouse(bool track);
void getMouseDiff(int& x, int& y); void getMouseDiff(int& x, int& y);
static Application* getApp(); static Application* getApp();
@ -94,7 +96,7 @@ namespace nf {
RECT getWindowRect() const; RECT getWindowRect() const;
void calculateNewWindowPos(int& x, int& y); void calculateNewWindowPos(int& x, int& y);
void toggleFullscreen(); void toggleFullscreen();
void updateInput(); void updateMouse();
void runMainGameThread(); void runMainGameThread();
void doStateChange(); void doStateChange();
static void setApp(Application* app); static void setApp(Application* app);
@ -130,7 +132,7 @@ namespace nf {
std::string m_nextState; std::string m_nextState;
//Array of booleans that represent keyboard and mouse input minus the scrollwheel //Array of booleans that represent keyboard and mouse input minus the scrollwheel
bool m_input[164]; std::array<std::pair<bool, bool>, 164> m_keys;
unsigned int m_mouseX, m_mouseY; unsigned int m_mouseX, m_mouseY;
bool m_trackingMouse; bool m_trackingMouse;
bool m_mouseTrackFirst; bool m_mouseTrackFirst;