cpp_launchpad/compile/target/linux/draw/window_service.cpp

198 lines
4.2 KiB
C++

#include "draw/opengl_service.h"
#include "draw/window_service.h"
#include "inform/text_service.h"
#include "target/linux/draw/opengl_x11_service.h"
#include <X11/Xlib.h>
#include <X11/X.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
window_service_t::window_service_t(
window_service_t::configuration_t* configuration_p
) {
configuration_m = configuration_p;
}
void_t<window_service_t::error_t> window_service_t::create_window(
std::string* window_title
) {
void_t<window_service_t::error_t> void_l;
void_l.error_m = window_service_t::error_t::unknown;
Display* display_l = XOpenDisplay(
NULL
);
if (display_l == NULL) {
void_l.error_m = window_service_t::error_t::x_server_connection_failure;
return void_l;
}
Window root_window_l = DefaultRootWindow(
display_l
);
// Create OpenGL X11 Service for binding
opengl_x11_service_t::configuration_t opengl_x11_service_configuration_l;
opengl_x11_service_factory_t opengl_x11_service_factory_l;
opengl_x11_service_t* opengl_x11_service_l = opengl_x11_service_factory_l.create(
&opengl_x11_service_configuration_l
).value_m;
XVisualInfo* visual_info_l = opengl_x11_service_l->get_visual_info(
display_l
).value_m;
Colormap colormap_l = XCreateColormap(
display_l,
root_window_l,
visual_info_l->visual,
AllocNone
);
XSetWindowAttributes set_window_attributes_l;
set_window_attributes_l.colormap = colormap_l;
set_window_attributes_l.event_mask = ExposureMask | KeyPressMask;
Window window_l = XCreateWindow(
display_l,
root_window_l,
0,
0,
320,
240,
0,
visual_info_l->depth,
InputOutput,
visual_info_l->visual,
CWColormap | CWEventMask,
&set_window_attributes_l
);
XSelectInput(
display_l,
window_l,
ExposureMask | KeyPressMask
);
XMapWindow(
display_l,
window_l
);
XStoreName(
display_l,
window_l,
window_title->data()
);
opengl_x11_service_l->initialize_context(
display_l,
visual_info_l,
window_l
);
// Create OpenGL Service for rendering
opengl_service_t::configuration_t opengl_service_configuration_l;
opengl_service_factory_t opengl_service_factory_l;
opengl_service_t* opengl_service_l = opengl_service_factory_l.create(
&opengl_service_configuration_l
).value_m;
opengl_service_l->enable_depth_test(
);
int display_fd_l = ConnectionNumber(display_l);
fd_set in_fds;
int cycle_l = 0;
XWindowAttributes window_attributes_l;
XEvent event_l;
while (true) {
FD_ZERO(&in_fds);
FD_SET(display_fd_l, &in_fds);
// Set our timer.
timeval timeval_l;
timeval_l.tv_usec = 8333;
timeval_l.tv_sec = 0;
// Wait for X Event or a Timer
int select_l = select(
display_fd_l + 1,
&in_fds,
0,
0,
&timeval_l
);
if (select_l >= 0) {
// event received
switch (event_l.type) {
case KeyPress:
opengl_x11_service_l->dispose(
display_l
);
XDestroyWindow(
display_l,
window_l
);
XCloseDisplay(
display_l
);
break;
case Expose:
XGetWindowAttributes(
display_l,
window_l,
&window_attributes_l
);
opengl_service_l->resize_viewport(
0,
0,
window_attributes_l.width,
window_attributes_l.height
);
opengl_service_l->draw(
cycle_l
);
opengl_x11_service_l->swap_buffers(
display_l,
window_l
);
break;
default:
break;
}
} else {
// timeout
}
while (XPending(display_l)) {
XNextEvent(
display_l,
&event_l
);
}
cycle_l += 1;
}
void_l.error_m = window_service_t::error_t::none;
return void_l;
}
return_t<window_service_t::error_t, window_service_t*> window_service_factory_t::create(
window_service_t::configuration_t* configuration_p
) {
return_t<window_service_t::error_t, window_service_t*> return_l;
return_l.error_m = window_service_t::error_t::none;
window_service_t* window_service_l = new window_service_t(
configuration_p
);
return_l.value_m = window_service_l;
return return_l;
}
void_t<window_service_t::error_t> window_service_factory_t::dispose(
window_service_t* window_service_p
) {
delete window_service_p;
void_t<window_service_t::error_t> void_l;
void_l.error_m = window_service_t::error_t::none;
return void_l;
}