reduce repetition, use memmove for queue popping

This commit is contained in:
Siddhartha Menon 2024-02-27 18:43:46 +05:30
parent f0807d2be1
commit e5f26e7cd6
No known key found for this signature in database
GPG Key ID: 5E98696308D4E9EB

View File

@ -106,7 +106,7 @@
#include <stdlib.h> // Required for: srand(), rand(), atexit() #include <stdlib.h> // Required for: srand(), rand(), atexit()
#include <stdio.h> // Required for: sprintf() [Used in OpenURL()] #include <stdio.h> // Required for: sprintf() [Used in OpenURL()]
#include <string.h> // Required for: strrchr(), strcmp(), strlen(), memset() #include <string.h> // Required for: strrchr(), strcmp(), strlen(), memset(), memmove()
#include <time.h> // Required for: time() [Used in InitTimer()] #include <time.h> // Required for: time() [Used in InitTimer()]
#include <math.h> // Required for: tan() [Used in BeginMode3D()], atan2f() [Used in LoadVrStereoConfig()] #include <math.h> // Required for: tan() [Used in BeginMode3D()], atan2f() [Used in LoadVrStereoConfig()]
@ -2735,48 +2735,36 @@ bool IsKeyUp(int key)
return up; return up;
} }
// Get the last key pressed static int popQueue(int* queue, int* len)
int GetKeyPressed(void)
{ {
int value = 0; int value = 0;
if (CORE.Input.Keyboard.keyPressedQueueCount > 0) if (*len > 0)
{ {
// Get character from the queue head // Get character from the queue head
value = CORE.Input.Keyboard.keyPressedQueue[0]; value = queue[0];
// Shift elements 1 step toward the head // Shift elements 1 step toward the head
for (int i = 0; i < (CORE.Input.Keyboard.keyPressedQueueCount - 1); i++) memmove(queue, queue + 1, (*len - 1) * sizeof(int));
CORE.Input.Keyboard.keyPressedQueue[i] = CORE.Input.Keyboard.keyPressedQueue[i + 1];
// Reset last character in the queue // Reset last character in the queue and update its length
CORE.Input.Keyboard.keyPressedQueue[CORE.Input.Keyboard.keyPressedQueueCount - 1] = 0; queue[*len - 1] = 0;
CORE.Input.Keyboard.keyPressedQueueCount--; (*len)--;
} }
return value; return value;
} }
// Get the last key pressed
int GetKeyPressed(void)
{
return popQueue(CORE.Input.Keyboard.keyPressedQueue, &CORE.Input.Keyboard.keyPressedQueueCount);
}
// Get the last char pressed // Get the last char pressed
int GetCharPressed(void) int GetCharPressed(void)
{ {
int value = 0; return popQueue(CORE.Input.Keyboard.charPressedQueue, &CORE.Input.Keyboard.charPressedQueueCount);
if (CORE.Input.Keyboard.charPressedQueueCount > 0)
{
// Get character from the queue head
value = CORE.Input.Keyboard.charPressedQueue[0];
// Shift elements 1 step toward the head
for (int i = 0; i < (CORE.Input.Keyboard.charPressedQueueCount - 1); i++)
CORE.Input.Keyboard.charPressedQueue[i] = CORE.Input.Keyboard.charPressedQueue[i + 1];
// Reset last character in the queue
CORE.Input.Keyboard.charPressedQueue[CORE.Input.Keyboard.charPressedQueueCount - 1] = 0;
CORE.Input.Keyboard.charPressedQueueCount--;
}
return value;
} }
// Set a custom key to exit program // Set a custom key to exit program