Compare commits

...

3 Commits

Author SHA1 Message Date
15c3b026a9 Correct include 2024-01-14 16:12:16 +00:00
13aadb4281 Add utility functions and fix bug when reallocating lines array 2024-01-14 02:44:35 +00:00
e4d3024364 Reorganise files 2024-01-14 02:17:11 +00:00
4 changed files with 43 additions and 15 deletions

View File

@@ -1,9 +1,9 @@
#!/bin/bash #!/bin/bash
CC=clang CC=clang
CFLAGS="-g -Wall -Iintern $(pkg-config --cflags sdl2)" CFLAGS="-g -Wall -Iinclude -Iintern $(pkg-config --cflags sdl2)"
LIBS="$(pkg-config --libs sdl2) -lm" LIBS="$(pkg-config --libs sdl2) -lm"
SRC=*.c SRC=src/*.c
OUT=main OUT=main
(set -x ; $CC $CFLAGS $LIBS $SRC -o $OUT) (set -x ; $CC $CFLAGS $LIBS $SRC -o $OUT)

View File

@@ -8,6 +8,22 @@
#define square(x) (x * x) #define square(x) (x * x)
typedef struct line line_t;
struct line {
point_t p0;
point_t p1;
};
typedef struct triangle triangle_t;
struct triangle {
point_t p0;
point_t p1;
point_t p2;
};
INTERNAL void draw_line(SDL_Renderer *renderer, line_t ln);
INTERNAL void draw_triangle(SDL_Renderer *renderer, triangle_t triangle);
bool init_arrows_arr(arrows_t *lines, u64 capacity) { bool init_arrows_arr(arrows_t *lines, u64 capacity) {
u64 size = capacity * sizeof(arrow_t); u64 size = capacity * sizeof(arrow_t);
@@ -36,6 +52,8 @@ void add_arrow(arrows_t *lines, arrow_t ln) {
lines->lines = ptr; lines->lines = ptr;
return; return;
} }
lines->capacity = new_capacity;
} }
lines->lines[(lines->count)++] = (arrow_t){ lines->lines[(lines->count)++] = (arrow_t){
@@ -57,8 +75,9 @@ INTERNAL f32 vec2_magnitude(const vec2_t *vec) {
return sqrtf((f32)vec->x * vec->x + (f32)vec->y * vec->y); return sqrtf((f32)vec->x * vec->x + (f32)vec->y * vec->y);
} }
INTERNAL void draw_arrow_head(SDL_Renderer *renderer, const arrow_t *arrow, INTERNAL triangle_t calculate_arrow_head(SDL_Renderer *renderer,
const point_t *arrow_end) { const arrow_t *arrow,
const point_t *arrow_end) {
// m = line_slope // m = line_slope
// dx = change_in_x // dx = change_in_x
// dy = change_in_y // dy = change_in_y
@@ -129,14 +148,7 @@ INTERNAL void draw_arrow_head(SDL_Renderer *renderer, const arrow_t *arrow,
arrow_base_p1.y = arrow_end->y + perpendicular_dy; arrow_base_p1.y = arrow_end->y + perpendicular_dy;
} }
SDL_RenderDrawLine(renderer, arrow_end->x, arrow_end->y, arrow_base_p0.x, return (triangle_t){arrow_tip, arrow_base_p0, arrow_base_p1};
arrow_base_p0.y);
SDL_RenderDrawLine(renderer, arrow_end->x, arrow_end->y, arrow_base_p1.x,
arrow_base_p1.y);
SDL_RenderDrawLine(renderer, arrow_tip.x, arrow_tip.y, arrow_base_p0.x,
arrow_base_p0.y);
SDL_RenderDrawLine(renderer, arrow_tip.x, arrow_tip.y, arrow_base_p1.x,
arrow_base_p1.y);
} }
void draw_arrow(SDL_Renderer *renderer, const arrow_t *arrow, colour_t colour) { void draw_arrow(SDL_Renderer *renderer, const arrow_t *arrow, colour_t colour) {
@@ -147,10 +159,26 @@ void draw_arrow(SDL_Renderer *renderer, const arrow_t *arrow, colour_t colour) {
arrow->origin.x + arrow->direction.x, arrow->origin.x + arrow->direction.x,
arrow->origin.y + arrow->direction.y, arrow->origin.y + arrow->direction.y,
}; };
line_t line = (line_t){arrow->origin, end};
if (vec2_magnitude(&(arrow->direction)) != 0) { if (vec2_magnitude(&(arrow->direction)) != 0) {
draw_arrow_head(renderer, arrow, &end); triangle_t triangle = calculate_arrow_head(renderer, arrow, &end);
draw_triangle(renderer, triangle);
} }
SDL_RenderDrawLine(renderer, arrow->origin.x, arrow->origin.y, end.x, end.y); draw_line(renderer, line);
}
INTERNAL void draw_line(SDL_Renderer *renderer, line_t ln) {
SDL_RenderDrawLine(renderer, ln.p0.x, ln.p0.y, ln.p1.x, ln.p1.y);
}
INTERNAL void draw_triangle(SDL_Renderer *renderer, triangle_t triangle) {
line_t ln0 = (line_t){triangle.p0, triangle.p1};
line_t ln1 = (line_t){triangle.p0, triangle.p2};
line_t ln2 = (line_t){triangle.p1, triangle.p2};
draw_line(renderer, ln0);
draw_line(renderer, ln1);
draw_line(renderer, ln2);
} }

View File

@@ -1,8 +1,8 @@
#include "SDL_mouse.h"
#include "aliases/aliases.h" #include "aliases/aliases.h"
#include "drawing.h" #include "drawing.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <SDL2/SDL_events.h> #include <SDL2/SDL_events.h>
#include <SDL2/SDL_mouse.h>
#include <SDL2/SDL_render.h> #include <SDL2/SDL_render.h>
#include <SDL2/SDL_video.h> #include <SDL2/SDL_video.h>
#include <stdbool.h> #include <stdbool.h>