From f2cae9dbd96fde6eeb47d2f3ba3404944230dfba Mon Sep 17 00:00:00 2001
From: zlago
Date: Tue, 28 Jan 2025 18:35:26 +0100
Subject: fix rendering tiny rooms
---
src/main.c | 49 +++++++++++++++++++++++++++++++++++++------------
src/res/test.tmx | 4 ++--
src/res/tiny.tmx | 54 +++++++++++++++++++++++-------------------------------
src/tilemap.c | 26 ++++++++++++++++++++++++--
4 files changed, 86 insertions(+), 47 deletions(-)
diff --git a/src/main.c b/src/main.c
index 41c239b..ce94712 100644
--- a/src/main.c
+++ b/src/main.c
@@ -240,10 +240,19 @@ int game_load_level(char *level) {
game_state = STATE_FADE_OUT;
fade = 255;
- int x = (entities.player[0].x / 16) - (WINDOW_WIDTH / 2);
- int y = (entities.player[0].y / 16) - (WINDOW_HEIGHT / 2);
- if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > tilemap->width * 8) {x = tilemap->width * 8 - WINDOW_WIDTH;}
- if (y < 0) {y = 0;} else if (y + WINDOW_HEIGHT > tilemap->height * 8) {y = tilemap->height * 8 - WINDOW_HEIGHT;}
+ int x, y;
+ if (tilemap->width < WINDOW_WIDTH / 8) {
+ x = -((WINDOW_WIDTH - tilemap->width * 8) / 2);
+ } else {
+ x = (entities.player[0].x / 16) - (WINDOW_WIDTH / 2);
+ if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > tilemap->width * 8) {x = tilemap->width * 8 - WINDOW_WIDTH;}
+ }
+ if (tilemap->height <= WINDOW_HEIGHT / 8) {
+ y = -((WINDOW_HEIGHT - tilemap->height * 8) / 2);
+ } else {
+ y = (entities.player[0].y / 16) - (WINDOW_HEIGHT / 2);
+ if (y < 0) {y = 0;} else if (y + WINDOW_HEIGHT > tilemap->height * 8) {y = tilemap->height * 8 - WINDOW_HEIGHT;}
+ }
game_render(framebuffer, x, y);
SDL_SetRenderTarget(renderer, NULL);
@@ -654,10 +663,18 @@ void main_loop(void) {
}
}
- x = (entities.player[0].x / 16) - (WINDOW_WIDTH / 2);
- y = (entities.player[0].y / 16) - (WINDOW_HEIGHT / 2);
- if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > tilemap->width * 8) {x = tilemap->width * 8 - WINDOW_WIDTH;}
- if (y < 0) {y = 0;} else if (y + WINDOW_HEIGHT > tilemap->height * 8) {y = tilemap->height * 8 - WINDOW_HEIGHT;}
+ if (tilemap->width < WINDOW_WIDTH / 8) {
+ x = -((WINDOW_WIDTH - tilemap->width * 8) / 2);
+ } else {
+ x = (entities.player[0].x / 16) - (WINDOW_WIDTH / 2);
+ if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > tilemap->width * 8) {x = tilemap->width * 8 - WINDOW_WIDTH;}
+ }
+ if (tilemap->height <= WINDOW_HEIGHT / 8) {
+ y = -((WINDOW_HEIGHT - tilemap->height * 8) / 2);
+ } else {
+ y = (entities.player[0].y / 16) - (WINDOW_HEIGHT / 2);
+ if (y < 0) {y = 0;} else if (y + WINDOW_HEIGHT > tilemap->height * 8) {y = tilemap->height * 8 - WINDOW_HEIGHT;}
+ }
game_render(framebuffer, x, y);
game_render_flush(framebuffer);
@@ -675,10 +692,18 @@ void main_loop(void) {
if (fade == 255) {
if (game_load_level(game_next_level)) {
game_state = STATE_FADE_OUT;
- x = (entities.player[0].x / 16) - (WINDOW_WIDTH / 2);
- y = (entities.player[0].y / 16) - (WINDOW_HEIGHT / 2);
- if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > tilemap->width * 8) {x = tilemap->width * 8 - WINDOW_WIDTH;}
- if (y < 0) {y = 0;} else if (y + WINDOW_HEIGHT > tilemap->height * 8) {y = tilemap->height * 8 - WINDOW_HEIGHT;}
+ if (tilemap->width < WINDOW_WIDTH / 8) {
+ x = -((WINDOW_WIDTH - tilemap->width * 8) / 2);
+ } else {
+ x = (entities.player[0].x / 16) - (WINDOW_WIDTH / 2);
+ if (x < 0) {x = 0;} else if (x + WINDOW_WIDTH > tilemap->width * 8) {x = tilemap->width * 8 - WINDOW_WIDTH;}
+ }
+ if (tilemap->height <= WINDOW_HEIGHT / 8) {
+ y = -((WINDOW_HEIGHT - tilemap->height * 8) / 2);
+ } else {
+ y = (entities.player[0].y / 16) - (WINDOW_HEIGHT / 2);
+ if (y < 0) {y = 0;} else if (y + WINDOW_HEIGHT > tilemap->height * 8) {y = tilemap->height * 8 - WINDOW_HEIGHT;}
+ }
game_render(framebuffer, x, y);
SDL_SetRenderTarget(renderer, NULL);
diff --git a/src/res/test.tmx b/src/res/test.tmx
index 5ac87be..a449d81 100644
--- a/src/res/test.tmx
+++ b/src/res/test.tmx
@@ -43,8 +43,8 @@