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

Author Topic: Is it fine to have sf::Font and sf::Text as global  (Read 9030 times)

0 Members and 1 Guest are viewing this topic.

Neil

  • Newbie
  • *
  • Posts: 27
    • View Profile
Re: Is it fine to have sf::Font and sf::Text as global
« Reply #15 on: December 29, 2014, 02:46:29 am »
You can get the address of this singleton:

http://stackoverflow.com/questions/270947/can-any-one-provide-me-a-sample-of-singleton-in-c/271104#271104

Why is a singleton a bad idea for a resource manager which stores unchanging resources loaded from disk? I'm not a singleton fanatic, believe it or not, but if there's ever a good use of a singleton, this seems to be the one.

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Is it fine to have sf::Font and sf::Text as global
« Reply #16 on: December 29, 2014, 02:56:08 am »
I forgot about instance functions and thats not exactly what I meant. If you dont have any "instance" of a singleton, but instead just a class with static methods (Like sf::Keyboard), you cannot get the address of it.

My main argument for not using a singleton is that if you dont handle your instances properly then the destruction can cause crashes when exiting the program (But who cares right? It's already closed). Otherwise, as long as the structure is robust and all that good stuff I dont see why it cant work, just my preference I suppose.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Is it fine to have sf::Font and sf::Text as global
« Reply #17 on: December 29, 2014, 03:01:38 am »
While I tend to lean on the side of singletons being just as bad as globals, at some point it comes down to how you define a "singleton," and I suspect we're on the verge of talking past each other because of that.

The key thing in this context is that the resources (fonts and texts) should have a clear ownership and access policy that the implementer consciously chose.  Globals and singletons typically result from programmers not bothering to think about that, which is the real reason they're so problematic.

paupav has to make this choice since he knows far more about his program than we do.  But I can think of at least three sane options that might fit:

1) If the "menu" class is the only thing that ever uses these resources, then those resources should be static members of menu, the constructor loads them if necessary, and nobody except menu's methods gets access to them.  Nice and simple.

2) If there are multiple GUI objects that all share these resources (say a few menus, some buttons, some labels, and so on), it may make sense to have a class like GUIManager that owns the common resources and passes them to the individual objects (hopefully in the form of a const reference).  Maybe you already have a class managing all these objects and this should simply be added to it.

3) If multiple objects share these resources and a manager class doesn't make a lot of sense for whatever reason (nothing else for it to manage, the objects are all of very different types, etc) then you should create a class that does nothing but manage resources and instantiate it in main().  You then pass the rest of your objects/subsystems a reference (again, ideally const) to either the manager itself or specific resources it loaded.  There may be a little bit of #2 involved here if your program has a lot of layers.

#3 is probably what most of us would recommend doing in most game-like programs.  It may seem like a pain because you have to pass a resource or a resource manager through a bunch of functions instead of just making them globally accessible, but that's a good thing.  It forces you to design your classes and their interfaces in a way that makes it easy to inject dependencies like this, rather than relying on a singleton or a bunch of globals just "being there".

Personally, the only thing that I have no qualms about making global/singleton is a logger.  You want the ability to log stuff in literally every single function in your program, and you want there to be only one logger for all those functions, but adding an extra parameter to literally every function is a bit silly.  A resource manager class definitely shouldn't be used in every function, so you should at least make an effort to do that without a global/singleton.
« Last Edit: December 29, 2014, 03:10:30 am by Ixrec »

paupav

  • Full Member
  • ***
  • Posts: 156
    • View Profile
    • Email
Re: Is it fine to have sf::Font and sf::Text as global
« Reply #18 on: December 30, 2014, 05:40:17 pm »
Ok, I just wanted to know if there is any downside of having it global, and I've got answer.
I will try to find out what is Resoruce Manager such as:
 https://github.com/SFML/SFML/wiki/Tutorial:-Image-Manager

@Ixrec Thanks for the alternatives, but I think that resource manager should work fine in this case, so I'll stick with #2.

So I have to replace namespaces with classes then.

Also I've found this and I think that this should be in the tutorials.
https://github.com/SFML/SFML/wiki/FAQ#prog-global

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Is it fine to have sf::Font and sf::Text as global
« Reply #19 on: December 30, 2014, 05:55:29 pm »
In principle doesn't belong in the SFML tutorials because they're tutorials about SFML, not general programming or C++ programming.  But considering how much general programming stuff is already in that FAQ, and scattered all over this forum, I wouldn't exactly be opposed to the tutorials having a "General Programming Guidelines" section at the top to cover "basic" things like globals and RAII for the newbies we inevitably get.  That might be worth a thread over in general discussions.

I skimmed that Image Manager tutorial just to be safe and it looks pretty good to me.  The only thing I'd mention is that since all that code specifies sf::Image, if you ever want multiple types of resources to be neatly managed you may want to make it a template class.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Is it fine to have sf::Font and sf::Text as global
« Reply #20 on: December 30, 2014, 06:08:28 pm »
In principle doesn't belong in the SFML tutorials because they're tutorials about SFML, not general programming or C++ programming.  But considering how much general programming stuff is already in that FAQ, and scattered all over this forum, I wouldn't exactly be opposed to the tutorials having a "General Programming Guidelines" section at the top to cover "basic" things like globals and RAII for the newbies we inevitably get.  That might be worth a thread over in general discussions.
Yeah, a well thought out list of common problem issues/topics and better ways to do things that we can point new users at would be good. Perhaps we should just create a wiki page for that.

I skimmed that Image Manager tutorial just to be safe and it looks pretty good to me.  The only thing I'd mention is that since all that code specifies sf::Image, if you ever want multiple types of resources to be neatly managed you may want to make it a template class.
Or one could just use the Thor Resource Module.
« Last Edit: December 30, 2014, 06:10:24 pm by Jesper Juhl »

 

anything