[Solved] Artifact of the black line in the client area

Hello,

I have a black line artifact in the client area that disappears when the window is resized:

resize-opengl-window-sdl2-cpp

#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>

#include <glad/glad.h>
#include <iostream>

SDL_Window *window;
const float maxFPS = 5.f;

void fatalError(const std::string &message)
{
    std::cout << message << std::endl;
    if (window)
    {
        SDL_DestroyWindow(window);
    }
    SDL_Quit();
    exit(-1);
}

int main()
{
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        fatalError("Failed to initialize");
    }

    const int winW = 300;
    const int winH = 300;
    window = SDL_CreateWindow("OpenGL, SDL2, C++",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        winW,
        winH,
        SDL_WINDOW_OPENGL);
    if (!window)
    {
        fatalError("Failed to create the SDL window");
    }

    SDL_SetWindowResizable(window, SDL_TRUE);

    SDL_GLContext glContext = SDL_GL_CreateContext(window);
    if (!glContext)
    {
        fatalError("Failed to create the SDL_GL context");
    }

    if (!gladLoadGL())
    {
        fatalError("Failed to initialize the GLAD library");
    }

    glViewport(0, 0, winW, winH);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    glClearColor(0.5f, 0.5f, 1.f, 1.f);
    
    SDL_Event event;
    bool running = true;
    while (running)
    {
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                running = false;
                break;
            }
        }
        float startTicks = SDL_GetTicks();

        glClear(GL_COLOR_BUFFER_BIT);

        SDL_GL_SwapWindow(window);

        // Limit FPS to the max FPS
        float frameTicks = SDL_GetTicks() - startTicks;
        if (1000.f / maxFPS > frameTicks)
        {
            SDL_Delay(1000.f / maxFPS - frameTicks);
        }
    }

    SDL_GL_DeleteContext(glContext);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}

makefile

# build command: mingw32-make
# -mwindows - a key to hide the console

INC = -I"E:\Libs\SDL2-2.24.0-mingw-64bit\include" \
	  -I"E:\Libs\glad-0.1.36-mingw-64bit\include"

LIB = -L"E:\Libs\SDL2-2.24.0-mingw-64bit\lib" \
	  -L"E:\Libs\glad-0.1.36-mingw-64bit\lib"

all: main.o
	g++ main.o $(LIB) -lSDL2.dll -lglad -o app.exe

main.o: main.cpp
	g++ -c $(INC) main.cpp

Not sure if it matters but the docs says you should call SDL_GL_SetAttribute before creating the window.

1 Like

@Peter87 I tried it but everything the same:

int main()
{
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        fatalError("Failed to initialize");
    }
    
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

    const int winW = 300;
    const int winH = 300;
    window = SDL_CreateWindow("OpenGL, SDL2, C++",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        winW,
        winH,
        SDL_WINDOW_OPENGL);
    if (!window)
    {
        fatalError("Failed to create the SDL window");
    }
    
    SDL_SetWindowResizable(window, SDL_TRUE);

    SDL_GLContext glContext = SDL_GL_CreateContext(window);
    if (!glContext)
    {
        fatalError("Failed to create the SDL_GL context");
    }

I tried to add SDL_WINDOWEVENT_RESIZED but it does not effect:

    while (running)
    {
        while (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_QUIT:
                running = false;
                break;
            case SDL_WINDOWEVENT:
                switch (event.window.event)
                {
                case SDL_WINDOWEVENT_RESIZED:
                    glViewport(0, 0, event.window.data1, event.window.data2);
                    break;
                }
                break;
            }
        }

I was helped to solve the problem here: opengl - Artifact of the black line in the client area - Stack Overflow

It appears to be a SDL bug, though for me the effect was different (the window wasn’t resizable at all).

Instead of calling SDL_SetWindowResizable(window, SDL_TRUE);, pass SDL_WINDOW_RESIZABLE when creating the window.

    const int winW = 300;
    const int winH = 300;
    window = SDL_CreateWindow("OpenGL, SDL2, C++",
        SDL_WINDOWPOS_CENTERED,
        SDL_WINDOWPOS_CENTERED,
        winW,
        winH,
        SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);

I created the issue: Passing SDL_WINDOW_RESIZABLE when creating the window, instead of calling SDL_SetWindowResizable · Issue #6324 · libsdl-org/SDL · GitHub