Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Create sprite constantly?  (Read 2444 times)

0 Members and 1 Guest are viewing this topic.

lucifercartwright

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Create sprite constantly?
« 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?


sjaustirni

  • Jr. Member
  • **
  • Posts: 95
    • View Profile
Re: Create sprite constantly?
« Reply #1 on: October 29, 2017, 11:11:10 am »
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.
« Last Edit: October 29, 2017, 06:04:37 pm by sjaustirni »

lucifercartwright

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Create sprite constantly?
« Reply #2 on: October 29, 2017, 12:48:31 pm »
 
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.

Hapax

  • Hero Member
  • *****
  • Posts: 3372
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Create sprite constantly?
« Reply #3 on: October 29, 2017, 03:43:18 pm »
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);
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

lucifercartwright

  • Newbie
  • *
  • Posts: 10
    • View Profile
    • Email
Re: Create sprite constantly?
« Reply #4 on: October 30, 2017, 05:44:21 am »
Ah thank you.