31
General / Re: Trying to load sprites from List of Sprites, all sprites share one tex?
« on: May 07, 2016, 09:51:02 am »
There are seseveral problems with your solution.
- your vector holding textures is problematic since if the vector copies sprites when it needs to grow you'll invalidate any pointers held by sprites to elements in the vector. You can mitigate that either by holding pointers (or better yet, smart pointers) to textures in your vector or by making sure the vector never resizes by calling .reserve() on ot before you push_back the textures and then never push more into it than you reserve.
- don't use srand()/rand() they produce really poor random numbers and time(NULL) is a horrible seed. Use the facilities in the <tt>random</tt> header instead. See these two links:
http://en.cppreference.com/w/cpp/numeric/random
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
- setting textures on all sprites every frame is inefficient and needless.
- your vector holding textures is problematic since if the vector copies sprites when it needs to grow you'll invalidate any pointers held by sprites to elements in the vector. You can mitigate that either by holding pointers (or better yet, smart pointers) to textures in your vector or by making sure the vector never resizes by calling .reserve() on ot before you push_back the textures and then never push more into it than you reserve.
- don't use srand()/rand() they produce really poor random numbers and time(NULL) is a horrible seed. Use the facilities in the <tt>random</tt> header instead. See these two links:
http://en.cppreference.com/w/cpp/numeric/random
https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
- setting textures on all sprites every frame is inefficient and needless.