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 :)
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 :)
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.
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.
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);
...
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.
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.
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.
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.