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

Author Topic: Architecture from a basic state manager  (Read 1888 times)

0 Members and 1 Guest are viewing this topic.

Michalus

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Architecture from a basic state manager
« on: July 13, 2013, 09:59:04 am »



Hi !
My Questions:
Whats the best way, how I can set the state from the state manager in a state?

Give it a better way as my class Graphics to protect the properties from the render window?

And whats your meaning about this?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Architecture from a basic state manager
« Reply #1 on: July 13, 2013, 01:10:53 pm »
Does Game inherit State? That would be meaningful, since the game is a state like the main menu.

How does the StateManager store the states? Does it use a state stack, where new states can be pushed on top of others? To set a state, you could have a mapping from an enum to the state.

An adapter class Graphics is a good way to provide only a subset of the sf::RenderWindow functionality.
« Last Edit: July 13, 2013, 01:12:58 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Michalus

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: Architecture from a basic state manager
« Reply #2 on: July 13, 2013, 05:28:14 pm »
Yes with a stack.
Oh I have forgot the push and opo methods, but thats in this case not Important.
I want to knwo the best way, how I State kann change the State of the Statemanager without an pointer to the Statemnager, because I want to protect the update, draw, handleEvent methods from a State(Why should a State run the update Method!?).

Anonymouseable

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Architecture from a basic state manager
« Reply #3 on: July 16, 2013, 12:22:20 am »
You can make the handleEvents/update/draw methods private and make the Game be a friend class of StateManager, then have a pointer to the StateManager as a member of the State class.

I think.

Please don't do this without anyone who knows what they're doing confirming what I've said.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11016
    • View Profile
    • development blog
    • Email
Re: Architecture from a basic state manager
« Reply #4 on: July 16, 2013, 02:35:34 am »
You can make the handleEvents/update/draw methods private and make the Game be a friend class of StateManager, then have a pointer to the StateManager as a member of the State class.
Bad idea. The keyword friend plays very rarely a role. If you're forced to use it, the design might not be very good.

There are many approaches for states. An easy implementation is, where you give the state itself full control and each state decide which state will run next, e.g. the state manager calls stateX.run() and then checks what run returns and based on that information starts the next state. With that there's no need for the state to know anything about the state manager and the draw/update/handelEvent functions are encapsulated within the state class - only the run function is exposed.
This way is very easy to implement and works good for about all cases. Of course there are other ways, but I'm not going further into them. You can always test things out and see what fits your needs best.
There's never "the one and only solution". ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/