From 14e3617736c63bd22b0785ad418acef825db221f Mon Sep 17 00:00:00 2001 From: dec05eba Date: Thu, 28 Oct 2021 17:10:41 +0200 Subject: Use a separate glx context for every window Fix vertex buffer causing crash on render if update is called with an empty list. --- src/window/window.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/window') diff --git a/src/window/window.c b/src/window/window.c index 2e9a1ba..f29b920 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -49,6 +49,12 @@ static int mgl_window_init(mgl_window *self, const char *title, int width, int h if(parent_window == 0) parent_window = DefaultRootWindow(context->connection); + self->glx_context = context->gl.glXCreateContext(context->connection, context->visual_info, NULL, 1); + if(!self->glx_context) { + fprintf(stderr, "glXCreateContext failed\n"); + return -1; + } + Colormap color_map = XCreateColormap(context->connection, DefaultRootWindow(context->connection), ((XVisualInfo*)context->visual_info)->visual, AllocNone); if(!color_map) { fprintf(stderr, "XCreateColormap failed\n"); @@ -68,6 +74,7 @@ static int mgl_window_init(mgl_window *self, const char *title, int width, int h if(!XChangeWindowAttributes(context->connection, existing_window, CWColormap | CWEventMask | CWBitGravity, &window_attr)) { fprintf(stderr, "XChangeWindowAttributes failed\n"); XFreeColormap(context->connection, color_map); + mgl_window_deinit(self); return -1; } XFreeColormap(context->connection, color_map); @@ -89,8 +96,8 @@ static int mgl_window_init(mgl_window *self, const char *title, int width, int h 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); + /* TODO: Check for failure? */ + context->gl.glXMakeCurrent(context->connection, self->window, self->glx_context); set_vertical_sync_enabled(self->window, 1); context->gl.glEnable(GL_TEXTURE_2D); context->gl.glEnable(GL_BLEND); @@ -123,6 +130,10 @@ int mgl_window_init_from_existing_window(mgl_window *self, mgl_window_handle exi void mgl_window_deinit(mgl_window *self) { mgl_context *context = mgl_get_context(); + if(self->glx_context) { + context->gl.glXDestroyContext(context->connection, self->glx_context); + self->glx_context = NULL; + } if(self->window) { XDestroyWindow(context->connection, self->window); self->window = 0; @@ -172,3 +183,4 @@ void mgl_window_display(mgl_window *self) { mgl_context *context = mgl_get_context(); context->gl.glXSwapBuffers(context->connection, self->window); } + -- cgit v1.2.3