SFML community forums

Help => Window => Topic started by: dotty on February 16, 2012, 02:24:57 pm

Title: Can i pass a RenderWindow to another class
Post by: dotty on February 16, 2012, 02:24:57 pm
Can I pass a RenderWindow object to another class and use it's method (like .Draw()) inside the other class, rather having to run Draw a million times in my main.cpp file?
Title: Can i pass a RenderWindow to another class
Post by: TheCake on February 16, 2012, 03:56:12 pm
Of course, but make sure you don't copy it. Use references (or pointer) to the sf::RenderWindow.

Code: [Select]
class MyCustomClass
{
    public:
        void classMethod(sf::RenderWindow& windowRef);
}


You can also store a reference in your class.
Title: Can i pass a RenderWindow to another class
Post by: dotty on February 16, 2012, 09:13:06 pm
Could you expand that. I'm defining sf::RenderWindow& win in the my class, and when I try to set it to a RenderWindow reference I'm getting a "noncopyable" error.
Title: Can i pass a RenderWindow to another class
Post by: TheCake on February 16, 2012, 11:40:30 pm
I suppose you want to do something like that ...
Code: [Select]
#include <SFML/Graphics.hpp>

class MyClass
{
public:

    MyClass(sf::RenderWindow& window) :
        m_window(window),
        m_text("My Text")
    {}

    void Draw()
    {
        m_window.Draw(m_text);
    }

private:

    sf::RenderWindow& m_window;
    sf::Text m_text;
};

int main(int argc, char* argv[])
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Test");

    MyClass myObject(window);

    while(window.IsOpen())
    {
        sf::Event ev;
        while(window.PollEvent(ev))
        {
            if(ev.Type == sf::Event::Closed)
                window.Close();
        }

        window.Clear();
        myObject.Draw();
        window.Display();
    }

    return 0;
}


Remember that a reference MUST be initialized when it's declared. In the case of an object member reference, it must be initialized in the constructor's initialization list. If you can't initialize the reference, you may consider using a pointer (which can be set at any time).
Title: Can i pass a RenderWindow to another class
Post by: dotty on February 16, 2012, 11:57:19 pm
Ok, that makes sense. How would I get that to work in something like this

Code: [Select]


#include <SFML/Graphics.hpp>

class MyClass {
public:
    MyClass(sf::RenderWindow& window);
    void Draw();
private:
    sf::RenderWindow& m_window;
    sf::Text m_text;
};

MyClass::MyClass(sf::RenderWindow& window){
this->m_window = window;
this->m_text = sf::Text("Hello World");
}

void MyClass::Draw(){
m_window.Draw(m_text);
}



int main(int argc, char* argv[])
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Test");

    MyClass myObject(window);

    while(window.IsOpen())
    {
        sf::Event ev;
        while(window.PollEvent(ev))
        {
            if(ev.Type == sf::Event::Closed)
                window.Close();
        }

        window.Clear();
        myObject.Draw();
        window.Display();
    }

    return 0;
}

Title: Can i pass a RenderWindow to another class
Post by: RaptorIV on February 17, 2012, 01:34:37 am
try an initializing m_window in an initialization list:

Code: [Select]

MyClass::MyClass(sf::RenderWindow& window)
: m_window(window)
{
   this->m_text = sf::Text("Hello World");
}
Title: Can i pass a RenderWindow to another class
Post by: Nexus on February 18, 2012, 01:15:01 pm
There are lots of beginner questions like this recently. I suggest to read a C++ book before using SFML, otherwise you will always have problems and use overcomplicated workarounds because you're not familiar with the programming language.