Draw the level

This commit is contained in:
Bill Rossi 2025-03-08 10:08:44 -05:00
parent daf7317c08
commit 3d23849838
4 changed files with 53 additions and 0 deletions

4
game.c
View File

@ -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;

2
game.h
View File

@ -16,11 +16,13 @@ typedef struct Game Game;
#include <stdlib.h>
#include "player.h"
#include "entity.h"
#include "level.h"
struct Game {
Player *player;
Entities *enemies;
Entities *bullets;
Level *level;
/*
Levels levels;
*/

28
level.c Normal file
View File

@ -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);
}
}
}

19
level.h Normal file
View File

@ -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