thrive/01_text_adventure/input.c
2024-08-28 17:27:35 -04:00

45 lines
972 B
C

#include "../raylib.h"
#include "input.h"
#include <stdio.h>
#define ENTER 257
#define BACKSPACE 259
void handleKeyPress(Input*, int);
void handle_pressed_keys(Input *input) {
int c;
while(c = GetKeyPressed()) {
handleKeyPress(input, c);
}
}
void handleKeyPress(Input *input, int c) {
if (c == BACKSPACE) {
pop_character(input);
} else if (c == ENTER) {
clear_input_buffer(input);
} else if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
push_character(input, c);
}
}
void push_character(Input *input, int c) {
if (input->input_length < INPUT_BUFFER_MAX_LENGTH) {
input->input_buffer[input->input_length++] = c;
input->input_buffer[input->input_length] = 0;
}
}
void pop_character(Input *input) {
if (input->input_length > 0) {
input->input_buffer[input->input_length - 1] = 0;
input->input_length--;
}
}
void clear_input_buffer(Input *input) {
input->input_buffer[0] = 0;
input->input_length = 0;
}