工作区中黑线的瑕疵

nkkqxpd9  于 2022-10-18  发布在  其他
关注(0)|答案(1)|浏览(146)

我在工作区中有一条黑线,当调整窗口大小时,它会消失:

我调用了glViewport:


# 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");
    }

    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");
    }

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

    glViewport(0, 0, winW, winH);
    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;
            case SDL_WINDOWEVENT:
                switch (event.window.event)
                {
                case SDL_WINDOWEVENT_RESIZED:
                    glViewport(0, 0, event.window.data1, event.window.data2);
                    break;
                }
                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;
}

生成文件


# 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

SDL2官方论坛上的相同主题:https://discourse.libsdl.org/t/artifact-of-the-black-line-in-the-client-area/39110
不知道这是否重要,但docs说您应该在创建窗口之前调用SDL_GL_SetAttribute
@Peter87我试过了,但一切都一样:

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");
    }
r7xajy2e

r7xajy2e1#

它似乎是一个SDL错误,尽管对我来说效果是不同的(窗口根本不能调整大小)。
不调用SDL_SetWindowResizable(window, SDL_TRUE);,而是在创建窗口时传递SDL_WINDOW_RESIZABLE

相关问题