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 @@ - - + + diff --git a/src/res/tiny.tmx b/src/res/tiny.tmx index 6d593af..ece2481 100644 --- a/src/res/tiny.tmx +++ b/src/res/tiny.tmx @@ -1,16 +1,16 @@ - + - + - + @@ -21,38 +21,30 @@ - + -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,9,13,13,13,13,13,5,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,3,4,4,4,4,4,2,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,9,13,13,13,13,13,5,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,3,4,4,4,4,4,2,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0, +0,0,9,13,13,13,13,13,5,0,0, +0,0,3,4,4,4,4,4,2,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,9,13,13,13,13,13,5,0,0, +0,0,3,4,4,4,4,4,2,0,0, +0,0,0,0,0,0,0,0,0,0,0 - + -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,38,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,56,53,54,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,38,0,0,0, +0,0,56,53,54,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0 diff --git a/src/tilemap.c b/src/tilemap.c index 34e7c43..4819053 100644 --- a/src/tilemap.c +++ b/src/tilemap.c @@ -90,14 +90,36 @@ collision_T tilemap_area(struct tilemap *tilemap, int x1, int y1, int x2, int y2 void tilemap_background(struct tilemap *tilemap, int x, int y, int w, int h) { SDL_SetRenderDrawColor(renderer, tilemap->backdrop.r, tilemap->backdrop.g, tilemap->backdrop.b, tilemap->backdrop.a); SDL_RenderFillRect(renderer, &(SDL_Rect) {0, 0, w, h}); + int bx = 0, by = 0, vw = w, vh = h; + if (x < 0) { + bx = -x; + vw = w + x * 2; + x = 0; + } + if (y < 0) { + by = -y; + vh = h + y * 2; + y = 0; + } for (int i = 0; i < tilemap->middleground; i++) { - SDL_RenderCopy(renderer, tilemap->tilemaps[i], &(SDL_Rect) {x * tilemap->parallax[i].x, y * tilemap->parallax[i].y, w, h}, &(SDL_Rect) {0, 0, w, h}); + SDL_RenderCopy(renderer, tilemap->tilemaps[i], &(SDL_Rect) {x * tilemap->parallax[i].x, y * tilemap->parallax[i].y, vw, vh}, &(SDL_Rect) {bx, by, vw, vh}); } } void tilemap_foreground(struct tilemap *tilemap, int x, int y, int w, int h) { + int bx = 0, by = 0, vw = w, vh = h; + if (x < 0) { + bx = -x; + vw = w + x * 2; + x = 0; + } + if (y < 0) { + by = -y; + vh = h + y * 2; + y = 0; + } for (int i = tilemap->middleground; i < tilemap->layers; i++) { - SDL_RenderCopy(renderer, tilemap->tilemaps[i], &(SDL_Rect) {x * tilemap->parallax[i].x, y * tilemap->parallax[i].y, w, h}, &(SDL_Rect) {0, 0, w, h}); + SDL_RenderCopy(renderer, tilemap->tilemaps[i], &(SDL_Rect) {x * tilemap->parallax[i].x, y * tilemap->parallax[i].y, vw, vh}, &(SDL_Rect) {bx, by, vw, vh}); } } -- cgit 1.4.1-2-gfad0