2016-09-27 17:01:21 +00:00
|
|
|
#include "opengl.hpp"
|
2016-09-13 19:03:36 +00:00
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <sys/time.h>
|
2016-09-14 02:14:39 +00:00
|
|
|
#include <unistd.h>
|
2016-09-27 18:56:17 +00:00
|
|
|
#include "options.hpp"
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
#define UNUSED(foo) (void) foo;
|
2016-09-13 19:03:36 +00:00
|
|
|
|
|
|
|
timespec diff(timespec start, timespec end) {
|
2016-09-27 18:56:17 +00:00
|
|
|
timespec result;
|
|
|
|
if ((end.tv_nsec - start.tv_nsec) < 0) {
|
|
|
|
result.tv_sec = end.tv_sec - start.tv_sec - 1;
|
|
|
|
result.tv_nsec = 1000000000 + end.tv_nsec - start.tv_nsec;
|
|
|
|
} else {
|
|
|
|
result.tv_sec = end.tv_sec - start.tv_sec;
|
|
|
|
result.tv_nsec = end.tv_nsec - start.tv_nsec;
|
|
|
|
}
|
|
|
|
return result;
|
2016-09-13 19:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
endofthejedi::GLWindow::GLWindow(unsigned int width, unsigned int height)
|
|
|
|
: m_width(width), m_height(height) {
|
2016-09-27 18:56:17 +00:00
|
|
|
m_display = XOpenDisplay(NULL);
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
if (m_display == NULL) {
|
|
|
|
throw std::runtime_error("XOpenDisplay NULL");
|
|
|
|
}
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
m_rootwnd = DefaultRootWindow(m_display);
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
m_visualinfo = glXChooseVisual(m_display, 0, m_attributes);
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
if (m_visualinfo == NULL) {
|
|
|
|
throw std::runtime_error("glXChooseVisual NULL");
|
|
|
|
}
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
m_colormap =
|
|
|
|
XCreateColormap(m_display, m_rootwnd, m_visualinfo->visual, AllocNone);
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
m_swa.colormap = m_colormap;
|
|
|
|
m_swa.event_mask = ExposureMask | KeyPressMask;
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
m_window = XCreateWindow(
|
|
|
|
m_display, m_rootwnd, 0, 0, width, height, 0, m_visualinfo->depth,
|
|
|
|
InputOutput, m_visualinfo->visual, CWColormap | CWEventMask, &m_swa);
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
m_atomWmDeleteWindow = XInternAtom(m_display, "WM_DELETE_WINDOW", False);
|
|
|
|
XSetWMProtocols(m_display, m_window, &m_atomWmDeleteWindow, 1);
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
XMapWindow(m_display, m_window);
|
|
|
|
XStoreName(m_display, m_window, "NAME");
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
m_glcontext = glXCreateContext(m_display, m_visualinfo, NULL, GL_TRUE);
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
XSync(m_display, False);
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
glXMakeCurrent(m_display, m_window, m_glcontext);
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
std::cout << "GL Renderer: " << glGetString(GL_RENDERER) << "\n";
|
|
|
|
std::cout << "GL Version: " << glGetString(GL_VERSION) << "\n";
|
|
|
|
std::cout << "GLSL Version: " << glGetString(GL_SHADING_LANGUAGE_VERSION)
|
|
|
|
<< "\n";
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
init();
|
2016-09-13 19:03:36 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
XClearWindow(m_display, m_window);
|
|
|
|
XMapRaised(m_display, m_window);
|
2016-09-13 19:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
endofthejedi::GLWindow::~GLWindow() {
|
2016-09-27 18:56:17 +00:00
|
|
|
glXMakeCurrent(m_display, None, NULL);
|
|
|
|
glXDestroyContext(m_display, m_glcontext);
|
|
|
|
XFree(m_visualinfo);
|
|
|
|
XDestroyWindow(m_display, m_window);
|
|
|
|
XCloseDisplay(m_display);
|
2016-09-13 19:03:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-14 02:14:39 +00:00
|
|
|
void endofthejedi::GLWindow::handleevents() {
|
2016-09-27 18:56:17 +00:00
|
|
|
if (XPending(m_display) > 0) {
|
|
|
|
XEvent xev;
|
|
|
|
XNextEvent(m_display, &xev);
|
|
|
|
handle(xev);
|
|
|
|
}
|
2016-09-14 02:14:39 +00:00
|
|
|
}
|
|
|
|
|
2016-09-13 19:03:36 +00:00
|
|
|
void endofthejedi::GLWindow::handle(XEvent event) {
|
2016-09-27 18:56:17 +00:00
|
|
|
if (event.type == Expose) {
|
|
|
|
XWindowAttributes attribs;
|
|
|
|
XGetWindowAttributes(m_display, m_window, &attribs);
|
|
|
|
m_width = attribs.width;
|
|
|
|
m_height = attribs.height;
|
|
|
|
resize();
|
|
|
|
} else if (event.type == ClientMessage) {
|
|
|
|
if (event.xclient.data.l[0] == m_atomWmDeleteWindow) {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
} else if (event.type == DestroyNotify) {
|
|
|
|
stop();
|
2016-09-13 19:03:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void endofthejedi::GLWindow::swap() { glXSwapBuffers(m_display, m_window); }
|
|
|
|
|
|
|
|
void endofthejedi::GLWindow::loop() {
|
2016-09-27 18:56:17 +00:00
|
|
|
m_running = true;
|
2016-09-14 02:14:39 +00:00
|
|
|
|
2016-09-27 18:56:17 +00:00
|
|
|
timespec prev;
|
|
|
|
timespec current;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC_RAW, &prev);
|
2016-09-13 19:03:36 +00:00
|
|
|
clock_gettime(CLOCK_MONOTONIC_RAW, ¤t);
|
2016-09-27 18:56:17 +00:00
|
|
|
|
|
|
|
double delta = 0.0;
|
|
|
|
double sleeptime = 0.0;
|
|
|
|
|
|
|
|
while (m_running) {
|
|
|
|
handleevents();
|
|
|
|
render(delta);
|
|
|
|
swap();
|
|
|
|
|
|
|
|
if (m_maxfps > 0) {
|
|
|
|
sleeptime = ((1000000000.0 / m_maxfps) - delta) + sleeptime;
|
|
|
|
if (sleeptime > 0.0) {
|
|
|
|
usleep((unsigned int)(sleeptime / 1000.0));
|
|
|
|
sleeptime = sleeptime - (unsigned int)(sleeptime / 1000.0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clock_gettime(CLOCK_MONOTONIC_RAW, ¤t);
|
|
|
|
delta = diff(prev, current).tv_nsec;
|
|
|
|
prev = current;
|
|
|
|
if (delta > 0.0) {
|
|
|
|
m_fps = (1000000000.0 / delta);
|
|
|
|
if (ISSET_FLAG(SHOW_FPS)) {
|
|
|
|
std::cout << m_fps << "\n";
|
|
|
|
}
|
|
|
|
}
|
2016-09-14 02:14:39 +00:00
|
|
|
}
|
2016-09-13 19:03:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void endofthejedi::GLWindow::stop() { m_running = false; }
|
|
|
|
|
|
|
|
void endofthejedi::GLWindow::init() {}
|
|
|
|
void endofthejedi::GLWindow::render(double time) { UNUSED(time) }
|
|
|
|
void endofthejedi::GLWindow::resize() {}
|