在 Xlib 中,如何制作动画直到事件发生?

发布于 2024-09-02 05:01:00 字数 932 浏览 5 评论 0原文

我一直在尝试使用 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 技术交流群。

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

发布评论

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

评论(1

只怪假的太真实 2024-09-09 05:01:00

ConnectionNumber(display) 返回的文件描述符与 select 结合使用() 并使用超时参数。如果 select() 返回 0,则再绘制一些帧。请记住在 select() 之前调用 XSync(),以便 X 服务器获取您的更新。

int fd,r;
struct timeval tv;
FD_SET rfds;

fd=ConnectionNumber(display);
FD_ZERO(&rfds);
FD_SET(fd,&rfds);
memset(&tv,0,sizeof(tv));
tv.tv_usec = 100000; /* delay in microseconds */
r=select(fd+1,&rfds,0,0,&tv);
if(r == 0) { /* draw frame */ }
else if (r < 0) { /* error; try again if errno=EINTR */ }
else { /* pull events out */ }

Use the file descriptor returned by ConnectionNumber(display) with select() and use the timeout argument. If select() returns 0, then draw some more frames. Remember to call XSync() before you select() so that the X server gets your update.

int fd,r;
struct timeval tv;
FD_SET rfds;

fd=ConnectionNumber(display);
FD_ZERO(&rfds);
FD_SET(fd,&rfds);
memset(&tv,0,sizeof(tv));
tv.tv_usec = 100000; /* delay in microseconds */
r=select(fd+1,&rfds,0,0,&tv);
if(r == 0) { /* draw frame */ }
else if (r < 0) { /* error; try again if errno=EINTR */ }
else { /* pull events out */ }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文