SDL 版本 2.26.2
OpenGL核心版本4.0
我正在使用 SDL 为 OpenGL 项目创建窗口和上下文。我将 RGB 设置为 8 位,双缓冲,然后创建我的窗口和上下文。创建上下文后,我使用 SDL_GL_GetAttribute() 检索像素“形状”,这样我就知道我在使用什么。然而,每次我这样做,我都会得到一个错误,说明
Failed to retrieve red size. Error message:
OpenGL error: 00000502
["alpha":8, "blue":8, "red":0, "green":8]
我在循环中使用正确的值调用 SDL_GL_GetAttribute(),但我每次都会收到此错误。在查看 OpenGL 文档时,我发现错误 502 表示“无效操作”,但我不知道无效操作可能是什么。任何帮助或建议将不胜感激。
编辑
在尝试为此最小化代码时,错误更改为以下内容:
[warning] source\app.d:61:main Attempt to read red size failed, OpenGL error: GL_INVALID_ENUM
生成该警告的代码如下:
import std.stdio;
import std.experimental.logger;
import std.conv;
import bindbc.sdl;
import bindbc.opengl;
SDL_Window* window;
SDL_Renderer* render;
SDL_GLContext context;
void main()
{
auto support = loadSDL();
if (support != sdlSupport) {
if (support == SDLSupport.noLibrary) {
writeln("the library could not be found");
}
else if (support == SDLSupport.badLibrary) {
writeln("the version of SDL library loaded does not match the expected version");
}
else {
writeln("Something bad happened here...");
}
}
loadSDL("SDL2.lib");
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
window = SDL_CreateWindow("testbed",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 768,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
);
context = SDL_GL_CreateContext(window);
auto ogl_support = loadOpenGL();
SDL_GL_SetSwapInterval(1);
SDL_ShowWindow(window);
SDL_Event event;
bool running = true;
int red, green, blue;
if (SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &red) < 0)
log(LogLevel.warning, "Attempt to read red size failed, " ~ to!string(SDL_GetError()));
if (SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &green) < 0)
log(LogLevel.warning, "Attempt to read green size failed, " ~ to!string(SDL_GetError()));
if (SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &blue) < 0)
log(LogLevel.warning, "Attempt to read red size failed, " ~ to!string(SDL_GetError()));
writeln(text(red), " ", text(green), " ", text(blue));
while(running) {
SDL_GL_SwapWindow(window);
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
running = false;
break;
}
}
}
SDL_Quit();
}
再一次,当我尝试使用 SDL_GL_GetAttribute(SDL_GL_RED_SIZE, ...) 时发生错误,但在其他方面似乎可以很好地检索绿色和蓝色等价物。