add api FIFO based character input.

This commit is contained in:
ushiostarfish 2019-11-09 20:46:41 +09:00
parent 5d9df629d7
commit bbc3480ad0
2 changed files with 42 additions and 0 deletions

View File

@ -353,6 +353,9 @@ static char currentKeyState[512] = { 0 }; // Registers current frame key s
static int lastKeyPressed = -1; // Register last key pressed static int lastKeyPressed = -1; // Register last key pressed
static int exitKey = KEY_ESCAPE; // Default exit key (ESC) static int exitKey = KEY_ESCAPE; // Default exit key (ESC)
static unsigned int inputCharacterQueue[16] = { 0 }; // Input characters stream queue as produced by the operating system text input system
static int inputCharacterQueueCount = 0; // Input characters stream queue count
#if defined(PLATFORM_RPI) #if defined(PLATFORM_RPI)
// NOTE: For keyboard we will use the standard input (but reconfigured...) // NOTE: For keyboard we will use the standard input (but reconfigured...)
static struct termios defaultKeyboardSettings; // Used to store default keyboard settings static struct termios defaultKeyboardSettings; // Used to store default keyboard settings
@ -2213,6 +2216,32 @@ int GetKeyPressed(void)
return lastKeyPressed; return lastKeyPressed;
} }
bool HasWaitingInputCharacter()
{
return 0 < inputCharacterQueueCount;
}
unsigned int PullNextInputCharacter()
{
if (inputCharacterQueueCount <= 0)
{
return 0;
}
// take a character from the head
unsigned int c = inputCharacterQueue[0];
// shift elements 1 step toward the head.
inputCharacterQueueCount--;
for (int i = 0; i < inputCharacterQueueCount; i++)
{
inputCharacterQueue[i] = inputCharacterQueue[i + 1];
}
// this is not required, but this can keep clean memory
inputCharacterQueue[inputCharacterQueueCount] = 0;
return c;
}
// Set a custom key to exit program // Set a custom key to exit program
// NOTE: default exitKey is ESCAPE // NOTE: default exitKey is ESCAPE
void SetExitKey(int key) void SetExitKey(int key)
@ -3919,6 +3948,16 @@ static void CharCallback(GLFWwindow *window, unsigned int key)
// http://www.glfw.org/docs/latest/input_guide.html#input_char // http://www.glfw.org/docs/latest/input_guide.html#input_char
lastKeyPressed = key; lastKeyPressed = key;
// If the capacity over, is will waste the old one.
static const int CAPACITY = sizeof(inputCharacterQueue) / sizeof(inputCharacterQueue[0]);
if (CAPACITY <= inputCharacterQueueCount)
{
PullNextInputCharacter();
}
// add to queue
inputCharacterQueue[inputCharacterQueueCount++] = key;
} }
// GLFW3 CursorEnter Callback, when cursor enters the window // GLFW3 CursorEnter Callback, when cursor enters the window

View File

@ -977,6 +977,9 @@ RLAPI bool IsKeyUp(int key); // Detect if a key
RLAPI int GetKeyPressed(void); // Get latest key pressed RLAPI int GetKeyPressed(void); // Get latest key pressed
RLAPI void SetExitKey(int key); // Set a custom key to exit program (default is ESC) RLAPI void SetExitKey(int key); // Set a custom key to exit program (default is ESC)
RLAPI bool HasWaitingInputCharacter(); // Check if input character exists at least one in the internal input character stream. The characters are produced by the operating system text input system.
RLAPI unsigned int PullNextInputCharacter(); // Pull a input character from the the internal input character stream
// Input-related functions: gamepads // Input-related functions: gamepads
RLAPI bool IsGamepadAvailable(int gamepad); // Detect if a gamepad is available RLAPI bool IsGamepadAvailable(int gamepad); // Detect if a gamepad is available
RLAPI bool IsGamepadName(int gamepad, const char *name); // Check gamepad name (if available) RLAPI bool IsGamepadName(int gamepad, const char *name); // Check gamepad name (if available)