From 900ffecae885c388d8afef90aedd259002a7dc51 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Fri, 3 Jan 2025 21:24:56 -0500 Subject: [PATCH] Add a room and the ability to look at it --- 01_text_adventure/game.c | 4 ++-- 01_text_adventure/game.h | 3 +++ 01_text_adventure/input.c | 5 +++++ 01_text_adventure/input.h | 1 + 01_text_adventure/main.c | 6 ++++++ 01_text_adventure/room.h | 11 +++++++++++ 6 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 01_text_adventure/room.h diff --git a/01_text_adventure/game.c b/01_text_adventure/game.c index 47dec9a..74320cb 100644 --- a/01_text_adventure/game.c +++ b/01_text_adventure/game.c @@ -4,7 +4,7 @@ void gs_handle_command(GameState *gs, const char *command) { Input *input = gs->input; if (strcmp(input->input_buffer, "QUIT") == 0) { *(gs->should_close) = true; - } else { - push_input_buffer_to_log(input); + } else if (strcmp(input->input_buffer, "LOOK") == 0) { + push_line_to_log(input->log, gs->rooms[0].description); } } diff --git a/01_text_adventure/game.h b/01_text_adventure/game.h index 9e4cab3..fcd5d67 100644 --- a/01_text_adventure/game.h +++ b/01_text_adventure/game.h @@ -4,10 +4,13 @@ #include typedef struct GameState GameState; #include "input.h" +#include "room.h" struct GameState { bool *should_close; Input *input; + Room *rooms; + int rooms_count; }; void gs_handle_command(GameState *gs, const char *command); diff --git a/01_text_adventure/input.c b/01_text_adventure/input.c index d70cb9c..6e7ff88 100644 --- a/01_text_adventure/input.c +++ b/01_text_adventure/input.c @@ -30,10 +30,15 @@ void push_input_buffer_to_log(Input *input) { push_line_to_log(input->log, input->input_buffer); } +void push_command_to_log(Input *input) { + push_line_to_log(input->log, &(input->command)); +} + void handleKeyPress(Input *input, int c) { if (c == BACKSPACE) { pop_character(input); } else if (c == ENTER) { + push_command_to_log(input); gs_handle_command(input->gs, input->input_buffer); clear_input_buffer(input); } else if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == ' ') { diff --git a/01_text_adventure/input.h b/01_text_adventure/input.h index a6ce9eb..b3a3f57 100644 --- a/01_text_adventure/input.h +++ b/01_text_adventure/input.h @@ -9,6 +9,7 @@ typedef struct Input Input; #define INPUT_BUFFER_MAX_LENGTH 80 struct Input { + char command; // Don't move this char input_buffer[INPUT_BUFFER_MAX_LENGTH]; int input_length; Vector2 position; diff --git a/01_text_adventure/main.c b/01_text_adventure/main.c index ccd08df..4a0b855 100644 --- a/01_text_adventure/main.c +++ b/01_text_adventure/main.c @@ -21,8 +21,14 @@ int main(void) { Input *input = create_input(input_position); input->log = log; input->gs = gs; + input->command = '>'; // Don't change this gs->input = input; + gs->rooms = malloc(sizeof(Room)); + Room *room = &gs->rooms[0]; + room->name = "First room"; + room->description = "You are in an enormous room. It is big but empty."; + while (!WindowShouldClose() && !*(gs->should_close)) { BeginDrawing(); diff --git a/01_text_adventure/room.h b/01_text_adventure/room.h new file mode 100644 index 0000000..5e7bb51 --- /dev/null +++ b/01_text_adventure/room.h @@ -0,0 +1,11 @@ +#ifndef _FD_ROOM_ +#define _FD_ROOM_ + +typedef struct Room Room; + +struct Room { + char *name; + char *description; +}; + +#endif