Page 1 of 1

Loading Tilesets for different levels

Posted: Wed Apr 29, 2015 12:29 am
by BroOfTheSun
Hi, I'm having trouble understanding how to load tilesets properly for multiple images to use with different levels. I want to load two images to use for the APLAN and BPLAN for one level. Then change to another level using different images for the APLAN and BPLAN. When I load the tilesets in the code for each level, I run into an issue where the second level doesn't load properly. See below for an example of this:

Code: Select all

// Start with level one. Here is an example of how I'm loading the map.
void LevelOne()
{
	u16 ind = TILE_USERINDEX;
	
	Map *map = unpackMap(levelOne_planA.map, NULL);
	Map *mapBG = unpackMap(levelOne_planB.map, NULL);
	
	VDP_loadTileSet(levelOne_planB.tileset, ind, NULL);
	VDP_setMapEx(BPLAN, mapBG, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, ind), 0, 0, 0, 0, 32, 32);
	ind += levelOne_planB.tileset->numTile;
	
	VDP_loadTileSet(levelOne_planA.tileset, ind, NULL);
	VDP_setMapEx(APLAN, map, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, ind), 0, 0, 0, 0, 32, 32);
	ind += levelOne_planA.tileset->numTile;
	
	while(level == 1)
	{
		// Logic for updating the level
	}
	
	MEM_free(map);
	MEM_free(mapBG);
}

//When I transition to level two, I follow similar logic to loading the map.
void LevelTwo()
{
	u16 ind = TILE_USERINDEX;
		
	Map *map = unpackMap(levelTwo_planA.map, NULL);
	Map *mapBG = unpackMap(levelTwo_planB.map, NULL);
	
	VDP_loadTileSet(levelTwo_planB.tileset, ind, NULL);
	VDP_setMapEx(BPLAN, mapBG, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, ind), 0, 0, 0, 0, 32, 32);
	ind += levelTwo_planB.tileset->numTile;
	
	VDP_loadTileSet(levelTwo_planA.tileset, ind, NULL);
	VDP_setMapEx(APLAN, map, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, ind), 0, 0, 0, 0, 32, 32);
	ind += levelTwo_planA.tileset->numTile;
	
	while(level == 2)
	{
		// Logic for updating the level
	}
	
	MEM_free(map);
	MEM_free(mapBG);
}
When I use the logic above to load both levels, levelTwo does not load correctly. It looks like VDP_setMapEx() is not using the right tiles to load the level. I'm thinking that I'm not setting 'ind' correctly, but I don't understand why.

I am able to resolve the issue by loading all tilemaps upfront, and keep a reference to where the ind starts for each level. Please see an example of this logic below:

Code: Select all

u16 levelOne_planAInd;
u16 levelOne_planBInd;
u16 levelTwo_planAInd;
u16 levelTwo_planBInd;

void main()
{
	u16 ind = TILE_USERINDEX;
	
	levelOne_planBInd = ind;
	VDP_loadTileSet(levelOne_planB.tileset, ind, NULL);
	ind += levelOne_planB.tileset->numTile;
	
	levelOne_planAInd = ind;
	VDP_loadTileSet(levelOne_planA.tileset, ind, NULL);
	ind += levelOne_planA.tileset->numTile;
	
	levelTwo_planBInd = ind;
	VDP_loadTileSet(levelTwo_planB.tileset, ind, NULL);
	ind += levelTwo_planB.tileset->numTile;
	
	levelTwo_planAInd = ind;
	VDP_loadTileSet(levelTwo_planA.tileset, ind, NULL);
	ind += levelTwo_planA.tileset->numTile;
	
	while(1)
	{
		while(level == 1) { LevelOne(); }
		while(level == 2) { LevelTwo(); }
	}
}

void LevelOne()
{	
	Map *map = unpackMap(levelOne_planA.map, NULL);
	Map *mapBG = unpackMap(levelOne_planB.map, NULL);
	
        // Change level to load based on ind set in main
	VDP_setMapEx(BPLAN, mapBG, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, levelOne_planBInd), 0, 0, 0, 0, 32, 32);
	
	VDP_setMapEx(APLAN, map, TILE_ATTR_FULL(PAL0, FALSE, FALSE, FALSE, levelOne_planAInd), 0, 0, 0, 0, 32, 32);
	
	while(level == 1)
	{
		// Logic for updating the level
	}
	
	MEM_free(map);
	MEM_free(mapBG);
}

...
I'm not sure if I'm loading tilesets correctly. Will I need to keep a reference of where the tile index is for each tileset? It seems like I'm missing something on how to set the tile index correctly, without having to keep a reference of where the index starts for each tileset.

Posted: Wed Apr 29, 2015 8:19 am
by Stef
There is nothing wrong with your code, the only difference is that you load all tileset in VRAM with the second method where you load only 1 level with the first method. Your code should work...
Did you try to open VDP debug of Gens/GensKMod to see if your tileset is correctly loaded for the second level ? You can also test the return of the loadTileset(..) method as it may fail when you use compressed tileset (not enough memory to unpack the tileset).

Posted: Thu Apr 30, 2015 1:23 am
by BroOfTheSun
I didn't know about the VDP debug with Gens/GensKMod. That helped me to figure out what the issue was, which was that I wasn't optimizing the tiles being loaded, and was loading way too many tiles.

I was able to resolve the issue by fixing my images and tilesets. Thanks!

Posted: Thu Apr 30, 2015 2:15 pm
by Stef
Emulators debug features are *mandatory* when you develop ;) Glad you fixed your problem !