Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
deleted 13 characters in body
Source Link

everybody

I have tried achieving a good delta time and fps counter over the last few days, read and watched a lot about it, but still can't seem to get it to work.

everybody

I have tried achieving a good delta time and fps counter over the last few days, read and watched a lot about it, but still can't seem to get it to work.

I have tried achieving a good delta time and fps counter over the last few days, read and watched a lot about it, but still can't seem to get it to work.

Source Link

Delta Time FPS with SDL 2 in C++

everybody

I have tried achieving a good delta time and fps counter over the last few days, read and watched a lot about it, but still can't seem to get it to work.

Here is an example:

#include <iostream> #include <SDL.h> #include <SDL_image.h> int main(int argc, char* argv[]) { if (SDL_Init(SDL_INIT_VIDEO) != 0) { printf("error initializing SDL: %s\n", SDL_GetError()); } SDL_Window* window = SDL_CreateWindow("Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1000, 900, SDL_WINDOW_SHOWN); if (!window) { printf("error creating window: %s\n", SDL_GetError()); } Uint32 renderFlags = SDL_RENDERER_ACCELERATED; SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, renderFlags); if (!renderer) { printf("error creating renderer"); SDL_DestroyWindow(window); SDL_Quit(); } SDL_Surface* surface = IMG_Load("dot.bmp"); if (!surface) { printf("Error creating surface: %s\n", SDL_GetError()); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); } SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); SDL_FreeSurface(surface); if (!texture) { printf("error creating texture: %s\n", SDL_GetError()); SDL_DestroyRenderer(renderer); SDL_DestroyWindow(window); SDL_Quit(); } SDL_Rect dest; dest.x = 0; dest.y = 0; SDL_QueryTexture(texture, NULL, NULL, &dest.w, &dest.h); Uint64 NOW = SDL_GetPerformanceCounter(); Uint64 LAST = 0; double deltaTime = 0; while (true) { LAST = NOW; NOW = SDL_GetPerformanceCounter(); deltaTime = (double)((NOW - LAST) / (double)SDL_GetPerformanceFrequency()); SDL_RenderClear(renderer); dest.x += 50 * deltaTime; dest.y += 50 * deltaTime; SDL_RenderCopy(renderer, texture, NULL, &dest); SDL_RenderPresent(renderer); std::cout << "Delta Time: " << deltaTime << std::endl; std::cout << "FPS: " << 60.0 - deltaTime << std::endl; SDL_Delay(1000.0f / (60.0 - deltaTime)); } return 0; } 

I used the suggestion from this post: How to calculate delta time with SDL?

I print out "delta time" and "FPS" to the console and, while the deltaTime is slightly different each time, the FPS is stable 60 (which is the same value that I use in SDL_Delay to calculate delay in ms). But the test image is clearly moving not smoothly, it stutters and moves at inconsistent speed, and I can't understand why.

Please help me understand what I am doing wrong. I just can't understand it even after looking through many examples.