simplified per advice of @upkb

This commit is contained in:
benev0 2023-12-19 17:24:08 -05:00
parent 10fb93d500
commit 8326af5272

View File

@ -17,10 +17,7 @@
#include "raylib.h" #include "raylib.h"
#define MAX_INPUT_CHARS 40 #define MAX_INPUT_CHARS 50
bool isKeyChar(int);
//------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------
// Program main entry point // Program main entry point
@ -33,11 +30,10 @@ int main(void)
const int screenHeight = 450; const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [text] example - backspace input"); InitWindow(screenWidth, screenHeight, "raylib [text] example - backspace input");
SetTargetFPS(1);
char textField[MAX_INPUT_CHARS + 1] = "\0"; char textField[MAX_INPUT_CHARS + 1] = "\0";
int usedSize = 0; int textPos = 0;
SetTargetFPS(1);
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
// Main game loop // Main game loop
@ -46,56 +42,41 @@ int main(void)
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
// get first letter and key from the appropiate queues
int letter = GetCharPressed(); int letter = GetCharPressed();
int key = GetKeyPressed(); int key = GetKeyPressed();
while (key || letter) while (letter || key)
{ {
// if a the backspace key was pressed and there is a character to be deleted
// ingest (key) backspace // then read backspace in from key queue
// if possible delete one character from the string if (key == KEY_BACKSPACE && textPos > 0)
if (key == KEY_BACKSPACE && usedSize > 0)
{ {
textField[usedSize - 1] = 0; textField[textPos -1] = 0;
usedSize--; textPos--;
key = GetKeyPressed();
} }
// ingest (key) non character // if there is a letter and space in the string
else if (!isKeyChar(key)) // then read letter in from character queue
if (letter && textPos < MAX_INPUT_CHARS)
{ {
key = GetKeyPressed(); textField[textPos] = (char) letter;
textPos++;
} }
// ingest (letter) and (key) // get next key press and character from queue
// if possible write ascii character to the string
else if ((letter > 0) && (letter >= 32) && (letter <= 126) && (usedSize < MAX_INPUT_CHARS))
{
textField[usedSize] = (char) letter;
textField[usedSize + 1] = 0;
usedSize++;
letter = GetCharPressed(); letter = GetCharPressed();
key = GetKeyPressed(); key = GetKeyPressed();
} }
// ingest (letter) and (key)
else
{
letter = GetCharPressed();
key = GetKeyPressed();
}
}
// Draw // Draw
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
BeginDrawing(); BeginDrawing();
ClearBackground(RAYWHITE); ClearBackground(RAYWHITE);
// TODO: Draw everything that requires to be drawn at this point: DrawText("Type something:", 40, 20, 20, BLACK);
DrawText(textField, 40, 60, 20, BLACK);
DrawText(textField, 40, 200, 20, BLACK); // Example
EndDrawing(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -103,22 +84,8 @@ int main(void)
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
return 0; return 0;
} }
bool isKeyChar(int key)
{
return (key == KEY_SPACE)
|| (key == KEY_APOSTROPHE)
|| (key >= KEY_COMMA && key <= KEY_NINE)
|| (key == KEY_SEMICOLON)
|| (key == KEY_EQUAL)
|| (key >= KEY_A && key <= KEY_Z)
|| (key >= KEY_LEFT_BRACKET && key <= KEY_RIGHT_BRACKET)
|| (key == KEY_GRAVE)
|| (key >= KEY_KP_0 && key <= KEY_KP_EQUAL);
}