Get Tile Index of background specific position (X, Y)

SGDK only sub forum

Moderator: Stef

Post Reply
cloudstrifer
Very interested
Posts: 102
Joined: Mon Feb 19, 2018 7:31 pm

Get Tile Index of background specific position (X, Y)

Post by cloudstrifer » Thu Dec 17, 2020 10:33 pm

Hi!

Can someone help me?
I need to know tile index in specific position of background.

Thank you!

Joe Musashi
Interested
Posts: 10
Joined: Sat Oct 13, 2018 10:08 pm

Re: Get Tile Index of background specific position (X, Y)

Post by Joe Musashi » Fri Jan 08, 2021 4:46 pm

I just ran into the same problem.

SGDK does have a VDP_setTileMapXY() function in SGDK/src/vdp_tile.c. You can modify the code so that it reads from VRAM, something like:

Code: Select all

u16 VDP_getTileMapXY(VDPPlane plane, u16 x, u16 y)
{
    vu32* const plctrl = (u32*)GFX_CTRL_PORT;
    vu16* const pwdata = (u16*)GFX_DATA_PORT;

    const u16 addr = VDP_getPlaneAddress(plane, x, y);

    *plctrl = GFX_READ_VRAM_ADDR((u32)addr);
    return *pwdata;
}
Then you get the index by masking out the attributes:

Code: Select all

u16 tile = VDP_getTileMapXY(...);
u16 tileIndex = tile & ~TILE_ATTR_MASK;

Post Reply