# SDL2 - Inconsistent FPS with VSYNC

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.