在 Xlib 中,如何制作动画直到事件发生?
我一直在尝试使用 Xlib 在 C 程序中制作动画,我想在事件发生时做一些事情,否则我想继续制作动画。这是我当前正在做的示例代码片段:
while( 1 )
{
// If an event occurs, stop and do whatever is needed.
// If no event occurs, skip this if statement.
if ( XEventsQueued( display, QueuedAlready ) > 0 )
{
XNextEvent( display, &event )
switch ( event.type )
{
// Don't do anything
case Expose:
while ( event.xexpose.count != 0 )
break;
// Do something, when a button is pressed
case ButtonPress:
...
break;
// Do something, when a key is pressed
case KeyPress:
...
break;
}
}
animate(); // Do animation step i.e. change any drawings...
repaint(); // Paint again with the new changes from animation...
}
所以基本上,如果用户尚未单击鼠标或按下键盘中的某个键,我想继续循环。当用户按下按键或单击鼠标时,我想停下来并执行特定操作。我上面的代码中的问题是,每当我执行操作时它都不会停止。如果我删除 if 语句,动画将阻塞,直到事件发生,但我不希望这样。这是一个简单的问题,但我对 Xlib/动画有点陌生,因此我们将不胜感激。谢谢。
I've been trying to animate in a C program using Xlib and I wanna do something when an event occurs, otherwise I wanna keep animating. Here's an example code snippet of what I am doing currently:
while( 1 )
{
// If an event occurs, stop and do whatever is needed.
// If no event occurs, skip this if statement.
if ( XEventsQueued( display, QueuedAlready ) > 0 )
{
XNextEvent( display, &event )
switch ( event.type )
{
// Don't do anything
case Expose:
while ( event.xexpose.count != 0 )
break;
// Do something, when a button is pressed
case ButtonPress:
...
break;
// Do something, when a key is pressed
case KeyPress:
...
break;
}
}
animate(); // Do animation step i.e. change any drawings...
repaint(); // Paint again with the new changes from animation...
}
So basically, I wanna keep looping if the user hasn't clicked the mouse OR pressed a key in the keyboard yet. When the user presses a key OR clicks the mouse, I wanna stop and do a specific action. The problem in my above code is that, it doesnt stop whenever I do an action. If I remove the if statement, the animation blocks until an event occurs, however I do not want this. It's a simple problem, but I'm kinda new to Xlib/animations so any help would be highly appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
ConnectionNumber(display)
返回的文件描述符与select 结合使用()
并使用超时参数。如果select()
返回 0,则再绘制一些帧。请记住在select()
之前调用XSync()
,以便 X 服务器获取您的更新。Use the file descriptor returned by
ConnectionNumber(display)
withselect()
and use the timeout argument. Ifselect()
returns 0, then draw some more frames. Remember to callXSync()
before youselect()
so that the X server gets your update.