How to play an animation only once ?

i am currently working on a game in c++, which is called bird shooter, i have birds flying around the screen left to right, and i tried to implement an entity that is the bird pooping, and when the poo falls it has a certain animation, and when it “hits” something it supposed to play some other animation, which is the poo splat.
my problem is that it is currently looping the splat poo animation instead of only playing once.
i tried to use SDL_Delay, but it stops execution of the whole game loop, everything is stuck for the delay duration, i was wondering how to fix that, did a little research but didn’t find any good solutions…
i read some suggestions that suggested to implement threads, but that seems a little bit of an overkill, though maybe theres a way to create a delay with a callback function on a separate thread ?
any suggestions on how to fix my problem ?
if you’re interested in the code, here it is, GitHub - MTN95/BirdShooter: A Simple BirdShooter Game Made With SDL2
please let me know if it’s ok, if not i’ll remove the link,

This is really an architecture issue. Your first problem is making SplatBirdPoo its own class. This is something that really should be handled in data rather than hard-coded.

Set up two classes: AnimationManager and Animation. The AnimationManager’s job is to run all animations, and the Animation’s job is to hold data describing how the animation runs. Everything the animation does, the sprite, the frames to play, the playback speed, the effects to spawn, etc should be in the Animation class. Put the data describing the various animations in your Assets. (JSON works pretty well for stuff like this.)

One member on the Animation class should describe what happens when you reach the end of the animation: should it loop, or terminate? When the AnimationManager reaches the end of the frame set, it checks this, and either resets the current frame counter to 0 (loop) or destroys the animation object and removes it from the list of animations being played (terminate).

1 Like

thank you ! while reading this i face palmed myself, such an easy answer, but i didn’t thought of it.
but i guess that how you learn…
thanks again !
note: i never actually used JSON at all, why is it recommended to use ?

Because bloatware is all the rage these days, so your game should be too. :wink:

But seriously, JSON is simple to understand, easy to create files (any notepad will do), functional, and is supported in every programming language. If necessary, you can use other text formats of configuration files.

1 Like

Good to know!, i guess it’s time to learn about it, thanks for the explanation!

It’s the age-old story of Columbus’ Egg: it looks incredibly simple after someone else has figured it out and showed you the trick. (I’m not claiming credit for it; this is how game engines from RPG Maker to Unity and Unreal have been doing it for decades.)

  1. It’s a very simple way to deal with data. It’s not super-highly-efficient — I wouldn’t recommend it for large amounts of data (ie. in the gigabyte realm or higher) or stuff that has to be processed with high performance constraints — but for small amounts of data that only needs to be read in once at the start of the program it works really, really well.
  1. It’s got good tooling in every major language.
  2. Having your data storage be human-readable means that if anything goes wrong it’s an order of magnitude easier to debug than binary formats.
1 Like


well that’s really encouraging… the thing is when i started this project i didn’t really planned anything, just rolled with what ever idea i had, when it came to making this classes that you suggested, i actually though i won’t need them as i will not have a lot of animations, but i actually did implement this, works great, i guess it’s a good start to build upon in the future.

i learned the basics today of using JSON files, specifically with nlohmann/json API.
don’t know if its the best, thats just the first one that came in the initial search.