3
\$\begingroup\$

I'm working on a game using SDL2. Today I noticed the game has some jolty issues when not fullscreened so I made an extremely simplified program that simply moves a square from left to right to see if the result is the same.

I expected the squares movement to be very smooth but instead it's very jolty/laggy (when not fullscreened).

At first I thought it was to do with how I was calculating the time between each frame but after outputting the 'ftime' I can see the time each frame takes ranges from 13 to 30(ish) as opposed to a consistent 16/17.

Here's the code:

#include <iostream>
#include <SDL.h>

const int GAME_WIDTH = 1920;
const int GAME_HEIGHT = 1080;

//frame independent movement variables
double old_time = 0;
double current_time = 0;
double ftime = 0;

#undef main
int main(int argc, char** argv)
{
    SDL_Window* mainWindow;
    SDL_Renderer* mainRenderer;

    SDL_Init(SDL_INIT_EVERYTHING);

    mainWindow = SDL_CreateWindow("MiracleSquare", 50, 50, GAME_WIDTH, GAME_HEIGHT, 0);
    mainRenderer = SDL_CreateRenderer(mainWindow, -1, SDL_RENDERER_PRESENTVSYNC);


    SDL_Rect miracleRect; //creating the SDL square
    miracleRect.x = 0;
    miracleRect.y = 400;
    miracleRect.w = 100;
    miracleRect.h = 100;

    bool running = true;

    while (running)
    {
        old_time = current_time;
        current_time = SDL_GetTicks();
        ftime = current_time - old_time;

        std::cout << ftime << std::endl;

        SDL_Event e; //stops crashing on input
        if (SDL_PollEvent(&e))
        {
            switch (e.type)
            {
            case SDL_KEYDOWN:
                break;
            default:
                break;
            }
        }


        miracleRect.x += (ftime / 1000.0f) * 100.0f;
        if (miracleRect.x> 1920) miracleRect.x = 0;

        SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255);
        SDL_RenderClear(mainRenderer);
        SDL_SetRenderDrawColor(mainRenderer, 255, 255, 255, 255);
        SDL_RenderDrawRect(mainRenderer, &miracleRect);
        SDL_RenderPresent(mainRenderer);
    }

    return 0;
}

Am I doing something wrong? As far as I can tell this should produce a smooth movement.

Any help would be greatly appreciated!

Edit: Since posting this I've figured out it has something to do with my use of dual monitors. If, for example, I have a video playing on chrome on my secondary monitor, the lag issue is very prominent. I'm now looking into if this issue is avoidable, or if I'll just have to ignore it.

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Which platform are you running on? If it's Windows, then there are chances that the window manager or another application interferes with your graphical updates. I tried to diagnose some stuttering problems in window mode. In one scenario I found a background application messing with graphical settings. In other situations I isolated the problem to a resource conflict between the desktop window manager and the application. Sometimes (for reasons I didn't manage to find out) the desktop window manager will lock the graphical device just long enough for the application to miss the frame.

If this hits you there's not much you can do in my opinion. You can go full screen, or try disable window composition. See these answers from me on how to disable window composition, or how to diagnose stuttering on your own:

https://stackoverflow.com/questions/18182016/strange-opengl-rendering-stutter/21663076#21663076 https://stackoverflow.com/questions/21356302/linear-movement-stutter/21367858#21367858

\$\endgroup\$
1
  • \$\begingroup\$ I'm running on windows, and I think you're right. Thanks for the answer and info, has helped me a great deal! \$\endgroup\$
    – Liam
    Mar 21, 2015 at 0:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .