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

Author Topic: Declaring a global RenderWindow  (Read 3687 times)

0 Members and 1 Guest are viewing this topic.

owenmin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Declaring a global RenderWindow
« on: March 12, 2013, 02:02:23 am »
Is it wise to do so? I know it's usually bad practice to use global variables, but seeing as i will eventually have lots of things being drawn to this window from different classes/functions, can i declare it globally without causing any significant problems ?

io

  • Jr. Member
  • **
  • Posts: 52
  • z/OS by day, SFML by night
    • View Profile
Re: Declaring a global RenderWindow
« Reply #1 on: March 12, 2013, 02:17:44 am »
I recall reading this post:

http://en.sfml-dev.org/forums/index.php?topic=10855.msg74954#msg74954


Although I did a quick test and it does compile/run (though I think I misread the post now that I look back at it ;]):

#include <SFML/Graphics.hpp>

sf::RenderWindow window(sf::VideoMode(800, 600), "SFML Playground");

int main( int argc, char* argv[])
{
    sf::RectangleShape rectangle;
    rectangle.setSize(sf::Vector2f(100, 50));
    rectangle.setOutlineColor(sf::Color::Red);
    rectangle.setOutlineThickness(5);
    rectangle.setPosition(10, 20);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {

                window.close();
            }
        }
        window.draw( rectangle );
        window.display();
        window.clear();
    }
   return 0;
}
 


But why exactly would you need it global?   If you have your main game loop drawing and updating states, it should be limited to that scope?

Any object can define its own draw function

    private:
        //we use this so we can have a nice draw(object) format
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
 

void Face::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    target.draw( *m_LeftEye );
    target.draw( *m_RightEye );
    target.draw( *m_Mouth );
    target.draw( *m_Nose );
}
 


That way in main you can just do a window.draw(face)
« Last Edit: March 12, 2013, 02:27:28 am by io »

owenmin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Declaring a global RenderWindow
« Reply #2 on: March 12, 2013, 03:15:32 am »
I scrapped the idea of having a global RenderWindow as i realized there was no need for it and it's just overall bad practice.

I now have a Game class that initializes a window for me.

void Face::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    target.draw( *m_LeftEye );
    target.draw( *m_RightEye );
    target.draw( *m_Mouth );
    target.draw( *m_Nose );
}
 

So basically a class that has multiple calls to the draw function making up the face then when you do window.draw(face), this would produce a full face (theoretically) ?

If it works like that then yes, it would be a good feature to implement in the future when i get the hang of SFML.

io

  • Jr. Member
  • **
  • Posts: 52
  • z/OS by day, SFML by night
    • View Profile
Re: Declaring a global RenderWindow
« Reply #3 on: March 12, 2013, 03:29:56 am »
I probably gave a little bit of a bad example, but to explain:

I had a face object that I defined that draw function.  That draws other objects that I have that also have that draw function defined.  So in main I can just do a draw of my face object. 

(I'll post some more when I get back home)


the following is all heavy pseudo code

so in main:
renderwindow window

gameLoop()
{
   pool events

   do stuff
   
   window.clear()
   window.draw( ObjectA )
   window.display()
}
 

objecta.hpp
class
{
  public:
       ...
  private:
          //we use this so we can have a nice draw(object) format
        virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
        ....
        ObjectB anotherThing;
};
 


objecta.cpp
void Face::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
    target.draw( anotherThing );
}
 

objectb.cpp
  another draw function defined
 


You can peek at what I did w/ http://en.sfml-dev.org/forums/index.php?topic=10363.0
« Last Edit: March 12, 2013, 03:52:59 am by io »

 

anything