首页 > 解决方案 > 即使使用相同的键,如何进行有效的顺序输入和键状态?

问题描述

我正在制作一个游戏,我希望有一个系统可以检查输入组合以形成命令。我的输入适用于单次按键,同时按下多个按键,并且在某一点上使用不同的按键进行顺序输入。

我的输入系统是一个特定长度的键状态,以及 4 个相同长度的缓冲区。

int length;
const Uint8* keystate;
Uint8 *firstInput;
Uint8* secondInput;
Uint8* thirdInput;
Uint8* fourthInput;

然后当键盘类初始化时,我将它们设置为相同的长度并初始化我的键状态

keystate = SDL_GetKeyboardState(&length);
firstInput = new Uint8[length]; //all of them like this.

然后在更新期间我检查关键状态和发布这样

if(keystate[Left] && keystate[lPunch])
{
    //example
}
if(keystate[lPunch] && !keystate[Crouch] && firstInput[Crouch])
{
    //example of checking for release
}
//Outside of if statements I have the buffers mem copied, if I am doing that wrong please do tell me
//I wish for this system to be efficient
SDL_PumpEvents();
std::memcpy(firstInput, keystate, length);
std::memcpy(secondInput, keystate, length);
std::memcpy(thirdInput, keystate, length);
std::memcpy(fourthInput, keystate, length);
keystate = SDL_GetKeyboardState(&length);

系统在不使用相同键时工作,所以我如何更有效地使用它并重复键

//here's my attempt at using the same key, note nothing happens even with code inside
if(keystate[Left] && !secondInput[Left] && firstInput[Left]
{
    //Do dash
}

编辑一个:添加 memcpy 的顺序似乎确实会在处理一个在另一个前面时改变结果,例如

memcpy(secondInput, firstInput, length);
memcpy(firstInput, keystate, length);

这与我的破折号配对似乎让玩家不断冲刺

标签: c++inputsdlgame-enginesdl-2

解决方案


推荐阅读