0
\$\begingroup\$

I'm using C++ and SDL2 to learn about coding by creating a clone of a 2d tile-based game I enjoy. To setup my problem; There can be multiple items dropped on a tile by the player or other entities which can overlap and be placed at any location within the tile.

All items on screen are just sdl rects with textures rendered to them however the items usually aren't squares. If multiple items are stacked on top of each other I'm not sure how to determine which item they are actually trying to select.

I can find items easily if there is only ever one in a tile or if they stay exclusively out of each others textures but with overlapping textures im stumped.

Can someone explain a method for me to determine which item the player is trying to select?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

I can't comment, so i'll write it as an answer. I know about one way of doing it.

It sounds like you want something like pixel-perfect mouse selection. i.e. You only select an item if you click on a opaque pixel of the items' sprite? But then, if you are just learning to code, it might not be the easiest thing to implement..

I'm not familiar with SDL2. It could provide some utilities for this?

If not, you would have to use shaders. Here is one example tutorial of how you could do it. It's OpenGL with Java. But I'm sure you could find similar videos or posts for C++ / SDL2. Either way, the video intro briefly explains the concept.

Maybe another solution would be to scroll through the items when you hover over them, switching the item that is in front.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .