SFML community forums
Help => General => Topic started by: valenciano on June 24, 2018, 03:39:34 pm
-
Hello everyone !
Here is a small project I have written :
https://ufile.io/pjlv0
You will find all the source code above.
I'm trying to create a main menu but I'm not sure the logic is good, what is the right way to create a main menu (and submenus) with SFML ? Because I don't see how to write "clean" code where there would be event handling for the menu and then event handling for the main game.
My current logic is to create a Game object where the window is opened and destroyed when the user exits the window.
Regards
-
There's never just one solution to a problem. ;)
The general suggested approach is to use a sort of state machine.
Also for code sharing it's generally better to use Gist or Pastebin, as people (me included) won't download a random archive.
-
If you mean OS looking ones at the top like in Notepad, Firefox, Visual Studio, etc. have then you could use something like ImGUI (there is a SFML backend in SFML Proejcts forum).
If you mean something else than it's up to you to do it and it depends on what you want.
-
Thanks for your answers,
Yes I mean a game menu with text and a cursor like I did in my project that you can download.
Nothing relating to an OS menu, just a game menu.
So, how would you handle the events for both the menu and then the game ?
-
As eXpl0it3r mentioned, one viable approach would be to use a state machine design. There are several different ways you could implement a state machine, but the high level idea is you have some way of keeping track of what state you are in. Then, only pass events to your menu objects when you are in a "menu" state and only pass events to you game objects when you are in your "game" state.
-
Thanks for your answer,
Where can I learn more about this state machine design concept then ? It would be really interesting
-
The most basic machine is just keeping some state on side and using both it and input to decide what to do and how to change the state.
http://gameprogrammingpatterns.com/state.html
https://en.wikipedia.org/wiki/Finite-state_machine
Try to avoid more CS sounding parts of Wikipedia though, those and related things are huge in CS, compiler theory, proving stuff and complexity, etc. so there's a lot of really scientific and math-y stuff online too. I've had tons of it as part of my own degree.
-
Implementation wise it might just be a variable that tells you whether you're in the menu state or game state (or game over state, etc).
More advanced approaches use dedicated classes for state management. Here's an example that isn't really perfect, but may help you understand the concept: https://github.com/eXpl0it3r/SmallGameEngine