Isolate the node drawing code into separate files

This commit is contained in:
Abdelrahman Said 2024-01-15 00:00:00 +00:00
parent 95d9f5e5cf
commit c397bcd17d
3 changed files with 49 additions and 38 deletions

35
include/nodes.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef NODES_H
#define NODES_H
#include "window.h"
#define MAX_NODES 1024
#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;
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;
};
bool aabb(const node_t *node, i32 x, i32 y);
void draw_node(const window_t *wnd, const node_t *node);
#endif // !NODES_H

View File

@ -1,4 +1,5 @@
#include "aliases/aliases.h" #include "aliases/aliases.h"
#include "nodes.h"
#include "window.h" #include "window.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_events.h> #include <SDL2/SDL_events.h>
@ -12,33 +13,7 @@
#define WINDOW_WIDTH 1280 #define WINDOW_WIDTH 1280
#define WINDOW_HEIGHT 720 #define WINDOW_HEIGHT 720
#define MAX_NODES 1024
#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];
SDL_Event event; SDL_Event event;
@ -51,9 +26,7 @@ struct compositor {
bool move_node; 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 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) {
@ -140,11 +113,6 @@ 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) { void add_node(compositor_t *comp, const char *path, i32 x, i32 y) {
if (comp->count + 1 >= MAX_NODES) { if (comp->count + 1 >= MAX_NODES) {
return; return;
@ -161,8 +129,3 @@ void add_node(compositor_t *comp, const char *path, i32 x, i32 y) {
.path = path, .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);
}

13
src/nodes.c Normal file
View File

@ -0,0 +1,13 @@
#include "nodes.h"
#include "aliases/aliases.h"
#include <stdbool.h>
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 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);
}