gpt4 book ai didi

c - 不显示 SDL 的 OpenGL 窗口

转载 作者:太空狗 更新时间:2023-10-29 12:21:02 26 4
gpt4 key购买 nike

我正在使用 OpenGL/SDL 开发一个程序,但窗口无法绘制。该窗口的条目出现在任务栏和 alt+tab 菜单中,但不显示缩略图。当我单击它时,它被标记为事件状态,但屏幕上没有任何变化。

我可以验证 glClear(GL_COLOR_BUFFER_BIT)SDL_GL_SwapBuffers() 正在从我的渲染循环中调用,并且 SDL_SetVideoMode 正在成功,但它仍然不会呈现。这不是共享库问题,因为其他 SDL/GL 应用程序运行良好。

有什么想法吗?

编辑:

这是图形初始化:

void GraphicsManager::initGraphics() {
// Access prefs
PreferencesManager* prefMgr = PreferencesManager::getInstance();
int width = prefMgr->getIntKey("res_width");
int height = prefMgr->getIntKey("res_height");

if(SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Cannot initialize SDL video!\n");
exit(1);
}

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);

m_screen = SDL_SetVideoMode(width, height, 0, SDL_OPENGL);
if(!m_screen) {
fprintf(stderr, "Error: Invalid screen pointer\n");
exit(2);
}

SDL_ShowCursor(SDL_DISABLE);

// Setup OpenGL stuffs
glDisable(GL_DEPTH_TEST); // We're only using 2D
glClearColor(0.0, 0.0, 0.0, 0.0);
glViewport(0.0, 0.0, width, height);
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, width, height, 0.0, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glEnable(GL_TEXTURE_2D);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapBuffers();

// Init FPS stuff
m_avgFps = 60.0f;
m_lastUpdate = SDL_GetTicks();
}

这是我的主循环:

while(stateMgr->getState() != GS_QUIT) {
// Process input events
inputMgr->processEvents();

// Update the current system mode
stateMgr->update();

// Update GUI state
guiMgr->update();

// Render
gfxMgr->render();
//char buf[16];
//snprintf(buf, 16, "FPS: %4.2f", gfxMgr->getFramerate());
//testWindow->getChild("TestRoot/Framerate")->setText(buf);
SDL_Delay(10);
}

这是渲染函数:

void GraphicsManager::render() {
std::list<IRenderCallback*>::iterator rci;

// Clear the screen
glClear(GL_COLOR_BUFFER_BIT);

// Run render callbacks
/*for(rci=m_renderCallbacks.begin();rci != m_renderCallbacks.end();rci++) {
(*rci)->preRender();
}

// Iterate through all renderables and render them
std::list<Renderable*>::iterator i;
for(i=m_renderables.begin();i != m_renderables.end();i++) {
(*i)->render();
}

// Run render callbacks
for(rci=m_renderCallbacks.begin();rci != m_renderCallbacks.end();rci++) {
(*rci)->preFlip();
}*/

// Flip the buffers
SDL_GL_SwapBuffers();

// Update FPS stuff
Uint32 now = SDL_GetTicks();
Uint32 timeTaken = now - m_lastUpdate;
m_lastUpdate = now;
double seconds = (double)timeTaken / 1000.0f;
m_avgFps = (1.0f / seconds);

// Run render callbacks
for(rci=m_renderCallbacks.begin();rci != m_renderCallbacks.end();rci++) {
(*rci)->postRender();
}
}

最佳答案

分解一下。做应该有效的(大部分)最简单的事情:

#include <SDL.h>
#include <SDL_opengl.h>
#include <cstdio>
#include <iostream>

using namespace std;

int main( int argc, char** argv )
{
if(SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
fprintf(stderr, "Cannot initialize SDL video!\n");
exit(1);
}

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);

int width = 640;
int height = 480;

SDL_Surface* m_screen = SDL_SetVideoMode(width, height, 0, SDL_OPENGL);
if(!m_screen) {
fprintf(stderr, "Error: Invalid screen pointer\n");
exit(2);
}

cout << "GL_VERSION : " << glGetString(GL_VERSION) << endl;
cout << "GL_VENDOR : " << glGetString(GL_VENDOR) << endl;
cout << "GL_RENDERER : " << glGetString(GL_RENDERER) << endl;

SDL_ShowCursor(SDL_DISABLE);

// Setup OpenGL stuffs
glDisable(GL_DEPTH_TEST); // We're only using 2D
glClearColor(0.0, 0.0, 0.0, 0.0);
glViewport(0.0, 0.0, width, height);
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, width, height, 0.0, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

//glEnable(GL_TEXTURE_2D);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapBuffers();

float angle = 0;
bool running = true;
while( running )
{
// handle all pending events
SDL_Event event;
while( SDL_PollEvent(&event) )
{
switch (event.type)
{
case SDL_QUIT:
running = false;
break;
default:
break;
}
}

angle += 1;
while( angle > 360 )
angle -= 360;

// Render
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glTranslatef( width/2, height/2, 0 );
glScalef( 150, 150, 0 );

glRotatef( angle, 0, 0, 1 );
glColor3ub(255,0,0);
glBegin(GL_TRIANGLES);
glVertex2i(0,0);
glVertex2i(1,0);
glVertex2i(1,1);
glEnd();

glPopMatrix();

// Flip the buffers
SDL_GL_SwapBuffers();

SDL_Delay(10);
}

SDL_Quit();
return 0;
}

如果这可行,您所要做的(哈哈!)就是弄清楚简单的程序执行流程与您的大型程序有何不同。如果它不起作用,您的环境可能会以某种方式出现问题。

GL_VERSION/VENDOR/RENDERER 检查在您的系统上打印什么?

关于c - 不显示 SDL 的 OpenGL 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8458941/

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