Compare commits
3 Commits
53d0a4698f
...
924ed544aa
Author | SHA1 | Date | |
---|---|---|---|
924ed544aa | |||
f72497e1b7 | |||
b329e0feb4 |
9
include/math_utils.h
Normal file
9
include/math_utils.h
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#ifndef MATH_UTILS_H
|
||||||
|
#define MATH_UTILS_H
|
||||||
|
|
||||||
|
#include "aliases/aliases.h"
|
||||||
|
|
||||||
|
i32 min(i32 a, i32 b);
|
||||||
|
i32 max(i32 a, i32 b);
|
||||||
|
|
||||||
|
#endif // !MATH_UTILS_H
|
@ -57,6 +57,7 @@ bool ui_button(const window *wnd, ui_ctx *ctx, rect rect,
|
|||||||
ui_elem_colours colours);
|
ui_elem_colours colours);
|
||||||
rect ui_node(const window *wnd, ui_ctx *ctx, rect rect,
|
rect ui_node(const window *wnd, ui_ctx *ctx, rect rect,
|
||||||
ui_elem_colours colours);
|
ui_elem_colours colours);
|
||||||
quad ui_quad(const window *wnd, ui_ctx *ctx, quad qd, ui_elem_colours colours);
|
line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||||
|
ui_elem_colours colours);
|
||||||
|
|
||||||
#endif // !UI_H
|
#endif // !UI_H
|
||||||
|
@ -76,6 +76,11 @@ 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){20, 40},
|
||||||
|
(point){60, 200},
|
||||||
|
};
|
||||||
|
|
||||||
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),
|
||||||
@ -152,6 +157,8 @@ 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]));
|
||||||
}
|
}
|
||||||
|
5
src/math_utils.c
Normal file
5
src/math_utils.c
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
#include "math_utils.h"
|
||||||
|
|
||||||
|
i32 min(i32 a, i32 b) { return a <= b ? a : b; }
|
||||||
|
|
||||||
|
i32 max(i32 a, i32 b) { return a >= b ? a : b; }
|
102
src/ui.c
102
src/ui.c
@ -1,7 +1,13 @@
|
|||||||
#include "ui.h"
|
#include "ui.h"
|
||||||
#include "SDL_events.h"
|
#include "SDL_events.h"
|
||||||
#include "aliases/aliases.h"
|
#include "aliases/aliases.h"
|
||||||
|
#include "math_utils.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define NOODLE_WIDTH 2
|
||||||
|
|
||||||
INTERNAL bool aabb(rect rec, i32 x, i32 y);
|
INTERNAL bool aabb(rect rec, i32 x, i32 y);
|
||||||
|
|
||||||
@ -126,63 +132,79 @@ rect ui_node(const window *wnd, ui_ctx *ctx, rect rec,
|
|||||||
return rec;
|
return rec;
|
||||||
}
|
}
|
||||||
|
|
||||||
quad ui_quad(const window *wnd, ui_ctx *ctx, quad qd, ui_elem_colours colours) {
|
line ui_noodle(const window *wnd, ui_ctx *ctx, line ln,
|
||||||
|
ui_elem_colours colours) {
|
||||||
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
|
if (ctx->count + 1 >= MAX_UI_ELEMENTS) {
|
||||||
return (quad){0};
|
return (line){0};
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 id = (ctx->count)++;
|
u64 id = (ctx->count)++;
|
||||||
|
|
||||||
fill_quad(wnd, qd, colours.fill);
|
bool horizontal = ln.p0.y == ln.p1.y;
|
||||||
|
|
||||||
|
rect bounding_box = (rect){0};
|
||||||
|
|
||||||
|
if (horizontal) {
|
||||||
|
i32 x = min(ln.p0.x, ln.p1.x);
|
||||||
|
|
||||||
|
bounding_box.topleft = (point){x, ln.p0.y};
|
||||||
|
bounding_box.w = abs(ln.p1.x - ln.p0.x);
|
||||||
|
bounding_box.h = NOODLE_WIDTH;
|
||||||
|
|
||||||
|
fill_rect(wnd, bounding_box, colours.fill);
|
||||||
|
} else {
|
||||||
|
quad qd = (quad){
|
||||||
|
.p0 = ln.p0,
|
||||||
|
.p1 = (point){ln.p0.x + NOODLE_WIDTH, ln.p0.y},
|
||||||
|
.p2 = ln.p1,
|
||||||
|
.p3 = (point){ln.p1.x + NOODLE_WIDTH, ln.p1.y},
|
||||||
|
};
|
||||||
|
|
||||||
|
fill_quad(wnd, qd, colours.fill);
|
||||||
|
|
||||||
|
i32 _min_x_1 = min(qd.p0.x, qd.p1.x);
|
||||||
|
i32 _min_x_2 = min(qd.p2.x, qd.p3.x);
|
||||||
|
i32 min_x = min(_min_x_1, _min_x_2);
|
||||||
|
|
||||||
|
i32 _max_x_1 = max(qd.p0.x, qd.p1.x);
|
||||||
|
i32 _max_x_2 = max(qd.p2.x, qd.p3.x);
|
||||||
|
i32 max_x = max(_max_x_1, _max_x_2);
|
||||||
|
|
||||||
|
i32 _min_y_1 = min(qd.p0.y, qd.p1.y);
|
||||||
|
i32 _min_y_2 = min(qd.p2.y, qd.p3.y);
|
||||||
|
i32 min_y = min(_min_y_1, _min_y_2);
|
||||||
|
|
||||||
|
i32 _max_y_1 = max(qd.p0.y, qd.p1.y);
|
||||||
|
i32 _max_y_2 = max(qd.p2.y, qd.p3.y);
|
||||||
|
i32 max_y = max(_max_y_1, _max_y_2);
|
||||||
|
|
||||||
|
bounding_box = (rect){
|
||||||
|
.topleft.x = min_x,
|
||||||
|
.topleft.y = min_y,
|
||||||
|
.w = max_x - min_x,
|
||||||
|
.h = max_y - min_y,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != id)) {
|
if (wnd != ctx->wnd || (ctx->active >= 0 && ctx->active != id)) {
|
||||||
return qd;
|
return ln;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->mouse_up) {
|
if (ctx->mouse_up) {
|
||||||
ctx->hovered = ctx->active = -1;
|
ctx->hovered = ctx->active = -1;
|
||||||
ctx->rel_x = ctx->rel_y = 0;
|
ctx->rel_x = ctx->rel_y = 0;
|
||||||
return qd;
|
return ln;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ctx->hovered == id && ctx->active == id) {
|
if (ctx->hovered == id && ctx->active == id) {
|
||||||
qd.p0.x += ctx->rel_x;
|
ln.p0.x += ctx->rel_x;
|
||||||
qd.p0.y += ctx->rel_y;
|
ln.p0.y += ctx->rel_y;
|
||||||
qd.p1.x += ctx->rel_x;
|
|
||||||
qd.p1.y += ctx->rel_y;
|
|
||||||
qd.p2.x += ctx->rel_x;
|
|
||||||
qd.p2.y += ctx->rel_y;
|
|
||||||
qd.p3.x += ctx->rel_x;
|
|
||||||
qd.p3.y += ctx->rel_y;
|
|
||||||
|
|
||||||
return qd;
|
return ln;
|
||||||
}
|
}
|
||||||
|
|
||||||
i32 _min_x_1 = qd.p0.x < qd.p1.x ? qd.p0.x : qd.p1.x;
|
if (!aabb(bounding_box, ctx->mouse_x, ctx->mouse_y)) {
|
||||||
i32 _min_x_2 = qd.p2.x < qd.p3.x ? qd.p2.x : qd.p3.x;
|
return ln;
|
||||||
i32 min_x = _min_x_1 < _min_x_2 ? _min_x_1 : _min_x_2;
|
|
||||||
|
|
||||||
i32 _max_x_1 = qd.p0.x > qd.p1.x ? qd.p0.x : qd.p1.x;
|
|
||||||
i32 _max_x_2 = qd.p2.x > qd.p3.x ? qd.p2.x : qd.p3.x;
|
|
||||||
i32 max_x = _max_x_1 > _max_x_2 ? _max_x_1 : _max_x_2;
|
|
||||||
|
|
||||||
i32 _min_y_1 = qd.p0.y < qd.p1.y ? qd.p0.y : qd.p1.y;
|
|
||||||
i32 _min_y_2 = qd.p2.y < qd.p3.y ? qd.p2.y : qd.p3.y;
|
|
||||||
i32 min_y = _min_y_1 < _min_y_2 ? _min_y_1 : _min_y_2;
|
|
||||||
|
|
||||||
i32 _max_y_1 = qd.p0.y > qd.p1.y ? qd.p0.y : qd.p1.y;
|
|
||||||
i32 _max_y_2 = qd.p2.y > qd.p3.y ? qd.p2.y : qd.p3.y;
|
|
||||||
i32 max_y = _max_y_1 > _max_y_2 ? _max_y_1 : _max_y_2;
|
|
||||||
|
|
||||||
rect rec = (rect){
|
|
||||||
.topleft.x = min_x,
|
|
||||||
.topleft.y = min_y,
|
|
||||||
.w = max_x - min_x,
|
|
||||||
.h = max_y - min_y,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!aabb(rec, ctx->mouse_x, ctx->mouse_y)) {
|
|
||||||
return qd;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx->hovered = id;
|
ctx->hovered = id;
|
||||||
@ -191,7 +213,7 @@ quad ui_quad(const window *wnd, ui_ctx *ctx, quad qd, ui_elem_colours colours) {
|
|||||||
ctx->active = id;
|
ctx->active = id;
|
||||||
}
|
}
|
||||||
|
|
||||||
return qd;
|
return ln;
|
||||||
}
|
}
|
||||||
|
|
||||||
INTERNAL bool aabb(rect rec, i32 x, i32 y) {
|
INTERNAL bool aabb(rect rec, i32 x, i32 y) {
|
||||||
|
61
src/window.c
61
src/window.c
@ -1,5 +1,6 @@
|
|||||||
#include "window.h"
|
#include "window.h"
|
||||||
#include "aliases/aliases.h"
|
#include "aliases/aliases.h"
|
||||||
|
#include "math_utils.h"
|
||||||
#include <SDL2/SDL_rect.h>
|
#include <SDL2/SDL_rect.h>
|
||||||
#include <SDL2/SDL_render.h>
|
#include <SDL2/SDL_render.h>
|
||||||
#include <SDL2/SDL_video.h>
|
#include <SDL2/SDL_video.h>
|
||||||
@ -89,42 +90,44 @@ void draw_triangle(const window *wnd, triangle triangle, colour colour) {
|
|||||||
draw_line(wnd, &ln2, colour);
|
draw_line(wnd, &ln2, colour);
|
||||||
}
|
}
|
||||||
|
|
||||||
INTERNAL inline i32 min(i32 a, i32 b, i32 c) {
|
INTERNAL inline bool inside_triangle(triangle tri, point p) {
|
||||||
i32 _min = a <= b ? a : b;
|
// Based on the following video:
|
||||||
return _min <= c ? _min : c;
|
// https://www.youtube.com/watch?v=HYAgJN3x4GA
|
||||||
|
f32 cy_min_ay = tri.p2.y - tri.p0.y;
|
||||||
|
f32 cx_min_ax = tri.p2.x - tri.p0.x;
|
||||||
|
f32 by_min_ay = tri.p1.y - tri.p0.y;
|
||||||
|
f32 bx_min_ax = tri.p1.x - tri.p0.x;
|
||||||
|
|
||||||
|
f32 w1 =
|
||||||
|
(tri.p0.x * cy_min_ay + (p.y - tri.p0.y) * cx_min_ax - p.x * cy_min_ay) /
|
||||||
|
(by_min_ay * cx_min_ax - bx_min_ax * cy_min_ay);
|
||||||
|
|
||||||
|
f32 w2 = (p.y - tri.p0.y - w1 * by_min_ay) / cy_min_ay;
|
||||||
|
|
||||||
|
return w1 >= 0.0f && w2 >= 0.0f && (w1 + w2) <= 1.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
INTERNAL inline i32 max(i32 a, i32 b, i32 c) {
|
void fill_triangle(const window *wnd, triangle tri, colour colour) {
|
||||||
i32 _max = a >= b ? a : b;
|
// Basic triangle filling algorithm inspired by
|
||||||
return _max >= c ? _max : c;
|
|
||||||
}
|
|
||||||
|
|
||||||
INTERNAL inline i32 half_space(i32 x1, i32 x2, i32 y1, i32 y2, i32 x, i32 y) {
|
|
||||||
return (x2 - x1) * (y - y1) - (y2 - y1) * (x - x1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void fill_triangle(const window *wnd, triangle triangle, colour colour) {
|
|
||||||
// Basic triangle filling algorithm from
|
|
||||||
// https://web.archive.org/web/20050408192410/http://sw-shader.sourceforge.net/rasterizer.html
|
// https://web.archive.org/web/20050408192410/http://sw-shader.sourceforge.net/rasterizer.html
|
||||||
i32 x1 = triangle.p0.x;
|
// but uses barycentric coordinates instead of half space
|
||||||
i32 x2 = triangle.p1.x;
|
i32 x1 = tri.p0.x;
|
||||||
i32 x3 = triangle.p2.x;
|
i32 x2 = tri.p1.x;
|
||||||
|
i32 x3 = tri.p2.x;
|
||||||
|
|
||||||
i32 y1 = triangle.p0.y;
|
i32 y1 = tri.p0.y;
|
||||||
i32 y2 = triangle.p1.y;
|
i32 y2 = tri.p1.y;
|
||||||
i32 y3 = triangle.p2.y;
|
i32 y3 = tri.p2.y;
|
||||||
|
|
||||||
// Find bounding rect
|
// Find bounding rect
|
||||||
i32 min_x = min(x1, x2, x3);
|
i32 min_x = min(min(x1, x2), x3);
|
||||||
i32 max_x = max(x1, x2, x3);
|
i32 max_x = max(max(x1, x2), x3);
|
||||||
i32 min_y = min(y1, y2, y3);
|
i32 min_y = min(min(y1, y2), y3);
|
||||||
i32 max_y = max(y1, y2, y3);
|
i32 max_y = max(max(y1, y2), y3);
|
||||||
|
|
||||||
for (i32 y = min_y; y < max_y; ++y) {
|
for (i32 y = min_y; y < max_y; ++y) {
|
||||||
for (i32 x = min_x; x < max_x; ++x) {
|
for (i32 x = min_x; x < max_x; ++x) {
|
||||||
if (half_space(x2, x1, y2, y1, x, y) >= 0 &&
|
if (inside_triangle(tri, (point){x, y})) {
|
||||||
half_space(x3, x2, y3, y2, x, y) >= 0 &&
|
|
||||||
half_space(x1, x3, y1, y3, x, y) >= 0) {
|
|
||||||
draw_point(wnd, (point){x, y}, colour);
|
draw_point(wnd, (point){x, y}, colour);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -144,8 +147,8 @@ void draw_quad(const window *wnd, quad qd, colour colour) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void fill_quad(const window *wnd, quad qd, colour colour) {
|
void fill_quad(const window *wnd, quad qd, colour colour) {
|
||||||
triangle t0 = (triangle){qd.p3, qd.p1, qd.p0};
|
triangle t0 = (triangle){qd.p0, qd.p1, qd.p2};
|
||||||
triangle t1 = (triangle){qd.p0, qd.p2, qd.p3};
|
triangle t1 = (triangle){qd.p1, qd.p2, qd.p3};
|
||||||
|
|
||||||
fill_triangle(wnd, t0, colour);
|
fill_triangle(wnd, t0, colour);
|
||||||
fill_triangle(wnd, t1, colour);
|
fill_triangle(wnd, t1, colour);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user