在SDL中设置OGL主版本会导致错误

时间:2012-11-08 15:50:25

标签: c opengl gcc sdl opengl-3

我目前正试图用C来降低OpenGL。 我有当前的代码:

#include <stdio.h>
#include <stdlib.h>

#define null (void*)0

#define GL3_PROTOTYPES 1
#include <GL/gl3.h>

#include <SDL.h>
#define PROGRAM_NAME "Tutorial1"

void sdldie(const char *msg)
{
    printf("%s: %s\n", msg, SDL_GetError());
    SDL_Quit();
    exit(1);
}

void checkSDLError(int line)
{
    #ifndef NDEBUG
        const char *error = SDL_GetError();
        if(*error != '\0')
        {
            printf("SDL Error: %s\n", error);
            if(&line != null)
                printf(" + line: %i\n", line);
            SDL_ClearError();
        }
    #endif
}

int main(int argc, char *argv[])
{
    SDL_Window *mainwindow; //window handle
    SDL_GLContext maincontext; //opengl context handle

    if(SDL_Init(SDL_INIT_VIDEO) < 0)//initialize video subsystem
        sdldie("Unable to initialize SDL"); //DIE ON ERROR

    //request OGL 3.2 context (default to SDL core profile)
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);

    //turn on double buffering with a 24 bit Z buffer
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);

    mainwindow = SDL_CreateWindow(PROGRAM_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
    512, 512, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
    if(!mainwindow)
        sdldie("Unable to create window");

    checkSDLError(__LINE__);

    //create an opengl context and attach it to the window
    maincontext = SDL_GL_CreateContext(mainwindow);
    checkSDLError(__LINE__);

    //VSync, turn it off later
    SDL_GL_SetSwapInterval(1);

    //clear buffer with red background
    glClearColor(1.0,0.0,0.0,1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    //swap back buffer to the front
    SDL_GL_SwapWindow(mainwindow);
    //wait 2 secs
    SDL_Delay(2000);

    //repeat with green
    glClearColor(0.0,1.0,0.0,1.0);
    glClear(GL_COLOR_BUFFER_BIT);
    //swap back buffer to the front
    SDL_GL_SwapWindow(mainwindow);
    //wait 2 secs
    SDL_Delay(2000);

    //delete all dis shit
    SDL_GL_DeleteContext(maincontext);
    SDL_DestroyWindow(mainwindow);
    SDL_Quit();

    return 0;
}

注意我们选择OGL版本的行(主要是在初始化视频后)... 当我在这里的代码中执行此操作时,我最终会收到错误:

X Error of failed request:  GLXBadFBConfig
  Major opcode of failed request:  154 (GLX)
  Minor opcode of failed request:  34 ()
  Serial number of failed request:  166
  Current serial number in output stream:  165

造成这种情况的一行具体是:

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);

当它被注释掉时,事情完美无缺(但可能与另一个OGL版本D:)

我正在使用最新版本的FOSS Radeon驱动程序(在Arch上),SDL2.0 HG版本,最新稳定的GLX和gl3.h已经手动下载...在arch linux和gcc 4.7.2版本< / p>

知道为什么会这样(/如何摆脱它)? 并且设置该属性是必要的吗?

1 个答案:

答案 0 :(得分:0)

SDL2尚未发布,可能包含严重错误。在您的情况下,SDL_GL_CreateContext中的错误。

2013年3月10日提交了解决此问题的提交:SDL_GL_CreateContext() causes fatal X11 protocol errors that should just be caught instead

我使用最新的SDL2提交成功创建了一个OpenGl 3.2上下文。