2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Window.hpp>
|
2019-04-13 19:16:32 +08:00
|
|
|
|
2022-07-05 00:20:58 +08:00
|
|
|
#include <X11/Xlib.h>
|
2019-04-13 19:16:32 +08:00
|
|
|
#define GLAD_GL_IMPLEMENTATION
|
2021-04-20 21:59:24 +08:00
|
|
|
#include <gl.h>
|
2019-04-13 19:16:32 +08:00
|
|
|
|
2022-06-13 00:41:53 +08:00
|
|
|
#include <array>
|
2023-04-24 20:13:52 +08:00
|
|
|
#include <iostream>
|
|
|
|
|
2015-01-13 01:15:11 +08:00
|
|
|
#include <cmath>
|
2021-12-10 03:55:33 +08:00
|
|
|
#include <cstdlib>
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Initialize OpenGL states into the specified view
|
|
|
|
///
|
|
|
|
/// \param Window Target window to initialize
|
|
|
|
///
|
2021-12-10 03:55:33 +08:00
|
|
|
/// \return True if operation was successful, false otherwise
|
|
|
|
///
|
2014-09-30 22:07:25 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
2021-12-10 01:11:37 +08:00
|
|
|
[[nodiscard]] bool initialize(sf::Window& window)
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
// Activate the window
|
2021-12-10 01:11:37 +08:00
|
|
|
if (!window.setActive())
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to set the window as active" << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Setup OpenGL states
|
|
|
|
// Set color and depth clear value
|
2019-04-13 19:16:32 +08:00
|
|
|
|
|
|
|
#ifdef SFML_OPENGL_ES
|
|
|
|
glClearDepthf(1.f);
|
|
|
|
#else
|
2014-09-30 22:07:25 +08:00
|
|
|
glClearDepth(1.f);
|
2019-04-13 19:16:32 +08:00
|
|
|
#endif
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
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();
|
2023-05-20 09:04:31 +08:00
|
|
|
const float extent = std::tan(sf::degrees(45).asRadians());
|
2019-04-13 19:16:32 +08:00
|
|
|
|
|
|
|
#ifdef SFML_OPENGL_ES
|
|
|
|
glFrustumf(-extent, extent, -extent, extent, 1.0f, 500.0f);
|
|
|
|
#else
|
|
|
|
glFrustum(-extent, extent, -extent, extent, 1.0f, 500.0f);
|
|
|
|
#endif
|
2015-01-13 01:15:46 +08:00
|
|
|
|
|
|
|
// Enable position and texture coordinates vertex components
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glEnableClientState(GL_COLOR_ARRAY);
|
2021-12-10 01:11:37 +08:00
|
|
|
|
|
|
|
return true;
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// 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
|
|
|
|
///
|
2021-12-10 03:55:33 +08:00
|
|
|
/// \return True if operation was successful, false otherwise
|
|
|
|
///
|
2014-09-30 22:07:25 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
2021-12-10 01:11:37 +08:00
|
|
|
[[nodiscard]] bool draw(sf::Window& window, float elapsedTime)
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
// Activate the window
|
2021-12-10 01:11:37 +08:00
|
|
|
if (!window.setActive())
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to set the window as active" << std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// 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);
|
2014-04-24 18:55:47 +08:00
|
|
|
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);
|
2014-09-30 22:07:25 +08:00
|
|
|
|
2015-01-13 01:15:46 +08:00
|
|
|
// Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
|
2022-02-18 03:32:28 +08:00
|
|
|
// clang-format off
|
2022-06-13 00:41:53 +08:00
|
|
|
constexpr std::array<GLfloat, 216> cube =
|
2015-01-13 01:15:46 +08:00
|
|
|
{
|
|
|
|
// 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
|
|
|
|
};
|
2022-02-18 03:32:28 +08:00
|
|
|
// clang-format on
|
2015-01-13 01:15:46 +08:00
|
|
|
|
|
|
|
// Draw the cube
|
2022-06-13 00:41:53 +08:00
|
|
|
glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube.data());
|
|
|
|
glColorPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), cube.data() + 3);
|
2015-01-13 01:15:46 +08:00
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
2021-12-10 01:11:37 +08:00
|
|
|
|
|
|
|
return true;
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Entry point of application
|
|
|
|
///
|
|
|
|
/// \return Error code
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
// Open a connection with the X server
|
2021-12-03 22:45:32 +08:00
|
|
|
Display* display = XOpenDisplay(nullptr);
|
2014-09-30 22:07:25 +08:00
|
|
|
if (!display)
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2016-08-14 08:52:49 +08:00
|
|
|
// Get the default screen
|
2023-05-20 09:04:31 +08:00
|
|
|
const int screen = DefaultScreen(display);
|
2016-08-14 08:52:49 +08:00
|
|
|
|
|
|
|
// Let's create the main window
|
|
|
|
XSetWindowAttributes attributes;
|
|
|
|
attributes.background_pixel = BlackPixel(display, screen);
|
|
|
|
attributes.event_mask = KeyPressMask;
|
2023-05-20 09:04:31 +08:00
|
|
|
const Window window = XCreateWindow(display,
|
|
|
|
RootWindow(display, screen),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
650,
|
|
|
|
330,
|
|
|
|
0,
|
|
|
|
DefaultDepth(display, screen),
|
|
|
|
InputOutput,
|
|
|
|
DefaultVisual(display, screen),
|
|
|
|
CWBackPixel | CWEventMask,
|
|
|
|
&attributes);
|
2016-08-14 08:52:49 +08:00
|
|
|
if (!window)
|
2014-09-30 22:07:25 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
2016-08-14 08:52:49 +08:00
|
|
|
// Set the window's name
|
2022-07-05 00:20:58 +08:00
|
|
|
XStoreName(display, window, "SFML Window");
|
2016-08-14 08:52:49 +08:00
|
|
|
|
|
|
|
// Let's create the windows which will serve as containers for our SFML views
|
2023-05-20 09:04:31 +08:00
|
|
|
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);
|
2016-08-14 08:52:49 +08:00
|
|
|
|
|
|
|
// Show our windows
|
|
|
|
XMapWindow(display, window);
|
|
|
|
XFlush(display);
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Create our SFML views
|
2016-08-14 08:52:49 +08:00
|
|
|
sf::Window sfmlView1(view1);
|
|
|
|
sf::Window sfmlView2(view2);
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Create a clock for measuring elapsed time
|
2023-05-20 09:04:31 +08:00
|
|
|
const sf::Clock clock;
|
2014-09-30 22:07:25 +08:00
|
|
|
|
2019-04-13 19:16:32 +08:00
|
|
|
// Load OpenGL or OpenGL ES entry points using glad
|
2021-12-10 01:11:37 +08:00
|
|
|
if (!sfmlView1.setActive())
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to set view 1 as active" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2019-04-13 19:16:32 +08:00
|
|
|
|
|
|
|
#ifdef SFML_OPENGL_ES
|
|
|
|
gladLoadGLES1(reinterpret_cast<GLADloadfunc>(sf::Context::getFunction));
|
|
|
|
#else
|
|
|
|
gladLoadGL(reinterpret_cast<GLADloadfunc>(sf::Context::getFunction));
|
|
|
|
#endif
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
// Initialize our views
|
2021-12-10 01:11:37 +08:00
|
|
|
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;
|
|
|
|
}
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Start the event loop
|
|
|
|
bool running = true;
|
|
|
|
while (running)
|
|
|
|
{
|
2016-08-14 08:52:49 +08:00
|
|
|
while (XPending(display))
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
2016-08-14 08:52:49 +08:00
|
|
|
// 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;
|
|
|
|
}
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw something into our views
|
2021-12-10 01:11:37 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
// Display the views on screen
|
2014-04-24 18:55:47 +08:00
|
|
|
sfmlView1.display();
|
|
|
|
sfmlView2.display();
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
2018-09-15 19:08:28 +08:00
|
|
|
// Close our SFML views before destroying the underlying window
|
|
|
|
sfmlView1.close();
|
|
|
|
sfmlView2.close();
|
|
|
|
|
|
|
|
// Destroy the window
|
|
|
|
XDestroyWindow(display, window);
|
|
|
|
|
2016-08-14 08:52:49 +08:00
|
|
|
// Close the display
|
|
|
|
XCloseDisplay(display);
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|