Pausable timer implementation for SDL in CColors changing in SDL programSDL boilerplateSDL/C++ Pong cloneCollision-detection for an SDL gameSDL Input ManagmentSDL game: Snake clone2D SDL Asteroids-like gamePong game using SDLTTF/SDL based class for text handlingFrame limiting in an SDL game

Sailing the cryptic seas

How to write cleanly even if my character uses expletive language?

Why one should not leave fingerprints on bulbs and plugs?

A limit with limit zero everywhere must be zero somewhere

Are all passive ability checks floors for active ability checks?

How difficult is it to simply disable/disengage the MCAS on Boeing 737 Max 8 & 9 Aircraft?

What are substitutions for coconut in curry?

PTIJ: Who should I vote for? (21st Knesset Edition)

SOQL: Populate a Literal List in WHERE IN Clause

Could the Saturn V actually have launched astronauts around Venus?

How can I track script which gives me "command not found" right after the login?

Happy pi day, everyone!

How big is a MODIS 250m pixel in reality?

What do Xenomorphs eat in the Alien series?

How to make healing in an exploration game interesting

Does Mathematica reuse previous computations?

If I can solve Sudoku can I solve Travelling Salesman Problem(TSP)? If yes, how?

Why is the President allowed to veto a cancellation of emergency powers?

It's a yearly task, alright

Use of undefined constant bloginfo

In a future war, an old lady is trying to raise a boy but one of the weapons has made everyone deaf

A link redirect to http instead of https: how critical is it?

How to change two letters closest to a string and one letter immediately after a string using notepad++

Do the common programs (for example: "ls", "cat") in Linux and BSD come from the same source code?



Pausable timer implementation for SDL in C


Colors changing in SDL programSDL boilerplateSDL/C++ Pong cloneCollision-detection for an SDL gameSDL Input ManagmentSDL game: Snake clone2D SDL Asteroids-like gamePong game using SDLTTF/SDL based class for text handlingFrame limiting in an SDL game













0












$begingroup$


I've written a timer module in C for an SDL game that I'm working on that I'd like to get some eyeballs on. I wrote this for 2 requirements: I needed a timer that would signal on an elapsed interval, AND that was pausable and restartable. The timers in SDL signal a callback on a set interval, but they aren't pausable. I've seen other pausable timer implementations for SDL, but they've all required a query for a tick count.



I realized that since I already had an infinite loop using the SDL engine, I could leverage that to drive a pausable timer. I've included a small test program that you can use to evaluate the timer module if you want.



BE ADVISED: If you are sensitive to flashing visual stimuli, you shouldn't run the test program.



Also, the test program is NOT the code that I need reviewed.



The timer module works well, and the caveats I'm aware of already are:



  • The more simultaneous timers you use, the greater the likelihood that you will run into timer lag.

  • The work done in timer callbacks should be short and sweet, and callbacks should return as fast as possible. On the upside, with this implementation, there are no threading issues with timer callbacks.

If anyone can spot any gotchas that I'm not aware of, especially as they relate to using the SDL library, I would really appreciate it. Here's the code:



timer header file:



#ifndef TIMER_H
#define TIMER_H

typedef struct Timer Timer;
typedef void(*TimerCallback)(void *data);

/*
Initializes the timer mechanism, and allocates resources for 'nTimers'
number of simultaneous timers.

Returns non-zero on failure.
*/
int timer_InitTimers(int nTimers);

/*
Add this to the main game loop, either before or after the loop that
polls events. If timing is very critical, add it both before and after.
*/
void timer_PollTimers(void);

/*
Creates an idle timer that has to be started with a call to 'timer_Start()'.

Returns NULL on failure. Will fail if 'timer_InitTimers()' has not already
been called.
*/
Timer *timer_Create(Uint32 interval, TimerCallback fCallback, void *data);

/*
Pauses a timer. If the timer is already paused, this is a no-op.

Fails with non-zero if 'timer' is NULL or not a valid timer.
*/
int timer_Pause(Timer *timer);

/*
Starts a timer. If the timer is already running, this function resets the
delta time for the timer back to zero.

Fails with non-zero if 'timer' is NULL or not a valid timer.
*/
int timer_Start(Timer *timer);

/*
Cancels an existing timer. If 'timer' is NULL, this is a no-op.
*/
void timer_Cancel(Timer *timer);

/*
Releases the resources allocated for the timer mechanism. Call at program
shutdown, along with 'SDL_Quit()'.
*/
void timer_Quit(void);

/*
Returns true if the timer is running, or false if the timer is paused or
is NULL.
*/
int timer_IsRunning(Timer *timer);

#endif


timer source file:



#include <SDL.h>
#include "timer.h"

static Timer *Chunk; /* BLOB of timers to use */
static int ChunkCount;
static Timer *Timers; /* Linked list of active timers */
static Uint64 TicksPerMillisecond;
static Uint64 Tolerance; /* Fire the timer if it's this close */

struct Timer
int active;
int running;
TimerCallback callback;
void *user;
Timer *next;
Uint64 span;
Uint64 last;
;

static void addTimer(Timer *t)
Timer *n = NULL;

if (Timers == NULL)
Timers = t;

else
n = Timers;
while (n->next != NULL)
n = n->next;

n->next = t;



static void removeTimer(Timer *t)
Timer *n = NULL;
Timer *p = NULL;

if (t == Timers)
Timers = Timers->next;

else
p = Timers;
n = Timers->next;
while (n != NULL)
if (n == t)
p->next = t->next;
SDL_memset(n, 0, sizeof(*n));
break;

p = n;
n = n->next;




int timer_InitTimers(int n)
TicksPerMillisecond = SDL_GetPerformanceFrequency() / 1000;
Tolerance = TicksPerMillisecond / 2; /* 0.5 ms tolerance */
Chunk = calloc(n, sizeof(Timer));
if (Chunk == NULL)
//LOG_ERROR(Err_MallocFail);
return 1;

ChunkCount = n;
return 0;


Timer *timer_Create(Uint32 interval, TimerCallback fCallback, void *data)
Timer *t = Chunk;
int i = 0;

while (i < ChunkCount)
if (!t->active)
t->span = TicksPerMillisecond * interval - Tolerance;
t->callback = fCallback;
t->user = data;
t->active = 1;
addTimer(t);
return t;

i++;
t++;

return NULL;


void timer_PollTimers(void)
Timer *t = Timers;
Uint64 ticks = SDL_GetPerformanceCounter();

while (t)
/* if a timer is not 'active', it shouldn't be 'running' */
SDL_assert(t->active);

if (t->running && ticks - t->last >= t->span)
t->callback(t->user);
t->last = ticks;

