如何在 GLUT 中调度函数

发布于 2024-10-30 21:35:01 字数 716 浏览 1 评论 0原文

我有一些从主函数调用的函数。下面是代码。

int main(int argc, char *argv[])

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

    glutInitWindowSize (500, 500);

    glutInitWindowPosition (100, 100);

    glutCreateWindow("Game for Gamers");

    glutDisplayFunc(display_func);

    glutKeyboardFunc(keyboard);

    glutSpecialFunc(special);

    glutTimerFunc(50,refreshcheck,0);

    glutIdleFunc (animate);

    glutReshapeFunc (reshape);

    glutMainLoop();


    return 0;

}

这里“refreshcheck”首先调用我想在执行“display_func”函数后调用“refreshcheck”函数。如何安排它。当游戏在“refreshcheck”函数之前加载到内存中时,我想首先调用“display_func”执行函数,它当游戏运行时,没有必要在“display_func”之后调用“refreshcheck”,但第一次这很重要。

I have some function which are calling from main function.Below is code.

int main(int argc, char *argv[])

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

    glutInitWindowSize (500, 500);

    glutInitWindowPosition (100, 100);

    glutCreateWindow("Game for Gamers");

    glutDisplayFunc(display_func);

    glutKeyboardFunc(keyboard);

    glutSpecialFunc(special);

    glutTimerFunc(50,refreshcheck,0);

    glutIdleFunc (animate);

    glutReshapeFunc (reshape);

    glutMainLoop();


    return 0;

}

Here "refreshcheck" is calling first i want to call "refreshcheck" function after execution of "display_func" function.How will schedule it.I want to call "display_func" execute function first when game loaded in memory before "refreshcheck" function, it is not necessary to call "refreshcheck" after "display_func" when game is running, but for first time this is important.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記柔刀 2024-11-06 21:35:01

一般来说,您不应该在 displayFunc 内创建对象(正如 dark_charlie 正确指出的那样)。但这只是不好的做法,仅此而已。

如果您需要碰撞检测,您应该使其与渲染同步,即从您的 displayFunc (在其末尾)调用它。

我想你想使用计时器的原因是要有恒定的步长物理原理。这通常以另一种方式完成,使用 gettimeofday() (或 Windows 上的 GetTickCount())来获取时间,并根据帧之间经过的时间,物理被调用 N 次。与使用 GLUT 计时器相比,这将为您带来更高的精度,因为计时器的精度最多只能达到 10 毫秒,这在处理快速移动的对象时可能会很大,从而导致卡顿。

You should not be, in general, creating objects inside displayFunc (as dark_charlie correctly pointed out). But that is just bad practice, nothing more.

If you require collision detection, you should make it synchronous to rendering, i.e. call it from your displayFunc (at it's end).

I guess the reason you want to use timer is to have constant step physics. This is usually done the other way, using gettimeofday() (or GetTickCount() on windows) to get the time, and based on the time elapsed between the frames, the physics is called N times. That will get you far better precision than using GLUT timers, as timers are only precise up to 10s of milliseconds, which can be a lot when dealing with fast moving objects, leading to stuttering.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文