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

SGDK only sub forum

Moderator: Stef

Post Reply
cloudstrifer
Very interested
Posts: 118
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: 17
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;

cloudstrifer
Very interested
Posts: 118
Joined: Mon Feb 19, 2018 7:31 pm

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

Post by cloudstrifer » Thu Feb 04, 2021 12:00 am

Very Nice.

Thank you!

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

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

Post by Stef » Thu Feb 04, 2021 10:14 am

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)

izidor
Interested
Posts: 15
Joined: Tue Jan 26, 2021 12:20 pm

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

Post by izidor » Thu Feb 04, 2021 2:43 pm

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

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

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

Post by Stef » Fri Feb 05, 2021 1:20 pm

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.

cloudstrifer
Very interested
Posts: 118
Joined: Mon Feb 19, 2018 7:31 pm

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

Post by cloudstrifer » Wed Feb 10, 2021 1:50 pm

Thank you!

Post Reply