t = t->next;



int timer_Pause(Timer* t)
if (t && t->active)
t->running = 0;
t->last = 0;
return 0;

return 1;


int timer_Start(Timer *t)
if (t && t->active)
t->running = 1;
t->last = SDL_GetPerformanceCounter();
return 0;

return 1;


void timer_Cancel(Timer *t)
if (t) removeTimer(t);


void timer_Quit(void)
Timers = NULL;
free(Chunk);


int timer_IsRunning(Timer *t)
if (t)
return t->running;

return 0;



test program:



#include <stdio.h>
#include <SDL.h>
#include "timer.h"

Uint32 EVENT_TYPE_TIMER_RED;
Uint32 EVENT_TYPE_TIMER_BLUE;
Uint32 EVENT_TYPE_TIMER_GREEN;
Uint32 EVENT_TYPE_TIMER_YELLOW;

Uint32 colorRed;
Uint32 colorBlue;
Uint32 colorGreen;
Uint32 colorYellow;

SDL_Rect rectRed;
SDL_Rect rectBlue;
SDL_Rect rectGreen;
SDL_Rect rectYellow;

Timer* timerRed;
Timer* timerBlue;
Timer *timerGreen;
Timer *timerYellow;

int isRed;
int isBlue;
int isGreen;
int isYellow;

static void handleTimerRed(void*);
static void handleTimerBlue(void*);
static void handleTimerGreen(void*);
static void handleTimerYellow(void*);

SDL_Event QuitEvent = SDL_QUIT ;
SDL_Renderer *render;
SDL_Window *window;
SDL_Surface *surface;

static void initGlobals(void)
rectRed = (SDL_Rect) 0, 0, 128, 128 ;
rectBlue = (SDL_Rect) 640 - 128, 0, 128, 128 ;
rectGreen = (SDL_Rect) 0, 480 - 128, 128, 128 ;
rectYellow = (SDL_Rect) 640 - 128, 480 - 128, 128, 128 ;

EVENT_TYPE_TIMER_RED = SDL_RegisterEvents(4);
EVENT_TYPE_TIMER_BLUE = EVENT_TYPE_TIMER_RED + 1;
EVENT_TYPE_TIMER_GREEN = EVENT_TYPE_TIMER_RED + 2;
EVENT_TYPE_TIMER_YELLOW = EVENT_TYPE_TIMER_RED + 3;

timerRed = timer_Create(250, handleTimerRed, NULL);
timerBlue = timer_Create(500, handleTimerBlue, NULL);
timerGreen = timer_Create(750, handleTimerGreen, NULL);
timerYellow = timer_Create(1000, handleTimerYellow, NULL);

colorRed = SDL_MapRGB(surface->format, 170, 0, 0);
colorBlue = SDL_MapRGB(surface->format, 0, 0, 170);
colorGreen = SDL_MapRGB(surface->format, 0, 170, 0);
colorYellow = SDL_MapRGB(surface->format, 255, 255, 0);

SDL_FillRect(surface, NULL, 0);
SDL_FillRect(surface, &rectRed, colorRed);
SDL_FillRect(surface, &rectBlue, colorBlue);
SDL_FillRect(surface, &rectGreen, colorGreen);
SDL_FillRect(surface, &rectYellow, colorYellow);

isRed = isBlue = isGreen = isYellow = 1;


static void handleEvent(SDL_Event *evt)
SDL_Texture *tex;

if (evt->type == SDL_KEYDOWN)
if (evt->key.keysym.sym == SDLK_ESCAPE)
SDL_PushEvent(&QuitEvent);

else if (evt->key.keysym.sym == SDLK_r)
if (timer_IsRunning(timerRed))
timer_Pause(timerRed);

else
timer_Start(timerRed);


else if (evt->key.keysym.sym == SDLK_b)
if (timer_IsRunning(timerBlue))
timer_Pause(timerBlue);

else
timer_Start(timerBlue);


else if (evt->key.keysym.sym == SDLK_g)
if (timer_IsRunning(timerGreen))
timer_Pause(timerGreen);

else
timer_Start(timerGreen);


else if (evt->key.keysym.sym == SDLK_y)
if (timer_IsRunning(timerYellow))
timer_Pause(timerYellow);

else
timer_Start(timerYellow);



else if (evt->type == EVENT_TYPE_TIMER_RED)
if (isRed)
SDL_FillRect(surface, &rectRed, 0);
isRed = 0;

else
SDL_FillRect(surface, &rectRed, colorRed);
isRed = 1;

tex = SDL_CreateTextureFromSurface(render, surface);
SDL_RenderCopy(render, tex, NULL, NULL);
SDL_RenderPresent(render);
SDL_DestroyTexture(tex);

else if (evt->type == EVENT_TYPE_TIMER_BLUE)
if (isBlue)
SDL_FillRect(surface, &rectBlue, 0);
isBlue = 0;

else
SDL_FillRect(surface, &rectBlue, colorBlue);
isBlue = 1;

tex = SDL_CreateTextureFromSurface(render, surface);
SDL_RenderCopy(render, tex, NULL, NULL);
SDL_RenderPresent(render);
SDL_DestroyTexture(tex);

else if (evt->type == EVENT_TYPE_TIMER_GREEN)
if (isGreen)
SDL_FillRect(surface, &rectGreen, 0);
isGreen = 0;

else
SDL_FillRect(surface, &rectGreen, colorGreen);
isGreen = 1;

tex = SDL_CreateTextureFromSurface(render, surface);
SDL_RenderCopy(render, tex, NULL, NULL);
SDL_RenderPresent(render);
SDL_DestroyTexture(tex);

else if (evt->type == EVENT_TYPE_TIMER_YELLOW)
if (isYellow)
SDL_FillRect(surface, &rectYellow, 0);
isYellow = 0;

else
SDL_FillRect(surface, &rectYellow, colorYellow);
isYellow = 1;

tex = SDL_CreateTextureFromSurface(render, surface);
SDL_RenderCopy(render, tex, NULL, NULL);
SDL_RenderPresent(render);
SDL_DestroyTexture(tex);



int main(int argc, char* args[])

(void)(argc);
(void)(args);
SDL_Event event = 0 ;
int run = 0;
SDL_Texture *texture = NULL;

