Dynamic allocation or not Dynamic allocation?

Good afternoon everyone!

I am currently making a simple game in C ++ and SDL2 for fun. I have a class ‘Actor’ that has a fixed number of possible states (standing, walking, running …) each with its own ‘sprite’, and at the time of writing the constuctor a question has arisen: since ‘Actor’ is likely to use all his states at some time (some virtuoso may be able to pass the game without firing or running ;)) Would it be very bad practice to ‘load’ them all in memory when building it? I mean, instead of waiting for, let´s say, the ‘fire’ button to press to erase the current state, create the new state and load its ‘sprite’ and then move to it, all the resources loaded in memory at thje beginning of the game and thus avoid the creation and dynamic destruction at runtime that I understand is somewhat expensive in terms of CPU cycles. As I said it is a simple game and the sprites are too, that is, they are not heavy.

Thanks in advance for your time.

If you have a handful of sprites that represent the entire set of images for the Actor, just load them all at startup (if there are a lot of different types of Actors, maybe load everything at startup since people will tolerate a loading progress bar thing). Especially since it seems likely you’ll need them all anyhow.

I wouldn’t load them right as you need them, when it means you’re definitely going to get a lag the first time the player fires.

If there’s more than one Actor, load them once and share them between every instance, so if you have, say, 20 enemies on the screen, you don’t have 20 copies of the set of sprites loaded.

Extra credit for finding good ways to group the sprite loading (these are all the enemies on this level so only load these specific characters’ sprites, these are boss-specific and can be loaded right before we start the Big Boss Fight, etc), so they load ahead of time but mostly only when needed.

2 Likes

Thanks icculus!

I specially like the ‘people will tolerate a loading progress bar thing’ part :grin:
Will do it at startup then!. :+1:

If you are using png sprites there is good free online tool for grouping them to one texture https://www.leshylabs.com/apps/sstool/