fix: shrink buffers when unused fraction gets too large

This commit is contained in:
2026-03-10 14:48:55 +00:00
parent 2d6746c99c
commit 143a33558d
9 changed files with 229 additions and 215 deletions

View File

@@ -30,7 +30,7 @@ void vektor_uictrl_init(GtkApplication* app, VektorWidgetState* stateOut) {
GtkIconTheme* theme =
gtk_icon_theme_get_for_display(gdk_display_get_default());
/*if (gtk_icon_theme_has_icon(theme, "vektor-circle-symbolic"))
g_print("GTK sees it!\n");
else
@@ -63,11 +63,11 @@ void vektor_uictrl_init(GtkApplication* app, VektorWidgetState* stateOut) {
stateOut->workspaceColorPicker =
VEKTOR_COLOR_WHEEL(gtk_builder_get_object(builder, "color_picker"));
stateOut->sidepanelEntryR =
stateOut->sidepanelEntryR =
GTK_ENTRY(gtk_builder_get_object(builder, "spin_color_r"));
stateOut->sidepanelEntryG =
stateOut->sidepanelEntryG =
GTK_ENTRY(gtk_builder_get_object(builder, "spin_color_g"));
stateOut->sidepanelEntryB =
stateOut->sidepanelEntryB =
GTK_ENTRY(gtk_builder_get_object(builder, "spin_color_b"));
// Set window properties

View File

@@ -92,16 +92,24 @@ static void init_shader(void) {
g_error("Failed to load shader files");
standard_shader_program = create_shader_program(frag_src, vert_src);
selection_shader_program = create_shader_program(selection_frag_src, vert_src);
selection_shader_program =
create_shader_program(selection_frag_src, vert_src);
shader_standard_uProjMatrixLoc = glGetUniformLocation(standard_shader_program, "uProjection");
shader_selection_uProjMatrixLoc = glGetUniformLocation(selection_shader_program, "uProjection");
shader_selection_uTimeLoc = glGetUniformLocation(selection_shader_program, "uTime");
shader_selection_uC1Loc = glGetUniformLocation(selection_shader_program, "uColor1");
shader_selection_uC2Loc = glGetUniformLocation(selection_shader_program, "uColor2");
shader_standard_uProjMatrixLoc =
glGetUniformLocation(standard_shader_program, "uProjection");
shader_selection_uProjMatrixLoc =
glGetUniformLocation(selection_shader_program, "uProjection");
shader_selection_uTimeLoc =
glGetUniformLocation(selection_shader_program, "uTime");
shader_selection_uC1Loc =
glGetUniformLocation(selection_shader_program, "uColor1");
shader_selection_uC2Loc =
glGetUniformLocation(selection_shader_program, "uColor2");
shader_selection_uMinLoc = glGetUniformLocation(selection_shader_program, "uMin");
shader_selection_uMaxLoc = glGetUniformLocation(selection_shader_program, "uMax");
shader_selection_uMinLoc =
glGetUniformLocation(selection_shader_program, "uMin");
shader_selection_uMaxLoc =
glGetUniformLocation(selection_shader_program, "uMax");
if (shader_selection_uMinLoc == -1 || shader_selection_uMaxLoc == -1)
g_warning("Selection shader: uMin/uMax uniform not found in shader!");
@@ -126,41 +134,38 @@ static void init_geometry(void) {
glBindVertexArray(0);
}
static gboolean render(GtkGLArea* a, GdkGLContext* ctx, VektorCanvasRenderInfo* renderInfo) {
static gboolean render(GtkGLArea* a, GdkGLContext* ctx,
VektorCanvasRenderInfo* renderInfo) {
vb.count = 0;
vektor_rasterize(&vb, renderInfo->shapes);
size_t shape_vertex_count = vb.count; // remember how many vertices belong to shapes
size_t shape_vertex_count =
vb.count; // remember how many vertices belong to shapes
// create selection quad if a shape is selected
if(renderInfo->selectedShape != NULL &&
if (renderInfo->selectedShape != NULL &&
*(renderInfo->selectedShape) != NULL) {
VektorBBox bbox = vektor_primitive_get_bbox(
(*(renderInfo->selectedShape))->primitive
);
vektor_vb_add_quad(
&vb,
bbox.min,
bbox.max,
vektor_color_new(255,255,255,255)
);
(*(renderInfo->selectedShape))->primitive);
vektor_vb_add_quad(&vb, bbox.min, bbox.max,
vektor_color_new(255, 255, 255, 255));
}
glBufferData(GL_ARRAY_BUFFER, vb.count * sizeof(Vertex), vb.vertices,
GL_STATIC_DRAW);
// PASS 1 - draw shape vertices
glUseProgram(standard_shader_program);
float projectionMatrix[16] = {1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 1, 0, 0, 0, 0, 1};
glUniformMatrix4fv(shader_standard_uProjMatrixLoc, 1, GL_FALSE, projectionMatrix);
glUniformMatrix4fv(shader_standard_uProjMatrixLoc, 1, GL_FALSE,
projectionMatrix);
glBindVertexArray(vao);
glDisable(GL_CULL_FACE);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
@@ -168,26 +173,28 @@ static gboolean render(GtkGLArea* a, GdkGLContext* ctx, VektorCanvasRenderInfo*
// PASS 2 - draw selection quads
if (vb.count > shape_vertex_count) {
float time = (g_get_monotonic_time() - renderInfo->startupTime) / 10000000.0f;
float time =
(g_get_monotonic_time() - renderInfo->startupTime) / 10000000.0f;
// re-fetch bbox (we know a shape is selected)
VektorBBox bbox = vektor_primitive_get_bbox(
(*(renderInfo->selectedShape))->primitive
);
(*(renderInfo->selectedShape))->primitive);
glUseProgram(selection_shader_program);
float projectionMatrix[16] = {1, 0, 0, 0, 0, 1, 0, 0,
0, 0, 1, 0, 0, 0, 0, 1};
0, 0, 1, 0, 0, 0, 0, 1};
glUniformMatrix4fv(shader_selection_uProjMatrixLoc, 1, GL_FALSE, projectionMatrix);
glUniformMatrix4fv(shader_selection_uProjMatrixLoc, 1, GL_FALSE,
projectionMatrix);
glUniform1f(shader_selection_uTimeLoc, time);
glUniform2f(shader_selection_uMinLoc, bbox.min.x, bbox.min.y);
glUniform2f(shader_selection_uMaxLoc, bbox.max.x, bbox.max.y);
glUniform4f(shader_selection_uC1Loc, 0, 0, 0, 1);
glUniform4f(shader_selection_uC2Loc, 0.46, 0.46, 1, 1);
glDrawArrays(GL_TRIANGLES, shape_vertex_count, vb.count - shape_vertex_count);
glDrawArrays(GL_TRIANGLES, shape_vertex_count,
vb.count - shape_vertex_count);
}
glBindVertexArray(0);

View File

@@ -7,7 +7,6 @@
#include "src/core/primitives.h"
#include "uicontroller.h"
typedef struct VektorCanvas {
GtkGLArea* canvasWidget;
@@ -23,7 +22,7 @@ typedef struct VektorCanvas {
typedef struct VektorCanvasRenderInfo {
gint64 startupTime;
VektorShapeBuffer* shapes;
// a pointer to appstate->selectedShape
VektorShape** selectedShape;
} VektorCanvasRenderInfo;

View File

@@ -21,82 +21,76 @@ struct _VektorColorWheel {
G_DEFINE_TYPE(VektorColorWheel, vektor_color_wheel, GTK_TYPE_DRAWING_AREA)
static gboolean point_in_triangle(
double px, double py,
double ax, double ay,
double bx, double by,
double cx, double cy,
double* u, double* v, double* w)
{
double denom =
(by - cy)*(ax - cx) +
(cx - bx)*(ay - cy);
static gboolean point_in_triangle(double px, double py, double ax, double ay,
double bx, double by, double cx, double cy,
double* u, double* v, double* w) {
double denom = (by - cy) * (ax - cx) + (cx - bx) * (ay - cy);
*u =
((by - cy)*(px - cx) +
(cx - bx)*(py - cy)) / denom;
*u = ((by - cy) * (px - cx) + (cx - bx) * (py - cy)) / denom;
*v =
((cy - ay)*(px - cx) +
(ax - cx)*(py - cy)) / denom;
*v = ((cy - ay) * (px - cx) + (ax - cx) * (py - cy)) / denom;
*w = 1 - *u - *v;
return (*u >= 0 && *v >= 0 && *w >= 0);
}
static void closest_point_on_segment(
double px, double py,
double ax, double ay,
double bx, double by,
double *rx, double *ry)
{
static void closest_point_on_segment(double px, double py, double ax, double ay,
double bx, double by, double* rx,
double* ry) {
double abx = bx - ax;
double aby = by - ay;
double apx = px - ax;
double apy = py - ay;
double t = (apx*abx + apy*aby) / (abx*abx + aby*aby);
double t = (apx * abx + apy * aby) / (abx * abx + aby * aby);
if (t < 0) t = 0;
if (t > 1) t = 1;
if (t < 0)
t = 0;
if (t > 1)
t = 1;
*rx = ax + abx * t;
*ry = ay + aby * t;
}
static void closest_point_on_triangle(
double px, double py,
double ax, double ay,
double bx, double by,
double cx, double cy,
double *rx, double *ry)
{
double p1x,p1y;
double p2x,p2y;
double p3x,p3y;
static void closest_point_on_triangle(double px, double py, double ax,
double ay, double bx, double by,
double cx, double cy, double* rx,
double* ry) {
double p1x, p1y;
double p2x, p2y;
double p3x, p3y;
closest_point_on_segment(px,py, ax,ay, bx,by, &p1x,&p1y);
closest_point_on_segment(px,py, bx,by, cx,cy, &p2x,&p2y);
closest_point_on_segment(px,py, cx,cy, ax,ay, &p3x,&p3y);
closest_point_on_segment(px, py, ax, ay, bx, by, &p1x, &p1y);
closest_point_on_segment(px, py, bx, by, cx, cy, &p2x, &p2y);
closest_point_on_segment(px, py, cx, cy, ax, ay, &p3x, &p3y);
double d1 = (px-p1x)*(px-p1x) + (py-p1y)*(py-p1y);
double d2 = (px-p2x)*(px-p2x) + (py-p2y)*(py-p2y);
double d3 = (px-p3x)*(px-p3x) + (py-p3y)*(py-p3y);
double d1 = (px - p1x) * (px - p1x) + (py - p1y) * (py - p1y);
double d2 = (px - p2x) * (px - p2x) + (py - p2y) * (py - p2y);
double d3 = (px - p3x) * (px - p3x) + (py - p3y) * (py - p3y);
if (d1 <= d2 && d1 <= d3) { *rx = p1x; *ry = p1y; }
else if (d2 <= d3) { *rx = p2x; *ry = p2y; }
else { *rx = p3x; *ry = p3y; }
if (d1 <= d2 && d1 <= d3) {
*rx = p1x;
*ry = p1y;
} else if (d2 <= d3) {
*rx = p2x;
*ry = p2y;
} else {
*rx = p3x;
*ry = p3y;
}
}
static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot) {
static void vektor_color_wheel_snapshot(GtkWidget* widget,
GtkSnapshot* snapshot) {
VektorColorWheel* self = VEKTOR_COLOR_WHEEL(widget);
int width = gtk_widget_get_width(widget);
int height = gtk_widget_get_height(widget);
graphene_rect_t bounds = GRAPHENE_RECT_INIT(0,0,width,height);
graphene_rect_t bounds = GRAPHENE_RECT_INIT(0, 0, width, height);
cairo_t* cr = gtk_snapshot_append_cairo(snapshot, &bounds);
double cx = width / 2.0;
@@ -109,8 +103,8 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
double triangle_radius = wheel_radius * 0.75;
// wheel draw
for(int a = 0; a < 360; a++) {
double angle_1 = a*(M_PI / 180.0);
for (int a = 0; a < 360; a++) {
double angle_1 = a * (M_PI / 180.0);
double angle_2 = (a + 1) * (M_PI / 180.0);
cairo_new_path(cr);
@@ -118,7 +112,7 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
cairo_arc_negative(cr, cx, cy, inner_radius, angle_2, angle_1);
cairo_close_path(cr);
float r,g,b;
float r, g, b;
gtk_hsv_to_rgb(a / 360.0, 1.0, 1.0, &r, &g, &b);
cairo_set_source_rgb(cr, r, g, b);
@@ -126,7 +120,7 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
}
// triangle draw
double ax = cx + triangle_radius;
double ax = cx + triangle_radius;
double ay = cy;
double bx = cx - 0.5 * triangle_radius;
@@ -153,16 +147,20 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
// White gradient: from pure hue (right) → white (bottom)
cairo_pattern_t* white = cairo_pattern_create_linear(ax, ay, bx, by);
cairo_pattern_add_color_stop_rgba(white, 0.0, 1,1,1, 0.0); // transparent at pure hue
cairo_pattern_add_color_stop_rgba(white, 1.0, 1,1,1, 1.0); // opaque white at bottom
cairo_pattern_add_color_stop_rgba(white, 0.0, 1, 1, 1,
0.0); // transparent at pure hue
cairo_pattern_add_color_stop_rgba(white, 1.0, 1, 1, 1,
1.0); // opaque white at bottom
cairo_set_source(cr, white);
cairo_paint(cr);
cairo_pattern_destroy(white);
// Black gradient: from pure hue (right) → black (top)
cairo_pattern_t* black = cairo_pattern_create_linear(ax, ay, cx2, cy2);
cairo_pattern_add_color_stop_rgba(black, 0.0, 0,0,0, 0.0); // transparent at pure hue
cairo_pattern_add_color_stop_rgba(black, 1.0, 0,0,0, 1.0); // opaque black at top
cairo_pattern_add_color_stop_rgba(black, 0.0, 0, 0, 0,
0.0); // transparent at pure hue
cairo_pattern_add_color_stop_rgba(black, 1.0, 0, 0, 0,
1.0); // opaque black at top
cairo_set_source(cr, black);
cairo_paint(cr);
cairo_pattern_destroy(black);
@@ -170,26 +168,26 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
cairo_restore(cr);
// triangle outline
cairo_set_source_rgb(cr,.1,.1,.1);
cairo_set_source_rgb(cr, .1, .1, .1);
cairo_move_to(cr, ax, ay);
cairo_line_to(cr, bx, by);
cairo_line_to(cr, cx2, cy2);
cairo_close_path(cr);
cairo_set_source_rgb(cr,.1,.1,.1);
cairo_set_source_rgb(cr, .1, .1, .1);
cairo_stroke(cr);
// selectors draw
// triangle selector
double chroma_weight = self->saturation * self->lightness;
double white_weight = (1.0 - self->saturation) * self->lightness;
double black_weight = 1.0 - self->lightness;
double white_weight = (1.0 - self->saturation) * self->lightness;
double black_weight = 1.0 - self->lightness;
double px = ax * chroma_weight + bx * white_weight + cx2 * black_weight;
double py = ay * chroma_weight + by * white_weight + cy2 * black_weight;
cairo_arc(cr, px, py, 5, 0, 2*M_PI);
cairo_arc(cr, px, py, 5, 0, 2 * M_PI);
float fr, fg, fb;
vektor_color_wheel_get_colorout(self, &fr, &fg, &fb);
@@ -205,17 +203,12 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
cairo_new_path(cr);
cairo_arc(cr,
cx, cy,
wheel_radius,
selector_angle - selector_width,
selector_angle + selector_width);
cairo_arc(cr, cx, cy, wheel_radius, selector_angle - selector_width,
selector_angle + selector_width);
cairo_arc_negative(cr,
cx, cy,
inner_radius,
selector_angle + selector_width,
selector_angle - selector_width);
cairo_arc_negative(cr, cx, cy, inner_radius,
selector_angle + selector_width,
selector_angle - selector_width);
cairo_close_path(cr);
@@ -225,7 +218,8 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
cairo_destroy(cr);
}
static void on_click(GtkGestureClick* gesture, int n_press, double x, double y, gpointer data) {
static void on_click(GtkGestureClick* gesture, int n_press, double x, double y,
gpointer data) {
VektorColorWheel* wheel = VEKTOR_COLOR_WHEEL(data);
GtkWidget* widget = GTK_WIDGET(wheel);
@@ -240,7 +234,7 @@ static void on_click(GtkGestureClick* gesture, int n_press, double x, double y,
double cx = width / 2.0;
double cy = height / 2.0;
double ax = cx + triangle_radius;
double ax = cx + triangle_radius;
double ay = cy;
double bx = cx - 0.5 * triangle_radius;
@@ -249,33 +243,28 @@ static void on_click(GtkGestureClick* gesture, int n_press, double x, double y,
double cx2 = cx - 0.5 * triangle_radius;
double cy2 = cy - 0.866 * triangle_radius;
double u,v,w;
gboolean inside = point_in_triangle(x,y, ax,ay, bx,by, cx2,cy2, &u,&v,&w);
double u, v, w;
gboolean inside =
point_in_triangle(x, y, ax, ay, bx, by, cx2, cy2, &u, &v, &w);
if(wheel->dragging_triangle) {
if (wheel->dragging_triangle) {
if(!inside) { // if outside triangle, snap to its edge
double sx,sy;
if (!inside) { // if outside triangle, snap to its edge
double sx, sy;
closest_point_on_triangle(
x,y,
ax,ay,
bx,by,
cx2,cy2,
&sx,&sy
);
closest_point_on_triangle(x, y, ax, ay, bx, by, cx2, cy2, &sx, &sy);
x = sx;
y = sy;
point_in_triangle(x,y, ax,ay, bx,by, cx2,cy2, &u,&v,&w);
point_in_triangle(x, y, ax, ay, bx, by, cx2, cy2, &u, &v, &w);
}
double denom = u + v;
if (denom > 0.0001) { // avoid div-by-zero at black vertex
if (denom > 0.0001) { // avoid div-by-zero at black vertex
wheel->saturation = u / denom;
} else {
wheel->saturation = 0.0; // arbitrary, since S irrelevant at V=0
wheel->saturation = 0.0; // arbitrary, since S irrelevant at V=0
}
wheel->lightness = denom;
@@ -284,27 +273,30 @@ static void on_click(GtkGestureClick* gesture, int n_press, double x, double y,
double dy = y - cy;
double angle = atan2(dy, dx);
if(angle < 0) { angle += 2 * M_PI; }
if (angle < 0) {
angle += 2 * M_PI;
}
wheel->hue = angle / (2*M_PI);
wheel->hue = angle / (2 * M_PI);
}
g_signal_emit(wheel, signals[COLOR_CHANGED], 0);
gtk_widget_queue_draw(widget);
}
static void on_drag(GtkGestureDrag* gesture, double offset_x, double offset_y, gpointer data) {
double x,y;
gtk_gesture_drag_get_start_point(gesture,&x,&y);
static void on_drag(GtkGestureDrag* gesture, double offset_x, double offset_y,
gpointer data) {
double x, y;
gtk_gesture_drag_get_start_point(gesture, &x, &y);
x += offset_x;
y += offset_y;
on_click(NULL,0,x,y,data);
on_click(NULL, 0, x, y, data);
}
static void on_drag_begin(GtkGestureDrag* gesture, gdouble start_x, gdouble start_y, gpointer user_data) {
static void on_drag_begin(GtkGestureDrag* gesture, gdouble start_x,
gdouble start_y, gpointer user_data) {
// set dragging_wheel or dragging_triangle which are used
// to determine to where the cursor should snap to
@@ -319,13 +311,13 @@ static void on_drag_begin(GtkGestureDrag* gesture, gdouble start_x, gdouble star
double dx = start_x - cx;
double dy = start_y - cy;
double dist = sqrt(dx*dx+dy*dy);
double dist = sqrt(dx * dx + dy * dy);
double outer_radius = MIN(width, height) / 2.0;
double wheel_radius = outer_radius * 0.95;
double inner_radius = wheel_radius * 0.9;
if(dist > inner_radius) {
if (dist > inner_radius) {
wheel->dragging_wheel = TRUE;
wheel->dragging_triangle = FALSE;
} else if (dist < inner_radius) {
@@ -355,17 +347,8 @@ static void vektor_color_wheel_class_init(VektorColorWheelClass* klass) {
widget_class->snapshot = vektor_color_wheel_snapshot;
signals[COLOR_CHANGED] =
g_signal_new(
"color-changed",
G_TYPE_FROM_CLASS(klass),
G_SIGNAL_RUN_FIRST,
0,
NULL,
NULL,
NULL,
G_TYPE_NONE,
0
);
g_signal_new("color-changed", G_TYPE_FROM_CLASS(klass),
G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
}
GtkWidget* vektor_color_wheel_new(void) {
@@ -373,30 +356,23 @@ GtkWidget* vektor_color_wheel_new(void) {
}
VektorColor vektor_color_wheel_get_color(VektorColorWheel* wheel) {
float r,g,b;
gtk_hsv_to_rgb(wheel->hue,
wheel->saturation,
wheel->lightness,
&r, &g, &b);
return (VektorColor) {
.r = (unsigned char)(r*255),
.g = (unsigned char)(g*255),
.b = (unsigned char)(b*255) ,
.a = 255
};
float r, g, b;
gtk_hsv_to_rgb(wheel->hue, wheel->saturation, wheel->lightness, &r, &g, &b);
return (VektorColor){.r = (unsigned char)(r * 255),
.g = (unsigned char)(g * 255),
.b = (unsigned char)(b * 255),
.a = 255};
}
void vektor_color_wheel_get_colorout(VektorColorWheel* wheel, float* r, float* g, float* b) {
gtk_hsv_to_rgb(wheel->hue,
wheel->saturation,
wheel->lightness,
r, g, b);
void vektor_color_wheel_get_colorout(VektorColorWheel* wheel, float* r,
float* g, float* b) {
gtk_hsv_to_rgb(wheel->hue, wheel->saturation, wheel->lightness, r, g, b);
}
void vektor_color_wheel_set_color(VektorColorWheel* wheel, VektorColor c) {
float h,s,v;
gtk_rgb_to_hsv(c.r/255.0, c.g/255.0, c.b/255.0, &h, &s, &v);
float h, s, v;
gtk_rgb_to_hsv(c.r / 255.0, c.g / 255.0, c.b / 255.0, &h, &s, &v);
wheel->hue = (float)h;
wheel->saturation = (float)s;
wheel->lightness = (float)v;

View File

@@ -5,11 +5,13 @@
#include "src/util/color.h"
#define VEKTOR_TYPE_COLOR_WHEEL vektor_color_wheel_get_type()
G_DECLARE_FINAL_TYPE(VektorColorWheel, vektor_color_wheel, VEKTOR, COLOR_WHEEL, GtkDrawingArea)
G_DECLARE_FINAL_TYPE(VektorColorWheel, vektor_color_wheel, VEKTOR, COLOR_WHEEL,
GtkDrawingArea)
GtkWidget* vektor_color_wheel_new(void);
VektorColor vektor_color_wheel_get_color(VektorColorWheel* wheel);
void vektor_color_wheel_get_colorout(VektorColorWheel* wheel, float* r, float* g, float* b);
void vektor_color_wheel_get_colorout(VektorColorWheel* wheel, float* r,
float* g, float* b);
void vektor_color_wheel_set_color(VektorColorWheel* wheel, VektorColor c);
#endif