diff options
author | dec05eba <dec05eba@protonmail.com> | 2024-07-25 14:56:49 +0200 |
---|---|---|
committer | dec05eba <dec05eba@protonmail.com> | 2024-07-25 14:56:49 +0200 |
commit | b4799373f7fb9c8faec813e78a955438301cb9d1 (patch) | |
tree | 580b177912b3922b34681122099d742a57fd853b | |
parent | 05da9076d3d1b0d770c13572368f974786edcd92 (diff) |
Add sprite set size/height functions, option to hide window decorations in create window
-rw-r--r-- | include/mgl/graphics/sprite.h | 4 | ||||
-rw-r--r-- | include/mgl/window/window.h | 1 | ||||
-rw-r--r-- | src/graphics/sprite.c | 16 | ||||
-rw-r--r-- | src/window/window.c | 39 |
4 files changed, 59 insertions, 1 deletions
diff --git a/include/mgl/graphics/sprite.h b/include/mgl/graphics/sprite.h index 64d9c03..0193c4c 100644 --- a/include/mgl/graphics/sprite.h +++ b/include/mgl/graphics/sprite.h @@ -25,6 +25,10 @@ void mgl_sprite_set_position(mgl_sprite *self, mgl_vec2f position); void mgl_sprite_set_color(mgl_sprite *self, mgl_color color); void mgl_sprite_set_rotation(mgl_sprite *self, float degrees); void mgl_sprite_set_origin(mgl_sprite *self, mgl_vec2f origin); +/* This only has an effect if the sprite has a texture set */ +void mgl_sprite_set_size(mgl_sprite *self, mgl_vec2f size); +/* This only has an effect if the sprite has a texture set. Scales width in proportion */ +void mgl_sprite_set_height(mgl_sprite *self, float height); void mgl_sprite_draw(mgl_context *context, mgl_sprite *sprite); #endif /* MGL_SPRITE_H */ diff --git a/include/mgl/window/window.h b/include/mgl/window/window.h index 90bbd3d..eb7f587 100644 --- a/include/mgl/window/window.h +++ b/include/mgl/window/window.h @@ -75,6 +75,7 @@ typedef struct { bool hidden; /* false by default */ bool override_redirect; /* false by default */ bool support_alpha; /* support alpha for the window, false by default */ + bool hide_decorations; /* this is a hint, it may be ignored by the window manager, false by default */ mgl_color background_color; /* default: black */ const char *class_name; mgl_window_type window_type; /* default: normal */ diff --git a/src/graphics/sprite.c b/src/graphics/sprite.c index df3e902..95f733b 100644 --- a/src/graphics/sprite.c +++ b/src/graphics/sprite.c @@ -31,6 +31,22 @@ void mgl_sprite_set_origin(mgl_sprite *self, mgl_vec2f origin) { self->origin = origin; } +void mgl_sprite_set_size(mgl_sprite *self, mgl_vec2f size) { + if(!self->texture) + return; + + self->scale.x = size.x / (float)self->texture->width; + self->scale.y = size.y / (float)self->texture->height; +} + +void mgl_sprite_set_height(mgl_sprite *self, float height) { + if(!self->texture) + return; + + self->scale.x = height / (float)self->texture->width; + self->scale.y = self->scale.x; +} + /* TODO: Cache texture bind to not bind texture if its already bound and do not bind texture 0 */ void mgl_sprite_draw(mgl_context *context, mgl_sprite *sprite) { if(!sprite->texture) diff --git a/src/window/window.c b/src/window/window.c index 17066a0..8681db9 100644 --- a/src/window/window.c +++ b/src/window/window.c @@ -76,6 +76,7 @@ typedef struct { Atom net_wm_window_type_atom; Atom net_wm_window_type_normal_atom; Atom net_wm_window_type_dialog_atom; + Atom motif_wm_hints_atom; Cursor default_cursor; Cursor invisible_cursor; unsigned int prev_keycode_pressed; @@ -156,6 +157,7 @@ static int x11_context_init(x11_context *self, bool alpha) { self->glx_context = NULL; self->xim = NULL; self->xic = NULL; + /* TODO: Create all of these with one XInternAtoms call instead */ self->clipboard_atom = XInternAtom(context->connection, "CLIPBOARD", False); self->targets_atom = XInternAtom(context->connection, "TARGETS", False); self->text_atom = XInternAtom(context->connection, "TEXT", False); @@ -172,6 +174,7 @@ static int x11_context_init(x11_context *self, bool alpha) { self->net_wm_window_type_atom = XInternAtom(context->connection, "_NET_WM_WINDOW_TYPE", False); self->net_wm_window_type_normal_atom = XInternAtom(context->connection, "_NET_WM_WINDOW_TYPE_NORMAL", False); self->net_wm_window_type_dialog_atom = XInternAtom(context->connection, "_NET_WM_WINDOW_TYPE_DIALOG", False); + self->motif_wm_hints_atom = XInternAtom(context->connection, "_MOTIF_WM_HINTS", False); self->default_cursor = None; self->invisible_cursor = None; @@ -501,6 +504,30 @@ static void mgl_set_window_type(mgl_window *self, mgl_window_type window_type) { } } +typedef struct { + unsigned long flags; + unsigned long functions; + unsigned long decorations; + long input_mode; + unsigned long status; +} MotifHints; + +#define MWM_HINTS_DECORATIONS 2 + +#define MWM_DECOR_NONE 0 +#define MWM_DECOR_ALL 1 + +static void mgl_window_set_decorations_visible(mgl_window *self, bool visible) { + mgl_context *context = mgl_get_context(); + x11_context *x11_context = self->context; + + MotifHints motif_hints = {0}; + motif_hints.flags = MWM_HINTS_DECORATIONS; + motif_hints.decorations = visible ? MWM_DECOR_ALL : MWM_DECOR_NONE; + + XChangeProperty(context->connection, self->window, x11_context->motif_wm_hints_atom, x11_context->motif_wm_hints_atom, 32, PropModeReplace, (unsigned char*)&motif_hints, sizeof(motif_hints) / sizeof(long)); +} + static int mgl_window_init(mgl_window *self, const char *title, const mgl_window_create_params *params, Window existing_window) { self->window = 0; self->context = NULL; @@ -588,6 +615,11 @@ static int mgl_window_init(mgl_window *self, const char *title, const mgl_window } self->window = existing_window; + + if(params && params->hide_decorations) { + mgl_window_set_decorations_visible(self, false); + } + if(hide_window) XUnmapWindow(context->connection, existing_window); } else { @@ -601,6 +633,10 @@ static int mgl_window_init(mgl_window *self, const char *title, const mgl_window return -1; } + if(params && params->hide_decorations) { + mgl_window_set_decorations_visible(self, false); + } + mgl_window_set_title(self, title); if(!hide_window) XMapWindow(context->connection, self->window); @@ -625,7 +661,7 @@ static int mgl_window_init(mgl_window *self, const char *title, const mgl_window char host_name[HOST_NAME_MAX]; if(gethostname(host_name, sizeof(host_name)) == 0) { XTextProperty txt_prop; - txt_prop.value = host_name; + txt_prop.value = (unsigned char*)host_name; txt_prop.encoding = XA_STRING; txt_prop.format = 8; txt_prop.nitems = strlen(host_name); @@ -641,6 +677,7 @@ static int mgl_window_init(mgl_window *self, const char *title, const mgl_window XSetTransientForHint(context->connection, self->window, params->transient_for_window); } + /* TODO: Move this to above XMapWindow? */ mgl_window_type window_type = params ? params->window_type : MGL_WINDOW_TYPE_NORMAL; mgl_set_window_type(self, window_type); |