开发者

In Xlib, How can I animate until an event occurs?

开发者 https://www.devze.com 2022-12-31 12:51 出处:网络
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:

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 k开发者_开发问答ey 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.


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 */ }
0

精彩评论

暂无评论...
验证码 换一张
取 消