Wiki Page Content

SDL_atomic_t

A structure representing an atomic integer value.

Data Fields

int

value

the atomic integer value

Code Examples

/* This is a simple fps counter program
 *
 * Timers in SDL are run in a separate thread
 * so effort is needed to avoid data races
 */
SDL_atomic_t frames;

/* Calculate and display the average framerate over the set interval */
Uint32 fps_timer_callback(Uint32 interval, void *data)
{
        const float f = SDL_AtomicGet(&frames);
        const float iv = (float)interval / 1000.0f;
        printf("%.2f\tfps\n", f / iv);

        /* Reset frame counter */
        SDL_AtomicSet(&frames, 0);

        return interval;
}

int main(int argc, char **argv)
{
        SDL_Init(SDL_INIT_EVERYTHING);
        SDL_Window *win = SDL_CreateWindow("Counter",
                        SDL_WINDOWPOS_CENTERED,
                        SDL_WINDOWPOS_CENTERED,
                        640,
                        480,
                        0
                        );

        SDL_Surface *screen = SDL_GetWindowSurface(win);

        /* Our timer will be run every five seconds in a separate thread */
        SDL_AddTimer(5000, fps_timer_callback, NULL);

        SDL_Event e;
        while(1) {
                while (SDL_PollEvent(&e)) {
                        if (e.type == SDL_QUIT) {
                                goto quit;
                        }
                }
                SDL_FillRect(screen, NULL, 0xffffffff);
                SDL_UpdateWindowSurface(win);

                /* Add one frame */
                SDL_AtomicAdd(&frames, 1);
        }
quit:
        SDL_DestroyWindow(win);
        SDL_Quit();
        return 0;
}

Remarks

SDL_atomic_t is a structure so people don't accidentally use numeric operations on it. Instead, you should use the atomic operation functions to work with the integer value.


CategoryStruct, CategoryAtomic

None: SDL_atomic_t (last edited 2018-06-13 00:01:30 by ColaEuphoria)

Feedback
Please include your contact information if you'd like to receive a reply.
Submit