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

Author Topic: Can i pass a RenderWindow to another class  (Read 14143 times)

0 Members and 1 Guest are viewing this topic.

dotty

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Can i pass a RenderWindow to another class
« 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?

TheCake

  • Newbie
  • *
  • Posts: 19
    • View Profile
Can i pass a RenderWindow to another class
« Reply #1 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.

dotty

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Can i pass a RenderWindow to another class
« Reply #2 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.

TheCake

  • Newbie
  • *
  • Posts: 19
    • View Profile
Can i pass a RenderWindow to another class
« Reply #3 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).

dotty

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Can i pass a RenderWindow to another class
« Reply #4 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;
}


RaptorIV

  • Newbie
  • *
  • Posts: 30
    • View Profile
Can i pass a RenderWindow to another class
« Reply #5 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");
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6259
  • Thor Developer
    • View Profile
    • Bromeon
Can i pass a RenderWindow to another class
« Reply #6 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: first SFML book