Tiled2GBA icon indicating copy to clipboard operation
Tiled2GBA copied to clipboard

How to load a 3 layer tilemap

Open taellinglin opened this issue 3 years ago • 2 comments

#include <string.h>
#include <maxmod.h>
#include "toolbox.h"
#include "input.h"
#include "AutumnMap.h"
#include "blocks.h"
#include "level_map.h"
#include "soundbank.h"
#include "soundbank_bin.h"
#define IRQ_VBLANK 0x001

// Size of your map
#define MapWidth 32
#define MapHeight 32

// Interpret the map as an array of screen entries (16-bit values):
u16 *background = (u16*)(Autumn_MapTileMap0);
u16 *midground = (u16*)(Autumn_MapTileMap1);
u16 *foreground = (u16*)(Autumn_MapTileMap2);
// Size of a GBA screenblock
#define SbWidth 32
#define SbHeight 32
int map_x = 0;
int map_y = 0;
// change these to load a different part of the map:
int map_offset_x = 0;
int map_offset_y = 0;
int camera_x = 0; 
int camera_y = 0 ;
int camera_tile_x = 0;
int camera_tile_y = 0;
int camera_size_x = 240;
int camera_size_y = 160;
// which screenblock to copy to
const int sbb = 30;

void load_map() {
    for (int y = 0; y < SbHeight; y++) {
        for (int x = 0; x < SbWidth; x++) {
            // figure out the coordinates within the map
            map_x = map_offset_x + x;
            map_y = map_offset_y + y;
            //se_mem[sbb][x + y*SbWidth] |= SE_PALBANK(0);
            
            /*
            // copy the entries from the map data into the screenblock:
            se_mem[sbb][x + y*SbWidth] = my_map[map_x + map_y * MapWidth];
            */
            se_mem[sbb][x + y*SbWidth] = background[map_x + map_y * MapWidth];
            se_mem[sbb+32][x + y*SbWidth] = midground[map_x + map_y * MapWidth];
            se_mem[sbb+64][x + y*SbWidth] = foreground[map_x + map_y * MapWidth];
        }
    }
}

void renderBackground(){
	// Load palette
    memcpy(pal_bg_mem, Autumn_MapPalette, Autumn_MapPaletteLength);
    // Load tiles into CBB 0
    memcpy(&tile_mem[0][0], Autumn_MapTileSet, Autumn_MapTileSetLength);
    // Load map into SBB 30
    //load_map();
    /*
    memcpy(&se_mem[30][0], level1_bin, 32768/2);
	for (int i=0; i<32768/2; i++)
		se_mem[30][i] |= SE_PALBANK(0);

    */
    // set up BG0 for a 4bpp 64x32t map, using
    //   using charblock 0 and screenblock 31
    REG_BG0CNT= BG_CBB(0) | BG_SBB(30) | BG_4BPP | BG_REG_32x32;
}


int main()
{
	irqInit();
	irqSet( IRQ_VBLANK, mmVBlank );
	irqEnable(IRQ_VBLANK);
	mmInitDefault( (mm_addr)soundbank_bin, 28 );
	mmStart( MOD_FORTRESS, MM_PLAY_LOOP );
	renderBackground();
    REG_DISPCNT= DCNT_MODE0 | DCNT_BG0 | DCNT_BG1 | DCNT_BG2;
    // Scroll around some
	int diax=0, diay=0;
    int x= 0, y= 0;
    while(1)
    {
		mmFrame();
        vid_vsync();
        key_poll();

        x += key_tri_horz();
        y += key_tri_vert();
        camera_x = x + map_offset_x;
        camera_y = y + map_offset_y;
        REG_BG0HOFS = camera_x/2;
        REG_BG0VOFS = camera_y/2;
        REG_BG1VOFS = camera_x;
        REG_BG1HOFS = camera_y;
        REG_BG2VOFS = camera_x;
        REG_BG2HOFS= camera_y;       
        
        load_map();
    }

    return 0;
}

Why is this loading funny?

taellinglin avatar May 25 '21 09:05 taellinglin

Here's how I have my tileset and tilemap in Tiled image

Here's my overcomplicated code:

#include <string.h>
#include "AutumnMap.h"
#include "toolbox.h"
#include "input.h"

renderBackground(){
        // Load palette
    memcpy(pal_bg_mem, Autumn_MapPalette, Autumn_MapPaletteLength);
    // Load tiles into CBB 0
    memcpy(&tile_mem[0][0], Autumn_MapTileSet, Autumn_MapTileSetLength);
    // Load map into SBB 30
    memcpy(&se_mem[30][0], Autumn_MapTileMap0, Autumn_MapTileMap0Length);

    // set up BG0 for a 4bpp 64x32t map, using
    //   using charblock 0 and screenblock 31
    REG_BG0CNT= BG_CBB(0) | BG_SBB(30) | BG_8BPP | BG_REG_32x32;

}

