循环中的 SDL_GL_SwapBuffers() = 冻结?
我读了一些关于 OpenGL 的教程,现在我尝试将它与 SDL 一起使用。问题是,当我在 while 循环中使用 SDL_GL_SwapBuffers() 时,窗口就会冻结。这是一些代码:
#include "SDL.h"
#include "system.h"
#include "SDL_opengl.h"
System Sys(800, 600, 32);
SDL_Event kpress;
int main( int argc, char* args[] )
{
Sys.init();
bool quit = false;
while (!quit)
{
while (SDL_PollEvent(&kpress)) if(kpress.type == SDL_QUIT) quit = true;
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
------------------------------These are in system.h, class System----------------
bool System::init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
errorCode = 1;
return false;
}
if (SDL_SetVideoMode(screen_h, screen_w, bpp, SDL_OPENGL) == 0)
{
errorCode = 2;
return false;
}
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
if (!init_GL())
{
errorCode = 3;
return false;
}
SDL_WM_SetCaption("Engine", 0);
return true;
}
bool System::init_GL()
{
glClearColor(1, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, screen_h, screen_w, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if (glGetError() != GL_NO_ERROR) return false;
return true;
}
如果我绘制一些形状或使用计时器来限制 FPS - 没有任何变化。 你有什么想法吗?
I read a few tutorials about OpenGL and now I'm trying to use it with SDL. The thing is that when I use SDL_GL_SwapBuffers() in a while loop the window just freezes. Here's some code:
#include "SDL.h"
#include "system.h"
#include "SDL_opengl.h"
System Sys(800, 600, 32);
SDL_Event kpress;
int main( int argc, char* args[] )
{
Sys.init();
bool quit = false;
while (!quit)
{
while (SDL_PollEvent(&kpress)) if(kpress.type == SDL_QUIT) quit = true;
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}
------------------------------These are in system.h, class System----------------
bool System::init()
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
errorCode = 1;
return false;
}
if (SDL_SetVideoMode(screen_h, screen_w, bpp, SDL_OPENGL) == 0)
{
errorCode = 2;
return false;
}
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
if (!init_GL())
{
errorCode = 3;
return false;
}
SDL_WM_SetCaption("Engine", 0);
return true;
}
bool System::init_GL()
{
glClearColor(1, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, screen_h, screen_w, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
if (glGetError() != GL_NO_ERROR) return false;
return true;
}
If I draw some shapes or use a timer for limiting FPS - nothing changes.
Do you have any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的第一个建议:摆脱那个伪造的 System 类:到目前为止,它所做的所有任务都是纯粹的顺序/程序性任务,这应该反映在程序大纲中。人们倾向于将所有内容放入课堂中,因为他们被教导从对象模型的角度看待所有内容。但是这个 System 类必须遵循单例模式,在我看来,这是一种反模式。
您放置在 init_GL 中的所有内容都属于渲染循环。创建渲染上下文后,OpenGL 初始化结束。 OpenGL 状态未初始化,而是按需设置。 OpenGL对象会被初始化,但也是按需初始化的。
另外,您使用 glGetError 的方式不正确。需要循环调用它,直到不再报告错误为止。因此,如果报告了总帐错误,那么退出也没有什么意义。 OpenGL 错误应被视为诊断错误。
必须在调用
SDL_SetVideoMode
之前设置SDL_GL_SetAttribute
,因此您可能不是双缓冲。My first advice: Get rid of that bogus System class: So far all the tasks it does are purely sequential/procedural and that should be reflected in the programs outline. People tend to put everything into classes just becase they're taught see everything in terms of object models. But this System class would have to follow the singleton pattern, which, in my opinion, is an anti-pattern.
All the stuff you placed in init_GL belong into the rendering loop. OpenGL initialization ends after creating a render context. OpenGL state is not initialized it is set on demand. OpenGL objects are initialized, but also on demand.
Also you're using glGetError not correctly. It needs to be called in a loop until no more errors are reported. It thus also makes little sense to bail out if a GL error is reported. OpenGL errors should be considered diagnostic.
SDL_GL_SetAttribute
must be set before callingSDL_SetVideoMode
so you're probably not double buffering.嘿,你没有调用初始化 OpenGL 的函数:init_GL()
Hey, you aren't calling the function which initializes OpenGL: init_GL()
您没有检查 system::init 的结果 - 它可能在该函数的某个地方失败并且没有正确设置您的初始状态
youre not checking the result from system::init - it might be failing somewhere in that function and not setting up your initial state correctly
SDL
#defines
main()
为SDL_main()
以允许它在程序启动之前进行一些额外的初始化。您似乎通过静态初始化的类绕过了它。尝试在
main()
中构造您的System
对象。SDL
#defines
main()
to beSDL_main()
to allow it to do some extra initialization before program start. Which you seem to be bypassing via the statically initialized class.Try constructing your
System
object inmain()
.