Stephane-D / SGDK

SGDK - A free and open development kit for the Sega Mega Drive
https://www.patreon.com/SGDK
MIT License
1.78k stars 189 forks source link

Map rendering unexpected tiles when initial X position is different to 0 #223

Closed kusfo closed 3 years ago

kusfo commented 3 years ago

I was experiencing certain strange issues using MAP, usually depending on the initial position of the scroll. I was looking at the issue solved here: #209 and as it has a simple example, It's really useful for testing the bug. The code `#include

include "../res/maps.h"

include "../res/sprite.h"

include "kdebug.h"

Vect2D_s32 player = { .x = 0, .y = 600, };

Vect2D_s32 camera = { .x = 0, .y = 0, };

void handlePlayer() { u16 pad = JOY_readJoypad(JOY_1); if (pad & BUTTON_LEFT) player.x -= 8; if (pad & BUTTON_RIGHT) player.x += 8; if (pad & BUTTON_UP) player.y -= 8; if (pad & BUTTON_DOWN) player.y += 8; if (player.x < 0) player.x = 0; if (player.y < 0) player.y = 0; if (player.x > 1920) player.x = 1920; if (player.y > 1280) player.y = 1280; }

void handleCamera() { camera.x = player.x - 150; camera.y = player.y - 150; if (camera.x < 0) camera.x = 0; if (camera.y < 0) camera.y = 0; if (camera.x > 1600) camera.x = 1600; if (camera.y > 1056) camera.y = 1056; }

Sprite playerSprite; Map map;

int main(bool hardReset) { // load map VDP_loadTileSet(&resTileMap, TILE_USERINDEX, CPU); map = MAP_create(&resMap, BG_A, TILE_ATTR_FULL(PAL2, FALSE, FALSE, FALSE, TILE_USERINDEX)); // load sprites SPR_init(); playerSprite = SPR_addSprite(&resSprite, player.x, player.y, TILE_ATTR(PAL3, TRUE, FALSE, FALSE)); // init camera handleCamera(); MAP_scrollTo(map, camera.x, camera.y); // apply palettes VDP_setPalette(PAL3, resSprite.palette->data); VDP_setPalette(PAL2, resMap.palette->data); SYS_showFrameLoad(TRUE); while (TRUE) { handlePlayer(); handleCamera(); MAP_scrollTo(map, camera.x, camera.y); SPR_setPosition(playerSprite, player.x - camera.x, player.y - camera.y); SPR_update(); SYS_doVBlankProcess(); } return 0; } This is mostly @pladaria original code. If we run it, everything runs fine. However, if we modify the original x position... Vect2D_s32 player = { .x = 900,//000 .y = 600, };` And we run the code, we've the following issue:

test1 If you pay attention, ScrollX seems to be still at coordinate 0. If we move sonic a bit, everything is fixed automatically once the tilemap is updated: test2

So it seems there's an issue when starting at an horizontal coordinate other than 0. Afterwards, everything fixes by itself.

I'm also attaching the code just in case: sgdk_test_map.zip

Stephane-D commented 3 years ago

Thanks for providing a test case ! I was able to reproduce and fix it quickly :) Just update your map.c file from repository and rebuilt the library, it should be fixed now !