Initial commit

This commit is contained in:
Bill Rossi 2024-08-28 17:27:35 -04:00
commit c214227275
5 changed files with 90 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
raylib.h
game

View File

@ -0,0 +1,8 @@
build:
gcc main.c input.c -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 -o game
run: build
./game
clean:
rm -f ./game

44
01_text_adventure/input.c Normal file
View File

@ -0,0 +1,44 @@
#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;
}

View File

@ -0,0 +1,9 @@
#define INPUT_BUFFER_MAX_LENGTH 80
typedef struct Input {
char input_buffer[INPUT_BUFFER_MAX_LENGTH];
int input_length;
} Input;
void handle_pressed_keys(Input*);
void clear_input_buffer(Input*);

27
01_text_adventure/main.c Normal file
View File

@ -0,0 +1,27 @@
#include "../raylib.h"
#include "input.h"
#include <stdlib.h>
#define TARGET_FPS 60
int main(void)
{
InitWindow(800, 450, "Text Adventure");
SetTargetFPS(TARGET_FPS);
Input *input = malloc(sizeof(Input));
clear_input_buffer(input);
while (!WindowShouldClose())
{
BeginDrawing();
ClearBackground(BLACK);
handle_pressed_keys(input);
DrawText(input->input_buffer, 190, 200, 20, RAYWHITE);
EndDrawing();
}
CloseWindow();
return 0;
}