SFML Resource Manager

For my game i decided I want to make a resource manager. I've seen ones but have no clue how they work, is there a tutorial or resource manager example somewhere? (using SFML 2.0?) Or an explenation on a simple resource manager would help :)

Thanks for any responces :)
Start by working out what you want it to do. What do you want it do?
Thank you for the response

I want it to load a resource, mainly textures, to asign it to a Sprite. To delete a texture and to make a pointer to the address to once the texture is destroyed I can still use it through the pointer (correct me if I'm mistaken)

So basically I want to be able to control my textures, sprites and later on sounds from the manager class :)
to once the texture is destroyed I can still use it through the pointer (correct me if I'm mistaken)


You're mistaken. Once the object is destroyed, you shouldn't try using it.
I meant that once the function ends something is destroyed, i'm not sure what is. But when I load a texture in a function. When I try to draw it with a different function it doesn't draw. I read something about how a resource manager can fix that.

Hopefully you get what I mean :)
Sounds like you don't need a resource manager. You just need to control the lifetime of the object.

You can do this by creating the texture on the heap, using new. Pass the pointer to it around to anyone who needs it, and when you're done with it, delete it.

Once you understand how you can control the lifetime of an object like this, you'll be ready to do it with a smart pointer of some kind, and ultimately to do it with a resource manager object. The point is that you need to understand how to create an object and how to destroy it when you're done with it.
Congratulations on 6000 posts btw :)

Okay, so I don't need a resource manager. Is there any chance you can give me an example of how to create my texture on the heap? And how to delete it

I appreciate all your help, thank you
How are you creating textures at the moment?
This is how I created my texture. However when I tried to draw it outside the function It didn't work

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Graphics::DrawCircles(sf::RenderWindow& window)
{
	sf::Sprite circle;

	circle.setPosition(10, 10);
	circle.setTexture(texture);
	window.draw(circle);
}

Graphics::Graphics()
{
	if (!texture.loadFromFile("circle.png"))
	{
		std::cout << "Error loading Texutre: circle\n";
	}
}
The object named texture. Where is it created? I see you using it; I don't see it being created.
In graphics.h

Should've it been created in the DrawCircles function?
Inside the DrawCircles function, the only variables that will exist are:

1) Any global variables that exist everywhere (and are generally considered a bad idea).
2) Any variables you passed into the function (i.e. the parameters)
3) Any variables you created inside the function

This object named texture doesn't seem to be any of those.

You should create a variable wherever it's convenient and sensible to do so. There's no absolute right answer, but creating variables inside a header file is generally a bad idea.

In this case, you need the texture to exist and continue to exist. If you're just experimenting with simple code to see how your chosen framework works, you could do worse than simply creating the texture at the start of main, and passing around a pointer to it as needed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int main()
{
  // whatever else has to happen

  // Load a texture from a file
  sf::Texture sometextureObject;
  sometextureObject.loadFromFile("circle.png");

  // Now, any function that needs the texture should be passed a pointer to it

  // For example:

  drawCircleUsingThisTexture(window, &sometextureObject);

...


where

1
2
3
4
5
6
7
8
void drawCircleUsingThisTexture(sf::RenderWindow& window, sf::Texture* someTexture)
{
	sf::Sprite circle;

	circle.setPosition(10, 10);
	circle.setTexture(someTexture);
	window.draw(circle);
}


As you can see, in this simple example, the texture will exist for the duration of the function main, and you can pass around a pointer to it to whatever functions you need, which is what the SFML functions tend to want if I recall correctly.

Last edited on
What happens when my game gets to big to have all these textures loaded from the main? Is there an alternate method to do this?
Yes there is. You create the objects as you need them, and pass around pointers, and when you're sure that nobody needs the texture anymore, you destroy it.

sf::Texture* pointerToSometextureObject = new ( sf::Texture );
This creates an sf::Texture object on the heap.

pointerToSometextureObject->loadFromFile("circle.png");
Load it and do whatever else needs to be done with it, using the pointer

drawCircleUsingThisTexture(window, pointerToSometextureObject);
Pass around the pointer to it to whoever needs it

delete pointerToSometextureObject;
When you're sure nobody needs it anymore, delete it. This will call its destructor function, and mark the heap memory it was taking up as available for reuse.

You could create it anywhere you like. Just keep hold of the pointer to it. Of course, if you create them all over the place you'll soon lose track of what you've created when, so it's on you to think about what you're doing in advance.

This being the future, C++11 has some helpful smart pointers to help you, but the responsibility is still on you to think about when to create objects, and when they can be destroyed.

If you have a lot of them, you could even end up writing a class that takes care of some of these repeated operations for you - hey, a resource manager! But it's still on you to know what's going on.

I would suggest that if you're going to have something simple like one circle and one texture, you could create the texture at the same time you create the circle, which would help you keep track of them. It's very much up to you; I would suggest that you get it working very simply, and then just crack on with your programming, and soon enough you'll start to understand the complexity you can create for yourself and you'll start to have ideas about how to help yourself out.
Last edited on
I will get it working, working on it right now in fact.

I thank you for all the help you have given me, you have made me understand this whole concept :) thank you a lot man

PS: I got it to draw the texture :)
Last edited on
The SFML book has quite a good explanation also on how to manage your game assets and even walks you through creating an implementation to do that.

Here is an implementation of it on another one of my posts, can be a bit tricky to wrap you head around if you are not familiar with templates but if you look over it a bit it might help give you some ideas as to how they are implemented and designed.

http://www.cplusplus.com/forum/lounge/108556/#msg590208

Here is an example of it being used in an old outdated project of mine (Basically just the SFML book project with a few tweaks).

Loading some textures into the texture resource holder on initialization of the game.
https://github.com/bjumbeck/Space-Shooter/blob/master/World.cpp#L115

Grabbing a texture from the text resource manager to use in a sprite object (That will represent the scrolling background). Note that this does not actually do any loading of the texture into memory that was taking place earlier, all we are doing is basically grabbing a reference to that texture.
https://github.com/bjumbeck/Space-Shooter/blob/master/World.cpp#L142

There is some other examples of its use in that repo scattered around so feel free to look if you want (Though if you do sorry in advance for not organizing it into folders ;p and for the bad formatting never got around to cleaning up the different tab sizes that happened for some reason).

The main jobs of a resource manager are to serve up the heavyweight assets (sf::Texture, sf::Font, etc.) when they are needed, make sure those heavyweight assets are only loaded into memory once and to make the easily accessible to all portions of the game that need them. Anyways hopefully this could help a bit also.
Last edited on
Thank you for the extra help Zereo :) I will have a look at the links you have me as soon as I can
Topic archived. No new replies allowed.