From f2da59054cedd1c07779e72537da4d7b14616b48 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Fri, 2 Aug 2024 06:01:36 +0200 Subject: Ensure correct blending function for transparent window --- src/graphics/sprite.c | 5 +++++ src/graphics/text.c | 5 +++++ src/mgl.c | 2 ++ src/window/window.c | 28 ++++++++++++++++++++++++++-- 4 files changed, 38 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/graphics/sprite.c b/src/graphics/sprite.c index 3110cf9..613ec4c 100644 --- a/src/graphics/sprite.c +++ b/src/graphics/sprite.c @@ -1,5 +1,6 @@ #include "../../include/mgl/graphics/sprite.h" #include "../../include/mgl/graphics/texture.h" +#include "../../include/mgl/window/window.h" #include "../../include/mgl/mgl.h" void mgl_sprite_init(mgl_sprite *self, mgl_texture *texture) { @@ -60,6 +61,8 @@ void mgl_sprite_draw(mgl_context *context, mgl_sprite *sprite) { if(!sprite->texture) return; + mgl_window_set_texture_blend_func(context->current_window); + float texture_right = 1.0f; float texture_bottom = 1.0f; if(sprite->texture->pixel_coordinates) { @@ -86,4 +89,6 @@ void mgl_sprite_draw(mgl_context *context, mgl_sprite *sprite) { context->gl.glEnd(); mgl_texture_use(NULL); context->gl.glLoadIdentity(); /* TODO: Remove, but what about glRotatef above */ + + mgl_window_set_render_blend_func(context->current_window); } diff --git a/src/graphics/text.c b/src/graphics/text.c index 0942b62..76ac128 100644 --- a/src/graphics/text.c +++ b/src/graphics/text.c @@ -1,6 +1,7 @@ #include "../../include/mgl/graphics/text.h" #include "../../include/mgl/graphics/font.h" #include "../../include/mgl/system/utf8.h" +#include "../../include/mgl/window/window.h" #include "../../include/mgl/mgl.h" #include @@ -225,10 +226,14 @@ void mgl_text_draw(mgl_context *context, mgl_text *text) { text_draw_userdata.context = context; text_draw_userdata.prev_codepoint = 0; + mgl_window_set_texture_blend_func(context->current_window); + context->gl.glColor4ub(text->color.r, text->color.g, text->color.b, text->color.a); mgl_texture_use(&text->font->texture); context->gl.glBegin(GL_QUADS); mgl_text_for_each_codepoint(text, text_draw_callback, &text_draw_userdata); context->gl.glEnd(); mgl_texture_use(NULL); + + mgl_window_set_render_blend_func(context->current_window); } diff --git a/src/mgl.c b/src/mgl.c index 16379e0..6fd26a7 100644 --- a/src/mgl.c +++ b/src/mgl.c @@ -119,6 +119,8 @@ void mgl_deinit(void) { */ mgl_gl_unload(&context.gl); } + + context.current_window = NULL; } if(init_count > 0) diff --git a/src/window/window.c b/src/window/window.c index 586719d..ccbf8ef 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -85,6 +85,7 @@ typedef struct { GLXFBConfig *fbconfigs; GLXFBConfig fbconfig; XVisualInfo *visual_info; + bool support_alpha; /* This only contains connected and active monitors */ mgl_monitor monitors[MAX_MONITORS]; @@ -185,6 +186,7 @@ static int x11_context_init(x11_context *self, bool alpha) { self->fbconfigs = NULL; self->fbconfig = NULL; self->visual_info = NULL; + self->support_alpha = alpha; memset(self->monitors, 0, sizeof(self->monitors)); self->num_monitors = 0; @@ -574,6 +576,7 @@ static int mgl_window_init(mgl_window *self, const char *title, const mgl_window self->num_monitors = 0; mgl_context *context = mgl_get_context(); + context->current_window = self; Window parent_window = params ? params->parent_window : None; if(parent_window == 0) @@ -700,7 +703,7 @@ static int mgl_window_init(mgl_window *self, const char *title, const mgl_window context->gl.glEnable(GL_TEXTURE_2D); context->gl.glEnable(GL_BLEND); context->gl.glEnable(GL_SCISSOR_TEST); - context->gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + mgl_window_set_render_blend_func(self); context->gl.glEnableClientState(GL_VERTEX_ARRAY); context->gl.glEnableClientState(GL_TEXTURE_COORD_ARRAY); context->gl.glEnableClientState(GL_COLOR_ARRAY); @@ -765,6 +768,10 @@ void mgl_window_deinit(mgl_window *self) { self->clipboard_size = 0; self->open = false; + + mgl_context *context = mgl_get_context(); + if(context->current_window == self) + context->current_window = NULL; } /* Returns MGL_KEY_UNKNOWN on no match */ @@ -1256,7 +1263,13 @@ static void mgl_window_on_receive_event(mgl_window *self, XEvent *xev, mgl_event void mgl_window_clear(mgl_window *self, mgl_color color) { mgl_context *context = mgl_get_context(); - context->gl.glClearColor((float)color.r / 255.0f, (float)color.g / 255.0f, (float)color.b / 255.0f, (float)color.a / 255.0f); + x11_context *x11_context = self->context; + + const float alpha = (float)color.a / 255.0f; + if(x11_context->support_alpha) + context->gl.glClearColor(((float)color.r / 255.0f) * alpha, ((float)color.g / 255.0f) * alpha, ((float)color.b / 255.0f) * alpha, alpha); + else + context->gl.glClearColor((float)color.r / 255.0f, (float)color.g / 255.0f, (float)color.b / 255.0f, alpha); context->gl.glClear(GL_COLOR_BUFFER_BIT); } @@ -1783,6 +1796,17 @@ void mgl_window_set_key_repeat_enabled(mgl_window *self, bool enabled) { self->key_repeat_enabled = enabled; } +void mgl_window_set_texture_blend_func(mgl_window *self) { + mgl_context *context = mgl_get_context(); + context->gl.glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); +} + +void mgl_window_set_render_blend_func(mgl_window *self) { + mgl_context *context = mgl_get_context(); + x11_context *x11_context = self->context; + context->gl.glBlendFunc(x11_context->support_alpha ? GL_ONE : GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); +} + void mgl_window_flush(mgl_window *self) { mgl_context *context = mgl_get_context(); XFlush(context->connection); -- cgit v1.2.3