SFML community forums
Help => Graphics => Topic started by: lucifercartwright on October 29, 2017, 09:46:43 am
-
I have a drawImage function where I give it an index to a list of already loaded textures and a few other arguments such as coordinates, scale, angle, etc.
To set these variables I make a Sprite object everytime this function is called and I do window->display(spriteInstance) ;
The thing is this is called every time I render a frame.
My question is, is it okay to constantly create a sprite instance like this or will it use up a lot of CPU?
-
Whilst it is only one sprite you're creating this way, the performance hit may not be noticeable, it is advised to only put the very necessary code into your render() function, since it is the function that is expected to be called most often.
Rephrased - no, sprites should be created only when it is absolutely needed, optimally only once, preferably somewhere around the code where you load the textures.
-
Whilst if it is only one sprite you're creating this way, the performance hit may not be noticeable, it is advised to only put the very necessary code into your render() function, since it is the function that is expected to be called most often.
Rephrased - no, sprite creation should be called only when it is absolutely need, optimally only once, preferably somewhere around the code where you load the textures.
And there's definitely no way to simply draw an image from a function like this then? A function that takes arguments such as x, y, angle, scale, etc. Without creating a new sprite instance?
And yeah this could potentially be called hundreds of times per frame.
-
Sprites are pretty lightweight and shouldn't cause a significant delay by creating them per frame rather than updating persistant ones. Keeping persistant (create them before the loop and re-use them) sprites, though, may help in other ways such as not requiring updating certain values that are the same as before (for example, the colour of that sprite might be the same so there's no need to update it).
However, as I mentioned above, sprites are quite lightweight (they are effectively just tens of floats and a pointer) so they shouldn't be too slow. Just keep it as low as possible; if it's possible to do without recreating them, it probably makes more sense to do so.
Also, to clarify, to draw the sprite, it would be window->draw(spriteInstance); not window->display(spriteInstance);
-
Ah thank you.