SDL2 timers

Not sure if this belongs in this forum, I apologize if it doesn't.

I'm working with SDL2 and have made some pretty good ground. I'm stuck trying to draw something for only X amount of seconds. Basically I want to draw (SDL_RenderCopy()) a texture for only 5 seconds, like a power up that you have to pickup before the time runs out.

I've looked at the SDL_Timer stuff and it's use of a callback seems rather confusing, and if I'm being honest, overkill for what I'm trying to accomplish. I figured I could put some kind of equation together with just SDL_GetTicks(), but I'm not real sure what that operation would look like. Any suggestions?
There are many solutions to this, but ultimately you've got something that decides each time around the game loop, when drawing the frame on screen, whether or not to draw that item on the screen.

The most obvious way to do this that comes to mind is for the item to be an object that carries its own lifetime. Something really simple like this:

1
2
3
4
5
class objectToDrawOnScreen
{
  TICK_TYPE tickTimeToDestroyThisObject;
  // and all the drawing information and location etc
}

The objectToDrawOnScreen knows its own lifetime. When you create it, you set tickTimeToDestroyThisObject to "now plus five seconds".

Stick all those in a vector as they get created, and each time round the game loop when drawing the frame on screen, that vector is iterated over. If the current tick time exceed an object's tickTimeToDestroyThisObject, you don't draw it - you erase it from the vector. Otherwise, you draw it.

Nine times out of ten, simple is better, and the other one time out of ten, you've misunderstood the issue. Timers not necessary, no callbacks required.
Last edited on
I'm working with SDL_2 as well for a game I'm coding.

Have you used SDL_Timer to cap your FPS? if so just create a counter and increment it for
each iteration of your game update loop. When frameCounter % 60 == 0
you know that one second has passed, So you can keep rendering your animation until
5 seconds have gone.

hope this makes sense,

Regards

Hugo.

EDIT: good tutorial on how to cap FPS with SDL_2: http://lazyfoo.net/tutorials/SDL/25_capping_frame_rate/index.php
Last edited on
@hoogo no I have not used SDL_Timer. I don't cap my frame rate. I'm using an delta time based tick system for my physics for a frame rate independent approach.
Well if you're not relying on fps count I guess your best bet is to use timers
http://lazyfoo.net/tutorials/SDL/22_timing/index.php

and do something like:
1
2
3
4
5
6
7
bool five_seconds_passed = false
//five_seconds_passed would be true when your 5 second timer has executed

if (!five_seconds_passed)
{
    SDL_RenderCopy(/*[...]*/);
}
Last edited on
If you decide to use SDL_AddTimer just be aware that the callback function will run in another thread. If you have no prior experience with threads this is a topic you'll have study before writing any code because it is easy to get it wrong. Also note that many SDL functions are not thread-safe. In either case it's probably easier to use an approach similar to what Repeater describes.
Last edited on
Topic archived. No new replies allowed.