diff options
Diffstat (limited to 'examples/graph.c')
-rwxr-xr-x | examples/graph.c | 197 |
1 files changed, 98 insertions, 99 deletions
diff --git a/examples/graph.c b/examples/graph.c index 037635f..36c110f 100755 --- a/examples/graph.c +++ b/examples/graph.c @@ -361,135 +361,134 @@ void highlight_path(i64 src, i64 dst) { } } -i32 main(i32 argc, c8 **argv) { - (void) argc; - (void) argv; +b8 adding_edge = 0; +i64 adding_src = 0; +i64 adding_dst = 0; - platform = (Platform) { - .title = "Graph", - .frame_width = 960, - .frame_height = 720, - }; +b8 path_changed = 0; +i64 path_src = -1; +i64 path_dst = -1; - p_init(); +void update_and_render_frame(void) { + p_wait_events(); - add_node(100, 100); - add_node(300, 100); - add_node(120, 300); - - add_edge(0, 1); - add_edge(0, 2); - add_edge(1, 2); + // Input events - b8 adding_edge = 0; - i64 adding_src = 0; - i64 adding_dst = 0; + b8 hover_node = 0; - b8 path_changed = 0; - i64 path_src = -1; - i64 path_dst = -1; + for (i64 i = 0; i < MAX_NUM_NODES; ++i) + if (world.nodes[i].enabled) { + update_node(i); + if (world.nodes[i].hover) + hover_node = 1; + } - while (!platform.done) { - p_wait_events(); + for (i64 i = 0; i < MAX_NUM_EDGES; ++i) + if (world.edges[i].enabled) { + if (hover_node) + world.edges[i].hover = 0; + else + update_edge(i); + } - // Input events + if (platform.key_pressed[KEY_DELETE]) { + for (i64 i = 0; i < MAX_NUM_EDGES; ++i) + if (world.edges[i].enabled && world.edges[i].hover) + remove_edge(i); - b8 hover_node = 0; + for (i64 i = 0; i < MAX_NUM_NODES; ++i) + if (world.nodes[i].enabled && world.nodes[i].hover) + remove_node(i); + } + if (platform.key_pressed['1']) { for (i64 i = 0; i < MAX_NUM_NODES; ++i) - if (world.nodes[i].enabled) { - update_node(i); - if (world.nodes[i].hover) - hover_node = 1; + if (world.nodes[i].enabled && world.nodes[i].hover) { + path_src = i; + path_changed = 1; + break; } + } - for (i64 i = 0; i < MAX_NUM_EDGES; ++i) - if (world.edges[i].enabled) { - if (hover_node) - world.edges[i].hover = 0; - else - update_edge(i); + if (platform.key_pressed['2']) + for (i64 i = 0; i < MAX_NUM_NODES; ++i) + if (world.nodes[i].enabled && world.nodes[i].hover) { + path_dst = i; + path_changed = 1; + break; } - if (platform.key_pressed[KEY_DELETE]) { - for (i64 i = 0; i < MAX_NUM_EDGES; ++i) - if (world.edges[i].enabled && world.edges[i].hover) - remove_edge(i); + if (path_changed) { + highlight_path(path_src, path_dst); + path_changed = 0; + } - for (i64 i = 0; i < MAX_NUM_NODES; ++i) - if (world.nodes[i].enabled && world.nodes[i].hover) - remove_node(i); - } + if (platform.key_pressed[BUTTON_LEFT]) + add_node(platform.cursor_x, platform.cursor_y); - if (platform.key_pressed['1']) { - for (i64 i = 0; i < MAX_NUM_NODES; ++i) - if (world.nodes[i].enabled && world.nodes[i].hover) { - path_src = i; - path_changed = 1; - break; - } - } + if (platform.key_pressed[BUTTON_RIGHT]) + for (i64 i = 0; i < MAX_NUM_NODES; ++i) + if (world.nodes[i].enabled && world.nodes[i].hover) { + adding_edge = 1; + adding_src = i; + adding_dst = i; + break; + } - if (platform.key_pressed['2']) - for (i64 i = 0; i < MAX_NUM_NODES; ++i) - if (world.nodes[i].enabled && world.nodes[i].hover) { - path_dst = i; - path_changed = 1; - break; - } + if (adding_edge) + for (i64 i = 0; i < MAX_NUM_NODES; ++i) + if (world.nodes[i].enabled && world.nodes[i].hover) { + adding_dst = i; + break; + } - if (path_changed) { - highlight_path(path_src, path_dst); - path_changed = 0; - } + if (adding_edge && !platform.key_down[BUTTON_RIGHT]) { + adding_edge = 0; + add_edge(adding_src, adding_dst); + } - if (platform.key_pressed[BUTTON_LEFT]) - add_node(platform.cursor_x, platform.cursor_y); + // Render - if (platform.key_pressed[BUTTON_RIGHT]) - for (i64 i = 0; i < MAX_NUM_NODES; ++i) - if (world.nodes[i].enabled && world.nodes[i].hover) { - adding_edge = 1; - adding_src = i; - adding_dst = i; - break; - } + fill_rectangle(OP_SET, 0xffffff, 0, 0, platform.frame_width, platform.frame_height); - if (adding_edge) - for (i64 i = 0; i < MAX_NUM_NODES; ++i) - if (world.nodes[i].enabled && world.nodes[i].hover) { - adding_dst = i; - break; - } + if (adding_edge) { + f64 x0 = world.nodes[adding_src].x; + f64 y0 = world.nodes[adding_src].y; + f64 x1 = platform.cursor_x; + f64 y1 = platform.cursor_y; - if (adding_edge && !platform.key_down[BUTTON_RIGHT]) { - adding_edge = 0; - add_edge(adding_src, adding_dst); + if (adding_src != adding_dst) { + x1 = world.nodes[adding_dst].x; + y1 = world.nodes[adding_dst].y; } - // Render + fill_line(OP_SET, 0x7f007f, x0, y0, x1, y1, 30); + } - fill_rectangle(OP_SET, 0xffffff, 0, 0, platform.frame_width, platform.frame_height); + draw_graph(); - if (adding_edge) { - f64 x0 = world.nodes[adding_src].x; - f64 y0 = world.nodes[adding_src].y; - f64 x1 = platform.cursor_x; - f64 y1 = platform.cursor_y; + p_render_frame(); +} - if (adding_src != adding_dst) { - x1 = world.nodes[adding_dst].x; - y1 = world.nodes[adding_dst].y; - } +i32 main(i32 argc, c8 **argv) { + (void) argc; + (void) argv; - fill_line(OP_SET, 0x7f007f, x0, y0, x1, y1, 30); - } + platform = (Platform) { + .title = "Graph", + .frame_width = 960, + .frame_height = 720, + }; + + add_node(100, 100); + add_node(300, 100); + add_node(120, 300); - draw_graph(); + add_edge(0, 1); + add_edge(0, 2); + add_edge(1, 2); - p_render_frame(); - } + p_event_loop(); - p_cleanup(); return 0; } |