SDL_GL_GetAttribute()输出零?

时间:2017-04-05 00:18:14

标签: c++ sdl sdl-2

我目前正在开发一个使用OpenGL的SDL窗口。当我使用错误检查初始化SDL_GL_SetAttribute(colours here... r, g, b etc.)时,SDL_GL_GetAttribute()为alpha,buffer,r,g,b返回0,我认为这阻止我在SDL窗口中创建基本的OpenGL三角形。 ..我不确定。

我希望能就如何解决这个问题找到一些指示。

以下是初始化OpenGL和SDL的代码段:

 void Window::InitialiseSDL()
 {
 SDL_Init(  SDL_INIT_AUDIO  | SDL_INIT_EVENTS   | SDL_INIT_GAMECONTROLLER
         |  SDL_INIT_HAPTIC | SDL_INIT_JOYSTICK | SDL_INIT_TIMER
         |  SDL_INIT_VIDEO                                              );

Has_SDL_Init = SDL_WasInit(SDL_INIT_EVERYTHING);

if (!(Has_SDL_Init & SDL_INIT_AUDIO)) // You may be thinking wtf here? But!   if one sub system were to break, i'd know which one it exactly was.
{
    error.Fatal_SDL_Error("Class Window.h ----> InitialiseSystem() ----> SDL_INIT_AUDIO Has failed to initialise.", state);
}
if (!(Has_SDL_Init & SDL_INIT_EVENTS))
{
    error.Fatal_SDL_Error("Class Window.h ----> InitialiseSystem() ----> SDL_INIT_EVENTS Has failed to initialise.", state);
}
if (!(Has_SDL_Init & SDL_INIT_GAMECONTROLLER))
{
    error.Fatal_SDL_Error("Class Window.h ----> InitialiseSystem() ----> SDL_INIT_GAMECONTROLLER Has failed to initialise.", state);
}
if (!(Has_SDL_Init & SDL_INIT_HAPTIC))
{
    error.Fatal_SDL_Error("Class Window.h ----> InitialiseSystem() ----> SDL_INIT_HAPTIC Has failed to initialise.", state);
}
if (!(Has_SDL_Init & SDL_INIT_JOYSTICK))
{
    error.Fatal_SDL_Error("Class Window.h ----> InitialiseSystem() ----> SDL_INIT_JOYSTICK Has failed to initialise.", state);
}
if (!(Has_SDL_Init & SDL_INIT_TIMER))
{
    error.Fatal_SDL_Error("Class Window.h ----> InitialiseSystem() ----> SDL_INIT_TIMER Has failed to initialise.", state);
}
if (!(Has_SDL_Init & SDL_INIT_VIDEO))
{
    error.Fatal_SDL_Error("Class Window.h ----> InitialiseSystem() ----> SDL_INIT_VIDEO Has failed to initialise.", state);
}

int r, g, b, a, minor, major, visual, depthSize, stencil, buffer, dblbuffer;
int core, flagsss;

SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
SDL_GL_GetAttribute(SDL_GL_ACCELERATED_VISUAL, &visual);


SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);                  // Forces the use of Modern OpenGL. 
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &core);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_FLAGS, &flagsss);

// They all fail from this point below ( Only in SDL init method )

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 3);
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &r);

SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &g);

SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &b);

SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 1);
SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE,&a);

SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depthSize);

SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 5);
SDL_GL_GetAttribute(SDL_GL_STENCIL_SIZE, &stencil);

SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 24);
SDL_GL_GetAttribute(SDL_GL_BUFFER_SIZE, &buffer);

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_GetAttribute(SDL_GL_DOUBLEBUFFER, &dblbuffer);

this->GameWindow = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 
                                    this->windowWidth, this->windowHeight, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);
if (!GameWindow)
{
    ShutDownSystem();
}

SDL_SetWindowResizable(GameWindow, SDL_TRUE);
}

void Window::InitialiseOpenGL()
{
OpenGL_Context = SDL_GL_CreateContext(GameWindow);
SDL_GL_SetSwapInterval(1);

if (OpenGL_Context == nullptr)
{
    error.Fatal_SDL_Error("LegendRenderer ----> InitialiseOpenGL() ----> Failed to create an OpenGL context for SDL.", state);
    SetWindowEngineState(*state);
}

glewExperimental = GL_TRUE;
GLenum OpenGLError = glewInit();
if (OpenGLError != GLEW_OK)
{
    error.Fatal_SDL_Error("LegendRenderer ----> InitialiseOpenGL() ----> Failed to Initialise GLEW.", state);
    SetWindowEngineState(*state);
}

printf("OpenGL loaded\n");
printf("Vendor:   %s\n", glGetString(GL_VENDOR));
printf("Renderer: %s\n", glGetString(GL_RENDERER));
printf("Version:  %s\n", glGetString(GL_VERSION));


if (SDL_GL_GetSwapInterval < 0)
{
    error.Fatal_SDL_Error("LegendRenderer ----> InitialiseOpenGL() ----> Failed to set frame buffer interval for SDL.", state);
    SetWindowEngineState(*state);
}

int w, h;
SDL_GetWindowSize(GameWindow, &w, &h);
glViewport(0, 0, w, h);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
}

1 个答案:

答案 0 :(得分:2)

SDL_GL_GetAttribute()(强调我的):

  

使用此函数从当前[OpenGL]上下文获取属性的实际值。

你甚至在拥有一个窗口之前就已经调用了SDL_GL_GetAttribute(),更不用说当前的OpenGL上下文了。

在成功创建窗口后调用SDL_GL_GetAttribute() &amp; OpenGL上下文,并通过SDL_GL_MakeCurrent()使上下文变为当前(嗯,技术上不需要,因为SDL_GL_CreateContext()会使创建的上下文保持最新状态。)

例如,您可能已经请求了5个红色位,但最终获得的OpenGL上下文可能会提供8位。