首页 > 解决方案 > 创建渲染器时 SDL 终止

问题描述

当我运行此代码时:

#include <iostream>
#include <SDL.h>
#include <stdexcept>
#include <GL/gl3w.h>

int main() try {
    if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER ) != 0 )
        throw std::runtime_error{ "Could not initialize sdl" };

    SDL_GL_SetAttribute( SDL_GL_CONTEXT_FLAGS, 0 );
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 0 );

    // Create window with graphics context
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 );
    SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 8 );
    auto window_flags = (SDL_WindowFlags) ( SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI );
    auto window       = SDL_CreateWindow( "window", SDL_WINDOWPOS_CENTERED,
                                          SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags );
    auto gl_context   = SDL_GL_CreateContext( window );
    SDL_GL_MakeCurrent( window, gl_context );
    SDL_GL_SetSwapInterval( 1 ); // Enable vsync

    if ( gl3wInit() != 0 )
        throw std::runtime_error{ "Unable to initialize OpenGL loader" };

    auto renderer = SDL_CreateRenderer( window, -1, 0 );

    return 0;
}
catch ( std::exception& e ) {
    std::cerr << e.what() << "\n";
    return -1;
}

它产生以下输出:

X Error of failed request:  GLXBadDrawable
  Major opcode of failed request:  151 (GLX)
  Minor opcode of failed request:  5 (X_GLXMakeCurrent)
  Serial number of failed request:  259
  Current serial number in output stream:  259

Process finished with exit code 1

当我注释掉设置 opengl 版本的行时,它运行良好。

是什么导致了这个错误,为什么SDL_CreateRenderer终止而不是返回一个空指针?

标签: openglsdlsdl-2

解决方案


推荐阅读