freeglut中的glutidlefunc()有什么意义? [英] What's the point of glutidlefunc() in freeglut

查看:926
本文介绍了freeglut中的glutidlefunc()有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解了glutdisplayfunc()如何使显示循环和Iv在某些地方读取,您应该将游戏机制置于显眼的位置而不是显示;为什么不把它们放在一段时间(游戏运行)循环中?

I get how glutdisplayfunc() makes the display loop and Iv read in some places that you should put the game mechanics in glutidle instead of the display; why can't you just put them in a while (gameisrunning) loop?

推荐答案

在事件驱动的编程中,就像在交互式OpenGL应用程序中一样,主应用程序循环通常执行三件事:

In event-driven programming, like you have in interactive OpenGL applications, the main application loop generally does three things:

  1. 检查当前事件队列,并处理自上次检查以来发生的所有事件(例如,鼠标移动,按键等)
  2. 更新应用程序状态-例如玩家和对象的位置,游戏物理等-准备下一个渲染框架
  3. 渲染当前帧.

GLUT将在其glutMainLoop()中为您隐式执行这些步骤.您注册的几乎所有GLUT回调(例如glutKeyboardFunc()glutReshapeFunc()等)都会处理1.,并调用您设置为执行2的函数.

GLUT does these steps implicitly in its glutMainLoop() for you. Almost all of the GLUT callbacks you register (e.g., glutKeyboardFunc(), glutReshapeFunc(), etc.) take care of 1., and call functions you set up to presumably do 2.

现在,如果在glutIdleFunc()中注册了一个空闲函数,则在处理所有当前事件之后调用该函数.程序员通常会实现其空闲函数,以仅调用用于渲染场景的例程(即,传递给glutDisplayFunc()的例程).如果渲染花了一段时间"(例如几乎所有时间都渲染一帧),则此方法可能会出现问题,因为您可能没有时间完成渲染,然后处理下一帧的事件.如果要插入您提到的while循环,那么您将这样做,而这将绕过GLUT的本机事件处理机制.更好地与GLUT配合使用的更好方法是将空闲函数实现为仅调用glutPostRedisplay(),这告诉GLUT在处理完所有事件之后并在调用空闲函数之前安排对显示函数的调用.

Now, if you register an idle function with glutIdleFunc(), it's called after all the current events are processed. Often programmers implement their idle function to merely call the routine they use for rendering their scene (i.e., the routine they pass into glutDisplayFunc()). This approach can be problematic if rendering "takes a while" (like close to all of the time to render a frame), since you may not have time to complete rendering and then process events for the next frame. If you were to insert the while loop you mention, you'd be doing just this, which circumvents GLUT's native event processing mechanisms. The better approach to play nicely with GLUT is to implement your idle function to only call glutPostRedisplay(), which tells GLUT to schedule a call to your display function once all the events are processed and before your idle function is called.

这篇关于freeglut中的glutidlefunc()有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