SFML community forums
Help => General => Topic started by: Canvas on January 07, 2014, 12:26:07 am
-
Hello there people,
At the moment I have a simple std::vector<Object*> objects
Object is a custom class i have created which can be text, sprite, animated sprite etc etc.
Now in my GameState class the code is starting to get a little large and I was wondering If I can do this.
The objects variable is a vector of pointers to "Object"s. Is it safe for me to do the following.
Create a new class called "StateLoader"
StateLoader will then deal with setting up the objects for whatever the game state is in
and then return a std::vector<Object*>, is this a safe thing to do or not?
When I mean safe, if is ok to populate a vector of pointers, and then set a variable to be equal to that vector of pointers? I just want to populate my objects variable outside of my gamestate class as the class is getting abit large because of all the objects being set.
If anyone can shine some light onto this it would be great.
Regards,
Canvas
-
I strongly advise you against the use of such "can be anything" classes, it breaks the whole idea of having a strong typed language in the first place and you'll have to cast everything around, wasting a lot of resources and creating a lot of potential problems.
You also can't just assign the address of an object to the vector, because as soon as that object gets out of scope the address will point to an invalid memory space and if you used new to create space on the heap, you're doing manual memory management which is (mostly) obsolete with modern C++ - you might want to read some stuff on RAII.
-
what is a good way to load resources and store them in sfml then?
-
You can use STL containers, but use one container for each type and for resources such as textures use an unique_ptr.
You could take a look at the ResourceHolder (https://github.com/SFML/SFML-Game-Development-Book/tree/master/02_Resources/Include/Book) from the SFML Game Development book. ;)