File: X11.cpp

package info (click to toggle)
libsfml 3.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 13,700 kB
  • sloc: cpp: 52,774; ansic: 24,944; objc: 668; sh: 172; xml: 25; makefile: 18
file content (302 lines) | stat: -rw-r--r-- 8,944 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Window.hpp>

#include <X11/Xlib.h>
#define GLAD_GL_IMPLEMENTATION
#include <gl.h>

#include <array>
#include <iostream>

#include <cmath>
#include <cstdlib>

////////////////////////////////////////////////////////////
/// Initialize OpenGL states into the specified view
///
/// \param Window Target window to initialize
///
/// \return True if operation was successful, false otherwise
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool initialize(sf::Window& window)
{
    // Activate the window
    if (!window.setActive())
    {
        std::cerr << "Failed to set the window as active" << std::endl;
        return false;
    }

    // Setup OpenGL states
    // Set color and depth clear value

#ifdef SFML_OPENGL_ES
    glClearDepthf(1.f);
#else
    glClearDepth(1.f);
#endif

    glClearColor(0.f, 0.5f, 0.5f, 0.f);

    // Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST);
    glDepthMask(GL_TRUE);

    // Setup a perspective projection
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    const float extent = std::tan(sf::degrees(45).asRadians());

#ifdef SFML_OPENGL_ES
    glFrustumf(-extent, extent, -extent, extent, 1.0f, 500.0f);
#else
    glFrustum(-extent, extent, -extent, extent, 1.0f, 500.0f);
#endif

    // Enable position and texture coordinates vertex components
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);

    return true;
}

////////////////////////////////////////////////////////////
/// Draw the OpenGL scene (a rotating cube) into
/// the specified view
///
/// \param window      Target window for rendering
/// \param elapsedTime Time elapsed since the last draw
///
/// \return True if operation was successful, false otherwise
///
////////////////////////////////////////////////////////////
[[nodiscard]] bool draw(sf::Window& window, float elapsedTime)
{
    // Activate the window
    if (!window.setActive())
    {
        std::cerr << "Failed to set the window as active" << std::endl;
        return false;
    }

    // Clear color and depth buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Apply some transformations
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glTranslatef(0.f, 0.f, -200.f);
    glRotatef(elapsedTime * 10.f, 1.f, 0.f, 0.f);
    glRotatef(elapsedTime * 6.f, 0.f, 1.f, 0.f);
    glRotatef(elapsedTime * 18.f, 0.f, 0.f, 1.f);

    // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
    // clang-format off
    constexpr std::array<GLfloat, 216> cube =
    {
        // positions    // colors
        -50, -50, -50,  1, 1, 0,
        -50,  50, -50,  1, 1, 0,
        -50, -50,  50,  1, 1, 0,
        -50, -50,  50,  1, 1, 0,
        -50,  50, -50,  1, 1, 0,
        -50,  50,  50,  1, 1, 0,

         50, -50, -50,  1, 1, 0,
         50,  50, -50,  1, 1, 0,
         50, -50,  50,  1, 1, 0,
         50, -50,  50,  1, 1, 0,
         50,  50, -50,  1, 1, 0,
         50,  50,  50,  1, 1, 0,

        -50, -50, -50,  1, 0, 1,
         50, -50, -50,  1, 0, 1,
        -50, -50,  50,  1, 0, 1,
        -50, -50,  50,  1, 0, 1,
         50, -50, -50,  1, 0, 1,
         50, -50,  50,  1, 0, 1,

        -50,  50, -50,  1, 0, 1,
         50,  50, -50,  1, 0, 1,
        -50,  50,  50,  1, 0, 1,
        -50,  50,  50,  1, 0, 1,
         50,  50, -50,  1, 0, 1,
         50,  50,  50,  1, 0, 1,

        -50, -50, -50,  0, 1, 1,
         50, -50, -50,  0, 1, 1,
        -50,  50, -50,  0, 1, 1,
        -50,  50, -50,  0, 1, 1,
         50, -50, -50,  0, 1, 1,
         50,  50, -50,  0, 1, 1,

        -50, -50,  50,  0, 1, 1,
         50, -50,  50,  0, 1, 1,
        -50,  50,  50,  0, 1, 1,
        -50,  50,  50,  0, 1, 1,
         50, -50,  50,  0, 1, 1,
         50,  50,  50,  0, 1, 1
    };
    // clang-format on

    // Draw the cube
    glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube.data());
    glColorPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube.data() + 3);
    glDrawArrays(GL_TRIANGLES, 0, 36);

    return true;
}


