Hello there! So, I want to ask you guys, is my solution for limiting the frame rate a good one? Of course, this is just a primitive version of it.
#include <SDL2/SDL.h>
#include <stdio.h>
int main(int argc, const char * argv[]) {
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_CreateWindowAndRenderer(640, 480, SDL_WINDOW_SHOWN | SDL_RENDERER_PRESENTVSYNC, &window, &renderer);
Uint32 startTime = 0;
Uint32 endTime = 0;
Uint32 delta = 0;
short fps = 60;
short timePerFrame = 16; // miliseconds
while (1) {
if (!startTime) {
// get the time in ms passed from the moment the program started
startTime = SDL_GetTicks();
} else {
delta = endTime - startTime; // how many ms for a frame
}
// if less than 16ms, delay
if (delta < timePerFrame) {
SDL_Delay(timePerFrame - delta);
}
// if delta is bigger than 16ms between frames, get the actual fps
if (delta > timePerFrame) {
fps = 1000 / delta;
}
printf("FPS is: %i \n", fps);
startTime = endTime;
endTime = SDL_GetTicks();
}
return 0;
}