Xlib isn't that bad. It boils down to pretty much the same as a windows message pump, with the difference that instead of pulling the message and dispatching it, you just crack it open where it is.
X11 provides (on almost all platforms) a file desc for the X11 connection. This becomes readable when data is ready. This means you can select/epoll on the FD and when it becomes readable use XGetEvent to pull the event off. This is how you multiplex working with X11 events and sockets.
The xevent is a union of several structs. You read data out of it with normal C/C++ member notation. Typically you switch on the type. So you get something which says stuff like;
case ConfigureNotify:
width=event.xconfigure.width;
height=event.xconfigure.height;
windowid=event.xconfigure.window;
MyWindowClass *windowh = convertIdToPtr(windowid);
if (windowh)
windowh->sizeChanged(width,height); // go off and handle the change.
break;
Which doesn't look that different from the sort of thing one would do in the windows version.
So your game loop looks like this;
while(1)
{
get current time.
idle processing.
draw window.
flip window.
get new current time.
elapsed = difference in milliseconds.
epoll the filedescriptors, with a wait time of (1000/refreshrate)-elapsed.
get the results
go off and handle those results (read X11 events, read/write network data).
}
It's really not that different. The only thing to bear in mind is that almost all example X11 code uses XGetEvent() which will block if it can't get one -- obviously not a lot of use.
I use XCheckIfEvent which is kind of like PeekMessage() in that it can return "no I didn't get one", only it will rummage down the whole message queue looking for stuff and you can write arbitrary matching functions to give it which is actually quite powerful.
🎉 Celebrating 25 Years of GameDev.net! 🎉
Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement