From 3d2384983846293f9e18a769ed64056cc0ee4ac8 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sat, 8 Mar 2025 10:08:44 -0500 Subject: [PATCH] Draw the level --- game.c | 4 ++++ game.h | 2 ++ level.c | 28 ++++++++++++++++++++++++++++ level.h | 19 +++++++++++++++++++ 4 files changed, 53 insertions(+) create mode 100644 level.c create mode 100644 level.h diff --git a/game.c b/game.c index 8d25ab6..7d3e8dc 100644 --- a/game.c +++ b/game.c @@ -33,6 +33,9 @@ void initialize_game(Game *g) { g->bullets->entities = malloc(10 * sizeof(Entity*)); g->bullets->count = 0; g->bullets->capacity = 10; + + g->level = malloc(sizeof(Level)); + init_level(g, g->level); } void add_entity(Entities *entities, Entity *e) { @@ -113,6 +116,7 @@ void run_frame(Game *g) { void draw_frame(Game *g) { BeginDrawing(); ClearBackground(RAYWHITE); + draw_level(g->level); draw_player(g->player); FOREACH_ENEMY { INIT_ENEMY; diff --git a/game.h b/game.h index b4b9806..6f3bbba 100644 --- a/game.h +++ b/game.h @@ -16,11 +16,13 @@ typedef struct Game Game; #include #include "player.h" #include "entity.h" +#include "level.h" struct Game { Player *player; Entities *enemies; Entities *bullets; + Level *level; /* Levels levels; */ diff --git a/level.c b/level.c new file mode 100644 index 0000000..9489c66 --- /dev/null +++ b/level.c @@ -0,0 +1,28 @@ +#include "level.h" + +void init_level(Game *g, Level *l) { + l->width = 50; + l->length = 30; + l->data_size = l->width * l->length; + l->data = malloc(l->data_size * sizeof(char)); + for (int i = 0; i < l->data_size; i++) { + l->data[i] = 3; + } + l->game = g; +} + +Color COLORS[4] = {BLACK, DARKGRAY, GRAY, LIGHTGRAY}; +void draw_tile(Level *l, int x, int y) { + size_t index = x + (y * l->width); + size_t data = l->data[index] & 3; + Color c = COLORS[data]; + DrawRectangle(x * 32, y * 32, 32, 32, c); +} + +void draw_level(Level *l) { + for (int i = 0; i < l->width; i++) { + for (int j = 0; j < l->length; j++) { + draw_tile(l, i, j); + } + } +} diff --git a/level.h b/level.h new file mode 100644 index 0000000..750f90f --- /dev/null +++ b/level.h @@ -0,0 +1,19 @@ +#ifndef LEVEL_H +#define LEVEL_H + +typedef struct Level Level; + +#include "game.h" + +struct Level { + char *data; + size_t data_size; + int length; + int width; + Game *game; +}; + +void init_level(Game *g, Level *l); +void draw_level(Level *l); + +#endif