Warning: this page refers to an old version of SFML. Click here to switch to the latest version.
Integrating to a X11 interface
Introduction
In this new tutorial, we'll have a look at how SFML integrates into a X11 interface. If you are not familiar to Xlib programming you can first read a tutorial about it, as we won't explain it in this tutorial, only the way to put SFML into X11 windows.
X11 window creation
First, we have to create a X11 interface. We'll create a main window, and one subwindow into which we'll display SFML graphics. The following piece of code is just regular X11 code, no SFML specific code is involved yet :
// Open a connection with the X server
Display* Disp = XOpenDisplay(NULL);
if (!Disp)
return EXIT_FAILURE;
// Get the default screen
int Screen = DefaultScreen(Disp);
// Let's create the main window
XSetWindowAttributes Attributes;
Attributes.background_pixel = BlackPixel(Disp, Screen);
Attributes.event_mask = KeyPressMask;
Window Win = XCreateWindow(Disp, RootWindow(Disp, Screen),
0, 0, 650, 330, 0,
DefaultDepth(Disp, Screen),
InputOutput,
DefaultVisual(Disp, Screen),
CWBackPixel | CWEventMask, &Attributes);
if (!Win)
return EXIT_FAILURE;
// Set the window's name
XStoreName(Disp, Win, "SFML Window");
// Let's create the window which will serve as a container for our SFML view
Window View = XCreateWindow(Disp, Win,
10, 10, 310, 310, 0,
DefaultDepth(Disp, Screen),
InputOutput,
DefaultVisual(Disp, Screen),
0, NULL);
// Show our windows
XMapWindow(Disp, Win);
XMapWindow(Disp, View);
XFlush(Disp);
Defining a SFML view
Once all interface components are created, we can define our SFML view. To do so, we just construct
a sf::RenderWindows
instance with the subwindow identifier :
sf::RenderWindow SFMLView(View);
// Or, if you want to do it after construction :
SFMLView.Create(View);
And that's it, you have one SFML rendering window that will display SFML graphics into the specified interface window.
The main loop
The event loop is a regular X11 loop :
bool IsRunning = true;
while (IsRunning)
{
while (XPending(Disp))
{
// Process the next pending event
XEvent Event;
XNextEvent(Disp, &Event);
switch (Event.type)
{
case KeyPress :
IsRunning = false;
break;
}
}
// Our SFML rendering code goes here :
// ...
}
Then we can insert our SFML code :
// Clear the view
SFMLView.Clear();
// Draw a sprite
SFMLView.Draw(Sprite);
// Display the view on screen
SFMLView.Display();
Don't forget to clean up X11 resources before exiting the application :
// Close the display
XCloseDisplay(Disp);
Conclusion
Integrating SFML into a X11 interface is not complicated, and if you are used to X11
programming it won't require more effort than any other SFML application.
Let's now have a look at integration into
wxWidgets interfaces.