Ok, I wrote a small test app for it:
Code:
#include "SDL/SDL.h"
#include <stdio.h>
void FPS_Cap(unsigned cap)
{
static unsigned this_frame;
unsigned last_frame, time_taken;
cap = 1000 / cap;
last_frame = this_frame;
this_frame = SDL_GetTicks();
time_taken = this_frame - last_frame;
if(time_taken >= cap) return;
SDL_Delay(cap-time_taken);
}
int main()
{
unsigned int now, i;
printf("Using Frame Cap Function\n");
for(i=0; i<10; i++)
{
now = SDL_GetTicks();
FPS_Cap(60);
now = SDL_GetTicks() - now;
printf("%i\n", now);
}
printf("Using Fixed Delay\n");
for(i=0; i<10; i++)
{
now = SDL_GetTicks();
SDL_Delay(16);
now = SDL_GetTicks() - now;
printf("%i\n", now);
}
return 0;
}
And I got some weird output:
Code:
Using Frame Cap Function
0
16
0
16
0
27
0
17
0
17
Using Fixed Delay
16
17
16
16
16
16
16
16
17
16
For some reason every second time the frame cap function runs I get no delay Also, once, it seems to delay quite a bit too long. Can anyone see why this is happening, or what I could do to fix it?
It seems to me you have some logical problem
You enter this function say at 40ms
then store this moment
then decide, that you need to sleep for 20ms
then start drawing your frame (that takes for example 15ms)
then you go back to your function but to determine the required delay you do not take the moment when the frame was started to process (60ms) but the moment when you previously entered the funtion 40ms... so your new delay will be incorect IMHO
If I understand you correctly, that shouldent cause too much of a problem, this function is only intended to be run once, at the end of a loop after all drawing is done. It is however pretty crude as it uses the average frame time; if one or two frames take abnormally long to draw the the FPS will get slowed down.