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

Author Topic: State machine and sf::Drawable  (Read 5424 times)

0 Members and 1 Guest are viewing this topic.

EBregains

  • Newbie
  • *
  • Posts: 1
    • View Profile
State machine and sf::Drawable
« on: August 07, 2021, 12:57:35 am »
I have recently created my account here and wanted to ask this simple question just to start getting familiar with the forum.

I am starting my first project with SFML wich is a basic 2D scroller prototype. I decided to implement a State Machine for the Main Menu and the Game scene, using inheritance. Something like:

// Base class
class State {
...
virtual void Update(float deltaTime);
...
}
// Derived classes
class MainMenu : public State {
...
}
class GameScene : public State {
...
}

Should I make my State class inherit from sf::Drawable? That enables me to do:

_window->draw(myAmazingState.top());
//  rather than myAmazingState.top().Draw(_window);

Do you think this will cause me any trouble late in the development?

By the way, this will be called in my Application::Draw() function:

void Application::Draw() {
_window->clear(0,0,0,255);
...
_window->draw(myAmazingState.top());
...
_window->display();
}

I found SFML very well written. So I would love to start participating in this froum. Thanks in advance!
« Last Edit: August 07, 2021, 01:01:12 am by EBregains »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: State machine and sf::Drawable
« Reply #1 on: August 07, 2021, 12:13:31 pm »
Should I make my State class inherit from sf::Drawable? That enables me to do:

_window->draw(myAmazingState.top());
//  rather than myAmazingState.top().Draw(_window);
The question you should ask yourself: what do you want to achieve with the sf::Drawable? Ideally the answer is not syntax on its own, but rather some of the abstraction it provides. Do you use the sf::RenderStates parameter, for example?

If there's no benefit right now, I would tend to keep things simple. It's not uncommon that user classes have a custom Update() and Draw() function, for game logic and rendering. The advantage of a custom function is that you can pass extra parameters that don't fit the sf::Drawable interface, for example:
void GameScene::Draw(bool showGui, /* other options */)

If it should really prove to be a problem, it's quite an easy and quick change.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: