0
\$\begingroup\$

I'm coding a game's GUI in SDL2 and c++, I'm using SDL_RenderCopy to load the buttons on to the screen, but don't know how to get buttons in specific spots eg. bottom left corner, slightly below center. How do I achieve this?

\$\endgroup\$
1
\$\begingroup\$

You need to manually calculate the positions.

Positioning itself in SDL happens using destination rects. SDL_RenderCopy takes in a destination rectangle that you can use to scale and translate what you render.

Finding out the correct position is just a matter of math. For centering, you want to render at (windowWidth / 2.0f - buttonWidth / 2.0f, windowHeight / 2.0f - buttonHeight / 2.0f). Simply offset the Y coordinate if you need to render a button just below. For bottom-left, set the X coordinate to 0, and don't divide the window height.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.