Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Good way to create a menu ?  (Read 2741 times)

0 Members and 1 Guest are viewing this topic.

valenciano

  • Newbie
  • *
  • Posts: 16
    • View Profile
Good way to create a menu ?
« 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

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Good way to create a menu ?
« Reply #1 on: June 24, 2018, 06:53:04 pm »
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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Good way to create a menu ?
« Reply #2 on: June 24, 2018, 10:24:52 pm »
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.
Back to C++ gamedev with SFML in May 2023

valenciano

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Good way to create a menu ?
« Reply #3 on: June 25, 2018, 09:59:02 pm »
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 ?

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Good way to create a menu ?
« Reply #4 on: June 25, 2018, 10:59:27 pm »
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.

valenciano

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: Good way to create a menu ?
« Reply #5 on: June 26, 2018, 09:27:14 pm »
Thanks for your answer,

Where can I learn more about this state machine design concept then ? It would be really interesting

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Good way to create a menu ?
« Reply #6 on: June 27, 2018, 02:52:58 am »
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.
Back to C++ gamedev with SFML in May 2023

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Good way to create a menu ?
« Reply #7 on: June 27, 2018, 07:41:41 am »
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
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/