From e7f75cbf4d29751dc8b6c61ab19ade24e4bcda0f Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Mon, 28 Oct 2019 01:12:31 +0100 Subject: [PATCH] WIP --- source/blood/src/blood.cpp | 10 ++-- source/blood/src/credits.cpp | 2 +- source/blood/src/demo.cpp | 6 +-- source/blood/src/gamemenu.cpp | 6 +-- source/blood/src/messages.cpp | 10 ++-- source/blood/src/network.cpp | 6 +-- source/blood/src/osdcmd.cpp | 3 +- source/build/include/baselayer.h | 4 +- source/build/src/baselayer.cpp | 7 ++- source/build/src/osd.cpp | 3 +- source/build/src/sdlayer.cpp | 5 +- source/common/inputstate.h | 79 ++++++++++++++++++++++++++++++++ source/duke3d/src/game.cpp | 6 +-- source/duke3d/src/network.cpp | 3 -- source/duke3d/src/osdfuncs.cpp | 4 -- source/mact/include/keyboard.h | 17 +------ source/mact/src/control.cpp | 31 ------------- source/mact/src/keyboard.cpp | 3 ++ source/rr/src/game.cpp | 6 +-- source/rr/src/net.cpp | 2 - source/rr/src/osdfuncs.cpp | 4 -- source/sw/src/draw.cpp | 4 +- source/sw/src/game.h | 6 --- 23 files changed, 122 insertions(+), 105 deletions(-) diff --git a/source/blood/src/blood.cpp b/source/blood/src/blood.cpp index 94ae34803..49d830350 100644 --- a/source/blood/src/blood.cpp +++ b/source/blood/src/blood.cpp @@ -811,9 +811,9 @@ static void DoQuickSave(void) void LocalKeys(void) { - char alt = keystatus[sc_LeftAlt] | keystatus[sc_RightAlt]; - char ctrl = keystatus[sc_LeftControl] | keystatus[sc_RightControl]; - char shift = keystatus[sc_LeftShift] | keystatus[sc_RightShift]; + bool alt = inputState.AltPressed(); + bool ctrl = inputState.CtrlPressed(); + bool shift = inputState.ShiftPressed(); if (BUTTON(gamefunc_See_Chase_View) && !alt && !shift) { CONTROL_ClearButton(gamefunc_See_Chase_View); @@ -877,7 +877,7 @@ void LocalKeys(void) gPlayerMsg.Send(); } keyFlushScans(); - keystatus[key] = 0; + inputState.ClearKeyStatus(key); CONTROL_ClearButton(gamefunc_See_Chase_View); return; } @@ -1679,7 +1679,7 @@ RESTART: { if (handleevents() && quitevent) { - KB_KeyDown[sc_Escape] = 1; + inputState.SetKeyStatus(sc_Escape, 1); quitevent = 0; } netUpdate(); diff --git a/source/blood/src/credits.cpp b/source/blood/src/credits.cpp index 6e066f9ef..9d64d6b9e 100644 --- a/source/blood/src/credits.cpp +++ b/source/blood/src/credits.cpp @@ -99,7 +99,7 @@ char DoUnFade(int nTicks) void credLogosDos(void) { - char bShift = keystatus[sc_LeftShift] | keystatus[sc_RightShift]; + char bShift = inputState.ShiftPressed(); videoSetViewableArea(0, 0, xdim-1, ydim-1); DoUnFade(1); videoClearScreen(0); diff --git a/source/blood/src/demo.cpp b/source/blood/src/demo.cpp index 183d9c8f4..9db0dd9a4 100644 --- a/source/blood/src/demo.cpp +++ b/source/blood/src/demo.cpp @@ -289,8 +289,6 @@ void CDemo::ProcessKeys(void) char nKey; while ((nKey = keyGetScan()) != 0) { - char UNUSED(alt) = keystatus[0x38] | keystatus[0xb8]; - char UNUSED(ctrl) = keystatus[0x1d] | keystatus[0x9d]; switch (nKey) { case 1: @@ -333,8 +331,8 @@ _DEMOPLAYBACK: { if (handleevents() && quitevent) { - KB_KeyDown[sc_Escape] = 1; - quitevent = 0; + inputState.SetKeyStatus(sc_Escape, 1); + quitevent = 0; } MUSIC_Update(); while (totalclock >= gNetFifoClock && !gQuitGame) diff --git a/source/blood/src/gamemenu.cpp b/source/blood/src/gamemenu.cpp index a6e81f806..61da6c62a 100644 --- a/source/blood/src/gamemenu.cpp +++ b/source/blood/src/gamemenu.cpp @@ -240,7 +240,7 @@ void CGameMenuMgr::Process(void) event.at0 = kMenuEventEscape; break; case sc_Tab: - if (keystatus[sc_LeftShift] || keystatus[sc_RightShift]) + if (inputState.ShiftPressed()) event.at0 = kMenuEventUp; else event.at0 = kMenuEventDown; @@ -1906,7 +1906,7 @@ bool CGameMenuItemZEdit::Event(CGameMenuEvent &event) char key; if (event.at2 < 128) { - if (keystatus[sc_LeftShift] || keystatus[sc_RightShift]) + if (inputState.ShiftPressed()) key = g_keyAsciiTableShift[event.at2]; else key = g_keyAsciiTable[event.at2]; @@ -2099,7 +2099,7 @@ bool CGameMenuItemZEditBitmap::Event(CGameMenuEvent &event) char key; if (bScan && event.at2 < 128) { - if (keystatus[sc_LeftShift] || keystatus[sc_RightShift]) + if (inputState.ShiftPressed()) key = g_keyAsciiTableShift[event.at2]; else key = g_keyAsciiTable[event.at2]; diff --git a/source/blood/src/messages.cpp b/source/blood/src/messages.cpp index 355bf1f29..02cad3fad 100644 --- a/source/blood/src/messages.cpp +++ b/source/blood/src/messages.cpp @@ -595,9 +595,8 @@ void CPlayerMsg::ProcessKeys(void) char ch; if (key != 0) { - bool UNUSED(alt) = keystatus[sc_LeftAlt] || keystatus[sc_RightAlt]; - bool ctrl = keystatus[sc_LeftControl] || keystatus[sc_RightControl]; - bool shift = keystatus[sc_LeftShift] || keystatus[sc_RightShift]; + bool ctrl = (inputState.CtrlPressed()); + bool shift = (inputState.ShiftPressed()); switch (key) { case sc_Escape: @@ -616,8 +615,9 @@ void CPlayerMsg::ProcessKeys(void) CONTROL_ClearButton(gamefunc_See_Chase_View); Set(*CombatMacros[key-sc_F1]); Send(); - keystatus[key] = 0; - break; + inputState.ClearKeyStatus(key); + } + break; case sc_BackSpace: if (ctrl) Clear(); diff --git a/source/blood/src/network.cpp b/source/blood/src/network.cpp index 358359e69..213b45cf9 100644 --- a/source/blood/src/network.cpp +++ b/source/blood/src/network.cpp @@ -540,7 +540,7 @@ void netGetPackets(void) gStartNewGame = 1; break; case 255: - keystatus[1] = 1; + inputState.SetKeyStatus(sc_Escape, 1); break; } } @@ -637,8 +637,8 @@ void netWaitForEveryone(char a1) int p; do { - if (keystatus[sc_Escape] && a1) - exit(0); + if (inputState.EscapePressed() && a1) + Bexit(0); gameHandleEvents(); faketimerhandler(); for (p = connecthead; p >= 0; p = connectpoint2[p]) diff --git a/source/blood/src/osdcmd.cpp b/source/blood/src/osdcmd.cpp index bc2b60541..9926a7064 100644 --- a/source/blood/src/osdcmd.cpp +++ b/source/blood/src/osdcmd.cpp @@ -842,7 +842,8 @@ void GAME_onshowosd(int shown) // XXX: it's weird to fake a keypress like this. // if (numplayers == 1 && ((shown && !ud.pause_on) || (!shown && ud.pause_on))) -// KB_KeyDown[sc_Pause] = 1; +// inputState.SetKeyStatus(sc_Escape, 1); + } void GAME_clearbackground(int numcols, int numrows) diff --git a/source/build/include/baselayer.h b/source/build/include/baselayer.h index e7c758690..16cf841b4 100644 --- a/source/build/include/baselayer.h +++ b/source/build/include/baselayer.h @@ -11,6 +11,7 @@ #include "osd.h" #include "timer.h" #include "c_cvars.h" +#include "inputstate.h" #ifdef DEBUGGINGAIDS @@ -88,7 +89,6 @@ vec2_t CONSTEXPR const g_defaultVideoModes [] extern char inputdevices; // keys -#define NUMKEYS 256 #define KEYFIFOSIZ 64 char CONSTEXPR const g_keyAsciiTable[128] = { @@ -107,7 +107,6 @@ char CONSTEXPR const g_keyAsciiTableShift[128] = { 0 , 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, }; -extern char keystatus[NUMKEYS]; extern char g_keyFIFO[KEYFIFOSIZ]; extern char g_keyAsciiFIFO[KEYFIFOSIZ]; extern uint8_t g_keyAsciiPos; @@ -116,7 +115,6 @@ extern uint8_t g_keyFIFOend; extern char g_keyRemapTable[NUMKEYS]; extern char g_keyNameTable[NUMKEYS][24]; -extern int32_t keyGetState(int32_t key); extern void keySetState(int32_t key, int32_t state); // mouse diff --git a/source/build/src/baselayer.cpp b/source/build/src/baselayer.cpp index 570ca19f8..639a68a19 100644 --- a/source/build/src/baselayer.cpp +++ b/source/build/src/baselayer.cpp @@ -8,6 +8,7 @@ #include "a.h" #include "polymost.h" #include "cache1d.h" +#include "inputstate.h" #include "../../glbackend/glbackend.h" // video @@ -24,7 +25,6 @@ int32_t g_borderless=2; // input char inputdevices = 0; -char keystatus[NUMKEYS]; char g_keyFIFO[KEYFIFOSIZ]; char g_keyAsciiFIFO[KEYFIFOSIZ]; uint8_t g_keyFIFOpos; @@ -38,11 +38,10 @@ void (*keypresscallback)(int32_t, int32_t); void keySetCallback(void (*callback)(int32_t, int32_t)) { keypresscallback = callback; } -int32_t keyGetState(int32_t key) { return keystatus[g_keyRemapTable[key]]; } - void keySetState(int32_t key, int32_t state) { - keystatus[g_keyRemapTable[key]] = state; + inputState.SetKeyStatus(g_keyRemapTable[key], state); + //keystatus[g_keyRemapTable[key]] = state; if (state) { diff --git a/source/build/src/osd.cpp b/source/build/src/osd.cpp index b032a67cb..115b5ccdf 100644 --- a/source/build/src/osd.cpp +++ b/source/build/src/osd.cpp @@ -11,6 +11,7 @@ #include "scancodes.h" #include "common.h" #include "c_cvars.h" +#include "inputstate.h" #define XXH_STATIC_LINKING_ONLY #include "xxhash.h" @@ -1217,7 +1218,7 @@ int OSD_HandleScanCode(uint8_t scanCode, int keyDown) osddraw_t &draw = osd->draw; - if (scanCode == osd->keycode && (keystatus[sc_LeftShift] || (osd->flags & OSD_CAPTURE) || (osd->flags & OSD_PROTECTED) != OSD_PROTECTED)) + if (scanCode == osd->keycode && (inputState.ShiftPressed() || (osd->flags & OSD_CAPTURE) || (osd->flags & OSD_PROTECTED) != OSD_PROTECTED)) { if (keyDown) { diff --git a/source/build/src/sdlayer.cpp b/source/build/src/sdlayer.cpp index 82ef5ace4..051047c34 100644 --- a/source/build/src/sdlayer.cpp +++ b/source/build/src/sdlayer.cpp @@ -28,6 +28,7 @@ #include "resourcefile.h" #include "sc_man.h" #include "i_specialpaths.h" +#include "inputstate.h" #include "c_cvars.h" #include "../../glbackend/glbackend.h" @@ -2223,7 +2224,7 @@ int32_t handleevents_pollsdl(void) { for (j = 0; j < NUMKEYS; ++j) { - if (keyGetState(j)) + if (inputState.GetKeyStatus(j)) { if (keypresscallback) keypresscallback(j, 0); @@ -2236,7 +2237,7 @@ int32_t handleevents_pollsdl(void) if (ev.key.type == SDL_KEYDOWN) { - if (!keyGetState(code)) + if (!inputState.GetKeyStatus(code)) { if (keypresscallback) keypresscallback(code, 1); diff --git a/source/common/inputstate.h b/source/common/inputstate.h index 8a310520b..dc4cea964 100644 --- a/source/common/inputstate.h +++ b/source/common/inputstate.h @@ -1,7 +1,19 @@ #pragma once +#include "keyboard.h" +#include "control.h" +#include "scancodes.h" + +extern kb_scancode KB_LastScan; + // This encapsulates the entire game-readable input state which previously was spread out across several files. +enum +{ + NUMKEYS = 256, +}; + + // Order is that of EDuke32 by necessity because it exposes the key binds to scripting by index instead of by name. enum GameFunction_t { @@ -99,6 +111,8 @@ class InputState { FixedBitArray ButtonState, ButtonHeldState; + uint8_t KeyStatus[NUMKEYS]; + public: bool BUTTON(int x) @@ -145,6 +159,45 @@ public: ButtonState.Zero(); } + uint8_t GetKeyStatus(int key) + { + return KeyStatus[key]; + } + + void SetKeyStatus(int key, int state) + { + KeyStatus[key] = (uint8_t)state; + } + + void ClearKeyStatus(int key) + { + KeyStatus[key] = 0; + } + + void ClearAllKeyStatus() + { + memset(KeyStatus, 0, sizeof(KeyStatus)); + } + + bool AltPressed() + { + return KeyStatus[sc_LeftAlt] || KeyStatus[sc_RightAlt]; + } + + bool CtrlPressed() + { + KeyStatus[sc_LeftControl] || KeyStatus[sc_RightControl]; + } + + bool ShiftPressed() + { + return KeyStatus[sc_LeftShift] || KeyStatus[sc_RightShift]; + } + + bool EscapePressed() + { + return !!KeyStatus[sc_Escape]; + } }; @@ -175,3 +228,29 @@ inline bool BUTTONSTATECHANGED(int x) { return (BUTTON(x) != BUTTONHELD(x)); } + +inline uint8_t KB_KeyPressed(int scan) +{ + return inputState.GetKeyStatus(scan); +} + +inline void KB_ClearKeyDown(int scan) +{ + inputState.ClearKeyStatus(scan); +} + +inline bool KB_UnBoundKeyPressed(int scan) +{ + return (inputState.GetKeyStatus(scan) != 0 && !CONTROL_KeyBinds[scan].cmdstr); +} + +inline uint8_t KEY_PRESSED(int scan) // Shadow Warrior uses different names for many things. +{ + return inputState.GetKeyStatus(scan); +} + +inline void KB_ClearKeysDown(void) +{ + KB_LastScan = 0; + inputState.ClearAllKeyStatus(); +} diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp index 2f9796630..71243244a 100644 --- a/source/duke3d/src/game.cpp +++ b/source/duke3d/src/game.cpp @@ -6684,7 +6684,7 @@ MAIN_LOOP_RESTART: } ud.warp_on = 0; - KB_KeyDown[sc_Pause] = 0; // JBF: I hate the pause key + inputState.ClearKeyStatus(sc_Pause); // JBF: I hate the pause key if(g_netClient) { @@ -6695,8 +6695,8 @@ MAIN_LOOP_RESTART: { if (gameHandleEvents() && quitevent) { - KB_KeyDown[sc_Escape] = 1; - quitevent = 0; + inputState.SetKeyStatus(sc_Escape, 1); + quitevent = 0; } // only allow binds to function if the player is actually in a game (not in a menu, typing, et cetera) or demo diff --git a/source/duke3d/src/network.cpp b/source/duke3d/src/network.cpp index 8092a5eb7..5357759ac 100644 --- a/source/duke3d/src/network.cpp +++ b/source/duke3d/src/network.cpp @@ -4684,9 +4684,6 @@ void Net_WaitForServer(void) do { - if (quitevent || keystatus[1]) - G_GameExit(""); - if (G_FPSLimit()) { display_betascreen(); diff --git a/source/duke3d/src/osdfuncs.cpp b/source/duke3d/src/osdfuncs.cpp index 0e331ac4a..25579aa8c 100644 --- a/source/duke3d/src/osdfuncs.cpp +++ b/source/duke3d/src/osdfuncs.cpp @@ -113,10 +113,6 @@ void GAME_onshowosd(int shown) mouseLockToWindow((!shown) + 2); osdshown = shown; - - // XXX: it's weird to fake a keypress like this. -// if (numplayers == 1 && ((shown && !ud.pause_on) || (!shown && ud.pause_on))) -// KB_KeyDown[sc_Pause] = 1; } diff --git a/source/mact/include/keyboard.h b/source/mact/include/keyboard.h index 6305046fc..b52a34da3 100644 --- a/source/mact/include/keyboard.h +++ b/source/mact/include/keyboard.h @@ -60,7 +60,6 @@ static struct #define MAXKEYBOARDSCAN 256 -#define KB_KeyDown keystatus extern kb_scancode KB_LastScan; #define KB_GetLastScanCode() (KB_LastScan) @@ -72,31 +71,19 @@ extern kb_scancode KB_LastScan; { \ KB_SetLastScanCode(sc_None); \ } -#define KB_KeyPressed(scan) (keystatus[(scan)] != 0) -#define KB_ClearKeyDown(scan) \ - { \ - keystatus[(scan)] = FALSE; \ - } -#define KB_UnBoundKeyPressed(scan) (keystatus[(scan)] != 0 && !CONTROL_KeyBinds[scan].cmdstr) #define KB_GetCh keyGetChar #define KB_KeyWaiting keyBufferWaiting #define KB_FlushKeyboardQueue keyFlushChars #define KB_FlushKeyboardQueueScans keyFlushScans -static inline void KB_ClearKeysDown(void) -{ - KB_LastScan = 0; - Bmemset(keystatus, 0, sizeof(keystatus)); -} - static inline void KB_KeyEvent(int32_t scancode, int32_t keypressed) { if (keypressed) KB_LastScan = scancode; } -static inline void KB_Startup(void) { keySetCallback(KB_KeyEvent); } -static inline void KB_Shutdown(void) { keySetCallback((void (*)(int32_t, int32_t))NULL); } +void KB_Startup(void); +void KB_Shutdown(void); const char * KB_ScanCodeToString( kb_scancode scancode ); // convert scancode into a string kb_scancode KB_StringToScanCode( const char * string ); // convert a string into a scancode diff --git a/source/mact/src/control.cpp b/source/mact/src/control.cpp index 322ec553c..8bc3ba3ca 100644 --- a/source/mact/src/control.cpp +++ b/source/mact/src/control.cpp @@ -139,37 +139,6 @@ static void CONTROL_SetFlag(int which, int active) } } -#if 0 -int32_t CONTROL_KeyboardFunctionPressed(int32_t which) -{ - int32_t key1 = 0, key2 = 0; - - if (CONTROL_CheckRange(which)) return FALSE; - - if (!CONTROL_Flags[which].used) return FALSE; - - if (CONTROL_KeyMapping[which].key1 != KEYUNDEFINED && !KeyBindings[CONTROL_KeyMapping[which].key1].cmdstr) - key1 = KB_KeyDown[ CONTROL_KeyMapping[which].key1 ] ? TRUE : FALSE; - - if (CONTROL_KeyMapping[which].key2 != KEYUNDEFINED && !KeyBindings[CONTROL_KeyMapping[which].key2].cmdstr) - key2 = KB_KeyDown[ CONTROL_KeyMapping[which].key2 ] ? TRUE : FALSE; - - return key1 | key2; -} - -void CONTROL_ClearKeyboardFunction(int32_t which) -{ - if (CONTROL_CheckRange(which)) return; - - if (!CONTROL_Flags[which].used) return; - - if (CONTROL_KeyMapping[which].key1 != KEYUNDEFINED) - KB_KeyDown[ CONTROL_KeyMapping[which].key1 ] = 0; - - if (CONTROL_KeyMapping[which].key2 != KEYUNDEFINED) - KB_KeyDown[ CONTROL_KeyMapping[which].key2 ] = 0; -} -#endif void CONTROL_DefineFlag(int which, int toggle) { diff --git a/source/mact/src/keyboard.cpp b/source/mact/src/keyboard.cpp index 51a439ccc..fdd807776 100644 --- a/source/mact/src/keyboard.cpp +++ b/source/mact/src/keyboard.cpp @@ -57,3 +57,6 @@ kb_scancode KB_StringToScanCode(const char * string) return 0; } + +void KB_Startup(void) { keySetCallback(KB_KeyEvent); } +void KB_Shutdown(void) { keySetCallback((void (*)(int32_t, int32_t))NULL); } diff --git a/source/rr/src/game.cpp b/source/rr/src/game.cpp index 45eff5794..73d859bdf 100644 --- a/source/rr/src/game.cpp +++ b/source/rr/src/game.cpp @@ -8051,14 +8051,14 @@ MAIN_LOOP_RESTART: } ud.warp_on = 0; - KB_KeyDown[sc_Pause] = 0; // JBF: I hate the pause key + inputState.ClearKeyStatus(sc_Pause); // JBF: I hate the pause key do //main loop { if (handleevents() && quitevent) { - KB_KeyDown[sc_Escape] = 1; - quitevent = 0; + inputState.SetKeyStatus(sc_Escape, 1); + quitevent = 0; } Net_GetPackets(); diff --git a/source/rr/src/net.cpp b/source/rr/src/net.cpp index 8d32bcf7e..fa5349739 100644 --- a/source/rr/src/net.cpp +++ b/source/rr/src/net.cpp @@ -168,8 +168,6 @@ void Net_WaitForEverybody(void) do { - //if (quitevent || keystatus[sc_Escape]) G_GameExit(""); - if (G_FPSLimit()) { display_betascreen(); diff --git a/source/rr/src/osdfuncs.cpp b/source/rr/src/osdfuncs.cpp index 0489683c2..15ed8af4b 100644 --- a/source/rr/src/osdfuncs.cpp +++ b/source/rr/src/osdfuncs.cpp @@ -126,10 +126,6 @@ void GAME_onshowosd(int32_t shown) mouseLockToWindow((!shown) + 2); osdshown = shown; - - // XXX: it's weird to fake a keypress like this. -// if (numplayers == 1 && ((shown && !ud.pause_on) || (!shown && ud.pause_on))) -// KB_KeyDown[sc_Pause] = 1; } diff --git a/source/sw/src/draw.cpp b/source/sw/src/draw.cpp index 16448e6f6..df9710917 100644 --- a/source/sw/src/draw.cpp +++ b/source/sw/src/draw.cpp @@ -1044,12 +1044,12 @@ ResizeView(PLAYERp pp) if (dimensionmode == 2 || dimensionmode == 5 || dimensionmode == 6) { - if (PKEY_PRESSED(KEYSC_DASH)||PKEY_PRESSED(KEYSC_GMINUS)) + if (KEY_PRESSED(KEYSC_DASH)||KEY_PRESSED(KEYSC_GMINUS)) { if ((zoom -= (zoom >> 4)) < 48) zoom = 48; } - if (PKEY_PRESSED(KEYSC_EQUAL)||PKEY_PRESSED(KEYSC_GPLUS)) + if (KEY_PRESSED(KEYSC_EQUAL)||KEY_PRESSED(KEYSC_GPLUS)) { if ((zoom += (zoom >> 4)) > 4096) zoom = 4096; } diff --git a/source/sw/src/game.h b/source/sw/src/game.h index e262cc1ee..063f4a842 100644 --- a/source/sw/src/game.h +++ b/source/sw/src/game.h @@ -231,12 +231,6 @@ extern char MessageOutputString[256]; #define KEYSC_EPGUP sc_PgUp #define KEYSC_EPGDN sc_PgDn -#undef KB_KeyPressed -#define KB_KeyPressed( scan ) \ - ( KB_KeyDown[( scan )]) -#define KEY_PRESSED(sc) KB_KeyPressed((sc)) -#define PKEY_PRESSED(sc) KB_KeyPressed((sc)) - // // NETWORK - REDEFINABLE SHARED (SYNC) KEYS BIT POSITIONS //