如何同时使用GTK和glut? [英] How do I use GTK and glut together?

查看:125
本文介绍了如何同时使用GTK和glut?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,为了编写GTK应用程序,我编写了一堆代码来描述主窗口中放置的内容,然后调用:

gtk_main();

此后的任何代码语句都不会执行.

现在让我们假设我想让我的GTK应用程序显示我用glut写的东西,它本身包含了一堆关于需要设置哪些图形的语句,等等,然后以语句结尾:

glutMainLoop();

此后没有执行任何操作.

所以我的问题是,这两个语句中的任何一个都阻止我调用另一个.

  1. 是否可以在GTK小部件内执行glut主循环?
  2. 是否有一种方法可以编写可以同时调用GTK主循环和glut主循环的代码(但从主程序调用并在单独的X窗​​口中呈现,而不是在窗口小部件中呈现)?我感觉这可以通过线程"来完成...

解决方案

您正在运行主循环. gtk_main()运行直到调用gtk_quit().

gtk_main()在GTK.org

运行主循环,直到调用gtk_main_quit().您可以嵌套对gtk_main()的调用.在这种情况下,gtk_main_quit()将使主循环的最内层调用返回.

此外,glutMainLoop()以相同的方式工作,它将永远处理GL事件.

glutMainLoop()在OpenGL.org

glutMainLoop()进入GLUT事件处理循环.此例程在GLUT程序中最多应调用一次.一旦被调用,该例程将永远不会返回.它将在必要时调用已注册的所有回调.

因此,您不希望同时执行这两项操作(我认为它们可能会相互干扰,因此您可能会得到意想不到的结果),那么您将需要从过剩内部调用gtk_main_iteration(). /p>

gtk_main_iteration()在GTK .org

运行mainloop的单个迭代.如果没有等待处理的事件,GTK +将阻塞直到注意到下一个事件.如果您不想阻止,请查看gtk_main_iteration_do()或先使用gtk_events_pending()检查是否有任何待处理的事件.

现在..GLUT没有与gtk_main_iteration()等效的功能,因此您将需要注册GLUT回调.

您可以使用glutIdleFunc(void (*func)(void))向运行gtk_main_iteration()的GLUT注册回调,这将为每个帧运行一个回调- GTK的GL支持 .

I know that in order to write a GTK application, I write a bunch of code which describes what is put in the main window, then I call:

gtk_main();

Any code statements after this do not get executed.

Now let's suppose I'd like my GTK app to display something I wrote with glut, which itself contains a bunch of statements about what graphics need to be set etc. then ends with the statement:

glutMainLoop();

Anything after this is not executed.

So my problem is that either of these two statements prevents me from calling the other.

  1. Is there a way to execute a glut main loop inside a GTK widget ?
  2. Is there a way to write a code that could somehow simultaneously call both a GTK main loop and a glut main loop (but called from the main program and rendered in a separate X window, not within a widget)? I've got a feeling this could be done with "threads"...

解决方案

You are running the main loops. gtk_main() runs until gtk_quit() is called.

gtk_main() at GTK.org

Runs the main loop until gtk_main_quit() is called. You can nest calls to gtk_main(). In that case gtk_main_quit() will make the innermost invocation of the main loop return.

Also, glutMainLoop() works the same way, it processes GL events forever.

glutMainLoop() at OpenGL.org

glutMainLoop() enters the GLUT event processing loop. This routine should be called at most once in a GLUT program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered.

So, you you wan't both of these things to execute at the same time (I think they might interfere with each other so you might get unexpected results) then you will need to call gtk_main_iteration() from inside glut.

gtk_main_iteration() at GTK.org

Runs a single iteration of the mainloop. If no events are waiting to be processed GTK+ will block until the next event is noticed. If you don't want to block look at gtk_main_iteration_do() or check if any events are pending with gtk_events_pending() first.

Now.. GLUT doesn't have an equivalent to gtk_main_iteration() so you are going to need to register GLUT callbacks.

You could register a callback with GLUT that runs gtk_main_iteration() using glutIdleFunc(void (*func)(void)) which will run a callback for every frame - glutIdleFunc()..

Or you could give a callback to glutTimerFunc(unsigned int msecs, void (*func)(int value), value) to call and check the return value of gtk_main_iteration() every 200msec or so.

I'd probably experiment with both, glutIdleFunc() might not always get called regularly enough for good responsiveness.

It really is worth looking at driving GTK's GL support though.

这篇关于如何同时使用GTK和glut?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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