SFML community forums
Help => General => Topic started by: cdesseno on March 07, 2010, 09:12:21 pm
-
This question is more a general question than a SFML one. How I may declare the "sf::RenderWindow App" so I can use it in every class? Or is better passing it by a parameter?
Thanks.
-
It's perfectly fine to define a RenderWindow outside of the main() function. You just can't actually do anything with it until it's within the functions.
-
It's always better to encapsulate the render window. Normally only some classes of your application need it. In those cases it's better to give a reference to it. For example, you may use methods like:
void Render( sf::RenderTarget& target );
-
I'm not sure if I'm doing it "correctly", but I usually try not to need to do this.
I try to make my classes (except for window/drawing classes) never have to access the window themselves.
For example, if you have a class that has a sprite you need to draw, you can write a getSprite() function, that returns the sprite. Then do
RenderWindow.Draw (object.getSprite());
Someone else can hopefully explain whether or not this is a good idea... :lol:
-
Someone else can hopefully explain whether or not this is a good idea...
A class should offer services, not data. So actually, this would be a better idea:
object.Draw(RenderWindow);
This way nobody has to know that object is drawn with a sprite, all we know about this object is that it is able to draw itself into a render target.
A class that provides services and hides its internal data is generally more flexible and maintainable.
-
Yes, that makes much more sense.
A class should offer services, not data.
I probably would have told someone that even though I'm not really doing it myself, lol.
Thanks for the tips