if (SDL_Init(SDL_INIT_VIDEO

static void handleTimerRed(void *ignored)
SDL_Event event;
(void)(ignored);

event.type = EVENT_TYPE_TIMER_RED;
SDL_PushEvent(&event);


static void handleTimerBlue(void *ignored)
SDL_Event event;
(void)(ignored);

event.type = EVENT_TYPE_TIMER_BLUE;
SDL_PushEvent(&event);


static void handleTimerGreen(void *ignored)
SDL_Event event;
(void)(ignored);

event.type = EVENT_TYPE_TIMER_GREEN;
SDL_PushEvent(&event);


static void handleTimerYellow(void *ignored)
SDL_Event event;
(void)(ignored);

event.type = EVENT_TYPE_TIMER_YELLOW;
SDL_PushEvent(&event);









share







New contributor




Mark Benningfield is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$
















    0












    $begingroup$


    I've written a timer module in C for an SDL game that I'm working on that I'd like to get some eyeballs on. I wrote this for 2 requirements: I needed a timer that would signal on an elapsed interval, AND that was pausable and restartable. The timers in SDL signal a callback on a set interval, but they aren't pausable. I've seen other pausable timer implementations for SDL, but they've all required a query for a tick count.



    I realized that since I already had an infinite loop using the SDL engine, I could leverage that to drive a pausable timer. I've included a small test program that you can use to evaluate the timer module if you want.



    BE ADVISED: If you are sensitive to flashing visual stimuli, you shouldn't run the test program.



    Also, the test program is NOT the code that I need reviewed.



    The timer module works well, and the caveats I'm aware of already are:



    • The more simultaneous timers you use, the greater the likelihood that you will run into timer lag.

    • The work done in timer callbacks should be short and sweet, and callbacks should return as fast as possible. On the upside, with this implementation, there are no threading issues with timer callbacks.

    If anyone can spot any gotchas that I'm not aware of, especially as they relate to using the SDL library, I would really appreciate it. Here's the code:



    timer header file:



    #ifndef TIMER_H
    #define TIMER_H

    typedef struct Timer Timer;
    typedef void(*TimerCallback)(void *data);

    /*
    Initializes the timer mechanism, and allocates resources for 'nTimers'
    number of simultaneous timers.

    Returns non-zero on failure.
    */
    int timer_InitTimers(int nTimers);

    /*
    Add this to the main game loop, either before or after the loop that
    polls events. If timing is very critical, add it both before and after.
    */
    void timer_PollTimers(void);

    /*
    Creates an idle timer that has to be started with a call to 'timer_Start()'.

    Returns NULL on failure. Will fail if 'timer_InitTimers()' has not already
    been called.
    */
    Timer *timer_Create(Uint32 interval, TimerCallback fCallback, void *data);

    /*
    Pauses a timer. If the timer is already paused, this is a no-op.

    Fails with non-zero if 'timer' is NULL or not a valid timer.
    */
    int timer_Pause(Timer *timer);

    /*
    Starts a timer. If the timer is already running, this function resets the
    delta time for the timer back to zero.

    Fails with non-zero if 'timer' is NULL or not a valid timer.
    */
    int timer_Start(Timer *timer);

    /*
    Cancels an existing timer. If 'timer' is NULL, this is a no-op.
    */
    void timer_Cancel(Timer *timer);

    /*
    Releases the resources allocated for the timer mechanism. Call at program
    shutdown, along with 'SDL_Quit()'.
    */
    void timer_Quit(void);

    /*
    Returns true if the timer is running, or false if the timer is paused or
    is NULL.
    */
    int timer_IsRunning(Timer *timer);

    #endif


    timer source file:



    #include <SDL.h>
    #include "timer.h"

    static Timer *Chunk; /* BLOB of timers to use */
    static int ChunkCount;
    static Timer *Timers; /* Linked list of active timers */
    static Uint64 TicksPerMillisecond;
    static Uint64 Tolerance; /* Fire the timer if it's this close */

    struct Timer
    int active;
    int running;
    TimerCallback callback;
    void *user;
    Timer *next;
    Uint64 span;
    Uint64 last;
    ;

    static void addTimer(Timer *t)
    Timer *n = NULL;

    if (Timers == NULL)
    Timers = t;

    else
    n = Timers;
    while (n->next != NULL)
    n = n->next;

    n->next = t;



    static void removeTimer(Timer *t)
    Timer *n = NULL;
    Timer *p = NULL;

    if (t == Timers)
    Timers = Timers->next;

    else
    p = Timers;
    n = Timers->next;
    while (n != NULL)
    if (n == t)
    p->next = t->next;
    SDL_memset(n, 0, sizeof(*n));
    break;

    p = n;
    n = n->next;




    int timer_InitTimers(int n)
    TicksPerMillisecond = SDL_GetPerformanceFrequency() / 1000;
    Tolerance = TicksPerMillisecond / 2; /* 0.5 ms tolerance */
    Chunk = calloc(n, sizeof(Timer));
    if (Chunk == NULL)
    //LOG_ERROR(Err_MallocFail);
    return 1;

    ChunkCount = n;
    return 0;


    Timer *timer_Create(Uint32 interval, TimerCallback fCallback, void *data)
    Timer *t = Chunk;
    int i = 0;

    while (i < ChunkCount)
    if (!t->active)
    t->span = TicksPerMillisecond * interval - Tolerance;
    t->callback = fCallback;
    t->user = data;
    t->active = 1;
    addTimer(t);
    return t;

    i++;
    t++;

    return NULL;


    void timer_PollTimers(void)
    Timer *t = Timers;
    Uint64 ticks = SDL_GetPerformanceCounter();

    while (t)
    /* if a timer is not 'active', it shouldn't be 'running' */
    SDL_assert(t->active);

    if (t->running && ticks - t->last >= t->span)
    t->callback(t->user);
    t->last = ticks;

    t = t->next;



    int timer_Pause(Timer* t)
    if (t && t->active)
    t->running = 0;
    t->last = 0;
    return 0;

    return 1;


    int timer_Start(Timer *t)
    if (t && t->active)
    t->running = 1;
    t->last = SDL_GetPerformanceCounter();
    return 0;

    return 1;


    void timer_Cancel(Timer *t)
    if (t) removeTimer(t);


    void timer_Quit(void)
    Timers = NULL;
    free(Chunk);


    int timer_IsRunning(Timer *t)
    if (t)
    return t->running;

    return 0;



    test program:



    #include <stdio.h>
    #include <SDL.h>
    #include "timer.h"

    Uint32 EVENT_TYPE_TIMER_RED;
    Uint32 EVENT_TYPE_TIMER_BLUE;
    Uint32 EVENT_TYPE_TIMER_GREEN;
    Uint32 EVENT_TYPE_TIMER_YELLOW;

    Uint32 colorRed;
    Uint32 colorBlue;
    Uint32 colorGreen;
    Uint32 colorYellow;

    SDL_Rect rectRed;
    SDL_Rect rectBlue;
    SDL_Rect rectGreen;
    SDL_Rect rectYellow;

    Timer* timerRed;
    Timer* timerBlue;
    Timer *timerGreen;
    Timer *timerYellow;

    int isRed;
    int isBlue;
    int isGreen;
    int isYellow;

    static void handleTimerRed(void*);
    static void handleTimerBlue(void*);
    static void handleTimerGreen(void*);
    static void handleTimerYellow(void*);

    SDL_Event QuitEvent = SDL_QUIT ;
    SDL_Renderer *render;
    SDL_Window *window;
    SDL_Surface *surface;

    static void initGlobals(void)
    rectRed = (SDL_Rect) 0, 0, 128, 128 ;
    rectBlue = (SDL_Rect) 640 - 128, 0, 128, 128 ;
    rectGreen = (SDL_Rect) 0, 480 - 128, 128, 128 ;
    rectYellow = (SDL_Rect) 640 - 128, 480 - 128, 128, 128 ;

    EVENT_TYPE_TIMER_RED = SDL_RegisterEvents(4);
    EVENT_TYPE_TIMER_BLUE = EVENT_TYPE_TIMER_RED + 1;
    EVENT_TYPE_TIMER_GREEN = EVENT_TYPE_TIMER_RED + 2;
    EVENT_TYPE_TIMER_YELLOW = EVENT_TYPE_TIMER_RED + 3;

    timerRed = timer_Create(250, handleTimerRed, NULL);
    timerBlue = timer_Create(500, handleTimerBlue, NULL);
    timerGreen = timer_Create(750, handleTimerGreen, NULL);
    timerYellow = timer_Create(1000, handleTimerYellow, NULL);

    colorRed = SDL_MapRGB(surface->format, 170, 0, 0);
    colorBlue = SDL_MapRGB(surface->format, 0, 0, 170);
    colorGreen = SDL_MapRGB(surface->format, 0, 170, 0);
    colorYellow = SDL_MapRGB(surface->format, 255, 255, 0);

    SDL_FillRect(surface, NULL, 0);
    SDL_FillRect(surface, &rectRed, colorRed);
    SDL_FillRect(surface, &rectBlue, colorBlue);
    SDL_FillRect(surface, &rectGreen, colorGreen);
    SDL_FillRect(surface, &rectYellow, colorYellow);

    isRed = isBlue = isGreen = isYellow = 1;


    static void handleEvent(SDL_Event *evt)
    SDL_Texture *tex;

    if (evt->type == SDL_KEYDOWN)
    if (evt->key.keysym.sym == SDLK_ESCAPE)
    SDL_PushEvent(&QuitEvent);

    else if (evt->key.keysym.sym == SDLK_r)
    if (timer_IsRunning(timerRed))
    timer_Pause(timerRed);

    else
    timer_Start(timerRed);


    else if (evt->key.keysym.sym == SDLK_b)
    if (timer_IsRunning(timerBlue))
    timer_Pause(timerBlue);

    else
    timer_Start(timerBlue);


    else if (evt->key.keysym.sym == SDLK_g)
    if (timer_IsRunning(timerGreen))
    timer_Pause(timerGreen);

    else
    timer_Start(timerGreen);


    else if (evt->key.keysym.sym == SDLK_y)
    if (timer_IsRunning(timerYellow))
    timer_Pause(timerYellow);

    else
    timer_Start(timerYellow);



    else if (evt->type == EVENT_TYPE_TIMER_RED)
    if (isRed)
    SDL_FillRect(surface, &rectRed, 0);
    isRed = 0;

    else
    SDL_FillRect(surface, &rectRed, colorRed);
    isRed = 1;

    tex = SDL_CreateTextureFromSurface(render, surface);
    SDL_RenderCopy(render, tex, NULL, NULL);
    SDL_RenderPresent(render);
    SDL_DestroyTexture(tex);

    else if (evt->type == EVENT_TYPE_TIMER_BLUE)
    if (isBlue)
    SDL_FillRect(surface, &rectBlue, 0);
    isBlue = 0;

    else
    SDL_FillRect(surface, &rectBlue, colorBlue);
    isBlue = 1;

    tex = SDL_CreateTextureFromSurface(render, surface);
    SDL_RenderCopy(render, tex, NULL, NULL);
    SDL_RenderPresent(render);
    SDL_DestroyTexture(tex);

    else if (evt->type == EVENT_TYPE_TIMER_GREEN)
    if (isGreen)
    SDL_FillRect(surface, &rectGreen, 0);
    isGreen = 0;

    else
    SDL_FillRect(surface, &rectGreen, colorGreen);
    isGreen = 1;

    tex = SDL_CreateTextureFromSurface(render, surface);
    SDL_RenderCopy(render, tex, NULL, NULL);
    SDL_RenderPresent(render);
    SDL_DestroyTexture(tex);

    else if (evt->type == EVENT_TYPE_TIMER_YELLOW)
    if (isYellow)
    SDL_FillRect(surface, &rectYellow, 0);
    isYellow = 0;

    else
    SDL_FillRect(surface, &rectYellow, colorYellow);
    isYellow = 1;

    tex = SDL_CreateTextureFromSurface(render, surface);
    SDL_RenderCopy(render, tex, NULL, NULL);
    SDL_RenderPresent(render);
    SDL_DestroyTexture(tex);



    int main(int argc, char* args[])

    (void)(argc);
    (void)(args);
    SDL_Event event = 0 ;
    int run = 0;
    SDL_Texture *texture = NULL;

    if (SDL_Init(SDL_INIT_VIDEO

    static void handleTimerRed(void *ignored)
    SDL_Event event;
    (void)(ignored);

    event.type = EVENT_TYPE_TIMER_RED;
    SDL_PushEvent(&event);


    static void handleTimerBlue(void *ignored)
    SDL_Event event;
    (void)(ignored);

    event.type = EVENT_TYPE_TIMER_BLUE;
    SDL_PushEvent(&event);


    static void handleTimerGreen(void *ignored)
    SDL_Event event;
    (void)(ignored);

    event.type = EVENT_TYPE_TIMER_GREEN;
    SDL_PushEvent(&event);


    static void handleTimerYellow(void *ignored)
    SDL_Event event;
    (void)(ignored);

    event.type = EVENT_TYPE_TIMER_YELLOW;
    SDL_PushEvent(&event);









    share







    New contributor




    Mark Benningfield is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$














      0












      0








      0





      $begingroup$


      I've written a timer module in C for an SDL game that I'm working on that I'd like to get some eyeballs on. I wrote this for 2 requirements: I needed a timer that would signal on an elapsed interval, AND that was pausable and restartable. The timers in SDL signal a callback on a set interval, but they aren't pausable. I've seen other pausable timer implementations for SDL, but they've all required a query for a tick count.



      I realized that since I already had an infinite loop using the SDL engine, I could leverage that to drive a pausable timer. I've included a small test program that you can use to evaluate the timer module if you want.



      BE ADVISED: If you are sensitive to flashing visual stimuli, you shouldn't run the test program.



      Also, the test program is NOT the code that I need reviewed.



      The timer module works well, and the caveats I'm aware of already are:



      • The more simultaneous timers you use, the greater the likelihood that you will run into timer lag.

      • The work done in timer callbacks should be short and sweet, and callbacks should return as fast as possible. On the upside, with this implementation, there are no threading issues with timer callbacks.

      If anyone can spot any gotchas that I'm not aware of, especially as they relate to using the SDL library, I would really appreciate it. Here's the code:



      timer header file:



      #ifndef TIMER_H
      #define TIMER_H

      typedef struct Timer Timer;
      typedef void(*TimerCallback)(void *data);

      /*
      Initializes the timer mechanism, and allocates resources for 'nTimers'
      number of simultaneous timers.

      Returns non-zero on failure.
      */
      int timer_InitTimers(int nTimers);

      /*
      Add this to the main game loop, either before or after the loop that
      polls events. If timing is very critical, add it both before and after.
      */
      void timer_PollTimers(void);

      /*
      Creates an idle timer that has to be started with a call to 'timer_Start()'.

      Returns NULL on failure. Will fail if 'timer_InitTimers()' has not already
      been called.
      */
      Timer *timer_Create(Uint32 interval, TimerCallback fCallback, void *data);

      /*
      Pauses a timer. If the timer is already paused, this is a no-op.

      Fails with non-zero if 'timer' is NULL or not a valid timer.
      */
      int timer_Pause(Timer *timer);

      /*
      Starts a timer. If the timer is already running, this function resets the
      delta time for the timer back to zero.

      Fails with non-zero if 'timer' is NULL or not a valid timer.
      */
      int timer_Start(Timer *timer);

      /*
      Cancels an existing timer. If 'timer' is NULL, this is a no-op.
      */
      void timer_Cancel(Timer *timer);

      /*
      Releases the resources allocated for the timer mechanism. Call at program
      shutdown, along with 'SDL_Quit()'.
      */
      void timer_Quit(void);

      /*
      Returns true if the timer is running, or false if the timer is paused or
      is NULL.
      */
      int timer_IsRunning(Timer *timer);

      #endif


      timer source file:



      #include <SDL.h>
      #include "timer.h"

      static Timer *Chunk; /* BLOB of timers to use */
      static int ChunkCount;
      static Timer *Timers; /* Linked list of active timers */
      static Uint64 TicksPerMillisecond;
      static Uint64 Tolerance; /* Fire the timer if it's this close */

      struct Timer
      int active;
      int running;
      TimerCallback callback;
      void *user;
      Timer *next;
      Uint64 span;
      Uint64 last;
      ;

      static void addTimer(Timer *t)
      Timer *n = NULL;

      if (Timers == NULL)
      Timers = t;

      else
      n = Timers;
      while (n->next != NULL)
      n = n->next;

      n->next = t;



      static void removeTimer(Timer *t)
      Timer *n = NULL;
      Timer *p = NULL;

      if (t == Timers)
      Timers = Timers->next;

      else
      p = Timers;
      n = Timers->next;
      while (n != NULL)
      if (n == t)
      p->next = t->next;
      SDL_memset(n, 0, sizeof(*n));
      break;

      p = n;
      n = n->next;




      int timer_InitTimers(int n)
      TicksPerMillisecond = SDL_GetPerformanceFrequency() / 1000;
      Tolerance = TicksPerMillisecond / 2; /* 0.5 ms tolerance */
      Chunk = calloc(n, sizeof(Timer));
      if (Chunk == NULL)
      //LOG_ERROR(Err_MallocFail);
      return 1;

      ChunkCount = n;
      return 0;


      Timer *timer_Create(Uint32 interval, TimerCallback fCallback, void *data)
      Timer *t = Chunk;
      int i = 0;

      while (i < ChunkCount)
      if (!t->active)
      t->span = TicksPerMillisecond * interval - Tolerance;
      t->callback = fCallback;
      t->user = data;
      t->active = 1;
      addTimer(t);
      return t;

      i++;
      t++;

      return NULL;


      void timer_PollTimers(void)
      Timer *t = Timers;
      Uint64 ticks = SDL_GetPerformanceCounter();

      while (t)
      /* if a timer is not 'active', it shouldn't be 'running' */
      SDL_assert(t->active);

      if (t->running && ticks - t->last >= t->span)
      t->callback(t->user);
      t->last = ticks;

      t = t->next;



      int timer_Pause(Timer* t)
      if (t && t->active)
      t->running = 0;
      t->last = 0;
      return 0;

      return 1;


      int timer_Start(Timer *t)
      if (t && t->active)
      t->running = 1;
      t->last = SDL_GetPerformanceCounter();
      return 0;

      return 1;


      void timer_Cancel(Timer *t)
      if (t) removeTimer(t);


      void timer_Quit(void)
      Timers = NULL;
      free(Chunk);


      int timer_IsRunning(Timer *t)
      if (t)
      return t->running;

      return 0;



      test program:



      #include <stdio.h>
      #include <SDL.h>
      #include "timer.h"

      Uint32 EVENT_TYPE_TIMER_RED;
      Uint32 EVENT_TYPE_TIMER_BLUE;
      Uint32 EVENT_TYPE_TIMER_GREEN;
      Uint32 EVENT_TYPE_TIMER_YELLOW;

      Uint32 colorRed;
      Uint32 colorBlue;
      Uint32 colorGreen;
      Uint32 colorYellow;

      SDL_Rect rectRed;
      SDL_Rect rectBlue;
      SDL_Rect rectGreen;
      SDL_Rect rectYellow;

      Timer* timerRed;
      Timer* timerBlue;
      Timer *timerGreen;
      Timer *timerYellow;

      int isRed;
      int isBlue;
      int isGreen;
      int isYellow;

      static void handleTimerRed(void*);
      static void handleTimerBlue(void*);
      static void handleTimerGreen(void*);
      static void handleTimerYellow(void*);

      SDL_Event QuitEvent = SDL_QUIT ;
      SDL_Renderer *render;
      SDL_Window *window;
      SDL_Surface *surface;

      static void initGlobals(void)
      rectRed = (SDL_Rect) 0, 0, 128, 128 ;
      rectBlue = (SDL_Rect) 640 - 128, 0, 128, 128 ;
      rectGreen = (SDL_Rect) 0, 480 - 128, 128, 128 ;
      rectYellow = (SDL_Rect) 640 - 128, 480 - 128, 128, 128 ;

      EVENT_TYPE_TIMER_RED = SDL_RegisterEvents(4);
      EVENT_TYPE_TIMER_BLUE = EVENT_TYPE_TIMER_RED + 1;
      EVENT_TYPE_TIMER_GREEN = EVENT_TYPE_TIMER_RED + 2;
      EVENT_TYPE_TIMER_YELLOW = EVENT_TYPE_TIMER_RED + 3;

      timerRed = timer_Create(250, handleTimerRed, NULL);
      timerBlue = timer_Create(500, handleTimerBlue, NULL);
      timerGreen = timer_Create(750, handleTimerGreen, NULL);
      timerYellow = timer_Create(1000, handleTimerYellow, NULL);

      colorRed = SDL_MapRGB(surface->format, 170, 0, 0);
      colorBlue = SDL_MapRGB(surface->format, 0, 0, 170);
      colorGreen = SDL_MapRGB(surface->format, 0, 170, 0);
      colorYellow = SDL_MapRGB(surface->format, 255, 255, 0);

      SDL_FillRect(surface, NULL, 0);
      SDL_FillRect(surface, &rectRed, colorRed);
      SDL_FillRect(surface, &rectBlue, colorBlue);
      SDL_FillRect(surface, &rectGreen, colorGreen);
      SDL_FillRect(surface, &rectYellow, colorYellow);

      isRed = isBlue = isGreen = isYellow = 1;


      static void handleEvent(SDL_Event *evt)
      SDL_Texture *tex;

      if (evt->type == SDL_KEYDOWN)
      if (evt->key.keysym.sym == SDLK_ESCAPE)
      SDL_PushEvent(&QuitEvent);

      else if (evt->key.keysym.sym == SDLK_r)
      if (timer_IsRunning(timerRed))
      timer_Pause(timerRed);

      else
      timer_Start(timerRed);


      else if (evt->key.keysym.sym == SDLK_b)
      if (timer_IsRunning(timerBlue))
      timer_Pause(timerBlue);

      else
      timer_Start(timerBlue);


      else if (evt->key.keysym.sym == SDLK_g)
      if (timer_IsRunning(timerGreen))
      timer_Pause(timerGreen);

      else
      timer_Start(timerGreen);


      else if (evt->key.keysym.sym == SDLK_y)
      if (timer_IsRunning(timerYellow))
      timer_Pause(timerYellow);

      else
      timer_Start(timerYellow);



      else if (evt->type == EVENT_TYPE_TIMER_RED)
      if (isRed)
      SDL_FillRect(surface, &rectRed, 0);
      isRed = 0;

      else
      SDL_FillRect(surface, &rectRed, colorRed);
      isRed = 1;

      tex = SDL_CreateTextureFromSurface(render, surface);
      SDL_RenderCopy(render, tex, NULL, NULL);
      SDL_RenderPresent(render);
      SDL_DestroyTexture(tex);

      else if (evt->type == EVENT_TYPE_TIMER_BLUE)
      if (isBlue)
      SDL_FillRect(surface, &rectBlue, 0);
      isBlue = 0;

      else
      SDL_FillRect(surface, &rectBlue, colorBlue);
      isBlue = 1;

      tex = SDL_CreateTextureFromSurface(render, surface);
      SDL_RenderCopy(render, tex, NULL, NULL);
      SDL_RenderPresent(render);
      SDL_DestroyTexture(tex);

      else if (evt->type == EVENT_TYPE_TIMER_GREEN)
      if (isGreen)
      SDL_FillRect(surface, &rectGreen, 0);
      isGreen = 0;

      else
      SDL_FillRect(surface, &rectGreen, colorGreen);
      isGreen = 1;

      tex = SDL_CreateTextureFromSurface(render, surface);
      SDL_RenderCopy(render, tex, NULL, NULL);
      SDL_RenderPresent(render);
      SDL_DestroyTexture(tex);

      else if (evt->type == EVENT_TYPE_TIMER_YELLOW)
      if (isYellow)
      SDL_FillRect(surface, &rectYellow, 0);
      isYellow = 0;

      else
      SDL_FillRect(surface, &rectYellow, colorYellow);
      isYellow = 1;

      tex = SDL_CreateTextureFromSurface(render, surface);
      SDL_RenderCopy(render, tex, NULL, NULL);
      SDL_RenderPresent(render);
      SDL_DestroyTexture(tex);



      int main(int argc, char* args[])

      (void)(argc);
      (void)(args);
      SDL_Event event = 0 ;
      int run = 0;
      SDL_Texture *texture = NULL;

      if (SDL_Init(SDL_INIT_VIDEO

      static void handleTimerRed(void *ignored)
      SDL_Event event;
      (void)(ignored);

      event.type = EVENT_TYPE_TIMER_RED;
      SDL_PushEvent(&event);


      static void handleTimerBlue(void *ignored)
      SDL_Event event;
      (void)(ignored);

      event.type = EVENT_TYPE_TIMER_BLUE;
      SDL_PushEvent(&event);


      static void handleTimerGreen(void *ignored)
      SDL_Event event;
      (void)(ignored);

      event.type = EVENT_TYPE_TIMER_GREEN;
      SDL_PushEvent(&event);


      static void handleTimerYellow(void *ignored)
      SDL_Event event;
      (void)(ignored);

      event.type = EVENT_TYPE_TIMER_YELLOW;
      SDL_PushEvent(&event);









      share







      New contributor




      Mark Benningfield is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I've written a timer module in C for an SDL game that I'm working on that I'd like to get some eyeballs on. I wrote this for 2 requirements: I needed a timer that would signal on an elapsed interval, AND that was pausable and restartable. The timers in SDL signal a callback on a set interval, but they aren't pausable. I've seen other pausable timer implementations for SDL, but they've all required a query for a tick count.



      I realized that since I already had an infinite loop using the SDL engine, I could leverage that to drive a pausable timer. I've included a small test program that you can use to evaluate the timer module if you want.



      BE ADVISED: If you are sensitive to flashing visual stimuli, you shouldn't run the test program.



      Also, the test program is NOT the code that I need reviewed.



      The timer module works well, and the caveats I'm aware of already are:



      • The more simultaneous timers you use, the greater the likelihood that you will run into timer lag.

      • The work done in timer callbacks should be short and sweet, and callbacks should return as fast as possible. On the upside, with this implementation, there are no threading issues with timer callbacks.

      If anyone can spot any gotchas that I'm not aware of, especially as they relate to using the SDL library, I would really appreciate it. Here's the code:



      timer header file:



      #ifndef TIMER_H
      #define TIMER_H

      typedef struct Timer Timer;
      typedef void(*TimerCallback)(void *data);

      /*
      Initializes the timer mechanism, and allocates resources for 'nTimers'
      number of simultaneous timers.

      Returns non-zero on failure.
      */
      int timer_InitTimers(int nTimers);

      /*
      Add this to the main game loop, either before or after the loop that
      polls events. If timing is very critical, add it both before and after.
      */
      void timer_PollTimers(void);

      /*
      Creates an idle timer that has to be started with a call to 'timer_Start()'.

      Returns NULL on failure. Will fail if 'timer_InitTimers()' has not already
      been called.
      */
      Timer *timer_Create(Uint32 interval, TimerCallback fCallback, void *data);

      /*
      Pauses a timer. If the timer is already paused, this is a no-op.

      Fails with non-zero if 'timer' is NULL or not a valid timer.
      */
      int timer_Pause(Timer *timer);

      /*
      Starts a timer. If the timer is already running, this function resets the
      delta time for the timer back to zero.

      Fails with non-zero if 'timer' is NULL or not a valid timer.
      */
      int timer_Start(Timer *timer);

      /*
      Cancels an existing timer. If 'timer' is NULL, this is a no-op.
      */
      void timer_Cancel(Timer *timer);

      /*
      Releases the resources allocated for the timer mechanism. Call at program
      shutdown, along with 'SDL_Quit()'.
      */
      void timer_Quit(void);

      /*
      Returns true if the timer is running, or false if the timer is paused or
      is NULL.
      */
      int timer_IsRunning(Timer *timer);

      #endif


      timer source file:



      #include <SDL.h>
      #include "timer.h"

      static Timer *Chunk; /* BLOB of timers to use */
      static int ChunkCount;
      static Timer *Timers; /* Linked list of active timers */
      static Uint64 TicksPerMillisecond;
      static Uint64 Tolerance; /* Fire the timer if it's this close */

      struct Timer
      int active;
      int running;
      TimerCallback callback;
      void *user;
      Timer *next;
      Uint64 span;
      Uint64 last;
      ;

      static void addTimer(Timer *t)
      Timer *n = NULL;

      if (Timers == NULL)
      Timers = t;

      else
      n = Timers;
      while (n->next != NULL)
      n = n->next;

      n->next = t;



      static void removeTimer(Timer *t)
      Timer *n = NULL;
      Timer *p = NULL;

      if (t == Timers)
      Timers = Timers->next;

      else
      p = Timers;
      n = Timers->next;
      while (n != NULL)
      if (n == t)
      p->next = t->next;
      SDL_memset(n, 0, sizeof(*n));
      break;

      p = n;
      n = n->next;




      int timer_InitTimers(int n)
      TicksPerMillisecond = SDL_GetPerformanceFrequency() / 1000;
      Tolerance = TicksPerMillisecond / 2; /* 0.5 ms tolerance */
      Chunk = calloc(n, sizeof(Timer));
      if (Chunk == NULL)
      //LOG_ERROR(Err_MallocFail);
      return 1;

      ChunkCount = n;
      return 0;


      Timer *timer_Create(Uint32 interval, TimerCallback fCallback, void *data)
      Timer *t = Chunk;
      int i = 0;

      while (i < ChunkCount)
      if (!t->active)
      t->span = TicksPerMillisecond * interval - Tolerance;
      t->callback = fCallback;
      t->user = data;
      t->active = 1;
      addTimer(t);
      return t;

      i++;
      t++;

      return NULL;


      void timer_PollTimers(void)
      Timer *t = Timers;
      Uint64 ticks = SDL_GetPerformanceCounter();

      while (t)
      /* if a timer is not 'active', it shouldn't be 'running' */
      SDL_assert(t->active);

      if (t->running && ticks - t->last >= t->span)
      t->callback(t->user);
      t->last = ticks;

      t = t->next;



      int timer_Pause(Timer* t)
      if (t && t->active)
      t->running = 0;
      t->last = 0;
      return 0;

      return 1;


      int timer_Start(Timer *t)
      if (t && t->active)
      t->running = 1;
      t->last = SDL_GetPerformanceCounter();
      return 0;

      return 1;


      void timer_Cancel(Timer *t)
      if (t) removeTimer(t);


      void timer_Quit(void)
      Timers = NULL;
      free(Chunk);


      int timer_IsRunning(Timer *t)
      if (t)
      return t->running;

      return 0;



      test program:



      #include <stdio.h>
      #include <SDL.h>
      #include "timer.h"

      Uint32 EVENT_TYPE_TIMER_RED;
      Uint32 EVENT_TYPE_TIMER_BLUE;
      Uint32 EVENT_TYPE_TIMER_GREEN;
      Uint32 EVENT_TYPE_TIMER_YELLOW;

      Uint32 colorRed;
      Uint32 colorBlue;
      Uint32 colorGreen;
      Uint32 colorYellow;

      SDL_Rect rectRed;
      SDL_Rect rectBlue;
      SDL_Rect rectGreen;
      SDL_Rect rectYellow;

      Timer* timerRed;
      Timer* timerBlue;
      Timer *timerGreen;
      Timer *timerYellow;

      int isRed;
      int isBlue;
      int isGreen;
      int isYellow;

      static void handleTimerRed(void*);
      static void handleTimerBlue(void*);
      static void handleTimerGreen(void*);
      static void handleTimerYellow(void*);

      SDL_Event QuitEvent = SDL_QUIT ;
      SDL_Renderer *render;
      SDL_Window *window;
      SDL_Surface *surface;

      static void initGlobals(void)
      rectRed = (SDL_Rect) 0, 0, 128, 128 ;
      rectBlue = (SDL_Rect) 640 - 128, 0, 128, 128 ;
      rectGreen = (SDL_Rect) 0, 480 - 128, 128, 128 ;
      rectYellow = (SDL_Rect) 640 - 128, 480 - 128, 128, 128 ;

      EVENT_TYPE_TIMER_RED = SDL_RegisterEvents(4);
      EVENT_TYPE_TIMER_BLUE = EVENT_TYPE_TIMER_RED + 1;
      EVENT_TYPE_TIMER_GREEN = EVENT_TYPE_TIMER_RED + 2;
      EVENT_TYPE_TIMER_YELLOW = EVENT_TYPE_TIMER_RED + 3;

      timerRed = timer_Create(250, handleTimerRed, NULL);
      timerBlue = timer_Create(500, handleTimerBlue, NULL);
      timerGreen = timer_Create(750, handleTimerGreen, NULL);
      timerYellow = timer_Create(1000, handleTimerYellow, NULL);

      colorRed = SDL_MapRGB(surface->format, 170, 0, 0);
      colorBlue = SDL_MapRGB(surface->format, 0, 0, 170);
      colorGreen = SDL_MapRGB(surface->format, 0, 170, 0);
      colorYellow = SDL_MapRGB(surface->format, 255, 255, 0);

      SDL_FillRect(surface, NULL, 0);
      SDL_FillRect(surface, &rectRed, colorRed);
      SDL_FillRect(surface, &rectBlue, colorBlue);
      SDL_FillRect(surface, &rectGreen, colorGreen);
      SDL_FillRect(surface, &rectYellow, colorYellow);

      isRed = isBlue = isGreen = isYellow = 1;


      static void handleEvent(SDL_Event *evt)
      SDL_Texture *tex;

      if (evt->type == SDL_KEYDOWN)
      if (evt->key.keysym.sym == SDLK_ESCAPE)
      SDL_PushEvent(&QuitEvent);

      else if (evt->key.keysym.sym == SDLK_r)
      if (timer_IsRunning(timerRed))
      timer_Pause(timerRed);

      else
      timer_Start(timerRed);


      else if (evt->key.keysym.sym == SDLK_b)
      if (timer_IsRunning(timerBlue))
      timer_Pause(timerBlue);

      else
      timer_Start(timerBlue);


      else if (evt->key.keysym.sym == SDLK_g)
      if (timer_IsRunning(timerGreen))
      timer_Pause(timerGreen);

      else
      timer_Start(timerGreen);


      else if (evt->key.keysym.sym == SDLK_y)
      if (timer_IsRunning(timerYellow))
      timer_Pause(timerYellow);

      else
      timer_Start(timerYellow);



      else if (evt->type == EVENT_TYPE_TIMER_RED)
      if (isRed)
      SDL_FillRect(surface, &rectRed, 0);
      isRed = 0;

      else
      SDL_FillRect(surface, &rectRed, colorRed);
      isRed = 1;

      tex = SDL_CreateTextureFromSurface(render, surface);
      SDL_RenderCopy(render, tex, NULL, NULL);
      SDL_RenderPresent(render);
      SDL_DestroyTexture(tex);

      else if (evt->type == EVENT_TYPE_TIMER_BLUE)
      if (isBlue)
      SDL_FillRect(surface, &rectBlue, 0);
      isBlue = 0;

      else
      SDL_FillRect(surface, &rectBlue, colorBlue);
      isBlue = 1;

      tex = SDL_CreateTextureFromSurface(render, surface);
      SDL_RenderCopy(render, tex, NULL, NULL);
      SDL_RenderPresent(render);
      SDL_DestroyTexture(tex);

      else if (evt->type == EVENT_TYPE_TIMER_GREEN)
      if (isGreen)
      SDL_FillRect(surface, &rectGreen, 0);
      isGreen = 0;

      else
      SDL_FillRect(surface, &rectGreen, colorGreen);
      isGreen = 1;

      tex = SDL_CreateTextureFromSurface(render, surface);
      SDL_RenderCopy(render, tex, NULL, NULL);
      SDL_RenderPresent(render);
      SDL_DestroyTexture(tex);

      else if (evt->type == EVENT_TYPE_TIMER_YELLOW)
      if (isYellow)
      SDL_FillRect(surface, &rectYellow, 0);
      isYellow = 0;

      else
      SDL_FillRect(surface, &rectYellow, colorYellow);
      isYellow = 1;

      tex = SDL_CreateTextureFromSurface(render, surface);
      SDL_RenderCopy(render, tex, NULL, NULL);
      SDL_RenderPresent(render);
      SDL_DestroyTexture(tex);



      int main(int argc, char* args[])

      (void)(argc);
      (void)(args);
      SDL_Event event = 0 ;
      int run = 0;
      SDL_Texture *texture = NULL;

      if (SDL_Init(SDL_INIT_VIDEO

      static void handleTimerRed(void *ignored)
      SDL_Event event;
      (void)(ignored);

      event.type = EVENT_TYPE_TIMER_RED;
      SDL_PushEvent(&event);


      static void handleTimerBlue(void *ignored)
      SDL_Event event;
      (void)(ignored);

      event.type = EVENT_TYPE_TIMER_BLUE;
      SDL_PushEvent(&event);


      static void handleTimerGreen(void *ignored)
      SDL_Event event;
      (void)(ignored);

      event.type = EVENT_TYPE_TIMER_GREEN;
      SDL_PushEvent(&event);


      static void handleTimerYellow(void *ignored)
      SDL_Event event;
      (void)(ignored);

      event.type = EVENT_TYPE_TIMER_YELLOW;
      SDL_PushEvent(&event);







      c sdl





      share







      New contributor




      Mark Benningfield is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Mark Benningfield is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Mark Benningfield is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 1 min ago









      Mark BenningfieldMark Benningfield

      1012




      1012




      New contributor




      Mark Benningfield is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Mark Benningfield is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Mark Benningfield is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          0






          active

          oldest

          votes











          Your Answer





          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );






          Mark Benningfield is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215537%2fpausable-timer-implementation-for-sdl-in-c%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Mark Benningfield is a new contributor. Be nice, and check out our Code of Conduct.









          draft saved

          draft discarded


















          Mark Benningfield is a new contributor. Be nice, and check out our Code of Conduct.












          Mark Benningfield is a new contributor. Be nice, and check out our Code of Conduct.











          Mark Benningfield is a new contributor. Be nice, and check out our Code of Conduct.














          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f215537%2fpausable-timer-implementation-for-sdl-in-c%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          कुँवर स्रोत दिक्चालन सूची"कुँवर""राणा कुँवरके वंशावली"

          Why is a white electrical wire connected to 2 black wires?How to wire a light fixture with 3 white wires in box?How should I wire a ceiling fan when there's only three wires in the box?Two white, two black, two ground, and red wire in ceiling box connected to switchWhy is there a white wire connected to multiple black wires in my light box?How to wire a light with two white wires and one black wireReplace light switch connected to a power outlet with dimmer - two black wires to one black and redHow to wire a light with multiple black/white/green wires from the ceiling?Ceiling box has 2 black and white wires but fan/ light only has 1 of eachWhy neutral wire connected to load wire?Switch with 2 black, 2 white, 2 ground and 1 red wire connected to ceiling light and a receptacle?

          चैत्य भूमि चित्र दीर्घा सन्दर्भ बाहरी कडियाँ दिक्चालन सूची"Chaitya Bhoomi""Chaitya Bhoomi: Statue of Equality in India""Dadar Chaitya Bhoomi: Statue of Equality in India""Ambedkar memorial: Centre okays transfer of Indu Mill land"चैत्यभमि