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()]
@ -1799,7 +1799,7 @@ void TakeScreenshot(const char *fileName)
char path[512] = { 0 }; char path[512] = { 0 };
strcpy(path, TextFormat("%s/%s", CORE.Storage.basePath, GetFileName(fileName))); strcpy(path, TextFormat("%s/%s", CORE.Storage.basePath, GetFileName(fileName)));
ExportImage(image, path); // WARNING: Module required: rtextures ExportImage(image, path); // WARNING: Module required: rtextures
RL_FREE(imgData); RL_FREE(imgData);
@ -1955,7 +1955,7 @@ const char *GetFileName(const char *filePath)
const char *GetFileNameWithoutExt(const char *filePath) const char *GetFileNameWithoutExt(const char *filePath)
{ {
#define MAX_FILENAME_LENGTH 256 #define MAX_FILENAME_LENGTH 256
static char fileName[MAX_FILENAME_LENGTH] = { 0 }; static char fileName[MAX_FILENAME_LENGTH] = { 0 };
memset(fileName, 0, MAX_FILENAME_LENGTH); memset(fileName, 0, MAX_FILENAME_LENGTH);
@ -1963,7 +1963,7 @@ const char *GetFileNameWithoutExt(const char *filePath)
{ {
strcpy(fileName, GetFileName(filePath)); // Get filename.ext without path strcpy(fileName, GetFileName(filePath)); // Get filename.ext without path
int size = (int)strlen(fileName); // Get size in bytes int size = (int)strlen(fileName); // Get size in bytes
for (int i = size; i > 0; i--) // Reverse search '.' for (int i = size; i > 0; i--) // Reverse search '.'
{ {
if (fileName[i] == '.') if (fileName[i] == '.')
@ -1974,7 +1974,7 @@ const char *GetFileNameWithoutExt(const char *filePath)
} }
} }
} }
return fileName; return fileName;
} }
@ -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