WERE BACK BABEY

This commit is contained in:
Bill Rossi 2025-01-02 20:57:00 -05:00
parent 72a07a9446
commit 630d40120b
7 changed files with 60 additions and 10 deletions

View File

@ -1,7 +1,12 @@
build:
gcc log.c input.c main.c -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 -o game
CC=gcc
CFLAGS=-Wall -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
run: build
.PHONY: clean run
game:
$(CC) *.c $(CFLAGS) -o game
run: game
./game
clean:

10
01_text_adventure/game.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef _FD_GAME_
#define _FD_GAME_
#include <stdbool.h>
typedef struct GameState {
bool *should_close;
} GameState;
#endif

View File

@ -2,6 +2,7 @@
#include "input.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ENTER 257
#define BACKSPACE 259
@ -29,8 +30,14 @@ void handleKeyPress(Input *input, int c) {
if (c == BACKSPACE) {
pop_character(input);
} else if (c == ENTER) {
printf("%s", input->input_buffer);
fflush(stdout);
if (strcmp(input->input_buffer, "QUIT") == 0) {
*(input->gs->should_close) = true;
} else {
push_line_to_log(input->log, input->input_buffer);
clear_input_buffer(input);
}
} else if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == ' ') {
push_character(input, (char) c);
}
@ -67,3 +74,7 @@ Input *create_input(Vector2 position) {
input->input_buffer[0] = 0;
return input;
}
void free_input(Input* input) {
free(input);
}

View File

@ -1,3 +1,7 @@
#ifndef _FD_INPUT_
#define _FD_INPUT_
#include "game.h"
#include "log.h"
#define INPUT_BUFFER_MAX_LENGTH 80
@ -6,8 +10,12 @@ typedef struct Input {
int input_length;
Vector2 position;
Log *log;
GameState *gs;
} Input;
void handle_pressed_keys(Input*);
void draw_text(Input*);
Input *create_input(Vector2);
void free_input(Input*);
#endif

View File

@ -5,7 +5,7 @@
#include <string.h>
void draw_log(Log* log) {
for(int line_num = log->line_count - 1; line_num >= 0; line_num--) {
for(int line_num = 0; line_num < log->line_count; line_num++) {
DrawText(
log->lines[line_num],
190,
@ -29,3 +29,10 @@ void push_line_to_log(Log* log, char* line) {
log->lines[log->line_count - 1] = malloc(strlen(line) + 1);
strcpy(log->lines[log->line_count - 1], line);
}
void free_log(Log* log) {
for(int line_num = 0; line_num < log->line_count; line_num++) {
free(log->lines[line_num]);
}
free(log->lines);
}

View File

@ -1,5 +1,5 @@
#ifndef LOG
#define LOG
#ifndef _FD_LOG_
#define _FD_LOG_
typedef struct Log {
char** lines;
@ -9,5 +9,6 @@ typedef struct Log {
Log *create_log(void);
void draw_log(Log*);
void push_line_to_log(Log*, char*);
void free_log(Log*);
#endif

View File

@ -1,8 +1,10 @@
#include "game.h"
#include "../raylib.h"
#include "log.h"
#include "input.h"
#include <stdlib.h>
#include <stdbool.h>
#define TARGET_FPS 60
@ -11,12 +13,17 @@ int main(void)
InitWindow(800, 450, "Text Adventure");
SetTargetFPS(TARGET_FPS);
GameState gs;
gs.should_close = malloc(1);
*(gs.should_close) = false;
Log *log = create_log();
Vector2 input_position = { 190, 200 };
Input *input = create_input(input_position);
input->log = log;
input->gs = &gs;
while (!WindowShouldClose())
while (!WindowShouldClose() && !*(gs.should_close))
{
BeginDrawing();
ClearBackground(BLACK);
@ -26,7 +33,8 @@ int main(void)
EndDrawing();
}
CloseWindow();
free(input);
free_input(input);
free_log(log);
return 0;
}