Design questions - return pointer or not?

I've written a resource management class for SFML so that I can avoid worrying about handling my images/sounds/etc and dump responsibility on it.

Right now, I can use it like so:

1
2
3
4
5
6
7
8
9
10
11
12
sfexspace::ResourceManager RManager;
if (!RManager.LoadImage("src/Spacefighter.png", "Spaceship"))
    return EXIT_FAILURE;
if (!RManager.LoadSound("src/mus/chimes.wav", "Chimes"))
    return EXIT_FAILURE;

sf::Image* pImg = RManager.GetImage("Red Ball");
sf::Sprite sprite;
if (pImg)  // MAKE SURE THE POINTER IS NOT NULL
    sprite.SetImage(*pImg);

// Do whatever you want with the sprite.. 


However, I'm thinking of changing it so that I can obtain resources directly, without forcing the user to rely on and check pointers. This means that if the resource cannot be found by its respective GetSomeResource() function, I'd need to do something besides return NULL. Would this be a good situation to throw an exception? Or is my current design better?
Having to explicitly load the images before you use them kind of defeats the point of a resource manager IMO.

I would just have a GetImage or whatever function. That function would see if the image is already loaded. If not, it would load it and return it. Otherwise it would return the resource that's already loaded.

In the event of a failed load, throwing an exception certainly seems to be preferable here so you dont' have to put error checks everywhere.
How would that work? If I attempted to retrieve an image that didn't exist, how could I know where to load it from? My goal with this was to be able to load all resources at the very beginning, then not have to worry about their cleanup/scope/filepath later.

I really am curious to see how you could implement that sort of GetImage function, since if the function failed to find the image, I don't know how you could have it load and return the correct image.
Maybe you're looking for different things in a resource manager than me =P

My goal with this was to be able to load all resources at the very beginning, then not have to worry about their cleanup/scope/filepath later.


Loading all resources at the beginning isn't necessarily a good thing. Especially not for larger projects. If you have a lot of resources, loading all of them would cause a long load time at the beginning program and would use tons of memory.

I've found that it's better to load resources as you need them, rather than all at once. At long as the resource isn't loaded during something time-critical (I usually like to load mine "between screens"). If done that way, loading is fast enough to appear instantaneous to the user as long as you don't have to load a bunch all at once.

As for specifying a separate "key" for which to access the resource (like your "Chimes" / "Spaceship"), this seems like extra work to me. Why not just use the path+filename as the key and save yourself a step?

I really am curious to see how you could implement that sort of GetImage function, since if the function failed to find the image, I don't know how you could have it load and return the correct image.


It's not hard if you use the path+filename as the key:
GetImage("mypath/myfile.png");

If you don't want to deal with full paths, then make it a constant that's defined elsewhere:

Get(Resource::Chimes); where Resource::Chimes == "src/mus/chimes.wav"
Last edited on
Ooh..I get it.

I'd figured at first that filepaths would be inconvenient to work with, but this seems really cool. I think I'm going to end up rewriting this, haha.

Thanks for the help :)
np

When I get home from work (and if I remember) I'll show you the SFML-based resource manager I'm currently using. It has an added feature of keeping track of the last time a resource was used, so that resources that haven't been used in a long time can be dropped.
Topic archived. No new replies allowed.