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

Author Topic: Where to put manager classes  (Read 2718 times)

0 Members and 1 Guest are viewing this topic.

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Where to put manager classes
« on: October 20, 2012, 02:28:32 am »
Hey, for example: textures manager, that holds map of strings to textures. Where to put it? It should be accesible from anywhere sf::Texture is... so.. everywhere, that's the problem. What are the options even? A static pointer of a class that also has static method GetReference()? Externs? Creating in main and passing pointer around anyone who is likely to request duplicates of already loaded texture?
Back to C++ gamedev with SFML in May 2023

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Where to put manager classes
« Reply #1 on: October 21, 2012, 12:06:47 pm »
It should be accesible from anywhere sf::Texture is... so.. everywhere, that's the problem.
That's indeed the problem, your assumption is wrong ;)

You don't need it everywhere. You only need it in the part of your game which is responsible for the graphics, and in particular, sf::Texture. You don't need it for physics, audio, AI, or other game logic. So you can put your manager object in a class responsible for the graphics.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Where to put manager classes
« Reply #2 on: October 21, 2012, 12:24:37 pm »
Quote
You don't need it everywhere. You only need it in the part of your game which is responsible for the graphics, and in particular, sf::Texture. You don't need it for physics, audio, AI, or other game logic. So you can put your manager object in a class responsible for the graphics.
Well yes, it 'should be acessible from few places in which I might need same textures'. So there are few locations one of these should be placed I can think of. I'm really struggling with structuring anything nicely in oop. ;D Apparently global singletons are bad so I'm avoiding them. Should I just let any group of classes that are likely to request same textures keep a pointer to their texture manager? Is that 'dependancy injection'?
Back to C++ gamedev with SFML in May 2023

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Where to put manager classes
« Reply #3 on: October 21, 2012, 02:30:56 pm »
Should I just let any group of classes that are likely to request same textures keep a pointer to their texture manager?
Yes, that is a possibility.

You can also add a small abstraction layer in-between: Each texture gets an identifier (e.g. an enum or small description string). The classes that request textures then pass this ID to a higher-level class, which in turn lookups the exact resource path and returns the sf::Texture. This has the advantage that you don't need to refactor every place where textures are processed, in case something with the loading changes.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Where to put manager classes
« Reply #4 on: October 22, 2012, 02:07:56 am »
You need to share textures between game states?

I think you can have a texture manager instance inside your game state manager class, and a public accesor, so your game states can easily access the textures ( say goodbye to evil singletons  8) )

Pseudo-code:
   texture = gameStateMgr.getTextureManager().getTexture("file.png")
 
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Where to put manager classes
« Reply #5 on: October 22, 2012, 02:13:01 am »
May or may not need to share between several instances of a something(bullet, monster, whatever) so I need a way to request texture - freshly loaded or already existing from previous request and I'm considering the options.
Back to C++ gamedev with SFML in May 2023

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Where to put manager classes
« Reply #6 on: October 22, 2012, 05:00:14 am »
In my manager class I use a getTextureref method which receives an unsigned int to get a reference of the texture according to the number given, that way I can use the texture for whatever I want as long as I have filled the object with something. I was even thinking of adding the same functionality to operator[]. In my case I fill the manager with all images(and textures afterwards) I need beforehand and then use it as needed for either sprites or a class that uses them.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!