From 9289c2de69265e437a75cc8c312cb43ffc707595 Mon Sep 17 00:00:00 2001 From: itepechi <72330683+itepechi@users.noreply.github.com> Date: Sat, 30 Oct 2021 03:07:03 +0900 Subject: [PATCH] Refactor `background_image_anchor` for cleaner code --- kitty/bgimage_vertex.glsl | 4 ++-- kitty/data-types.h | 4 +++- kitty/options/to-c.h | 28 +++++++++++----------------- kitty/shaders.c | 22 +++++++--------------- 4 files changed, 23 insertions(+), 35 deletions(-) diff --git a/kitty/bgimage_vertex.glsl b/kitty/bgimage_vertex.glsl index 7e2c171d0..707000af2 100644 --- a/kitty/bgimage_vertex.glsl +++ b/kitty/bgimage_vertex.glsl @@ -8,7 +8,7 @@ #define tex_right 1 #define tex_bottom 1 -uniform float rescaled; +uniform float adjust_scale; uniform vec2 transform; // [ pos_left_relative, pos_top_relative ] uniform vec4 sizes; // [ window_width, window_height, image_width, image_height ] @@ -29,7 +29,7 @@ const vec2 tex_map[] = vec2[4]( float scaling_factor(int i) { - return rescaled * (sizes[i] / sizes[i + 2]) + (1 - rescaled); + return adjust_scale * (sizes[i] / sizes[i + 2]) + (1 - adjust_scale); } float position_divisor(int i) { diff --git a/kitty/data-types.h b/kitty/data-types.h index a1b9d3898..b2c490370 100644 --- a/kitty/data-types.h +++ b/kitty/data-types.h @@ -65,7 +65,9 @@ typedef enum MouseTrackingProtocols { NORMAL_PROTOCOL, UTF8_PROTOCOL, SGR_PROTOC typedef enum MouseShapes { BEAM, HAND, ARROW } MouseShape; typedef enum { NONE, MENUBAR, WINDOW, ALL } WindowTitleIn; typedef enum { TILING, SCALED, MIRRORED, CLAMPED } BackgroundImageLayout; -typedef enum { TOP_LEFT, TOP, TOP_RIGHT, LEFT, CENTER, RIGHT, BOTTOM_LEFT, BOTTOM, BOTTOM_RIGHT } BackgroundImageAnchor; +typedef struct { + float x, y; +} BackgroundImageAnchor; #define MAX_CHILDREN 512 #define BLANK_CHAR 0 diff --git a/kitty/options/to-c.h b/kitty/options/to-c.h index 32f48409e..9213b0f10 100644 --- a/kitty/options/to-c.h +++ b/kitty/options/to-c.h @@ -81,24 +81,18 @@ bglayout(PyObject *layout_name) { static BackgroundImageAnchor bganchor(PyObject *anchor_name) { const char *name = PyUnicode_AsUTF8(anchor_name); - if (strcmp(name, "top") == 0) { - return TOP; - } else if (strcmp(name, "top-right") == 0) { - return TOP_RIGHT; - } else if (strcmp(name, "left") == 0) { - return LEFT; - } else if (strcmp(name, "center") == 0) { - return CENTER; - } else if (strcmp(name, "right") == 0) { - return RIGHT; - } else if (strcmp(name, "bottom-left") == 0) { - return BOTTOM_LEFT; - } else if (strcmp(name, "bottom") == 0) { - return BOTTOM; - } else if (strcmp(name, "bottom-right") == 0) { - return BOTTOM_RIGHT; + BackgroundImageAnchor anchor = { .x = 0.5f, .y = 0.5f }; + if (strstr(name, "top") != NULL) { + anchor.y = 0.0f; + } else if (strstr(name, "bottom") != NULL) { + anchor.y = 1.0f; } - return TOP_LEFT; + if (strstr(name, "left") != NULL) { + anchor.x = 0.0f; + } else if (strstr(name, "right") != NULL) { + anchor.x = 1.0f; + } + return anchor; } #define STR_SETTER(name) { \ diff --git a/kitty/shaders.c b/kitty/shaders.c index 9276c01e7..61cc40e5b 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -166,7 +166,7 @@ typedef struct { static CellProgramLayout cell_program_layouts[NUM_PROGRAMS]; static ssize_t blit_vertex_array; typedef struct { - GLint image_location, rescaled_location, transform_location, sizes_location, opacity_location, premult_location; + GLint image_location, adjust_scale_location, transform_location, sizes_location, opacity_location, premult_location; } BGImageProgramLayout; static BGImageProgramLayout bgimage_program_layout = {0}; typedef struct { @@ -198,7 +198,7 @@ init_cell_program(void) { bgimage_program_layout.image_location = get_uniform_location(BGIMAGE_PROGRAM, "image"); bgimage_program_layout.opacity_location = get_uniform_location(BGIMAGE_PROGRAM, "opacity"); bgimage_program_layout.sizes_location = get_uniform_location(BGIMAGE_PROGRAM, "sizes"); - bgimage_program_layout.rescaled_location = get_uniform_location(BGIMAGE_PROGRAM, "rescaled"); + bgimage_program_layout.adjust_scale_location = get_uniform_location(BGIMAGE_PROGRAM, "adjust_scale"); bgimage_program_layout.transform_location = get_uniform_location(BGIMAGE_PROGRAM, "transform"); bgimage_program_layout.premult_location = get_uniform_location(BGIMAGE_PROGRAM, "premult"); tint_program_layout.tint_color_location = get_uniform_location(TINT_PROGRAM, "tint_color"); @@ -408,26 +408,18 @@ draw_bg(OSWindow *w) { bind_program(BGIMAGE_PROGRAM); bind_vertex_array(blit_vertex_array); - const bool rescaled = OPT(background_image_layout) == TILING || OPT(background_image_layout) == MIRRORED || OPT(background_image_layout) == CLAMPED; + const bool adjust_scale = OPT(background_image_layout) == TILING || OPT(background_image_layout) == MIRRORED || OPT(background_image_layout) == CLAMPED; static bool bgimage_constants_set = false; if (!bgimage_constants_set) { glUniform1i(bgimage_program_layout.image_location, BGIMAGE_UNIT); glUniform1f(bgimage_program_layout.opacity_location, OPT(background_opacity)); - glUniform1f(bgimage_program_layout.rescaled_location, (GLfloat)rescaled); + glUniform1f(bgimage_program_layout.adjust_scale_location, (GLfloat)adjust_scale); bgimage_constants_set = true; } float pos_left_relative = 0.0f, pos_top_relative = 0.0f; - if (rescaled) { - if (OPT(background_image_anchor) == TOP || OPT(background_image_anchor) == CENTER || OPT(background_image_anchor) == BOTTOM) { - pos_left_relative = 0.5f; - } else if (OPT(background_image_anchor) == TOP_RIGHT || OPT(background_image_anchor) == RIGHT || OPT(background_image_anchor) == BOTTOM_RIGHT) { - pos_left_relative = 1.0f; - } - if (OPT(background_image_anchor) == LEFT || OPT(background_image_anchor) == CENTER || OPT(background_image_anchor) == RIGHT) { - pos_top_relative = 0.5f; - } else if (OPT(background_image_anchor) == BOTTOM_LEFT || OPT(background_image_anchor) == BOTTOM || OPT(background_image_anchor) == BOTTOM_RIGHT) { - pos_top_relative = 1.0f; - } + if (adjust_scale) { + pos_left_relative = OPT(background_image_anchor).x; + pos_top_relative = OPT(background_image_anchor).y; } glUniform2f(bgimage_program_layout.transform_location, (GLfloat)pos_left_relative, (GLfloat)pos_top_relative); glUniform4f(bgimage_program_layout.sizes_location,