I noticed sf::Drawable's draw(...) method is protected. Why is that? (I'm not the best C++ programmer, so pardon me if the answer's rather obvious.)
As a followup question, what is the best way to call this method from outside my implementing class, StateMenu?
Here's my parent interface:
// a state the game can be in (menu, playing, credits).
class State : public EventHandler, public sf::Drawable {
public:
virtual void update(const sf::Time &delta) = 0;
};
Here's an implementer of that interface:
// the title menu of the game.
class StateMenu : public State {
public:
bool handleEvent(sf::Event &event);
void update(const sf::Time &delta);
void draw(sf::RenderTarget &target, sf::RenderStates states) const;
private:
sf::Time mTimeElapsed;
};
And here's some snippets from main.cpp:
sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), TITLE,
sf::Style::Titlebar | sf::Style::Close);
State *state;
void render() {
window.clear(sf::Color::Black);
state->draw(window, sf::RenderStates::Default);
window.display();
}
int main() {
...
state = new StateMenu;
...
while (window.isOpen()) {
...
render();
}
return 0;
}
Sorry if I posted too much code, and thanks in advance for any and all help!
Nevermind, all. Found the problem shortly after. If I declare draw(...) public in State, I can call it from main.
class State : public EventHandler, public sf::Drawable {
public:
virtual void update(const sf::Time &delta) = 0;
virtual void draw(sf::RenderTarget &target, sf::RenderStates states) const = 0;
};
I am under the assumption that this is because my State pointer in main.cpp is of type State and not of type StateMenu.
Still curious why the protected access control modifier was chosen, if anyone might indulge me. :)
As stated, don't call draw() directly -- it is invoked by the render targets.
Instead of
object.draw(target, states);
use
target.draw(object); // or
target.draw(object, states);
Ahhh, I was tired today. I forgot that's why one would implement sf::Drawable in the first place.
This bit of code in main: state->draw(window, sf::RenderStates::Default);
was an artifact of my previous drawing system.
Appreciate the efforts, Omega/Nexus.