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

Author Topic: Way to NOT pass reference to RenderWindow to a class?  (Read 3773 times)

0 Members and 1 Guest are viewing this topic.

pteberf

  • Newbie
  • *
  • Posts: 2
    • View Profile
Way to NOT pass reference to RenderWindow to a class?
« on: May 07, 2016, 04:06:51 pm »
Greetings, people!

I am developing a video game using SFML and nearly every drawable entity has its own class. There is also a somewhat engine class, containing my RenderWindow.

The problem is that in order to call window's draw() method in any drawable entity's entity_draw() method, I have to either pass a window reference to the entity_draw() method, or have a reference as a member of entity's class and initialise it in a constructor, obvioulsy passing RenderWindow reference to the constructor.

Both variants are very annoying and I am wondering whether there is a good way to "globalize" the RenderWindow somehow.

The best solution I found was creating another class, containing only the window and a videomode(MainWindow class) so they could be accessible from any project file. These two were private static members and constructor and destructor were also private, so I got somewhat of a static class. This however produced an unresolved external symbol error and trying to initialise RenderWindow and VideoMode in different ways didn't produce any result.

Help?  :D :D
Thank you.

korczurekk

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • Email
Re: Way to NOT pass reference to RenderWindow to a class?
« Reply #1 on: May 07, 2016, 04:12:47 pm »
Your class should inherit sf::Drawable and overload "draw" function, then you will be able to do something like that:
MyClass myObj1;
//...
window.draw(myObj1);
 

EDIT: http://www.sfml-dev.org/documentation/2.0/classsf_1_1Drawable.php

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Way to NOT pass reference to RenderWindow to a class?
« Reply #2 on: May 07, 2016, 04:40:12 pm »
If you want to draw to something then you have to pass this something to the function that does the drawing, I find this very natural and not annoying at all. And this is how everyone else does it too.

If you really want to use static members, then at least learn how to define them correctly (hint: Google know how to do this very well ;)).
Laurent Gomila - SFML developer

pteberf

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Way to NOT pass reference to RenderWindow to a class?
« Reply #3 on: May 07, 2016, 05:37:04 pm »
If you want to draw to something then you have to pass this something to the function that does the drawing, I find this very natural and not annoying at all. And this is how everyone else does it too.

@Laurent thanks for feedback. I'll stick with passing the window then. I just thought that not doing it might have been better and I am apparently wrong.  :D

BlueCobold

  • Full Member
  • ***
  • Posts: 105
    • View Profile
Re: Way to NOT pass reference to RenderWindow to a class?
« Reply #4 on: June 27, 2016, 12:51:05 pm »
You might consider only passing a RenderTarget& instead of a RenderWindow&.

 

anything