From 3b2fceed064c06d55e1cd33d51e855e909c81f75 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 10 Oct 2021 12:56:55 +0200 Subject: add event and test graphics --- src/window.c | 94 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 1 deletion(-) (limited to 'src/window.c') diff --git a/src/window.c b/src/window.c index 39855da..9836907 100644 --- a/src/window.c +++ b/src/window.c @@ -1,13 +1,105 @@ #include "../include/mgl/window.h" +#include "../include/mgl/mgl.h" +#include +#include -void mgl_window_init(mgl_window *self) { +#define GL_COLOR_BUFFER_BIT 0x00004000 +int mgl_window_create(mgl_window *self, const char *title, int width, int height, mgl_window_callback *callback, void *userdata) { + return mgl_window_create_with_params(self, title, width, height, DefaultRootWindow(mgl_get_context()->connection), callback, userdata); +} + +int mgl_window_create_with_params(mgl_window *self, const char *title, int width, int height, unsigned long parent_window, mgl_window_callback *callback, void *userdata) { + self->window = 0; + self->callback = *callback; + self->callback_userdata = userdata; + + mgl_context *context = mgl_get_context(); + + Colormap color_map = XCreateColormap(context->connection, DefaultRootWindow(context->connection), ((XVisualInfo*)context->visual_info)->visual, AllocNone); + if(!color_map) { + fprintf(stderr, "XCreateColormap failed\n"); + return -1; + } + + XSetWindowAttributes window_attr; + window_attr.colormap = color_map; + window_attr.event_mask = ExposureMask | KeyPressMask; + + self->window = XCreateWindow(context->connection, parent_window, 0, 0, width, height, 0, ((XVisualInfo*)context->visual_info)->depth, InputOutput, ((XVisualInfo*)context->visual_info)->visual, CWColormap | CWEventMask, &window_attr); + //XFreeColormap(context->connection, color_map); + if(!self->window) { + fprintf(stderr, "XCreateWindow failed\n"); + mgl_window_deinit(self); + return -1; + } + + /* TODO: Test utf8 */ + XStoreName(context->connection, self->window, title); + return 0; } void mgl_window_deinit(mgl_window *self) { + mgl_context *context = mgl_get_context(); + if(self->window) { + XDestroyWindow(context->connection, self->window); + self->window = 0; + } +} + +static void mgl_window_draw(mgl_window *self) { + mgl_context *context = mgl_get_context(); + + /* TODO: Get window size from window resize event instead */ + XWindowAttributes gwa; + XGetWindowAttributes(context->connection, self->window, &gwa); + + context->gl.glViewport(0, 0, gwa.width, gwa.height); + context->gl.glClear(GL_COLOR_BUFFER_BIT); + context->gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + if(self->callback.draw) + self->callback.draw(self, self->callback_userdata); + context->glx.glXSwapBuffers(context->connection, self->window); } void mgl_window_show(mgl_window *self) { + mgl_context *context = mgl_get_context(); + + XMapWindow(context->connection, self->window); + XFlush(context->connection); + + /* TODO: Switch current when rendering to another window, and set current to NULL when destroying the currently selected context */ + context->glx.glXMakeCurrent(context->connection, self->window, context->glx_context); + mgl_window_draw(self); +} + +static void on_receive_x11_event(mgl_window *window, XEvent *xev) { + switch(xev->type) { + case Expose: + mgl_window_draw(window); + break; + } +} + +void mgl_window_event_poll(mgl_window *self, int timeout_ms) { + Display *display = mgl_get_context()->connection; + const int x11_fd = ConnectionNumber(display); + + fd_set in_fds; + FD_ZERO(&in_fds); /* TODO: Optimize */ + FD_SET(x11_fd, &in_fds); + + struct timeval tv; + tv.tv_sec = timeout_ms / 1000; + tv.tv_usec = (timeout_ms * 1000) - (tv.tv_sec * 1000 * 1000); + const int num_ready_fds = select(1 + x11_fd, &in_fds, NULL, NULL, &tv); + if(num_ready_fds > 0) { + XEvent xev; + while(XPending(display)) { + XNextEvent(display, &xev); + on_receive_x11_event(self, &xev); + } + } } -- cgit v1.2.3