renderMidground(){
        // Load palette
    memcpy(pal_bg_mem, Autumn_MapPalette, Autumn_MapPaletteLength);
    // Load tiles into CBB 0
    memcpy(&tile_mem[1][0], Autumn_MapTileSet, Autumn_MapTileSetLength);
    // Load map into SBB 30
    memcpy(&se_mem[30][0], Autumn_MapTileMap1, Autumn_MapTileMap1Length);

    // set up BG0 for a 4bpp 64x32t map, using
    //   using charblock 0 and screenblock 31
    REG_BG1CNT= BG_CBB(1) | BG_SBB(30) | BG_8BPP | BG_REG_32x32;

}

renderForeground(){
        // Load palette
    memcpy(pal_bg_mem, Autumn_MapPalette, Autumn_MapPaletteLength);
    // Load tiles into CBB 0
    memcpy(&tile_mem[2][0], Autumn_MapTileSet, Autumn_MapTileSetLength);
    // Load map into SBB 30
    memcpy(&se_mem[30][0], Autumn_MapTileMap2, Autumn_MapTileMap2Length);

    // set up BG0 for a 4bpp 64x32t map, using
    //   using charblock 0 and screenblock 31
    REG_BG2CNT= BG_CBB(2) | BG_SBB(30) | BG_8BPP | BG_REG_32x32;

}
int main()
{

    //renderBackground();
    //renderMidground();
    renderForeground();

    REG_DISPCNT= DCNT_MODE0 |  DCNT_BG2;

    // Scroll around some
    int x= 0, y= 0;
    while(1)
    {
        vid_vsync();
        key_poll();

        x += key_tri_horz();
        y += key_tri_vert();

        REG_BG0HOFS= x;
        REG_BG0VOFS= y;
        REG_BG1HOFS = x;
        REG_BG1VOFS= y;
        REG_BG2HOFS = x;
        REG_BG2VOFS= y;
    }

    return 0;
}```

taellinglin avatar May 25 '21 16:05 taellinglin

#include <string.h>
#include "AutumnMap.h"
#include "toolbox.h"
#include "input.h"

renderBackground(){
        // Load palette
    memcpy(pal_bg_mem, Autumn_MapPalette, Autumn_MapPaletteLength);
    // Load tiles into CBB 0
    memcpy(&tile_mem[0][0], Autumn_MapTileSet, Autumn_MapTileSetLength);
    // Load map into SBB 30
    memcpy(&se_mem[31][0], Autumn_MapTileMap0, Autumn_MapTileMap0Length);

    // set up BG0 for a 4bpp 64x32t map, using
    //   using charblock 0 and screenblock 31
    REG_BG0CNT= BG_CBB(0) | BG_SBB(31) | BG_8BPP | BG_REG_32x32;

}

renderMidground(){
        // Load palette
    memcpy(pal_bg_mem, Autumn_MapPalette, Autumn_MapPaletteLength);
    // Load tiles into CBB 0
    memcpy(&tile_mem[0][0], Autumn_MapTileSet, Autumn_MapTileSetLength);
    // Load map into SBB 30
    memcpy(&se_mem[30][0], Autumn_MapTileMap2, Autumn_MapTileMap2Length);

    // set up BG0 for a 4bpp 64x32t map, using
    //   using charblock 0 and screenblock 31
    REG_BG1CNT= BG_CBB(0) | BG_SBB(30) | BG_8BPP | BG_REG_32x32;

}

renderForeground(){
        // Load palette
    memcpy(pal_bg_mem, Autumn_MapPalette, Autumn_MapPaletteLength);
    // Load tiles into CBB 0
    memcpy(&tile_mem[0][0], Autumn_MapTileSet, Autumn_MapTileSetLength);
    // Load map into SBB 30
    memcpy(&se_mem[29][0], Autumn_MapTileMap1, Autumn_MapTileMap1Length);

    // set up BG0 for a 4bpp 64x32t map, using
    //   using charblock 0 and screenblock 31
    REG_BG2CNT= BG_CBB(0) | BG_SBB(29) | BG_8BPP | BG_REG_32x32;

}
int main()
{

   
    renderBackground();
    renderMidground();
    renderForeground();

    REG_DISPCNT= DCNT_MODE0 |  DCNT_BG0 | DCNT_BG1 | DCNT_BG2;

    // Scroll around some
    int x= 0, y= 0;
    while(1)
    {
        vid_vsync();
        key_poll();

        x += key_tri_horz();
        y += key_tri_vert();

        REG_BG0HOFS= x;
        REG_BG0VOFS= y;
        REG_BG1HOFS = x;
        REG_BG1VOFS= y;
        REG_BG2HOFS = x/2;
        REG_BG2VOFS= y/2;
    }

    return 0;
}

image

It seems to be rendering absence of a tile with something arbitrary. Any Idea how to fix it?

taellinglin avatar May 25 '21 17:05 taellinglin