Double buffer the nodes to avoid drawing lag

This commit is contained in:
Abdelrahman Said 2024-02-28 22:33:28 +00:00
parent 4ed86611b5
commit 46a4802ce6

View File

@ -25,15 +25,15 @@ struct compositor {
u32 active_window; u32 active_window;
SDL_Event event; SDL_Event event;
bool running; bool running;
i64 node_hovered;
u64 count; u64 count;
node *nodes; node *nodes;
bool move_node; node *back_nodes;
ui_ctx ctx; ui_ctx ctx;
}; };
internal void add_node(compositor *comp, node_type type, node_data data, internal void add_node(compositor *comp, node_type type, node_data data,
u64 inputs, i32 x, i32 y, ui_elem_colours colours); u64 inputs, i32 x, i32 y, ui_elem_colours colours);
internal void update_node_graph(compositor *comp, const window *wnd);
internal void draw_node_graph(compositor *comp, const window *wnd); internal void draw_node_graph(compositor *comp, const window *wnd);
i32 run_main_loop(void) { i32 run_main_loop(void) {
@ -47,6 +47,8 @@ i32 run_main_loop(void) {
mem_arena_init(&comp.arena, ARENA_CAPACITY); mem_arena_init(&comp.arena, ARENA_CAPACITY);
comp.nodes = (node *)mem_arena_alloc(comp.arena, sizeof(node) * MAX_NODES); comp.nodes = (node *)mem_arena_alloc(comp.arena, sizeof(node) * MAX_NODES);
comp.back_nodes =
(node *)mem_arena_alloc(comp.arena, sizeof(node) * MAX_NODES);
window *main_window = &(comp.windows[0]); window *main_window = &(comp.windows[0]);
window *toolbox = &(comp.windows[1]); window *toolbox = &(comp.windows[1]);
@ -126,6 +128,8 @@ i32 run_main_loop(void) {
} }
} }
update_node_graph(&comp, main_window);
for (u64 i = 0; i < MAX_WINDOWS; ++i) { for (u64 i = 0; i < MAX_WINDOWS; ++i) {
clear_window(&(comp.windows[i]), bg_colour); clear_window(&(comp.windows[i]), bg_colour);
} }
@ -174,7 +178,8 @@ internal void add_node(compositor *comp, node_type type, node_data data,
u64 alloc_size = inputs * sizeof(noodle); u64 alloc_size = inputs * sizeof(noodle);
noodle *noodles = mem_arena_alloc(comp->arena, alloc_size); noodle *noodles = mem_arena_alloc(comp->arena, alloc_size);
if (!noodles) { noodle *back_noodles = mem_arena_alloc(comp->arena, alloc_size);
if (!noodles || !back_noodles) {
return; return;
} }
@ -185,7 +190,9 @@ internal void add_node(compositor *comp, node_type type, node_data data,
.h = NODE_HEIGHT, .h = NODE_HEIGHT,
}; };
comp->nodes[++(comp->count)] = (node){ u64 idx = ++(comp->count);
comp->nodes[idx] = comp->back_nodes[idx] = (node){
.rec = rec, .rec = rec,
.colours = colours, .colours = colours,
.type = type, .type = type,
@ -193,32 +200,54 @@ internal void add_node(compositor *comp, node_type type, node_data data,
.inputs = inputs, .inputs = inputs,
.noodles = noodles, .noodles = noodles,
}; };
comp->back_nodes[idx].noodles = back_noodles;
}
internal void update_node_graph(compositor *comp, const window *wnd) {
for (u64 i = NODE_START; i <= comp->count; ++i) {
node *node_elem = &(comp->nodes[i]);
const node *back_node = &(comp->back_nodes[i]);
node_elem->rec = back_node->rec;
for (u64 j = 0; j < node_elem->inputs; ++j) {
noodle *ndl = &(node_elem->noodles[j]);
const noodle *back_ndl = &(back_node->noodles[j]);
ndl->connected_node = back_ndl->connected_node;
ndl->ln = back_ndl->ln;
}
}
} }
internal void draw_node_graph(compositor *comp, const window *wnd) { internal void draw_node_graph(compositor *comp, const window *wnd) {
for (u64 i = NODE_START; i <= comp->count; ++i) { for (u64 i = NODE_START; i <= comp->count; ++i) {
node *node_elem = &(comp->nodes[i]); node *node_elem = &(comp->nodes[i]);
node *back_node = &(comp->back_nodes[i]);
f64 angle = 90.0; f64 angle = 90.0;
f64 angle_delta = 25.0; f64 angle_delta = 25.0;
i64 delta_multiplier = node_elem->inputs % 2 == 0 ? -1 : 0; i64 delta_multiplier = node_elem->inputs % 2 == 0 ? -1 : 0;
for (u64 j = 0; j < comp->nodes[i].inputs; ++j) { for (u64 j = 0; j < node_elem->inputs; ++j) {
f64 new_angle = angle + angle_delta * delta_multiplier; f64 new_angle = angle + angle_delta * delta_multiplier;
noodle *ndl = &(node_elem->noodles[j]); noodle *ndl = &(node_elem->noodles[j]);
noodle *back_ndl = &(back_node->noodles[j]);
if (ndl->ln.p0.x == ndl->ln.p1.x && ndl->ln.p0.y == ndl->ln.p1.y) { if (ndl->ln.p0.x == ndl->ln.p1.x && ndl->ln.p0.y == ndl->ln.p1.y) {
point origin = {node_elem->rec.topleft.x + node_elem->rec.w / 2, point origin = {node_elem->rec.topleft.x + node_elem->rec.w / 2,
node_elem->rec.topleft.y + node_elem->rec.h / 2}; node_elem->rec.topleft.y + node_elem->rec.h / 2};
ndl->ln = line_from_origin(origin, new_angle, DEFAULT_NOODLE_LENGTH); back_ndl->ln =
line_from_origin(origin, new_angle, DEFAULT_NOODLE_LENGTH);
} }
switch (ui_noodle(wnd, &(comp->ctx), ndl->ln, node_elem->colours, switch (ui_noodle(wnd, &(comp->ctx), ndl->ln, node_elem->colours,
node_elem->rec)) { node_elem->rec)) {
case NOODLE_ACTION_DRAGGING: case NOODLE_ACTION_DRAGGING:
ndl->ln.p0.x += comp->ctx.rel_x; back_ndl->ln.p0.x += comp->ctx.rel_x;
ndl->ln.p0.y += comp->ctx.rel_y; back_ndl->ln.p0.y += comp->ctx.rel_y;
break; break;
case NOODLE_ACTION_RELEASED: { case NOODLE_ACTION_RELEASED: {
bool connected = false; bool connected = false;
@ -234,8 +263,8 @@ internal void draw_node_graph(compositor *comp, const window *wnd) {
point p0 = {nd->rec.topleft.x + nd->rec.w / 2, point p0 = {nd->rec.topleft.x + nd->rec.w / 2,
nd->rec.topleft.y + nd->rec.h / 2}; nd->rec.topleft.y + nd->rec.h / 2};
ndl->ln.p0 = p0; back_ndl->ln.p0 = p0;
ndl->connected_node = k; back_ndl->connected_node = k;
connected = true; connected = true;
break; break;
@ -243,8 +272,8 @@ internal void draw_node_graph(compositor *comp, const window *wnd) {
} }
if (!connected) { if (!connected) {
ndl->ln.p0 = ndl->ln.p1; back_ndl->ln.p0 = ndl->ln.p1;
ndl->connected_node = EMPTY_NODE; back_ndl->connected_node = EMPTY_NODE;
} }
break; break;
@ -265,19 +294,20 @@ internal void draw_node_graph(compositor *comp, const window *wnd) {
} }
if (ui_node(wnd, &(comp->ctx), node_elem->rec, node_elem->colours)) { if (ui_node(wnd, &(comp->ctx), node_elem->rec, node_elem->colours)) {
node_elem->rec.topleft.x += comp->ctx.rel_x; back_node->rec.topleft.x += comp->ctx.rel_x;
node_elem->rec.topleft.y += comp->ctx.rel_y; back_node->rec.topleft.y += comp->ctx.rel_y;
for (u64 j = 0; j < node_elem->inputs; ++j) { for (u64 j = 0; j < node_elem->inputs; ++j) {
noodle *ndl = &(node_elem->noodles[j]); noodle *ndl = &(node_elem->noodles[j]);
noodle *back_ndl = &(back_node->noodles[j]);
if (ndl->connected_node == EMPTY_NODE) { if (ndl->connected_node == EMPTY_NODE) {
ndl->ln.p0.x += comp->ctx.rel_x; back_ndl->ln.p0.x += comp->ctx.rel_x;
ndl->ln.p0.y += comp->ctx.rel_y; back_ndl->ln.p0.y += comp->ctx.rel_y;
} }
ndl->ln.p1.x += comp->ctx.rel_x; back_ndl->ln.p1.x += comp->ctx.rel_x;
ndl->ln.p1.y += comp->ctx.rel_y; back_ndl->ln.p1.y += comp->ctx.rel_y;
} }
for (u64 j = NODE_START; j <= comp->count; ++j) { for (u64 j = NODE_START; j <= comp->count; ++j) {
@ -285,7 +315,7 @@ internal void draw_node_graph(compositor *comp, const window *wnd) {
continue; continue;
} }
node *nd = &(comp->nodes[j]); node *nd = &(comp->back_nodes[j]);
if (nd->inputs == 0) { if (nd->inputs == 0) {
continue; continue;
} }