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

Author Topic: How do I implement a main menu.  (Read 5671 times)

0 Members and 1 Guest are viewing this topic.

superman3275

  • Newbie
  • *
  • Posts: 6
    • View Profile
How do I implement a main menu.
« on: September 29, 2012, 05:47:38 am »
Okay, so I'm the kind of person who experiments. I figured out bow to make pong without a tutorial and did it. I added a score count too. Now, I'vespent a few hours trying to implement a main menu and I don't know how. I also don'tknow how to make buttons. All help is welcomed, :)

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: How do I implement a main menu.
« Reply #1 on: September 29, 2012, 06:03:12 am »
I'm no expert myself, but I just thought of something. For example you have a background image and text over it with sf::Text. You make it so your window polls events for keys, if you tap or touch the 'w' key, the program highlights one of the texts and same with 's', 'a' and 'd'. You also have another key (your choice) that verifies which text is highlighted and does actions accordingly, such as start the game, view scores, quit, etc.

Note that not necessarily text is needed but any drawable could do. That would be a key based main menu, for mouse usage I don't have a grasp myself so I won't say anything about it. Note that I haven't made a main menu myself, this is just the idea I have for a basic one. You don't necessarily need "true" buttons, you can just have a drawable of your choice work like one.

In case you want a true button then there's SFGUI that gives you a GUI based on SFML and made for working with it (as far as I know). There must be plenty of ways to do this, don't just stick to my comments and try looking further, you may get something far better.
« Last Edit: September 29, 2012, 06:30:53 am by masskiller »
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: How do I implement a main menu.
« Reply #2 on: September 29, 2012, 10:06:29 am »
One often uses some kind of state manager to handle different states in a game (e.g. game mode + menu). A nice way is to do this with classes, but a simple switch/if statement can do the trick too. You can find some example on the wiki or you can take a look at my simple state manager.

As for buttons, first just think about what a button essentially is. It's just an image that changes its look when being pressed or hovered over it. For an implementation you can either construct such a button with shapes or use a texture. Then you can get key stroke or mouse input with the sf::Keyboard and sf::Mouse. To check whether the mouse is over a button you can make use of the contains() function of a sf::Rect.

Learning things by doing it can work to some parts, but you won't learn new techniques, thus it's better to look around in the wiki section or look at existing code etc...
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/