From a47cc44ef7191d03f8ca1b8d4e6b9dd34fba1807 Mon Sep 17 00:00:00 2001 From: realtradam Date: Fri, 16 Jun 2023 01:18:13 -0400 Subject: matrix math wrapper as well as various refactors for consistant and cleaner code --- include/rodeo.h | 33 ++++++++++++++-------------- include/rodeo/gfx_t.h | 4 ++-- include/rodeo/math.h | 17 +++++++++++++++ include/rodeo/math/mat4.h | 38 ++++++++++++++++++++++++++++++++ include/rodeo/math/mat4_t.h | 19 ++++++++++++++++ include/rodeo/math/rng.h | 48 ++++++++++++++++++++++++++++++++++++++++ include/rodeo/math/rng_t.h | 16 ++++++++++++++ include/rodeo/math/vec2.h | 53 +++++++++++++++++++++++++++++++++++++++++++++ include/rodeo/math/vec2_t.h | 17 +++++++++++++++ include/rodeo/math/vec3.h | 53 +++++++++++++++++++++++++++++++++++++++++++++ include/rodeo/math/vec3_t.h | 18 +++++++++++++++ include/rodeo/math_t.h | 5 +++++ include/rodeo/window.h | 4 ++-- 13 files changed, 304 insertions(+), 21 deletions(-) create mode 100644 include/rodeo/math.h create mode 100644 include/rodeo/math/mat4.h create mode 100644 include/rodeo/math/mat4_t.h create mode 100644 include/rodeo/math/rng.h create mode 100644 include/rodeo/math/rng_t.h create mode 100644 include/rodeo/math/vec2.h create mode 100644 include/rodeo/math/vec2_t.h create mode 100644 include/rodeo/math/vec3.h create mode 100644 include/rodeo/math/vec3_t.h create mode 100644 include/rodeo/math_t.h (limited to 'include') diff --git a/include/rodeo.h b/include/rodeo.h index f60d527..795d6f7 100644 --- a/include/rodeo.h +++ b/include/rodeo.h @@ -9,17 +9,10 @@ #include "rodeo/collision.h" #include "rodeo/gfx.h" #include "rodeo/window.h" - -// -- external -- -#include "stc/cstr.h" +#include "rodeo/math.h" // -- system -- -#include -#include #include -#include -#include -#include /// --- Math --- @@ -29,15 +22,6 @@ rodeo_color_RGBAFloat_to_RGBA8(const rodeo_color_RGBAFloat_t color); rodeo_color_RGBAFloat_t rodeo_color_RGBA8_to_RGBAFloat(const rodeo_color_RGBA8_t color); -void -rodeo_random_seed_set(uint64_t seed); - -double -rodeo_random_double_get(void); - -uint64_t -rodeo_random_uint64_get(void); - /// --- Core --- void @@ -48,3 +32,18 @@ rodeo_mainLoop_run( void rodeo_debug_text_draw(uint16_t x, uint16_t y, const char *format, ...); +// intialize all subsystems +void +rodeo_init(float width, float height, cstr window_name, uint32_t audio_channels); + +// deintialize all subsystems +void +rodeo_deinit(void); + +// macro to intialize/deinitialize all subsystems +#define \ +mrodeo_do(width, height, window_name, audio_channels) \ + mrodeo_defer_do( \ + rodeo_init(width, height, window_name, audio_channels), \ + rodeo_deinit() \ + ) diff --git a/include/rodeo/gfx_t.h b/include/rodeo/gfx_t.h index 9ed5f14..40ea58c 100644 --- a/include/rodeo/gfx_t.h +++ b/include/rodeo/gfx_t.h @@ -6,12 +6,12 @@ // -- system -- #include -typedef struct irodeo_gfx_texture_internal irodeo_gfx_texture_internal_t; +typedef struct irodeo_gfx_texture_2d irodeo_gfx_texture_2d_t; typedef struct { - irodeo_gfx_texture_internal_t *internal_texture; + irodeo_gfx_texture_2d_t *data; uint32_t width; uint32_t height; } diff --git a/include/rodeo/math.h b/include/rodeo/math.h new file mode 100644 index 0000000..3896fef --- /dev/null +++ b/include/rodeo/math.h @@ -0,0 +1,17 @@ +#pragma once + +// --- internal --- +// public +#include "rodeo/math_t.h" +#include "rodeo/math/vec2.h" +#include "rodeo/math/vec3.h" +#include "rodeo/math/mat4.h" +#include "rodeo/math/rng.h" + +#define mrodeo_math_pi 3.1415927410125732421875f + +float +rodeo_math_radians_to_turns(float radians); + +float +rodeo_math_turns_to_radians(float turns); diff --git a/include/rodeo/math/mat4.h b/include/rodeo/math/mat4.h new file mode 100644 index 0000000..5dbc872 --- /dev/null +++ b/include/rodeo/math/mat4.h @@ -0,0 +1,38 @@ +#pragma once + +// -- internal -- +// public +#include "rodeo/math/mat4_t.h" +#include "rodeo/math.h" + +rodeo_math_mat4_t +rodeo_math_mat4_identity(void); + +rodeo_math_mat4_t +rodeo_math_mat4_zero(void); + +rodeo_math_mat4_t +rodeo_math_mat4_transpose(rodeo_math_mat4_t m); + +rodeo_math_mat4_t +rodeo_math_mat4_translate(rodeo_math_mat4_t m, rodeo_math_vec3_t v); + +rodeo_math_mat4_t +rodeo_math_mat4_scale(rodeo_math_mat4_t m, rodeo_math_vec3_t v); + +rodeo_math_mat4_t +rodeo_math_mat4_rotate(rodeo_math_mat4_t m, float turns, rodeo_math_vec3_t v); + +rodeo_math_mat4_t +rodeo_math_mat4_multiply(rodeo_math_mat4_t a, rodeo_math_mat4_t b); + +rodeo_math_mat4_t +rodeo_math_mat4_orthographic( + float left, + float right, + float bottom, + float top, + float near, + float far +); + diff --git a/include/rodeo/math/mat4_t.h b/include/rodeo/math/mat4_t.h new file mode 100644 index 0000000..e00656c --- /dev/null +++ b/include/rodeo/math/mat4_t.h @@ -0,0 +1,19 @@ +#pragma once + +typedef +struct +{ + float m00, m01, m02, m03; + float m10, m11, m12, m13; + float m20, m21, m22, m23; + float m30, m31, m32, m33; +} +rodeo_math_mat4_val_t; + +typedef +union +{ + rodeo_math_mat4_val_t val; + float raw[4][4]; +} +rodeo_math_mat4_t; diff --git a/include/rodeo/math/rng.h b/include/rodeo/math/rng.h new file mode 100644 index 0000000..53f1f76 --- /dev/null +++ b/include/rodeo/math/rng.h @@ -0,0 +1,48 @@ +#pragma once + +// -- internal -- +#include "rodeo/math/rng_t.h" + +// -- system -- +#include + +void +rodeo_math_rng_init(void); + +void +rodeo_math_rng_deinit(void); + +rodeo_math_rng_generator_t +rodeo_math_rng_generator_create(uint64_t seed); + +void +rodeo_math_rng_generator_destroy(rodeo_math_rng_generator_t generator); + +double +rodeo_math_rng_double_get(rodeo_math_rng_generator_t generator); + +#define irodeo_math_rng_double_get_default() rodeo_math_rng_double_get((rodeo_math_rng_generator_t){0}) + +float +rodeo_math_rng_float_get(rodeo_math_rng_generator_t generator); + +#define irodeo_math_rng_float_get_default() rodeo_math_rng_float_get((rodeo_math_rng_generator_t){0}) + +uint64_t +rodeo_math_rng_uint64_get(rodeo_math_rng_generator_t generator); + +#define irodeo_math_rng_uint64_get_default() rodeo_math_rng_uint64_get((rodeo_math_rng_generator_t){0}) + +uint32_t +rodeo_math_rng_uint32_get(rodeo_math_rng_generator_t generator); + +#define irodeo_math_rng_uint32_get_default() rodeo_math_rng_uint32_get((rodeo_math_rng_generator_t){0}) + +#define \ +mrodeo_math_rng_do( \ +) \ + mrodeo_defer_do( \ + rodeo_math_rng_init(), \ + rodeo_math_rng_deinit() \ + ) + diff --git a/include/rodeo/math/rng_t.h b/include/rodeo/math/rng_t.h new file mode 100644 index 0000000..8c527ed --- /dev/null +++ b/include/rodeo/math/rng_t.h @@ -0,0 +1,16 @@ +#pragma once + +// -- system -- +#include + +typedef struct irodeo_math_rng_generator irodeo_math_rng_generator_t; + +typedef +struct +{ + // note: a seed value of '0' is reserved for when using the + // global seed is desired. + uint64_t seed; + irodeo_math_rng_generator_t *data; +} +rodeo_math_rng_generator_t; diff --git a/include/rodeo/math/vec2.h b/include/rodeo/math/vec2.h new file mode 100644 index 0000000..88d524f --- /dev/null +++ b/include/rodeo/math/vec2.h @@ -0,0 +1,53 @@ +#pragma once + +// -- internal -- +// public +#include "rodeo/math/vec2_t.h" + +rodeo_math_vec2_t +rodeo_math_vec2_zero(void); + +rodeo_math_vec2_t +rodeo_math_vec2_one(void); + +float +rodeo_math_vec2_dot(rodeo_math_vec2_t a, rodeo_math_vec2_t b); + +float +rodeo_math_vec2_cross(rodeo_math_vec2_t a, rodeo_math_vec2_t b); + +rodeo_math_vec2_t +rodeo_math_vec2_add(rodeo_math_vec2_t a, rodeo_math_vec2_t b); + +rodeo_math_vec2_t +rodeo_math_vec2_subtract(rodeo_math_vec2_t a, rodeo_math_vec2_t b); + +rodeo_math_vec2_t +rodeo_math_vec2_multiply(rodeo_math_vec2_t a, rodeo_math_vec2_t b); + +rodeo_math_vec2_t +rodeo_math_vec2_scale(rodeo_math_vec2_t a, float b); + +rodeo_math_vec2_t +rodeo_math_vec2_divide(rodeo_math_vec2_t a, rodeo_math_vec2_t b); + +rodeo_math_vec2_t +rodeo_math_vec2_negate(rodeo_math_vec2_t a); + +rodeo_math_vec2_t +rodeo_math_vec2_normalize(rodeo_math_vec2_t a); + +rodeo_math_vec2_t +rodeo_math_vec2_rotate(rodeo_math_vec2_t a, float turns); + +float +rodeo_math_vec2_distance(rodeo_math_vec2_t a, rodeo_math_vec2_t b); + +float +rodeo_math_vec2_distanceSq(rodeo_math_vec2_t a, rodeo_math_vec2_t b); + +rodeo_math_vec2_t +rodeo_math_vec2_clamp(rodeo_math_vec2_t a, float minimum, float maximum); + +rodeo_math_vec2_t +rodeo_math_vec2_lerp(rodeo_math_vec2_t from, rodeo_math_vec2_t to, float t); diff --git a/include/rodeo/math/vec2_t.h b/include/rodeo/math/vec2_t.h new file mode 100644 index 0000000..22717c6 --- /dev/null +++ b/include/rodeo/math/vec2_t.h @@ -0,0 +1,17 @@ +#pragma once + +typedef +struct +{ + float x; + float y; +} +rodeo_math_vec2_val_t; + +typedef +union +{ + rodeo_math_vec2_val_t val; + float raw[2]; +} +rodeo_math_vec2_t; diff --git a/include/rodeo/math/vec3.h b/include/rodeo/math/vec3.h new file mode 100644 index 0000000..1b9cd31 --- /dev/null +++ b/include/rodeo/math/vec3.h @@ -0,0 +1,53 @@ +#pragma once + +// -- internal -- +// public +#include "rodeo/math/vec3_t.h" + +rodeo_math_vec3_t +rodeo_math_vec3_zero(void); + +rodeo_math_vec3_t +rodeo_math_vec3_one(void); + +float +rodeo_math_vec3_dot(rodeo_math_vec3_t a, rodeo_math_vec3_t b); + +float +rodeo_math_vec3_cross(rodeo_math_vec3_t a, rodeo_math_vec3_t b); + +rodeo_math_vec3_t +rodeo_math_vec3_add(rodeo_math_vec3_t a, rodeo_math_vec3_t b); + +rodeo_math_vec3_t +rodeo_math_vec3_subtract(rodeo_math_vec3_t a, rodeo_math_vec3_t b); + +rodeo_math_vec3_t +rodeo_math_vec3_multiply(rodeo_math_vec3_t a, rodeo_math_vec3_t b); + +rodeo_math_vec3_t +rodeo_math_vec3_scale(rodeo_math_vec3_t a, float b); + +rodeo_math_vec3_t +rodeo_math_vec3_divide(rodeo_math_vec3_t a, rodeo_math_vec3_t b); + +rodeo_math_vec3_t +rodeo_math_vec3_negate(rodeo_math_vec3_t a); + +rodeo_math_vec3_t +rodeo_math_vec3_normalize(rodeo_math_vec3_t a); + +rodeo_math_vec3_t +rodeo_math_vec3_rotate(rodeo_math_vec3_t a, float turns, rodeo_math_vec3_t axis); + +float +rodeo_math_vec3_distance(rodeo_math_vec3_t a, rodeo_math_vec3_t b); + +float +rodeo_math_vec3_distanceSq(rodeo_math_vec3_t a, rodeo_math_vec3_t b); + +rodeo_math_vec3_t +rodeo_math_vec3_clamp(rodeo_math_vec3_t a, float minimum, float maximum); + +rodeo_math_vec3_t +rodeo_math_vec3_lerp(rodeo_math_vec3_t from, rodeo_math_vec3_t to, float t); diff --git a/include/rodeo/math/vec3_t.h b/include/rodeo/math/vec3_t.h new file mode 100644 index 0000000..a3b3a36 --- /dev/null +++ b/include/rodeo/math/vec3_t.h @@ -0,0 +1,18 @@ +#pragma once + +typedef +struct +{ + float x; + float y; + float z; +} +rodeo_math_vec3_val_t; + +typedef +union +{ + rodeo_math_vec3_val_t val; + float raw[3]; +} +rodeo_math_vec3_t; diff --git a/include/rodeo/math_t.h b/include/rodeo/math_t.h new file mode 100644 index 0000000..9374c2a --- /dev/null +++ b/include/rodeo/math_t.h @@ -0,0 +1,5 @@ +#pragma once + +// --- internal --- +// public +#include "rodeo/math/vec2_t.h" diff --git a/include/rodeo/window.h b/include/rodeo/window.h index 2dcfab2..df73b4f 100644 --- a/include/rodeo/window.h +++ b/include/rodeo/window.h @@ -13,8 +13,8 @@ void rodeo_window_init( - uint16_t screen_height, - uint16_t screen_width, + uint32_t screen_height, + uint32_t screen_width, cstr title ); -- cgit v1.2.3