SDL2 screen tearing/flicker bug?

Hi everyone,

I have been trying to experiment some with SDL2 and I don’t have a ton of experience with graphics libraries. I came across a rendering issue in my game and I believe I have simplified the code as far as possible. Here is a video of the issue: (the flickering near the top of the screen).

https://streamable.com/0sf72

My laptop has switchable graphics (Intel HD 4600 and Nvidia GeForce 860m). The problem is only present if the program is run using the intel graphics. It is also only present when not using SDL’s vsync flag. Additionally, if I set SDL to software rendering instead of hardware-accelerated rendering, the bug does not exist.

This test just clears the screen with a dark color and then draws a red rectangle over the screen for each frame.

Thanks for any help in advance, this is driving me crazy.

Here is the code for this test:

#include “SDL.h”

//Screen dimension constants
const int SCREEN_WIDTH = 1920;
const int SCREEN_HEIGHT = 1080;

int main(int argc, char* args[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window = SDL_CreateWindow(“Test”, 0, 0, 1920, 1080, SDL_WINDOW_FULLSCREEN | SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_ShowWindow(window);

SDL_Rect screenRect = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };

int startTime = SDL_GetTicks();

while (SDL_GetTicks() - startTime < 10000) {

	SDL_SetRenderDrawColor(renderer, 0, 0x55, 0, 0xFF);
	SDL_RenderClear(renderer);

	SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0, 0xFF);
	SDL_RenderFillRect(renderer, &screenRect);

	SDL_RenderPresent(renderer);
}		

SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);

SDL_Quit();

return 0;

}

The bugged effect looks like some kind of double buffering issue, or rather the lack of double buffering. But SDL2 should use double buffering by default so maybe it’s some kind of a graphics driver issue.

I’m not sure if it’s even possible to not get double buffering, but you could still try calling something like this after creating the renderer to see if it’s on:

int doubleBuffering;
SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &doubleBuffering)

What operating system are you using? Maybe try updating the graphics drivers.

Also you should indent your code with 4 spaces to get proper formatting on the forum (or just select it and click the “Preformatted text” button on the editor)