I shuffled the operations around to try and get my function working and I'm really confused by my output. Heres the code:
Code:
#include "SDL/SDL.h"
#include <stdio.h>
void FPS_Cap(unsigned cap)
{
static unsigned this_frame;
static unsigned last_frame;
static unsigned time_taken;
cap = 1000 / cap;
time_taken = this_frame - last_frame;
printf("Last: %u This: %u Time: %u ", last_frame, this_frame, time_taken);
if(time_taken >= cap)
{
last_frame = this_frame;
this_frame = SDL_GetTicks();
printf("Delay: 0 ");
return;
}
SDL_Delay(cap-time_taken);
printf("Delay: %u ", cap-time_taken);
last_frame = this_frame;
this_frame = SDL_GetTicks();
}
int main()
{
unsigned then, now;
int i;
int a, b;
printf("Using Frame Cap Function\n");
for(i=0; i<10; i++)
{
then = SDL_GetTicks();
for(a=0; a<1000; a++)
for(b=0; b<1000; b++)
;
FPS_Cap(60);
now = SDL_GetTicks() - then;
printf("Total Time: %u\n", now);
}
return 0;
}
And heres my output:
Code:
Using Frame Cap Function
Last: 0 This: 0 Time: 0 Delay: 16 Total Time: 542
Last: 0 This: 2048506560 Time: 2048506560 Delay: 0 Total Time: 4
Last: 2048506560 This: 2048506564 Time: 4 Delay: 12 Total Time: 15
Last: 2048506564 This: 2048506579 Time: 15 Delay: 1 Total Time: 5
Last: 2048506579 This: 2048506584 Time: 5 Delay: 11 Total Time: 24
Last: 2048506584 This: 2048506608 Time: 24 Delay: 0 Total Time: 4
Last: 2048506608 This: 2048506612 Time: 4 Delay: 12 Total Time: 24
Last: 2048506612 This: 2048506636 Time: 24 Delay: 0 Total Time: 3
Last: 2048506636 This: 2048506639 Time: 3 Delay: 13 Total Time: 17
Last: 2048506639 This: 2048506656 Time: 17 Delay: 0 Total Time: 4
The for the first 2 loops the calculations are going to be wrong, but then it seems to be calculating the delay correctly. The total time I am getting in my main function seems totally random. Can anyone explain it?
This is annoying. I should have given up on it hours ago