SFML community forums

Help => General => Topic started by: physicskush on January 16, 2022, 12:30:56 am

Title: SFML adding a menu bar to my project
Post by: physicskush on January 16, 2022, 12:30:56 am
Hello people, I developed a chess game using SFML and now I'm stuck on trying to add a menu bar at the top (with buttons). I am asking you guys for help on how should I approach this problem.

From my understanding, I need an extra panel on top of my preexisting window, wherein I will add the buttons under a certain layout. So maybe extend the height of my window, and draw my chess board with a vertical offset downwards? But then, in my chess game I use the
event.mouseButton.y
coordinates all the time so will I have to subtract the height offset? That seems like an incorrect approach.

What is a good approach to this problem, can anyone guide me? I used
RenderWindow window(VideoMode(640,640), "Chess Game", style::Titlebar | Style::Close)
to generate my window object.
Title: Re: SFML adding a menu bar to my project
Post by: Nexus on January 16, 2022, 11:52:10 am
From my understanding, I need an extra panel on top of my preexisting window, wherein I will add the buttons under a certain layout. So maybe extend the height of my window, and draw my chess board with a vertical offset downwards?
Yes, that's one option. Another would be to keep drawing the board from Y=0 downwards, and set up a sf::View which begins at negative Y.

But then, in my chess game I use the
event.mouseButton.y
coordinates all the time so will I have to subtract the height offset? That seems like an incorrect approach.
In general, you should differentiate between "viewport coordinates" (like mouse) and "world coordinates" (your chess board). Always translate from one to the other via mapPixelToCoords() (https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1RenderTarget.php#a2d3e9d7c4a1f5ea7e52b06f53e3011f9), don't just static_cast. Then, it's also very easy to add custom logic in just one place instead of dozens.