Why does my SDL3 C++ app display very low fps but my frame count is very high

Joined
Mar 3, 2025
Messages
1
Reaction score
0
I am using gettimeofday() function to calculate how long my code takes. I am also counting how many frames have been displayed. The count shows that my code has done 6408 frames in 10 seconds - but on the screen I am only seeing about 8 fps.
Here is my code:

Code:
#define SDL_MAIN_USE_CALLBACKS
#include <SDL3/SDL_main.h>
#include <SDL3_image/SDL_image.h>
#include <SDL3/SDL.h>
#include <sys/time.h>
#include <time.h>
#include <math.h>

SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *fontTex;
long count=0;

SDL_AppResult SDL_AppInit(void **appstate, int argc, char **argv)
{
    if( !SDL_Init(SDL_INIT_VIDEO))
    {
        SDL_Log("init SDL_INIT_VIDEO failed", SDL_GetError());
        return SDL_APP_FAILURE;
    }

    window = SDL_CreateWindow("BLIT TEST", 800, 600, SDL_WINDOW_FULLSCREEN);
    if( !window)
    {
        SDL_Log("error creating window", SDL_GetError());
        return SDL_APP_FAILURE;
    }

    renderer = SDL_CreateRenderer(window, NULL);
    if( !renderer)
    {
        SDL_Log("error creating renderer", SDL_GetError());
        return SDL_APP_FAILURE;
    }

    const char path[] = "./assets/goldFont.png";
    fontTex = IMG_LoadTexture(renderer, path);

    SDL_SetTextureScaleMode(fontTex,SDL_SCALEMODE_NEAREST);//SDL_SCALEMODE_LINEAR

    return SDL_APP_CONTINUE;
}

void Render()
{
//    SDL_SetRenderDrawColor(renderer,0,255,0,255);
}

void Update()
{
}



SDL_AppResult SDL_AppIterate(void *appstate)
{
    struct timeval stop, start;
    
    SDL_RenderClear(renderer);

    gettimeofday(&start, NULL);

    for( int i=0; i<1000; i++)
    {
        float x = rand() % 3000;
        float y = rand() % 2000;

        SDL_FRect srcRect = {200,200,100,100};
        SDL_FRect destRect = {x,y,100,100};

        SDL_RenderTexture(renderer, fontTex, &srcRect, &destRect);

    }

    gettimeofday(&stop, NULL);
    long cpu_time_used1 = (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec;


    gettimeofday(&start, NULL);

    SDL_RenderPresent(renderer);

    gettimeofday(&stop, NULL);
    long cpu_time_used2 = (stop.tv_sec - start.tv_sec) * 1000000 + stop.tv_usec - start.tv_usec;


    char output[100];
    snprintf(output, 50, "%d,   %lu,   %lu", count++, cpu_time_used1, cpu_time_used2);
    SDL_Log(output);

    return SDL_APP_CONTINUE;
}

SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
    if( event->type == SDL_EVENT_QUIT)
    {
        return SDL_APP_SUCCESS;
    }
    return SDL_APP_CONTINUE;
}

void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
    SDL_DestroyRenderer(renderer);
    renderer = NULL;
    SDL_DestroyWindow(window);
    window = NULL;
    SDL_QuitSubSystem(SDL_INIT_VIDEO);
}

The output of my code looks like this (at the end):
6406, 83, 1198
6407, 101, 1184
6408, 106, 1186

These timing numbers seem very low - is it possible that SDL3 is that quick?
And why am I only seeing very low fps on screen?

If I change the for loop to:
for( int i=0; i<100; i++)

The fps goes very high. And these are the logs of ten seconds:
20197, 11, 294
20198, 8, 238
20199, 13, 264
So, my code is being called 20199 times in ten seconds!

any thoughts?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,283
Messages
2,571,405
Members
48,098
Latest member
inno vation

Latest Threads

Top