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

Author Topic: Noob: pass &RenderWindow to constructor. Set as Public v  (Read 1873 times)

0 Members and 1 Guest are viewing this topic.

Deftwun

  • Newbie
  • *
  • Posts: 27
    • View Profile
Noob: pass &RenderWindow to constructor. Set as Public v
« 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?

kalgon

  • Newbie
  • *
  • Posts: 37
    • View Profile
Noob: pass &RenderWindow to constructor. Set as Public v
« Reply #1 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

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Noob: pass &RenderWindow to constructor. Set as Public v
« Reply #2 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.

Why do you even pass the sf::RenderWindow to the constructor? Can't the class create its own window?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything