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

Author Topic: SFGUI with SFML Game  (Read 2279 times)

0 Members and 1 Guest are viewing this topic.

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
SFGUI with SFML Game
« on: January 01, 2014, 02:31:07 pm »
Hi everyone,

I wonder: How do you "correctly", as in good manner, use SFGUI in a game? I looked at the examples and they all use one big class with the methods and stuff and simply call the run() function in main().

However, I see two problems with this:
A: I can't possbly put my whole code in one class and
B: I'd like to seperate game  code from GUI code, but how can I do this, since the GUI needs an event loop too?
Do I have to check out threads?

Also, is there a way to write callback functions etc. and use SFGUI, without packing everything in one huge class? I don't think it is a really great design...

Kind Regards

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10823
    • View Profile
    • development blog
    • Email
Re: SFGUI with SFML Game
« Reply #1 on: January 01, 2014, 03:17:36 pm »
The examples are of course examples. They don't represent how one should write applications with SFGUI, but they are there to present features of SFGUI through example source code.
Your application/game should of course not be fitted into one big class, but packed into classes as needed. How exactly that should look like is hard to say, since it largely depends on your overall design. To be honest however I haven't found a satisfying design myself in the past.

B: I'd like to seperate game  code from GUI code, but how can I do this, since the GUI needs an event loop too?
In which way do you want to separate them? In theory events wouldn't even really be something of your game code, but it's rather layer below GUI and game. If you abstract the event handling, you might end up with code that would dispatch the events and then pass it to the game or GUI or both, depending in which state you are (HUD menu, game play, etc).

Do I have to check out threads?
Why do you get that idea? I really wonder why people always think "threads" will solve all problems... In reality they mostly create more problems, than they solve.

Also, is there a way to write callback functions etc. and use SFGUI, without packing everything in one huge class?
Check out the latest source code with C++11 support (i.e. std::function). ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Raincode

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: SFGUI with SFML Game
« Reply #2 on: January 01, 2014, 03:52:13 pm »
Hi, thanks very much for your reply.

I'm glad to hear those threads aren't necessary, I totally don't want to use that stuff^^

My Problem just is, he uses a big class with a run function containing an event loop (which, when running, my other code wouldn't be executed) and you can't just write a callback function without a class, because the the compiler will see it and say "What the *** is a button?", because I first create that button widget in main(), so something like
// Pseudocode
callback()
{
    button->SetLabel("Peace");
}

main()
auto button = createButton()
button->GetSignal.connect(std::bind(callback()));
 
won't compile

Say I have a main menu and a little window with different options ingame, like a HUD, do I just write to classes for the menu and for the HUD and depending on the state, run the appropriate one? Ok, however,
how do I run the HUD class and still have my normal game loop? I can't do
main()
    Hud.run(); // This enters an loop which goes as long as my application is running
    Game.run(); // so this basically never executes