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

View File

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

View File

@ -1,6 +1,7 @@
#pragma once
#include <chrono>
#include <unordered_map>
#include <array>
#include <Windows.h>
#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<std::pair<bool, bool>, 164> m_keys;
unsigned int m_mouseX, m_mouseY;
bool m_trackingMouse;
bool m_mouseTrackFirst;

View File

@ -3,6 +3,7 @@
#pragma once
#include <chrono>
#include <unordered_map>
#include <array>
#include <Windows.h>
#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<std::pair<bool, bool>, 164> m_keys;
unsigned int m_mouseX, m_mouseY;
bool m_trackingMouse;
bool m_mouseTrackFirst;