Page 1 of 1

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

Posted: Thu Dec 17, 2020 10:33 pm
by cloudstrifer
Hi!

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

Thank you!

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

Posted: Fri Jan 08, 2021 4:46 pm
by Joe Musashi
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;

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

Posted: Thu Feb 04, 2021 12:00 am
by cloudstrifer
Very Nice.

Thank you!

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

Posted: Thu Feb 04, 2021 10:14 am
by Stef
Indeed there is no VDP_getTileMapXY(..) function ins SGDK.
The reason of that is that it's somehow slow to retrieve tile index reading VRAM, usually you should have it in RAM or ROM as you initially set it yourself :) But i admit that in some situation it can be handy to directly get it from VRAM (as long we don't do it too much)

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

Posted: Thu Feb 04, 2021 2:43 pm
by izidor
At the risk of saying something stupid,
If you are using a MAP resource on the A or B plane, there is a function," u16 MAP_getTile(Map*map, u16 x, u16 y) " in file " Map.c " maybe do a comparison with the return value and the tile index which you can find with dump VRAM with K-mod

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

Posted: Fri Feb 05, 2021 1:20 pm
by Stef
Indeed, but you need to use the MAP resource for that :)
It's also why i said "usually you should have it in RAM or ROM as you initially set it yourself", normally you almost never need to directly read the VRAM to retrieve tilemap data value.

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

Posted: Wed Feb 10, 2021 1:50 pm
by cloudstrifer
Thank you!