From 4aa7273eea642bff78477b0b220c7056628b13a8 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 10 Oct 2021 17:29:31 +0200 Subject: Add texture loading (and render in test) --- src/window.c | 81 +++++++++++++++++++++++++++--------------------------------- 1 file changed, 37 insertions(+), 44 deletions(-) (limited to 'src/window.c') diff --git a/src/window.c b/src/window.c index 4d877d7..7d13ffe 100644 --- a/src/window.c +++ b/src/window.c @@ -3,10 +3,28 @@ #include #include -#define GL_COLOR_BUFFER_BIT 0x00004000 -#define GL_BLEND 0x0BE2 -#define GL_SRC_ALPHA 0x0302 -#define GL_ONE_MINUS_SRC_ALPHA 0x0303 +/* TODO: check for glx swap control extension string (GLX_EXT_swap_control, etc) */ +static void set_vertical_sync_enabled(Window window, int enabled) { + int result = 0; + mgl_context *context = mgl_get_context(); + + if(context->gl.glXSwapIntervalEXT) { + context->gl.glXSwapIntervalEXT(context->connection, window, enabled ? 1 : 0); + } else if(context->gl.glXSwapIntervalMESA) { + result = context->gl.glXSwapIntervalMESA(enabled ? 1 : 0); + } else if(context->gl.glXSwapIntervalSGI) { + result = context->gl.glXSwapIntervalSGI(enabled ? 1 : 0); + } else { + static int warned = 0; + if (!warned) { + warned = 1; + fprintf(stderr, "Warning: setting vertical sync not supported\n"); + } + } + + if(result != 0) + fprintf(stderr, "Warning: setting vertical sync failed\n"); +} 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); @@ -30,7 +48,7 @@ int mgl_window_create_with_params(mgl_window *self, const char *title, int width window_attr.event_mask = 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); + XFreeColormap(context->connection, color_map); if(!self->window) { fprintf(stderr, "XCreateWindow failed\n"); mgl_window_deinit(self); @@ -39,60 +57,32 @@ int mgl_window_create_with_params(mgl_window *self, const char *title, int width /* 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; - } -} - -/* TODO: check for glx swap control extension string (GLX_EXT_swap_control, etc) */ -static void set_vertical_sync_enabled(Window window, int enabled) { - int result = 0; - mgl_context *context = mgl_get_context(); - - if(context->gl.glXSwapIntervalEXT) { - context->gl.glXSwapIntervalEXT(context->connection, window, enabled ? 1 : 0); - } else if(context->gl.glXSwapIntervalMESA) { - result = context->gl.glXSwapIntervalMESA(enabled ? 1 : 0); - } else if(context->gl.glXSwapIntervalSGI) { - result = context->gl.glXSwapIntervalSGI(enabled ? 1 : 0); - } else { - static int warned = 0; - if (!warned) { - warned = 1; - fprintf(stderr, "Warning: setting vertical sync not supported\n"); - } - } - - if(result != 0) - fprintf(stderr, "Warning: setting vertical sync failed\n"); -} - -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->gl.glXMakeCurrent(context->connection, self->window, context->glx_context); set_vertical_sync_enabled(self->window, 1); + context->gl.glEnable(GL_TEXTURE_2D); context->gl.glEnable(GL_BLEND); context->gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); mgl_window_draw(self); + 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 on_receive_x11_event(mgl_window *window, XEvent *xev) { } -void mgl_window_event_poll(mgl_window *self) { +void mgl_window_events_poll(mgl_window *self) { Display *display = mgl_get_context()->connection; const int x11_fd = ConnectionNumber(display); @@ -114,6 +104,9 @@ void mgl_window_event_poll(mgl_window *self) { XNextEvent(display, &xev); on_receive_x11_event(self, &xev); } + } else if(num_ready_fds == -1) { + /* TODO: */ + fprintf(stderr, "Disconnected!\n"); } } -- cgit v1.2.3