Back to index

libsfml  1.6+dfsg2
X11.cpp
Go to the documentation of this file.
00001 
00003 // Headers
00005 #include <SFML/Window.hpp>
00006 #include <X11/Xlib.h>
00007 #include <GL/gl.h>
00008 #include <GL/glu.h>
00009 #include <iostream>
00010 
00011 
00018 void Initialize(sf::Window& Window)
00019 {
00020     // Activate the window
00021     Window.SetActive();
00022 
00023     // Setup OpenGL states
00024     // Set color and depth clear value
00025     glClearDepth(1.f);
00026     glClearColor(0.f, 0.5f, 0.5f, 0.f);
00027 
00028     // Enable Z-buffer read and write
00029     glEnable(GL_DEPTH_TEST);
00030     glDepthMask(GL_TRUE);
00031 
00032     // Setup a perspective projection
00033     glMatrixMode(GL_PROJECTION);
00034     glLoadIdentity();
00035     gluPerspective(90.f, 1.f, 1.f, 500.f);
00036 }
00037 
00046 void Draw(sf::Window& Window, float ElapsedTime)
00047 {
00048     // Activate the window
00049     Window.SetActive();
00050 
00051     // Clear color and depth buffers
00052     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00053 
00054     // Apply some transformations
00055     glMatrixMode(GL_MODELVIEW);
00056     glLoadIdentity();
00057     glTranslatef(0.f, 0.f, -200.f);
00058     glRotatef(ElapsedTime * 50, 1.f, 0.f, 0.f);
00059     glRotatef(ElapsedTime * 30, 0.f, 1.f, 0.f);
00060     glRotatef(ElapsedTime * 90, 0.f, 0.f, 1.f);
00061 
00062     // Draw a cube
00063     glBegin(GL_QUADS);
00064 
00065         glColor3f(1.f, 1.f, 0.f);
00066         glVertex3f(-50.f, -50.f, -50.f);
00067         glVertex3f(-50.f,  50.f, -50.f);
00068         glVertex3f( 50.f,  50.f, -50.f);
00069         glVertex3f( 50.f, -50.f, -50.f);
00070 
00071         glColor3f(1.f, 1.f, 0.f);
00072         glVertex3f(-50.f, -50.f, 50.f);
00073         glVertex3f(-50.f,  50.f, 50.f);
00074         glVertex3f( 50.f,  50.f, 50.f);
00075         glVertex3f( 50.f, -50.f, 50.f);
00076 
00077         glColor3f(0.f, 1.f, 1.f);
00078         glVertex3f(-50.f, -50.f, -50.f);
00079         glVertex3f(-50.f,  50.f, -50.f);
00080         glVertex3f(-50.f,  50.f,  50.f);
00081         glVertex3f(-50.f, -50.f,  50.f);
00082 
00083         glColor3f(0.f, 1.f, 1.f);
00084         glVertex3f(50.f, -50.f, -50.f);
00085         glVertex3f(50.f,  50.f, -50.f);
00086         glVertex3f(50.f,  50.f,  50.f);
00087         glVertex3f(50.f, -50.f,  50.f);
00088 
00089         glColor3f(1.f, 0.f, 1.f);
00090         glVertex3f(-50.f, -50.f,  50.f);
00091         glVertex3f(-50.f, -50.f, -50.f);
00092         glVertex3f( 50.f, -50.f, -50.f);
00093         glVertex3f( 50.f, -50.f,  50.f);
00094 
00095         glColor3f(1.f, 0.f, 1.f);
00096         glVertex3f(-50.f, 50.f,  50.f);
00097         glVertex3f(-50.f, 50.f, -50.f);
00098         glVertex3f( 50.f, 50.f, -50.f);
00099         glVertex3f( 50.f, 50.f,  50.f);
00100 
00101     glEnd();
00102 }
00103 
00104 
00111 int main()
00112 {
00113     // Open a connection with the X server
00114     Display* Disp = XOpenDisplay(NULL);
00115     if (!Disp)
00116         return EXIT_FAILURE;
00117 
00118     // Get the default screen
00119     int Screen = DefaultScreen(Disp);
00120 
00121     // Let's create the main window
00122     XSetWindowAttributes Attributes;
00123     Attributes.background_pixel = BlackPixel(Disp, Screen);
00124     Attributes.event_mask       = KeyPressMask;
00125     Window Win = XCreateWindow(Disp, RootWindow(Disp, Screen),
00126                                0, 0, 650, 330, 0,
00127                                DefaultDepth(Disp, Screen),
00128                                InputOutput,
00129                                DefaultVisual(Disp, Screen),
00130                                CWBackPixel | CWEventMask, &Attributes);
00131     if (!Win)
00132         return EXIT_FAILURE;
00133 
00134     // Set the window's name
00135     XStoreName(Disp, Win, "SFML Window");
00136 
00137     // Let's create the windows which will serve as containers for our SFML views
00138     Window View1 = XCreateWindow(Disp, Win,
00139                                  10, 10, 310, 310, 0,
00140                                  DefaultDepth(Disp, Screen),
00141                                  InputOutput,
00142                                  DefaultVisual(Disp, Screen),
00143                                  0, NULL);
00144     Window View2 = XCreateWindow(Disp, Win,
00145                                  330, 10, 310, 310, 0,
00146                                  DefaultDepth(Disp, Screen),
00147                                  InputOutput,
00148                                  DefaultVisual(Disp, Screen),
00149                                  0, NULL);
00150 
00151     // Show our windows
00152     XMapWindow(Disp, Win);
00153     XFlush(Disp);
00154 
00155     // Create our SFML views
00156     sf::Window SFMLView1(View1);
00157     sf::Window SFMLView2(View2);
00158 
00159     // Create a clock for measuring elapsed time
00160     sf::Clock Clock;
00161 
00162     // Initialize our views
00163     Initialize(SFMLView1);
00164     Initialize(SFMLView2);
00165 
00166     // Start the event loop
00167     bool IsRunning = true;
00168     while (IsRunning)
00169     {
00170         while (XPending(Disp))
00171         {
00172             // Get the next pending event
00173             XEvent Event;
00174             XNextEvent(Disp, &Event);
00175 
00176             // Process it
00177             switch (Event.type)
00178             {
00179                 // Any key is pressed : quit
00180                 case KeyPress :
00181                     IsRunning = false;
00182                     break;
00183             }
00184         }
00185 
00186         // Draw something into our views
00187         Draw(SFMLView1, Clock.GetElapsedTime());
00188         Draw(SFMLView2, Clock.GetElapsedTime() * 0.3f);
00189 
00190         // Display the views on screen
00191         SFMLView1.Display();
00192         SFMLView2.Display();
00193     }
00194 
00195     // Close the display
00196     XCloseDisplay(Disp);
00197 
00198     return EXIT_SUCCESS;
00199 }