Compare commits

..

3 Commits

Author SHA1 Message Date
d73275f04c Add line_from_origin function 2024-01-24 23:43:14 +00:00
f56e6b1bb1 Remove test code 2024-01-24 23:43:06 +00:00
c70f252349 Add absolute, radians and degrees math utilities 2024-01-24 23:42:40 +00:00
4 changed files with 23 additions and 7 deletions

View File

@ -4,8 +4,11 @@
#include "aliases/aliases.h" #include "aliases/aliases.h"
#define square(x) (x * x) #define square(x) (x * x)
#define absolute(x) (x < 0.0 ? x * -1.0 : x)
i32 min(i32 a, i32 b); i32 min(i32 a, i32 b);
i32 max(i32 a, i32 b); i32 max(i32 a, i32 b);
f64 radians(f64 degrees);
f64 degrees(f64 radians);
#endif // !MATH_UTILS_H #endif // !MATH_UTILS_H

View File

@ -76,11 +76,6 @@ i32 run_main_loop(void) {
i32 toolbox_button_x = (toolbox->width - BUTTON_WIDTH) / 2; i32 toolbox_button_x = (toolbox->width - BUTTON_WIDTH) / 2;
line ln = (line){
(point){80, 100},
(point){120, 300},
};
while (comp.running) { while (comp.running) {
while (SDL_PollEvent(&(comp.event))) { while (SDL_PollEvent(&(comp.event))) {
handle_ui_events(&(comp.windows[comp.active_window - 1]), &(comp.ctx), handle_ui_events(&(comp.windows[comp.active_window - 1]), &(comp.ctx),
@ -157,8 +152,6 @@ i32 run_main_loop(void) {
comp.nodes[i].colours); comp.nodes[i].colours);
} }
ln = ui_noodle(main_window, &(comp.ctx), ln, op_node_colours);
for (u64 i = 0; i < MAX_WINDOWS; ++i) { for (u64 i = 0; i < MAX_WINDOWS; ++i) {
swap_buffers(&(comp.windows[i])); swap_buffers(&(comp.windows[i]));
} }

View File

@ -1,5 +1,10 @@
#include "math_utils.h" #include "math_utils.h"
#include <math.h>
i32 min(i32 a, i32 b) { return a <= b ? a : b; } i32 min(i32 a, i32 b) { return a <= b ? a : b; }
i32 max(i32 a, i32 b) { return a >= b ? a : b; } i32 max(i32 a, i32 b) { return a >= b ? a : b; }
f64 radians(f64 degrees) { return degrees * M_PI / 180.0; }
f64 degrees(f64 radians) { return radians * 180.0 / M_PI; }

View File

@ -8,8 +8,10 @@
#include <stdlib.h> #include <stdlib.h>
#define NOODLE_HALF_WIDTH 2 #define NOODLE_HALF_WIDTH 2
#define DEFAULT_NOODLE_LENGTH 60
INTERNAL bool aabb(rect rec, i32 x, i32 y); INTERNAL bool aabb(rect rec, i32 x, i32 y);
INTERNAL line line_from_origin(point origin, f64 angle, i32 line_length);
void init_ui_ctx(ui_ctx *ctx) { void init_ui_ctx(ui_ctx *ctx) {
*ctx = (ui_ctx){0}; *ctx = (ui_ctx){0};
@ -239,3 +241,16 @@ INTERNAL bool aabb(rect rec, i32 x, i32 y) {
return x > rec.topleft.x && x <= rec.topleft.x + rec.w && y > rec.topleft.y && return x > rec.topleft.x && x <= rec.topleft.x + rec.w && y > rec.topleft.y &&
y <= rec.topleft.y + rec.h; y <= rec.topleft.y + rec.h;
} }
INTERNAL line line_from_origin(point origin, f64 angle, i32 line_length) {
f64 rad = radians(angle);
f64 direction = angle / absolute(angle) * -1;
i32 adjacent = line_length * cos(rad) * direction; // dx
i32 opposite = line_length * sin(rad) * direction; // dy
return (line){
(point){origin.x + adjacent, origin.y + opposite},
origin,
};
}