Add initial node drawing

This commit is contained in:
Abdelrahman Said 2024-01-14 23:51:56 +00:00
parent 63312be847
commit fe8ff990c6

View File

@ -8,23 +8,58 @@
#include <stdlib.h> #include <stdlib.h>
#define MAX_WINDOWS 2 #define MAX_WINDOWS 2
#define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720
#define WINDOW_WIDTH 800 #define MAX_NODES 1024
#define WINDOW_HEIGHT 600 #define NODE_WIDTH 70
#define NODE_HEIGHT 20
#define IO_NODE_FILL_COLOUR ((colour_t){.abgr = 0xff2c84b7})
#define IO_NODE_BORDER_COLOUR ((colour_t){.abgr = 0xff122d5e})
#define OP_NODE_FILL_COLOUR ((colour_t){.abgr = 0xffad6c3a})
#define OP_NODE_BORDER_COLOUR ((colour_t){.abgr = 0xff592516})
typedef struct node node_t;
typedef struct compositor compositor_t; typedef struct compositor compositor_t;
enum comp_ops {
COMP_OP_ADD,
COMP_OP_SUB,
COMP_OP_MUL,
COMP_OP_DIV,
COUNT_COMP_OPS,
};
struct node {
rect_t rect;
const char *path;
};
struct compositor { struct compositor {
window_t windows[MAX_WINDOWS]; window_t windows[MAX_WINDOWS];
bool running;
SDL_Event event; SDL_Event event;
bool running;
u64 mouse_x;
u64 mouse_y;
i64 node_hovered;
u64 count;
node_t *nodes;
bool move_node;
}; };
bool aabb(const node_t *node, i32 x, i32 y);
void add_node(compositor_t *comp, const char *path, i32 x, i32 y);
void draw_node(const window_t *wnd, const node_t *node);
i32 run_main_loop(void) { i32 run_main_loop(void) {
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
return EXIT_FAILURE; return EXIT_FAILURE;
} }
compositor_t comp = {0}; compositor_t comp = {0};
comp.nodes = (node_t *)malloc(sizeof(node_t) * MAX_NODES);
window_t *main_window = &(comp.windows[0]); window_t *main_window = &(comp.windows[0]);
@ -39,7 +74,6 @@ i32 run_main_loop(void) {
SDL_EventState(SDL_DROPFILE, SDL_ENABLE); SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
colour_t bg_colour = {.abgr = 0xffffffff}; colour_t bg_colour = {.abgr = 0xffffffff};
colour_t fg_colour = {.abgr = 0xff000000};
while (comp.running) { while (comp.running) {
while (SDL_PollEvent(&(comp.event))) { while (SDL_PollEvent(&(comp.event))) {
@ -47,11 +81,55 @@ i32 run_main_loop(void) {
case SDL_QUIT: case SDL_QUIT:
comp.running = false; comp.running = false;
break; break;
case SDL_MOUSEBUTTONDOWN:
if (comp.node_hovered != -1) {
comp.move_node = true;
}
break;
case SDL_MOUSEBUTTONUP:
comp.move_node = false;
break;
case SDL_MOUSEMOTION:
comp.mouse_x = comp.event.motion.x;
comp.mouse_y = comp.event.motion.y;
if (comp.move_node) {
i32 dx = comp.event.motion.xrel;
i32 dy = comp.event.motion.yrel;
node_t *node = &(comp.nodes[comp.node_hovered]);
node->rect.topleft.x += dx;
node->rect.topleft.y += dy;
} else {
comp.node_hovered = -1;
for (u64 i = 0; i < comp.count; ++i) {
node_t *node = &(comp.nodes[i]);
if (aabb(node, comp.mouse_x, comp.mouse_y)) {
comp.node_hovered = i;
break;
}
}
}
break;
case SDL_DROPFILE:
add_node(&comp, comp.event.drop.file, comp.mouse_x, comp.mouse_y);
break;
} }
} }
clear_window(main_window, bg_colour); clear_window(main_window, bg_colour);
for (u64 i = 0; i < comp.count; ++i) {
node_t *node = &(comp.nodes[i]);
draw_node(main_window, node);
}
swap_buffers(main_window); swap_buffers(main_window);
} }
@ -59,3 +137,30 @@ i32 run_main_loop(void) {
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
bool aabb(const node_t *node, i32 x, i32 y) {
return x > node->rect.topleft.x && x <= node->rect.topleft.x + node->rect.w &&
y > node->rect.topleft.y && y <= node->rect.topleft.y + node->rect.h;
}
void add_node(compositor_t *comp, const char *path, i32 x, i32 y) {
if (comp->count + 1 >= MAX_NODES) {
return;
}
comp->nodes[(comp->count)++] = (node_t){
.rect =
(rect_t){
.topleft.x = x,
.topleft.y = y,
.w = NODE_WIDTH,
.h = NODE_HEIGHT,
},
.path = path,
};
}
void draw_node(const window_t *wnd, const node_t *node) {
fill_rect(wnd, &(node->rect), IO_NODE_FILL_COLOUR);
draw_rect(wnd, &(node->rect), IO_NODE_BORDER_COLOUR);
}