[英]Is there a way to pass the wxWidget GUI to a wxThread?
I have made a game that uses a game loop. 我制作了一个使用游戏循环的游戏。 Now i am trying to implement a GUI for this game in wxWidgets. 现在,我正在尝试在wxWidgets中为该游戏实现GUI。
The problem is that when I initialize the GUI the control of the program remains on a GUI loop and doesn't go to the game loop. 问题是,当我初始化GUI时,程序的控件仍保留在GUI循环中,而没有进入游戏循环。
So, can i make the GUI run on its own thread? 那么,我可以让GUI在自己的线程上运行吗?
You can, but leave all code, which interacts with the GUI on the main thread/EDT (Event Dispatch Thread). 您可以但保留所有与主线程/ EDT(事件调度线程)上的GUI交互的代码。 Concurrent access to GUI elements is not intended by design and will cause problems. 并发访问GUI元素不是设计意图,并且会引起问题。 Instead, put the game loop into a wxThread
and fire appropriate events from there. 而是将游戏循环放入wxThread
并从那里触发适当的事件。 Those events can then be handled on the EDT to update the GUI or for rendering a frame. 然后可以在EDT上处理这些事件以更新GUI或渲染框架。
Another solution would be, to invoke single iterations of your game loop on the main thread, using wxTimer
. 另一个解决方案是使用wxTimer
在主线程上调用游戏循环的单个迭代。
Note: 注意:
wxTimer
does perfectly support throttling by frequency and coalescing of update events. wxTimer
确实通过频率和更新事件的合并来完美支持节流。 In a separate thread, you'd need to sleep for the remaining time of the desired cycle/iteration time or wait on a semaphore, triggered by the main thread. 在单独的线程中,您需要休眠所需的周期/迭代时间的剩余时间,或者等待由主线程触发的信号量。 wxMutex
. 实现访问权限,以与wxMutex
这样的同步对象共享游戏状态之类的wxMutex
。 Preferably, you could instead use std::mutex
and std::thread
from C++11. 最好改用C ++ 11中的std::mutex
和std::thread
。 Examples: 例子:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.