diff options
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/utils/include/engine/utils.h | 4 | ||||
| -rw-r--r-- | src/utils/include/engine/utils/fov.h | 6 | ||||
| -rw-r--r-- | src/utils/include/engine/utils/vector.h | 39 | ||||
| -rw-r--r-- | src/utils/src/fov.c | 28 | ||||
| -rw-r--r-- | src/utils/src/misc.c | 6 | ||||
| -rw-r--r-- | src/utils/src/vector.c | 32 |
7 files changed, 23 insertions, 94 deletions
diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 5e4d0d9..6acdba1 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -4,8 +4,8 @@ add_library(daw_utils src/hashmap.c src/misc.c src/stack.c - src/vector.c ) target_compile_options(daw_utils PUBLIC ${BUILD_OPTS}) target_include_directories(daw_utils PRIVATE ${DAW_INCLUDE_DIRS}) +target_link_libraries(daw_utils PRIVATE cglm) diff --git a/src/utils/include/engine/utils.h b/src/utils/include/engine/utils.h index aa1343b..5096aa4 100644 --- a/src/utils/include/engine/utils.h +++ b/src/utils/include/engine/utils.h @@ -6,7 +6,7 @@ extern "C" { #endif #include <engine/core/types.h> -#include <engine/utils/vector.h> +#include <cglm/ivec2.h> #define MIN(a, b) ((a < b) ? (a) : (b)) #define MAX(a, b) ((a > b) ? (a) : (b)) @@ -31,7 +31,7 @@ u32 hash(char* str); /* Masks surrounding tiles of a kernel size of 3x3 */ /* In reality we only need 9 bits for this, but I think I had a reason for using * i32 */ -i32* kernmap(const void* map, i32* dstmap, const v2_i32 mapsize, +i32* kernmap(const void* map, i32* dstmap, const ivec2 mapsize, predicate_t* predicate); /* Returns an index from the given weights. */ diff --git a/src/utils/include/engine/utils/fov.h b/src/utils/include/engine/utils/fov.h index 50df8fa..be64a9e 100644 --- a/src/utils/include/engine/utils/fov.h +++ b/src/utils/include/engine/utils/fov.h @@ -6,8 +6,8 @@ extern "C" { #endif #include <engine/core/types.h> -#include <engine/utils/vector.h> #include <stdbool.h> +#include <cglm/ivec2.h> /* `fov_shadowcast`: */ /* map: your 2D enum tile grid @@ -18,9 +18,9 @@ extern "C" { * distance to the source. range: visibility range/radius. src: 2D * point to calculate FOV from * */ -void fov_shadowcast(const void* map, const v2_i32 mapsize, +void fov_shadowcast(const void* map, const ivec2 mapsize, bool (*visblocking)(const void*), i32* lightmap, - const i32 range, v2_i32 src); + const i32 range, const ivec2 src); #ifdef __cplusplus } diff --git a/src/utils/include/engine/utils/vector.h b/src/utils/include/engine/utils/vector.h deleted file mode 100644 index 7f4c4a4..0000000 --- a/src/utils/include/engine/utils/vector.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef VECTOR_H -#define VECTOR_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include <engine/core/types.h> - -#include <stdbool.h> -#include <stdio.h> - -typedef struct { - i32 x; - i32 y; -} v2_i32; - -bool v2_i32_eq(const v2_i32 a, const v2_i32 b); - -v2_i32 v2_i32_add(v2_i32 a, v2_i32 b); -v2_i32 v2_i32_add_i(v2_i32 a, i32 b); -v2_i32 v2_i32_sub(v2_i32 a, v2_i32 b); -v2_i32 v2_i32_sub_i(v2_i32 a, i32 b); -v2_i32 v2_i32_div(v2_i32 a, v2_i32 b); -v2_i32 v2_i32_div_i(v2_i32 a, i32 b); -v2_i32 v2_i32_mul(v2_i32 a, v2_i32 b); -v2_i32 v2_i32_mul_i(v2_i32 a, i32 b); -v2_i32 v2_i32_mod(v2_i32 a, v2_i32 b); -v2_i32 v2_i32_mod_i(v2_i32 a, i32 b); -v2_i32 v2_i32_max(v2_i32 a, v2_i32 b); -v2_i32 v2_i32_min(v2_i32 a, v2_i32 b); -v2_i32 v2_i32_lerp(f32 dt, v2_i32 a, v2_i32 b); - -void v2_i32_fprintf(FILE* stream, v2_i32 a); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/utils/src/fov.c b/src/utils/src/fov.c index 6cd4c49..90df5af 100644 --- a/src/utils/src/fov.c +++ b/src/utils/src/fov.c @@ -3,9 +3,9 @@ #include <math.h> #include <stdint.h> -void fov_shadowcast_rec(const void* map, const v2_i32 mapsize, +void fov_shadowcast_rec(const void* map, const ivec2 mapsize, bool (*visblocking)(const void*), i32* lightmap, - const i32 range, v2_i32 src, const i32 row, f32 start, + const i32 range, ivec2 src, const i32 row, f32 start, const f32 end, const i8 xx, const i8 xy, const i8 yx, const i8 yy) { @@ -23,8 +23,8 @@ void fov_shadowcast_rec(const void* map, const v2_i32 mapsize, while (dx <= 0) { dx += 1; - const i32 mapx = src.x + dx * xx + dy * xy; - const i32 mapy = src.y + dx * yx + dy * yy; + const i32 mapx = src[0] + dx * xx + dy * xy; + const i32 mapy = src[1] + dx * yx + dy * yy; const f32 slope_l = (((f32)dx) - 0.5f) / (((f32)dy) + 0.5f); const f32 slope_r = (((f32)dx) + 0.5f) / (((f32)dy) - 0.5f); @@ -34,13 +34,13 @@ void fov_shadowcast_rec(const void* map, const v2_i32 mapsize, if (dx * dx + dy * dy < range_2) { /* set as visible */ - if (mapx >= 0 && mapx < (long)mapsize.x && mapy >= 0 && - mapy < (long)mapsize.y) { + if (mapx >= 0 && mapx < (long)mapsize[0] && mapy >= 0 && + mapy < (long)mapsize[1]) { // TODO: Calculate proper dist from source - i32 x_2 = (src.x - mapx) * (src.x - mapx); - i32 y_2 = (src.y - mapy) * (src.y - mapy); - lightmap[mapy * mapsize.x + mapx] = - MAX(lightmap[mapy * mapsize.x + mapx], + i32 x_2 = (src[0] - mapx) * (src[0] - mapx); + i32 y_2 = (src[1] - mapy) * (src[1] - mapy); + lightmap[mapy * mapsize[0] + mapx] = + MAX(lightmap[mapy * mapsize[0] + mapx], (i32)(range - sqrt((f64)(x_2 + y_2)))); } } @@ -50,7 +50,7 @@ void fov_shadowcast_rec(const void* map, const v2_i32 mapsize, const bool is_blocked = visblocking( (void*)((u64)map + sizeof(i32) /* ~ enum size */ - * (usize)(mapsize.x * mapy + mapx) /* index */ + * (usize)(mapsize[0] * mapy + mapx) /* index */ )); if (blocked) { @@ -74,9 +74,9 @@ void fov_shadowcast_rec(const void* map, const v2_i32 mapsize, /* http://www.roguebasin.com/index.php?title=FOV_using_recursive_shadowcasting */ -void fov_shadowcast(const void* map, const v2_i32 mapsize, +void fov_shadowcast(const void* map, const ivec2 mapsize, bool (*visblocking)(const void*), i32* lightmap, - const i32 range, const v2_i32 src) { + const i32 range, const ivec2 src) { const i8 m[4][8] = { {1, 0, 0, -1, -1, 0, 0, 1}, @@ -91,5 +91,5 @@ void fov_shadowcast(const void* map, const v2_i32 mapsize, } /* The center is the most lit square */ - lightmap[src.y * mapsize.x + src.x] = range; + lightmap[src[1] * mapsize[0] + src[0]] = range; } diff --git a/src/utils/src/misc.c b/src/utils/src/misc.c index 09094ec..063efbf 100644 --- a/src/utils/src/misc.c +++ b/src/utils/src/misc.c @@ -26,10 +26,10 @@ u32 hash(char* str) { /* Populates dstmap * on success: return pointer to dstmap * on failure: return NULL */ -i32* kernmap(const void* map, i32* dstmap, const v2_i32 mapsize, +i32* kernmap(const void* map, i32* dstmap, const ivec2 mapsize, predicate_t* predicate) { - const usize w = (usize)mapsize.x; - const usize h = (usize)mapsize.y; + const usize w = (usize)mapsize[0]; + const usize h = (usize)mapsize[1]; i32 mask[w * h]; if (w * h < 1) return NULL; diff --git a/src/utils/src/vector.c b/src/utils/src/vector.c deleted file mode 100644 index 5fedb1f..0000000 --- a/src/utils/src/vector.c +++ /dev/null @@ -1,32 +0,0 @@ -#include <engine/utils.h> -#include <engine/utils/vector.h> - -bool v2_i32_eq(const v2_i32 a, const v2_i32 b) { - return (a.x == b.x) && (a.y == b.y); -} -v2_i32 v2_i32_add(v2_i32 a, v2_i32 b) { return (v2_i32){a.x + b.x, a.y + b.y}; } -v2_i32 v2_i32_add_i(v2_i32 a, i32 b) { return (v2_i32){a.x + b, a.y + b}; } -v2_i32 v2_i32_sub(v2_i32 a, v2_i32 b) { return (v2_i32){a.x - b.x, a.y - b.y}; } -v2_i32 v2_i32_sub_i(v2_i32 a, i32 b) { return (v2_i32){a.x - b, a.y - b}; } -v2_i32 v2_i32_div(v2_i32 a, v2_i32 b) { return (v2_i32){a.x / b.x, a.y / b.y}; } -v2_i32 v2_i32_div_i(v2_i32 a, i32 b) { return (v2_i32){a.x / b, a.y / b}; } -v2_i32 v2_i32_mul(v2_i32 a, v2_i32 b) { return (v2_i32){a.x * b.x, a.y * b.y}; } -v2_i32 v2_i32_mul_i(v2_i32 a, i32 b) { return (v2_i32){a.x * b, a.y * b}; } -v2_i32 v2_i32_mod(v2_i32 a, v2_i32 b) { return (v2_i32){a.x % b.x, a.y % b.y}; } -v2_i32 v2_i32_mod_i(v2_i32 a, i32 b) { return (v2_i32){a.x % b, a.y % b}; } -v2_i32 v2_i32_max(v2_i32 a, v2_i32 b) { - return (v2_i32){MAX(a.x, b.x), MAX(a.y, b.y)}; -} -v2_i32 v2_i32_min(v2_i32 a, v2_i32 b) { - return (v2_i32){MIN(a.x, b.x), MIN(a.y, b.y)}; -} -v2_i32 v2_i32_lerp(f32 dt, v2_i32 a, v2_i32 b) { - return (v2_i32){ - .x = lerp(dt, (f32)a.x, (f32)b.x), - .y = lerp(dt, (f32)a.y, (f32)b.y), - }; -} - -void v2_i32_fprintf(FILE* stream, v2_i32 a) { - fprintf(stream, "<%d,%d>", a.x, a.y); -} |
