- cleaned up the input code a bit.

This commit is contained in:
Christoph Oelckers 2020-01-01 11:35:47 +01:00
parent bcb48d8441
commit d464017363
18 changed files with 122 additions and 126 deletions

View file

@ -1194,9 +1194,7 @@ RESTART:
gRestartGame = 0;
if (gGameOptions.nGameType > 0)
{
inputState.ClearKeysDown();
inputState.keyFlushChars();
inputState.keyFlushScans();
inputState.ClearAllInput();
}
else if (gDemo.at1 && !bAddUserMap && !bNoDemo)
gDemo.Playback();

View file

@ -38,6 +38,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "seq.h"
#include "sound.h"
#include "view.h"
#include "menu/menu.h"
extern bool gHaveNetworking;
@ -542,7 +543,10 @@ void netGetPackets(void)
gStartNewGame = 1;
break;
case 255:
inputState.SetKeyStatus(sc_Escape);
// What are we trying to do here? Opening the menu, maybe?
//inputState.SetKeyStatus(sc_Escape);
M_StartControlPanel(false);
M_SetMenu(NAME_MainMenu);
break;
}
}

View file

@ -1,8 +1,47 @@
/*
** Main input handler
**
**---------------------------------------------------------------------------
** Copyright 2019 Christoph Oelckers
** All rights reserved.
**
** Redistribution and use in source and binary forms, with or without
** modification, are permitted provided that the following conditions
** are met:
**
** 1. Redistributions of source code must retain the above copyright
** notice, this list of conditions and the following disclaimer.
** 2. Redistributions in binary form must reproduce the above copyright
** notice, this list of conditions and the following disclaimer in the
** documentation and/or other materials provided with the distribution.
** 3. The name of the author may not be used to endorse or promote products
** derived from this software without specific prior written permission.
**
** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**---------------------------------------------------------------------------
**
*/
#include "inputstate.h"
#include "v_draw.h"
#include "build.h"
#include "gamecvars.h"
//==========================================================================
//
//
//
//==========================================================================
void InputState::GetMouseDelta(ControlInfo * info)
{
vec2_t input;
@ -54,15 +93,50 @@ void InputState::GetMouseDelta(ControlInfo * info)
}
//==========================================================================
//
//
//
//==========================================================================
void InputState::keySetState(int32_t key, int32_t state)
{
KeyStatus[key] = (uint8_t)state;
if (state)
{
g_keyFIFO[g_keyFIFOend] = key;
g_keyFIFO[(g_keyFIFOend + 1) & (KEYFIFOSIZ - 1)] = state;
g_keyFIFOend = ((g_keyFIFOend + 2) & (KEYFIFOSIZ - 1));
}
}
//==========================================================================
//
//
//
//==========================================================================
void InputState::AddEvent(const event_t *ev)
{
if (ev->type == EV_KeyDown || ev->type == EV_KeyUp)
{
keySetState(ev->data1, ev->type == EV_KeyDown);
if (ev->data2) keySetChar(ev->data2);
if (ev->data2)
{
g_keyAsciiFIFO[g_keyAsciiEnd] = (char16_t)ev->data2;
g_keyAsciiEnd = ((g_keyAsciiEnd + 1) & (KEYFIFOSIZ - 1));
}
}
}
//==========================================================================
//
//
//
//==========================================================================
void I_StartTic();
int32_t handleevents(void)
@ -111,6 +185,12 @@ int32_t handleevents(void)
return 0;
}
//==========================================================================
//
//
//
//==========================================================================
void CONTROL_GetInput(ControlInfo* info)
{
memset(info, 0, sizeof(ControlInfo));

View file

@ -46,10 +46,10 @@ class InputState
uint8_t g_keyAsciiPos;
uint8_t g_keyAsciiEnd;
kb_scancode KB_LastScan;
vec2_t g_mousePos;
void keySetState(int32_t key, int32_t state);
public:
uint8_t GetKeyStatus(int key)
@ -57,21 +57,11 @@ public:
return KeyStatus[key];
}
void SetKeyStatus(int key, int state = 1)
{
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];
@ -111,22 +101,6 @@ public:
return ((g_keyAsciiEnd + 1) & (KEYFIFOSIZ - 1)) == g_keyAsciiPos;
}
void keySetState(int32_t key, int32_t state)
{
if (state && !GetKeyStatus(key))
{
KB_LastScan = key;
}
SetKeyStatus(key, state);
if (state)
{
g_keyFIFO[g_keyFIFOend] = key;
g_keyFIFO[(g_keyFIFOend + 1) & (KEYFIFOSIZ - 1)] = state;
g_keyFIFOend = ((g_keyFIFOend + 2) & (KEYFIFOSIZ - 1));
}
}
kb_scancode keyGetScan()
{
if (g_keyFIFOpos == g_keyFIFOend)
@ -157,12 +131,6 @@ public:
return c;
}
void keySetChar(int key)
{
g_keyAsciiFIFO[g_keyAsciiEnd] = (char16_t)key;
g_keyAsciiEnd = ((g_keyAsciiEnd + 1) & (KEYFIFOSIZ - 1));
}
void keyFlushChars(void)
{
memset(&g_keyAsciiFIFO, 0, sizeof(g_keyAsciiFIFO));
@ -174,28 +142,6 @@ public:
return (GetKeyStatus(scan) != 0 && Bindings.GetBind(scan) == nullptr);
}
kb_scancode GetLastScanCode()
{
return (KB_LastScan);
}
void SetLastScanCode(kb_scancode scancode)
{
KB_LastScan = (scancode);
}
void ClearLastScanCode()
{
KB_LastScan = sc_None;
}
void ClearKeysDown(void)
{
ClearLastScanCode();
ClearAllKeyStatus();
}
void AddEvent(const event_t* ev);
void MouseSetPos(int x, int y)
@ -217,7 +163,7 @@ public:
void ClearAllInput()
{
ClearKeysDown();
memset(KeyStatus, 0, sizeof(KeyStatus));
keyFlushChars();
keyFlushScans();
buttonMap.ResetButtonStates(); // this is important. If all input is cleared, the buttons must be cleared as well.

View file

@ -370,8 +370,7 @@ void M_StartControlPanel (bool makeSound)
gi->MenuOpened();
if (makeSound) gi->MenuSound(ActivateSound);
buttonMap.ResetButtonStates ();
inputState.ClearAllKeyStatus();
inputState.ClearAllInput();
for (int i = 0; i < NUM_MKEYS; ++i)
{
MenuButtons[i].ReleaseKey(0);

View file

@ -325,7 +325,7 @@ void G_DoCheats(void)
// cheat string matching logic below.
Bassert(g_cheatBufLen < (signed)sizeof(cheatbuf));
cheatbuf[g_cheatBufLen] = 0;
// inputState.ClearKeysDown();
// inputState.ClearAllInput();
for (cheatNum=0; cheatNum < NUMCHEATCODES; cheatNum++)
{

View file

@ -1080,7 +1080,7 @@ static int32_t VM_ResetPlayer(int const playerNum, int32_t vmFlags, int32_t cons
if (resetFlags & 4)
{
inputState.keyFlushChars();
inputState.ClearKeysDown();
inputState.ClearAllInput();
FX_StopAllSounds();
if (G_LoadPlayerMaybeMulti(*g_quickload) != 0)
{

View file

@ -2030,8 +2030,7 @@ MENU:
bInDemo = kTrue;
bPlayback = kTrue;
inputState.keyFlushChars();
inputState.ClearAllKeyStatus();
inputState.ClearAllInput();
break;
}
STARTGAME1:
@ -2228,8 +2227,7 @@ GAMELOOP:
// YELLOW
if (((bInDemo && inputState.keyBufferWaiting()) || !ReadPlaybackInputs()) && inputState.keyGetChar())
{
inputState.keyFlushChars();
inputState.ClearAllKeyStatus();
inputState.ClearAllInput();
bPlayback = kFalse;
bInDemo = kFalse;
@ -2277,7 +2275,7 @@ GAMELOOP:
// loc_11FBC:
while (bPause)
{
ClearAllKeys();
inputState.ClearAllInput();
if (WaitAnyKey(-1) != sc_Pause)
{
bPause = kFalse;
@ -2492,7 +2490,7 @@ void KeyFn1()
void DoGameOverScene()
{
FadeOut(0);
ClearAllKeys();
inputState.ClearAllInput();
if (LoadCinemaPalette(16) < 0) {
return;
@ -2529,7 +2527,7 @@ void DoTitle()
if (videoGetRenderMode() == REND_CLASSIC)
FadeIn();
ClearAllKeys();
inputState.ClearAllInput();
WaitAnyKey(2);
@ -2551,7 +2549,7 @@ void DoTitle()
if (videoGetRenderMode() == REND_CLASSIC)
FadeOut(0);
ClearAllKeys();
inputState.ClearAllInput();
PlayMovie("book.mov");
@ -2602,7 +2600,7 @@ void DoTitle()
var_18 += theArray[0];
inputState.ClearAllKeyStatus();
inputState.ClearAllInput();
while (LocalSoundPlaying())
{
HandleAsync();

View file

@ -163,12 +163,6 @@ ClearSpaceBar_
faketimerhandler_
*/
void ClearAllKeys()
{
inputState.ClearAllKeyStatus();
inputState.keyFlushChars();
}
void WaitNoKey(int nSecs, void (*pFunc) (void))
{
int nTotalTime = (kTimerTicks * nSecs) + (int)totalclock;

View file

@ -630,7 +630,7 @@ int menu_DrawTheMap(int nLevel, int nLevelNew, int nLevelBest)
int startTime = (int)totalclock;
ClearAllKeys();
inputState.ClearAllInput();
UnMaskStatus();
videoSetViewableArea(0, 0, xdim - 1, ydim - 1);
@ -1180,7 +1180,7 @@ void ComputeCinemaText(int nLine)
nCrawlY = 199;
nHeight = linecount * 10;
ClearAllKeys();
inputState.ClearAllInput();
}
void ReadyCinemaText(uint16_t nVal)
@ -1359,7 +1359,7 @@ void GoToTheCinema(int nVal)
videoNextPage();
CinemaFadeIn();
ClearAllKeys();
inputState.ClearAllInput();
int ebx = -1;
int edx = -1;
@ -1518,11 +1518,7 @@ int FindGString(const char *str)
uint8_t CheckForEscape()
{
if (!inputState.keyBufferWaiting() || (inputState.keyGetChar() != 27)) {
return kFalse;
}
return kTrue;
return inputState.CheckAllInput();
}
void DoStatic(int a, int b)
@ -1681,8 +1677,7 @@ void DoLastLevelCinema()
nString++;
inputState.keyFlushChars();
inputState.ClearAllKeyStatus();
inputState.ClearAllInput();
int v11 = (kTimerTicks * (var_1C + 2)) + (int)totalclock;

View file

@ -213,8 +213,7 @@ void PlayMovie(const char* fileName)
banktail = 0;
// clear keys
inputState.keyFlushChars();
inputState.ClearAllKeyStatus();
inputState.ClearAllInput();
if (bDoFade) {
StartFadeIn();

View file

@ -2637,7 +2637,7 @@ do_default_b:
FinishLevel();
}
else {
inputState.keySetState(32, 1);
//inputState.keySetState(32, 1); // Huh, what?
}
DestroyItemAnim(nValB);

View file

@ -52,7 +52,6 @@ struct PlayerInput // TODO consider adjusting this for demo compatibility
};
void InitInput();
void ClearAllKeys();
void WaitNoKey(int nSecs, void (*pFunc) (void));
int WaitAnyKey(int nSecs);

View file

@ -318,7 +318,7 @@ void G_DoCheats(void)
// cheat string matching logic below.
Bassert(cheatbuflen < (signed)sizeof(cheatbuf));
cheatbuf[cheatbuflen] = 0;
// inputState.ClearKeysDown();
// inputState.ClearAllInput();
for (cheatNum=0; cheatNum < NUMCHEATCODES; cheatNum++)
{

View file

@ -232,8 +232,7 @@ playanm(short anim_num)
ANIMnum = anim_num;
inputState.keyFlushChars();
inputState.ClearKeysDown();
inputState.ClearAllInput();
DSPRINTF(ds,"PlayAnm");
MONO_PRINT(ds);
@ -332,8 +331,7 @@ ENDOFANIMLOOP:
videoSetPalette(0, BASEPAL, 2);
inputState.keyFlushChars();
inputState.ClearKeysDown();
inputState.ClearAllInput();
ANIM_FreeAnim();
}
END_SW_NS

View file

@ -568,7 +568,7 @@ ScenePlayBack(void)
ready2send = 0;
DemoDone = FALSE;
ResetKeys();
inputState.ClearAllInput();
while (TRUE)
{

View file

@ -1399,14 +1399,6 @@ void NewLevel(void)
FinishAnim = 0;
}
void
ResetKeys(void)
{
int i;
inputState.ClearAllKeyStatus();
}
uint8_t* KeyPressedRange(uint8_t* kb, uint8_t* ke)
{
@ -1475,7 +1467,7 @@ void LogoLevel(void)
videoNextPage();
//FadeIn(0, 3);
ResetKeys();
inputState.ClearAllInput();
while (TRUE)
{
handleevents();
@ -1555,7 +1547,7 @@ void CreditsLevel(void)
totalclock = 0;
ototalclock = 0;
ResetKeys();
inputState.ClearAllInput();
curpic = CREDITS1_PIC;
while (TRUE)
@ -1590,7 +1582,7 @@ void CreditsLevel(void)
// put up a blank screen while loading
twod->AddColorOnlyQuad(0, 0, xdim, ydim, 0xff000000);
videoNextPage();
ResetKeys();
inputState.ClearAllInput();
Mus_Stop();
}
@ -1605,7 +1597,7 @@ void SybexScreen(void)
rotatesprite(0, 0, RS_SCALE, 0, 5261, 0, 0, TITLE_ROT_FLAGS, 0, 0, xdim - 1, ydim - 1);
videoNextPage();
ResetKeys();
inputState.ClearAllInput();
while (!inputState.CheckAllInput()) handleevents();
}
@ -1637,7 +1629,7 @@ TitleLevel(void)
videoNextPage();
//FadeIn(0, 3);
ResetKeys();
inputState.ClearAllInput();
while (TRUE)
{
handleevents();
@ -1762,7 +1754,7 @@ void MenuLevel(void)
// don't allow BorderAdjusting in these menus
BorderAdjust = FALSE;
ResetKeys();
inputState.ClearAllInput();
if (SW_SHAREWARE)
{
@ -1838,8 +1830,7 @@ void MenuLevel(void)
BorderAdjust = TRUE;
//LoadGameOutsideMoveLoop = FALSE;
inputState.ClearKeyStatus(sc_Escape);
inputState.ClearKeysDown();
inputState.ClearAllInput();
M_ClearMenus();
InMenuLevel = FALSE;
twod->AddColorOnlyQuad(0, 0, xdim, ydim, 0xff000000);
@ -2070,7 +2061,7 @@ void BonusScreen(PLAYERp pp)
twod->AddColorOnlyQuad(0, 0, xdim, ydim, 0xff000000);
videoNextPage();
inputState.ClearKeysDown();
inputState.ClearAllInput();
totalclock = ototalclock = 0;
limit = synctics;
@ -4004,8 +3995,7 @@ void GetMessageInput(PLAYERp pp)
case -1: // Cancel Input (pressed ESC) or Err
MessageInputMode = FALSE;
InputMode = FALSE;
inputState.ClearKeysDown();
inputState.keyFlushChars();
inputState.ClearAllInput();
break;
case FALSE: // Input finished (RETURN)
if (MessageInputString[0] == '\0')
@ -4013,8 +4003,7 @@ void GetMessageInput(PLAYERp pp)
// no input
MessageInputMode = FALSE;
InputMode = FALSE;
inputState.ClearKeysDown();
inputState.keyFlushChars();
inputState.ClearAllInput();
buttonMap.ClearButton(gamefunc_Inventory);
}
else
@ -4041,9 +4030,7 @@ void GetMessageInput(PLAYERp pp)
// broadcast message
MessageInputMode = FALSE;
InputMode = FALSE;
inputState.ClearKeysDown();
inputState.keyFlushChars();
buttonMap.ClearButton(gamefunc_Inventory);
inputState.ClearAllInput();
for (i = 0; i < NUMGAMEFUNCTIONS; i++)
buttonMap.ClearButton(i);

View file

@ -2339,7 +2339,6 @@ void AnimateCacheCursor(void); // game.c
void MapSetAll2D(uint8_t fill); // game.c
void TerminateGame(void); // game.c
void TerminateLevel(void); // game.c
void ResetKeys(void); // game.c
void drawoverheadmap(int cposx,int cposy,int czoom,short cang); // game.c
void COVERsetbrightness(int bright, unsigned char *pal); // game.c
void DrawMenuLevelScreen(void); // game.c