From cb9c9d13a429e2f8f7b0c5feb858d08f39ea54f0 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Sat, 8 Mar 2025 19:27:02 -0500 Subject: [PATCH] Only draw the tiles you need to --- level.c | 11 +++++++---- player.c | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/level.c b/level.c index 7b8410d..2311e17 100644 --- a/level.c +++ b/level.c @@ -36,17 +36,20 @@ void init_level(Game *g, Level *l, char *filepath) { } 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; +void draw_tile(Level *l, float x, float y) { + size_t index = (size_t)(x + (y * l->width)) % l->data_size; + size_t data = l->data[index]; Color c = COLORS[data]; DrawRectangle(x * 32, y * 32, 32, 32, c); } void draw_level(Level *l) { Camera2D *camera = l->game->camera; + float y = l->game->player->position.y; + y /= 32; + for (int i = 0; i < l->width; i++) { - for (int j = 0; j < l->length; j++) { + for (int j = y - 15; j < y + 15; j++) { draw_tile(l, i, j); } } diff --git a/player.c b/player.c index 0bfe5d2..98ee806 100644 --- a/player.c +++ b/player.c @@ -36,6 +36,7 @@ void handle_player_input(Player *p) { void draw_player(Player *p) { DrawTextureRec(p->spritesheet, (Rectangle) { 128, 128, 128, 128 }, (Vector2) { p->position.x - 64, p->position.y - 29 }, WHITE); + DrawRectangle(p->position.x, p->position.y, 5, 5, BLUE); } void move_player(Player *p) {