SGDK移植MegaDrive游戏时前2个Tile加载异常问题求助
Let's break down your problems and fix them step by step:
1. Fixing the First 2 Tiles Not Loading Correctly
The root issue here is a missing offset in your tile index mapping. When you call LoadMaps(TITLETILES, 2, TitleTiles, 1,1,0);, you're loading your TitleTiles tileset starting at tile position 2 in VRAM. But in your LoadMaps function, you're setting tilemap entries using the raw Tile[i][j] values (like 0 and 1) which point to unloaded tile positions—SGDK defaults tile 0 to transparent, and tile 1 has no data, hence the weird display behavior.
The Fix:
Update your LoadMaps function to add the TileLoc offset to each tile index when setting the tilemap:
void LoadMaps(int Tile[30][40], int TileLoc, Image TileImage, int PaletteNum, int Priority, int BGNum) { VDP_loadTileSet(TileImage.tileset,TileLoc,CPU); VDP_setPalette(PaletteNum, TileImage.palette->data); for (unsigned int i = 0; i < 30; i++) { for (unsigned int j = 0; j < 40; j++) { // Add TileLoc offset to point to the correct loaded tile position VDP_setTileMapXY(BGNum,TILE_ATTR_FULL(PaletteNum, Priority, FALSE, FALSE, Tile[i][j] + TileLoc),j,i); } } }
This ensures your TITLETILES array's indices (0,1,2...) map to the actual VRAM positions where you loaded your tiles (2,3,4...).
If you prefer to load tiles starting at position 0 instead (to avoid offsets entirely), just change your initial call to:
LoadMaps(TITLETILES, 0, TitleTiles, 1,1,0);
Just make sure you don't overwrite this tile range with other assets later.
2. Simplifying Tile Flip Operations
You don't need to create redundant flipped tile copies—SGDK's TILE_ATTR_FULL macro has built-in parameters for flipping, which modify how the hardware renders the tile without changing the underlying tile data.
How to Use It:
The third and fourth parameters of TILE_ATTR_FULL control horizontal (hFlip) and vertical (vFlip) flipping. Set them to TRUE to enable flipping:
// Horizontal flip VDP_setTileMapXY(BGNum,TILE_ATTR_FULL(PaletteNum, Priority, TRUE, FALSE, tile_index),x,y); // Vertical flip VDP_setTileMapXY(BGNum,TILE_ATTR_FULL(PaletteNum, Priority, FALSE, TRUE, tile_index),x,y); // Both flips at once VDP_setTileMapXY(BGNum,TILE_ATTR_FULL(PaletteNum, Priority, TRUE, TRUE, tile_index),x,y);
To Make It Reusable:
Modify your LoadMaps function to accept flip parameters, so you can toggle flipping for entire maps without rewriting code:
void LoadMaps(int Tile[30][40], int TileLoc, Image TileImage, int PaletteNum, int Priority, int BGNum, bool hFlip, bool vFlip) { VDP_loadTileSet(TileImage.tileset,TileLoc,CPU); VDP_setPalette(PaletteNum, TileImage.palette->data); for (unsigned int i = 0; i < 30; i++) { for (unsigned int j = 0; j < 40; j++) { VDP_setTileMapXY(BGNum,TILE_ATTR_FULL(PaletteNum, Priority, hFlip, vFlip, Tile[i][j] + TileLoc),j,i); } } }
Call it like this to apply horizontal flip to your title map:
LoadMaps(TITLETILES, 2, TitleTiles, 1,1,0, TRUE, FALSE);
This keeps your code clean and avoids redundant tile data in your assets or code.
内容的提问来源于stack exchange,提问作者Crisps

