SFML community forums

Help => Graphics => Topic started by: Deftwun on March 26, 2011, 03:28:04 am

Title: Noob: pass &RenderWindow to constructor. Set as Public v
Post by: Deftwun on March 26, 2011, 03:28:04 am
So basically I'm trying to pass a reference to an sf::renderwindow to the class 'game_object' and then set that as a public referenced object within the class itself.

That way any of Game_objects members can draw to that window without the reference being explicitly passed to the member itself (thats no problem just seems inefficient).

Heres a snippet of what I'm trying to do.

Class Header
Code: [Select]
class Game_Object {
    public:

        // constructor / destructor
        Game_Object(sf::RenderWindow &Window);
        virtual ~Game_Object();
 
        //{ SFML stuff
        sf::RenderWindow Draw_target;
        sf::Sprite Sprite

};


.cpp Constructor

Code: [Select]
Game_Object::Game_Object(sf::RenderWindow &Window){
    Draw_target = &Window;
}


Main
Code: [Select]
int main{
   sf::RenderWindow GameWindow;
   Game::Object Ship(GameWindow);
}



I've tried making 'draw_target' a pointer and using the '->' operator to access the 'draw' member of the render window. But the compiler didn't like that. Didn't seem right to me either. I mean its a pointer TO an object not an object itself right?

So how is this normally done if at all?
Title: Noob: pass &RenderWindow to constructor. Set as Public v
Post by: kalgon on March 26, 2011, 09:51:32 am
there are several possibilities, depends on what you do with your classes

yes you can pass a reference in the constructor (and store it as member)

OR

you can inherit Game from RenderWindow, so every game instance has game properties and IS a renderwindow, so you can draw directly

OR

if you have many instance of Game for only one window, you can use a static member
Title: Noob: pass &RenderWindow to constructor. Set as Public v
Post by: Nexus on March 26, 2011, 12:41:23 pm
You can't copy sf::RenderWindow, so one possibility is to store a reference to it. Reference members have to be initialized in the constructor initializer list. We have recently discussed a similar topic (http://www.sfml-dev.org/forum/viewtopic.php?t=4434).

Why do you even pass the sf::RenderWindow to the constructor? Can't the class create its own window?