Sunday, October 9, 2022
HomeGame Developmentsdl2 - Delta Time FPS with SDL 2 in C++

sdl2 – Delta Time FPS with SDL 2 in C++


I’ve tried reaching a very good delta time and fps counter over the previous few days, learn and watched rather a lot about it, however nonetheless can not seem to get it to work.

Here is an instance:

#embody <iostream>
#embody <SDL.h>
#embody <SDL_image.h>

int predominant(int argc, char* argv[]) {

    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        printf("error initializing SDL: %sn", 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: %sn", 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* floor = IMG_Load("dot.bmp");

    if (!floor) {
        printf("Error creating floor: %sn", SDL_GetError());
        SDL_DestroyRenderer(renderer);
        SDL_DestroyWindow(window);
        SDL_Quit();
    }

    SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, floor);
    SDL_FreeSurface(floor);
    if (!texture) {
        printf("error creating texture: %sn", 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;

    whereas (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 publish: How to calculate delta time with SDL?

I print out “delta time” and “FPS” to the console and, whereas the deltaTime is barely completely different every time, the FPS is secure 60 (which is identical worth that I take advantage of in SDL_Delay to calculate delay in ms).
But the take a look at picture is clearly shifting not easily, it stutters and strikes at inconsistent velocity, and I am unable to perceive why.

Please assist me perceive what I’m doing fallacious. I simply cannot perceive it even after wanting by means of many examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments