gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:56:44 26 4
gpt4 key购买 nike

我目前正在尝试用 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

知道为什么会发生这种情况吗(/如何摆脱它)?设置该属性是否有必要?

最佳答案

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

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

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

关于c - 在 SDL 中设置 OGL 主要版本会导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13292518/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com