////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Error code
///
////////////////////////////////////////////////////////////
int main()
{
    // Open a connection with the X server
    Display* display = XOpenDisplay(nullptr);
    if (!display)
        return EXIT_FAILURE;

    // Get the default screen
    const int screen = DefaultScreen(display);

    // Create the main window
    XSetWindowAttributes attributes;
    attributes.background_pixel = BlackPixel(display, screen);
    attributes.event_mask       = KeyPressMask;
    const Window window         = XCreateWindow(display,
                                        RootWindow(display, screen),
                                        0,
                                        0,
                                        650,
                                        330,
                                        0,
                                        DefaultDepth(display, screen),
                                        InputOutput,
                                        DefaultVisual(display, screen),
                                        CWBackPixel | CWEventMask,
                                        &attributes);
    if (!window)
        return EXIT_FAILURE;

    // Set the window's name
    XStoreName(display, window, "SFML Window");

    // Create the windows which will serve as containers for our SFML views
    const Window view1 = XCreateWindow(display,
                                       window,
                                       10,
                                       10,
                                       310,
                                       310,
                                       0,
                                       DefaultDepth(display, screen),
                                       InputOutput,
                                       DefaultVisual(display, screen),
                                       0,
                                       nullptr);
    const Window view2 = XCreateWindow(display,
                                       window,
                                       330,
                                       10,
                                       310,
                                       310,
                                       0,
                                       DefaultDepth(display, screen),
                                       InputOutput,
                                       DefaultVisual(display, screen),
                                       0,
                                       nullptr);

    // Show our windows
    XMapWindow(display, window);
    XFlush(display);

    // Create our SFML views
    sf::Window sfmlView1(view1);
    sf::Window sfmlView2(view2);

    // Create a clock for measuring elapsed time
    const sf::Clock clock;

    // Load OpenGL or OpenGL ES entry points using glad
    if (!sfmlView1.setActive())
    {
        std::cerr << "Failed to set view 1 as active" << std::endl;
        return EXIT_FAILURE;
    }

#ifdef SFML_OPENGL_ES
    gladLoadGLES1(sf::Context::getFunction);
#else
    gladLoadGL(sf::Context::getFunction);
#endif

    // Initialize our views
    if (!initialize(sfmlView1))
    {
        std::cerr << "Failed to initialize view 1" << std::endl;
        return EXIT_FAILURE;
    }

    if (!initialize(sfmlView2))
    {
        std::cerr << "Failed to initialize view 2" << std::endl;
        return EXIT_FAILURE;
    }

    // Start the event loop
    bool running = true;
    while (running)
    {
        while (XPending(display))
        {
            // Get the next pending event
            XEvent event;
            XNextEvent(display, &event);

            // Process it
            switch (event.type)
            {
                // Any key is pressed: quit
                case KeyPress:
                    running = false;
                    break;
            }
        }

        // Draw something into our views
        if (!draw(sfmlView1, clock.getElapsedTime().asSeconds()))
        {
            std::cerr << "Failed to draw on view 1" << std::endl;
            return EXIT_FAILURE;
        }

        if (!draw(sfmlView2, clock.getElapsedTime().asSeconds() * 0.3f))
        {
            std::cerr << "Failed to draw on view 2" << std::endl;
            return EXIT_FAILURE;
        }

        // Display the views on screen
        sfmlView1.display();
        sfmlView2.display();
    }

    // Close our SFML views before destroying the underlying window
    sfmlView1.close();
    sfmlView2.close();

    // Destroy the window
    XDestroyWindow(display, window);

    // Close the display
    XCloseDisplay(display);
}