Frame limiting in an SDL game I am a relatively novice programmer, and I am taking steps into graphics programming. I am writing a quick pong clone and want to cap the FPS to save system resources. This is how I have implemented this (I've omitted other code for brevity)

#include &lt;stdio.h&gt;
#include &lt;stdbool.h&gt;
#include "SDL.h"
#include "Windows.h"
#define WIN32_LEAN_AND_MEAN  
#define FPS 30 

Uint32 starttime, endtime, deltatime;

int main()
{
 //initialize SDL and do other stuff
 //start game loop
 timeBeginPeriod(1);
 while (appisrunning)
 {
 starttime = GetTickCount();
    SDL_PollEvent(&amp;event);
    if (event.type == SDL_QUIT)
    {
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
        appisrunning = false;
        break;
    }
 // render stuff here
 endtime = GetTickCount();
    if (endtime &gt; starttime)
    {
        deltatime = endtime - starttime;
    }
    else // handles wraparound
    {
        deltatime = ((2 ^ 32) - starttime) + endtime;
    }
    if (deltatime &gt; (1000 / FPS)) {} 
    else
    {
        Sleep((1000 / FPS) - deltatime);
    }
 }
 timeEndPeriod(1);
 return 0;
}


I use timeBeginPeriod(1) in an effort to give Sleep() a ~1ms resolution. According to the MSDN documentation, this function sets the timer resolution system wide and can result in higher CPU usage as the scheduler has to switch tasks more often (if I understand correctly). 

Is this the right approach for this task? What feedback do you have for me?
Code does not handle "wrap-around" correctly.

2 ^ 32 is the exclusive-or of 32 and 2.  That has the value of 34.  Instead, as unsigned math is defined to wrap-around, just subtract.

//if (endtime &gt; starttime) {
//    deltatime = endtime - starttime;
//} else {
//    deltatime = ((2 ^ 32) - starttime) + endtime;
//}

deltatime = endtime - starttime;


Missing objects

event, appisrunning, renderer, window are not obviously declared before use in SDL_PollEvent(&amp;event);.  Perhaps an SDL thing?

Unnecessary use of global variables

Uint32 starttime, endtime, deltatime; belongs in main().  Or even better, within the loop.

Use an auto formatter

The below code hints at manual formatting.  Use an auto formatter and save time.

 // render stuff here
 endtime = GetTickCount();
    if (endtime &gt; starttime)


An auto formatter would have the below and constant indent amount per level (rather than 2 and sometimes 4 spaces).

Example re-format

#include &lt;stdio.h&gt;
#include &lt;stdbool.h&gt;
#include "SDL.h"
#include "Windows.h"

#define WIN32_LEAN_AND_MEAN
#define FPS 30

int main(void) {
  timeBeginPeriod(1);
  while (appisrunning) {
    Uint32 starttime = GetTickCount();
    SDL_PollEvent(&amp;event);

    if (event.type == SDL_QUIT) {
      SDL_DestroyRenderer(renderer);
      SDL_DestroyWindow(window);
      SDL_Quit();
      appisrunning = false;
      break;
    }

    Uint32 endtime = GetTickCount();
    Uint32 deltatime = endtime - starttime;

    if (deltatime &gt; (1000 / FPS)) {
    } else {
      Sleep((1000 / FPS) - deltatime);
    }
  }
  timeEndPeriod(1);
  return 0;
}