aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorUshio <ushiostarfish@gmail.com>2019-11-24 20:46:00 +0900
committerRay <raysan5@gmail.com>2019-11-24 12:46:00 +0100
commitae301a1d233d9fcde676e0572cc2eab9206a8ad2 (patch)
treeb3658c0519a97ab0b0cf06c49b9e60f673461432
parentb132ae099b325f4f10e947af1016293a8b6d3256 (diff)
downloadraylib-ae301a1d233d9fcde676e0572cc2eab9206a8ad2.tar.gz
raylib-ae301a1d233d9fcde676e0572cc2eab9206a8ad2.zip
add api FIFO based character input. (#1012)
* add api FIFO based character input. * rename input character functions * replace tab to space x4 #1012
-rw-r--r--src/core.c39
-rw-r--r--src/raylib.h3
2 files changed, 42 insertions, 0 deletions
diff --git a/src/core.c b/src/core.c
index 518472e2..5d258a3a 100644
--- a/src/core.c
+++ b/src/core.c
@@ -353,6 +353,9 @@ static char currentKeyState[512] = { 0 }; // Registers current frame key s
static int lastKeyPressed = -1; // Register last key pressed
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)
// NOTE: For keyboard we will use the standard input (but reconfigured...)
static struct termios defaultKeyboardSettings; // Used to store default keyboard settings
@@ -2226,6 +2229,32 @@ int GetKeyPressed(void)
return lastKeyPressed;
}
+bool IsCharAvailable()
+{
+ return 0 < inputCharacterQueueCount;
+}
+unsigned int GetNextChar()
+{
+ 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
// NOTE: default exitKey is ESCAPE
void SetExitKey(int key)
@@ -3932,6 +3961,16 @@ static void CharCallback(GLFWwindow *window, unsigned int key)
// http://www.glfw.org/docs/latest/input_guide.html#input_char
lastKeyPressed = key;
+
+ // If the capacity over, is will waste the old one.
+ static const int CAPACITY = sizeof(inputCharacterQueue) / sizeof(inputCharacterQueue[0]);
+ if (CAPACITY <= inputCharacterQueueCount)
+ {
+ GetNextChar();
+ }
+
+ // add to queue
+ inputCharacterQueue[inputCharacterQueueCount++] = key;
}
// GLFW3 CursorEnter Callback, when cursor enters the window
diff --git a/src/raylib.h b/src/raylib.h
index a0bc09f4..3d7eae31 100644
--- a/src/raylib.h
+++ b/src/raylib.h
@@ -978,6 +978,9 @@ RLAPI bool IsKeyUp(int key); // Detect if a key
RLAPI int GetKeyPressed(void); // Get latest key pressed
RLAPI void SetExitKey(int key); // Set a custom key to exit program (default is ESC)
+RLAPI bool IsCharAvailable(); // 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 GetNextChar(); // Pull a input character from the the internal input character stream
+
// Input-related functions: gamepads
RLAPI bool IsGamepadAvailable(int gamepad); // Detect if a gamepad is available
RLAPI bool IsGamepadName(int gamepad, const char *name); // Check gamepad name (if available)