diff --git a/build.bat b/build.bat index 96330da..355c91d 100644 --- a/build.bat +++ b/build.bat @@ -2,8 +2,8 @@ @REM vcvarsall.bat to setup command-line compiler. @echo OFF -set ProjectName=dqnt_unit_test -set CompileEntryPoint=..\dqnt_unit_test.cpp +set ProjectName=dqn_unit_test +set CompileEntryPoint=..\dqn_unit_test.cpp REM Build tags file ctags -R diff --git a/dqnt.h b/dqn.h similarity index 59% rename from dqnt.h rename to dqn.h index ee4213e..7b4025c 100644 --- a/dqnt.h +++ b/dqn.h @@ -1,16 +1,16 @@ -#ifndef DQNT_H -#define DQNT_H +#ifndef DQN_H +#define DQN_H /* - #define DQNT_IMPLEMENTATION // Enable the implementation - #define DQNT_MAKE_STATIC // Make all functions be static - #include "dqnt.h" + #define DQN_IMPLEMENTATION // Enable the implementation + #define DQN_MAKE_STATIC // Make all functions be static + #include "dqn.h" */ -#ifdef DQNT_MAKE_STATIC - #define DQNT_FILE_SCOPE static +#ifdef DQN_MAKE_STATIC + #define DQN_FILE_SCOPE static #else - #define DQNT_FILE_SCOPE + #define DQN_FILE_SCOPE #endif //////////////////////////////////////////////////////////////////////////////// @@ -36,17 +36,17 @@ typedef int64_t i16; typedef double f64; typedef float f32; -#define DQNT_INVALID_CODE_PATH 0 -#define DQNT_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) -#define DQNT_ASSERT(expr) if (!(expr)) { (*((i32 *)0)) = 0; } +#define DQN_INVALID_CODE_PATH 0 +#define DQN_ARRAY_COUNT(array) (sizeof(array) / sizeof(array[0])) +#define DQN_ASSERT(expr) if (!(expr)) { (*((i32 *)0)) = 0; } -#define DQNT_PI 3.14159265359f -#define DQNT_ABS(x) (((x) < 0) ? (-(x)) : (x)) -#define DQNT_DEGREES_TO_RADIANS(x) ((x * (DQNT_PI / 180.0f))) -#define DQNT_RADIANS_TO_DEGREES(x) ((x * (180.0f / DQNT_PI))) -#define DQNT_MAX(a, b) ((a) < (b) ? (b) : (a)) -#define DQNT_MIN(a, b) ((a) < (b) ? (a) : (b)) -#define DQNT_SQUARED(x) ((x) * (x)) +#define DQN_PI 3.14159265359f +#define DQN_ABS(x) (((x) < 0) ? (-(x)) : (x)) +#define DQN_DEGREES_TO_RADIANS(x) ((x * (DQN_PI / 180.0f))) +#define DQN_RADIANS_TO_DEGREES(x) ((x * (180.0f / DQN_PI))) +#define DQN_MAX(a, b) ((a) < (b) ? (b) : (a)) +#define DQN_MIN(a, b) ((a) < (b) ? (a) : (b)) +#define DQN_SQUARED(x) ((x) * (x)) //////////////////////////////////////////////////////////////////////////////// // DArray - Dynamic Array @@ -58,240 +58,240 @@ typedef float f32; /* Example Usage: - uint32_t *uintArray = DQNT_DARRAY_INIT(uint32_t, 16); + uint32_t *uintArray = DQN_DARRAY_INIT(uint32_t, 16); uint32_t numberA = 48; uint32_t numberB = 52; - DQNT_DARRAY_PUSH(&uintArray, &numberA); - DQNT_DARRAY_PUSH(&uintArray, &numberB); + DQN_DARRAY_PUSH(&uintArray, &numberA); + DQN_DARRAY_PUSH(&uintArray, &numberB); - for (uint32_t i = 0; i < dqnt_darray_get_num_items(uintArray); i++) + for (uint32_t i = 0; i < dqn_darray_get_num_items(uintArray); i++) { printf(%d\n", uintArray[i]); } - dqnt_darray_free(uintArray); + dqn_darray_free(uintArray); */ // The init macro RETURNS a pointer to your type, you can index this as normal // with array notation []. // u32 type - The data type to initiate a dynamic array with // u32 startingCapacity - Initial number of available slots -#define DQNT_DARRAY_INIT(type, startingCapacity) \ - (type *)dqnt_darray_init_internal(sizeof(type), startingCapacity) +#define DQN_DARRAY_INIT(type, startingCapacity) \ + (type *)dqn_darray_init_internal(sizeof(type), startingCapacity) -// Pass in the pointer returned by DQNT_DARRAY_INIT. If the pointer is not +// Pass in the pointer returned by DQN_DARRAY_INIT. If the pointer is not // a valid DArray pointer, this will return 0. -DQNT_FILE_SCOPE u32 dqnt_darray_get_capacity(void *array); -DQNT_FILE_SCOPE u32 dqnt_darray_get_num_items(void *array); +DQN_FILE_SCOPE u32 dqn_darray_get_capacity(void *array); +DQN_FILE_SCOPE u32 dqn_darray_get_num_items(void *array); -// void **array - the address of the pointer returned by DQNT_DARRAY_INIT +// void **array - the address of the pointer returned by DQN_DARRAY_INIT // void *item - a pointer to the object to insert // The push macro RETURNS true/false if the push was successful or not. -#define DQNT_DARRAY_PUSH(array, item) \ - dqnt_darray_push_internal((void **)array, (void *)item, sizeof(*item)) +#define DQN_DARRAY_PUSH(array, item) \ + dqn_darray_push_internal((void **)array, (void *)item, sizeof(*item)) -// Pass in the pointer returned by DQNT_DARRAY_INIT. Returns if the free was +// Pass in the pointer returned by DQN_DARRAY_INIT. Returns if the free was // successful. This will return false if the array is not a valid DArray and // won't touch the pointer. -DQNT_FILE_SCOPE bool dqnt_darray_free(void *array); +DQN_FILE_SCOPE bool dqn_darray_free(void *array); //////////////////////////////////////////////////////////////////////////////// // Math //////////////////////////////////////////////////////////////////////////////// -DQNT_FILE_SCOPE f32 dqnt_math_lerp(f32 a, f32 t, f32 b); -DQNT_FILE_SCOPE f32 dqnt_math_sqrtf(f32 a); +DQN_FILE_SCOPE f32 dqn_math_lerp(f32 a, f32 t, f32 b); +DQN_FILE_SCOPE f32 dqn_math_sqrtf(f32 a); //////////////////////////////////////////////////////////////////////////////// // Vec2 //////////////////////////////////////////////////////////////////////////////// -typedef union DqntV2 { +typedef union DqnV2 { struct { f32 x, y; }; struct { f32 w, h; }; struct { f32 min, max; }; f32 e[2]; -} DqntV2; +} DqnV2; // Create a vector using ints and typecast to floats -DQNT_FILE_SCOPE DqntV2 dqnt_v2i(i32 x, i32 y); -DQNT_FILE_SCOPE DqntV2 dqnt_v2 (f32 x, f32 y); +DQN_FILE_SCOPE DqnV2 dqn_v2i(i32 x, i32 y); +DQN_FILE_SCOPE DqnV2 dqn_v2 (f32 x, f32 y); -DQNT_FILE_SCOPE DqntV2 dqnt_v2_add (DqntV2 a, DqntV2 b); -DQNT_FILE_SCOPE DqntV2 dqnt_v2_sub (DqntV2 a, DqntV2 b); -DQNT_FILE_SCOPE DqntV2 dqnt_v2_scale (DqntV2 a, f32 b); -DQNT_FILE_SCOPE DqntV2 dqnt_v2_hadamard(DqntV2 a, DqntV2 b); -DQNT_FILE_SCOPE f32 dqnt_v2_dot (DqntV2 a, DqntV2 b); -DQNT_FILE_SCOPE bool dqnt_v2_equals (DqntV2 a, DqntV2 b); +DQN_FILE_SCOPE DqnV2 dqn_v2_add (DqnV2 a, DqnV2 b); +DQN_FILE_SCOPE DqnV2 dqn_v2_sub (DqnV2 a, DqnV2 b); +DQN_FILE_SCOPE DqnV2 dqn_v2_scale (DqnV2 a, f32 b); +DQN_FILE_SCOPE DqnV2 dqn_v2_hadamard(DqnV2 a, DqnV2 b); +DQN_FILE_SCOPE f32 dqn_v2_dot (DqnV2 a, DqnV2 b); +DQN_FILE_SCOPE bool dqn_v2_equals (DqnV2 a, DqnV2 b); -DQNT_FILE_SCOPE f32 dqnt_v2_length_squared(DqntV2 a, DqntV2 b); -DQNT_FILE_SCOPE f32 dqnt_v2_length (DqntV2 a, DqntV2 b); -DQNT_FILE_SCOPE DqntV2 dqnt_v2_normalise (DqntV2 a); -DQNT_FILE_SCOPE bool dqnt_v2_overlaps (DqntV2 a, DqntV2 b); -DQNT_FILE_SCOPE DqntV2 dqnt_v2_perpendicular (DqntV2 a); +DQN_FILE_SCOPE f32 dqn_v2_length_squared(DqnV2 a, DqnV2 b); +DQN_FILE_SCOPE f32 dqn_v2_length (DqnV2 a, DqnV2 b); +DQN_FILE_SCOPE DqnV2 dqn_v2_normalise (DqnV2 a); +DQN_FILE_SCOPE bool dqn_v2_overlaps (DqnV2 a, DqnV2 b); +DQN_FILE_SCOPE DqnV2 dqn_v2_perpendicular (DqnV2 a); // Resize the dimension to fit the aspect ratio provided. Downscale only. -DQNT_FILE_SCOPE DqntV2 dqnt_v2_constrain_to_ratio(DqntV2 dim, DqntV2 ratio); +DQN_FILE_SCOPE DqnV2 dqn_v2_constrain_to_ratio(DqnV2 dim, DqnV2 ratio); //////////////////////////////////////////////////////////////////////////////// // Vec3 //////////////////////////////////////////////////////////////////////////////// -typedef union DqntV3 +typedef union DqnV3 { struct { f32 x, y, z; }; struct { f32 r, g, b; }; f32 e[3]; -} DqntV3; +} DqnV3; // Create a vector using ints and typecast to floats -DQNT_FILE_SCOPE DqntV3 dqnt_v3i(i32 x, i32 y, i32 z); -DQNT_FILE_SCOPE DqntV3 dqnt_v3 (f32 x, f32 y, f32 z); +DQN_FILE_SCOPE DqnV3 dqn_v3i(i32 x, i32 y, i32 z); +DQN_FILE_SCOPE DqnV3 dqn_v3 (f32 x, f32 y, f32 z); -DQNT_FILE_SCOPE DqntV3 dqnt_v3_add (DqntV3 a, DqntV3 b); -DQNT_FILE_SCOPE DqntV3 dqnt_v3_sub (DqntV3 a, DqntV3 b); -DQNT_FILE_SCOPE DqntV3 dqnt_v3_scale (DqntV3 a, f32 b); -DQNT_FILE_SCOPE DqntV3 dqnt_v3_hadamard(DqntV3 a, DqntV3 b); -DQNT_FILE_SCOPE f32 dqnt_v3_dot (DqntV3 a, DqntV3 b); -DQNT_FILE_SCOPE bool dqnt_v3_equals (DqntV3 a, DqntV3 b); +DQN_FILE_SCOPE DqnV3 dqn_v3_add (DqnV3 a, DqnV3 b); +DQN_FILE_SCOPE DqnV3 dqn_v3_sub (DqnV3 a, DqnV3 b); +DQN_FILE_SCOPE DqnV3 dqn_v3_scale (DqnV3 a, f32 b); +DQN_FILE_SCOPE DqnV3 dqn_v3_hadamard(DqnV3 a, DqnV3 b); +DQN_FILE_SCOPE f32 dqn_v3_dot (DqnV3 a, DqnV3 b); +DQN_FILE_SCOPE bool dqn_v3_equals (DqnV3 a, DqnV3 b); -DQNT_FILE_SCOPE DqntV3 dqnt_v3_cross(DqntV3 a, DqntV3 b); +DQN_FILE_SCOPE DqnV3 dqn_v3_cross(DqnV3 a, DqnV3 b); //////////////////////////////////////////////////////////////////////////////// // Vec4 //////////////////////////////////////////////////////////////////////////////// -typedef union DqntV4 +typedef union DqnV4 { struct { f32 x, y, z, w; }; struct { f32 r, g, b, a; }; f32 e[4]; - DqntV2 v2[2]; -} DqntV4; + DqnV2 v2[2]; +} DqnV4; // Create a vector using ints and typecast to floats -DQNT_FILE_SCOPE DqntV4 dqnt_v4i(i32 x, i32 y, i32 z); -DQNT_FILE_SCOPE DqntV4 dqnt_v4 (f32 x, f32 y, f32 z, f32 w); +DQN_FILE_SCOPE DqnV4 dqn_v4i(i32 x, i32 y, i32 z); +DQN_FILE_SCOPE DqnV4 dqn_v4 (f32 x, f32 y, f32 z, f32 w); -DQNT_FILE_SCOPE DqntV4 dqnt_v4_add (DqntV4 a, DqntV4 b); -DQNT_FILE_SCOPE DqntV4 dqnt_v4_sub (DqntV4 a, DqntV4 b); -DQNT_FILE_SCOPE DqntV4 dqnt_v4_scale (DqntV4 a, f32 b); -DQNT_FILE_SCOPE DqntV4 dqnt_v4_hadamard(DqntV4 a, DqntV4 b); -DQNT_FILE_SCOPE f32 dqnt_v4_dot (DqntV4 a, DqntV4 b); -DQNT_FILE_SCOPE bool dqnt_v4_equals (DqntV4 a, DqntV4 b); +DQN_FILE_SCOPE DqnV4 dqn_v4_add (DqnV4 a, DqnV4 b); +DQN_FILE_SCOPE DqnV4 dqn_v4_sub (DqnV4 a, DqnV4 b); +DQN_FILE_SCOPE DqnV4 dqn_v4_scale (DqnV4 a, f32 b); +DQN_FILE_SCOPE DqnV4 dqn_v4_hadamard(DqnV4 a, DqnV4 b); +DQN_FILE_SCOPE f32 dqn_v4_dot (DqnV4 a, DqnV4 b); +DQN_FILE_SCOPE bool dqn_v4_equals (DqnV4 a, DqnV4 b); //////////////////////////////////////////////////////////////////////////////// // 4D Matrix Mat4 //////////////////////////////////////////////////////////////////////////////// -typedef union DqntMat4 +typedef union DqnMat4 { - DqntV4 col[4]; + DqnV4 col[4]; // Column/row f32 e[4][4]; -} DqntMat4; +} DqnMat4; -DQNT_FILE_SCOPE DqntMat4 dqnt_mat4_identity (); -DQNT_FILE_SCOPE DqntMat4 dqnt_mat4_ortho (f32 left, f32 right, f32 bottom, f32 top, f32 zNear, f32 zFar); -DQNT_FILE_SCOPE DqntMat4 dqnt_mat4_translate(f32 x, f32 y, f32 z); -DQNT_FILE_SCOPE DqntMat4 dqnt_mat4_rotate (f32 radians, f32 x, f32 y, f32 z); -DQNT_FILE_SCOPE DqntMat4 dqnt_mat4_scale (f32 x, f32 y, f32 z); -DQNT_FILE_SCOPE DqntMat4 dqnt_mat4_mul (DqntMat4 a, DqntMat4 b); -DQNT_FILE_SCOPE DqntV4 dqnt_mat4_mul_vec4 (DqntMat4 a, DqntV4 b); +DQN_FILE_SCOPE DqnMat4 dqn_mat4_identity (); +DQN_FILE_SCOPE DqnMat4 dqn_mat4_ortho (f32 left, f32 right, f32 bottom, f32 top, f32 zNear, f32 zFar); +DQN_FILE_SCOPE DqnMat4 dqn_mat4_translate(f32 x, f32 y, f32 z); +DQN_FILE_SCOPE DqnMat4 dqn_mat4_rotate (f32 radians, f32 x, f32 y, f32 z); +DQN_FILE_SCOPE DqnMat4 dqn_mat4_scale (f32 x, f32 y, f32 z); +DQN_FILE_SCOPE DqnMat4 dqn_mat4_mul (DqnMat4 a, DqnMat4 b); +DQN_FILE_SCOPE DqnV4 dqn_mat4_mul_vec4 (DqnMat4 a, DqnV4 b); //////////////////////////////////////////////////////////////////////////////// // Other Math //////////////////////////////////////////////////////////////////////////////// -typedef struct DqntRect +typedef struct DqnRect { - DqntV2 min; - DqntV2 max; -} DqntRect; + DqnV2 min; + DqnV2 max; +} DqnRect; -DQNT_FILE_SCOPE DqntRect dqnt_rect (DqntV2 origin, DqntV2 size); -DQNT_FILE_SCOPE void dqnt_rect_get_size_2f(DqntRect rect, f32 *width, f32 *height); -DQNT_FILE_SCOPE DqntV2 dqnt_rect_get_size_v2(DqntRect rect); -DQNT_FILE_SCOPE DqntV2 dqnt_rect_get_centre (DqntRect rect); -DQNT_FILE_SCOPE DqntRect dqnt_rect_move (DqntRect rect, DqntV2 shift); -DQNT_FILE_SCOPE bool dqnt_rect_contains_p (DqntRect rect, DqntV2 p); +DQN_FILE_SCOPE DqnRect dqn_rect (DqnV2 origin, DqnV2 size); +DQN_FILE_SCOPE void dqn_rect_get_size_2f(DqnRect rect, f32 *width, f32 *height); +DQN_FILE_SCOPE DqnV2 dqn_rect_get_size_v2(DqnRect rect); +DQN_FILE_SCOPE DqnV2 dqn_rect_get_centre (DqnRect rect); +DQN_FILE_SCOPE DqnRect dqn_rect_move (DqnRect rect, DqnV2 shift); +DQN_FILE_SCOPE bool dqn_rect_contains_p (DqnRect rect, DqnV2 p); //////////////////////////////////////////////////////////////////////////////// // String Ops //////////////////////////////////////////////////////////////////////////////// -DQNT_FILE_SCOPE bool dqnt_char_is_digit (char c); -DQNT_FILE_SCOPE bool dqnt_char_is_alpha (char c); -DQNT_FILE_SCOPE bool dqnt_char_is_alphanum(char c); +DQN_FILE_SCOPE bool dqn_char_is_digit (char c); +DQN_FILE_SCOPE bool dqn_char_is_alpha (char c); +DQN_FILE_SCOPE bool dqn_char_is_alphanum(char c); -DQNT_FILE_SCOPE i32 dqnt_strcmp (const char *a, const char *b); +DQN_FILE_SCOPE i32 dqn_strcmp (const char *a, const char *b); // Returns the length without the null terminator -DQNT_FILE_SCOPE i32 dqnt_strlen (const char *a); -DQNT_FILE_SCOPE char *dqnt_strncpy(char *dest, const char *src, i32 numChars); +DQN_FILE_SCOPE i32 dqn_strlen (const char *a); +DQN_FILE_SCOPE char *dqn_strncpy(char *dest, const char *src, i32 numChars); -#define DQNT_I32_TO_STR_MAX_BUF_SIZE 11 -DQNT_FILE_SCOPE bool dqnt_str_reverse(char *buf, const i32 bufSize); -DQNT_FILE_SCOPE i32 dqnt_str_to_i32 (char *const buf, const i32 bufSize); -DQNT_FILE_SCOPE void dqnt_i32_to_str (i32 value, char *buf, i32 bufSize); +#define DQN_I32_TO_STR_MAX_BUF_SIZE 11 +DQN_FILE_SCOPE bool dqn_str_reverse(char *buf, const i32 bufSize); +DQN_FILE_SCOPE i32 dqn_str_to_i32 (char *const buf, const i32 bufSize); +DQN_FILE_SCOPE void dqn_i32_to_str (i32 value, char *buf, i32 bufSize); // Both return the number of bytes read, return 0 if invalid codepoint or UTF8 -DQNT_FILE_SCOPE u32 dqnt_ucs_to_utf8(u32 *dest, u32 character); -DQNT_FILE_SCOPE u32 dqnt_utf8_to_ucs(u32 *dest, u32 character); +DQN_FILE_SCOPE u32 dqn_ucs_to_utf8(u32 *dest, u32 character); +DQN_FILE_SCOPE u32 dqn_utf8_to_ucs(u32 *dest, u32 character); //////////////////////////////////////////////////////////////////////////////// // File Operations //////////////////////////////////////////////////////////////////////////////// -typedef struct DqntFile +typedef struct DqnFile { void *handle; u64 size; -} DqntFile; +} DqnFile; // Open a handle to the file -DQNT_FILE_SCOPE bool dqnt_file_open(char *const file, DqntFile *fileHandle); +DQN_FILE_SCOPE bool dqn_file_open(char *const file, DqnFile *fileHandle); // Return the number of bytes read -DQNT_FILE_SCOPE u32 dqnt_file_read (DqntFile file, void *buffer, u32 numBytesToRead); -DQNT_FILE_SCOPE void dqnt_file_close(DqntFile *file); +DQN_FILE_SCOPE u32 dqn_file_read (DqnFile file, void *buffer, u32 numBytesToRead); +DQN_FILE_SCOPE void dqn_file_close(DqnFile *file); // Return an array of strings of the files in the directory in UTF-8. numFiles // returns the number of strings read. // This is allocated using malloc and MUST BE FREED! Can be done manually or // using the helper function. -DQNT_FILE_SCOPE char **dqnt_dir_read (char *dir, u32 *numFiles); -DQNT_FILE_SCOPE void dqnt_dir_read_free(char **fileList, u32 numFiles); +DQN_FILE_SCOPE char **dqn_dir_read (char *dir, u32 *numFiles); +DQN_FILE_SCOPE void dqn_dir_read_free(char **fileList, u32 numFiles); //////////////////////////////////////////////////////////////////////////////// // Timer //////////////////////////////////////////////////////////////////////////////// -DQNT_FILE_SCOPE f64 dqnt_time_now_in_s(); -DQNT_FILE_SCOPE f64 dqnt_time_now_in_ms(); +DQN_FILE_SCOPE f64 dqn_time_now_in_s(); +DQN_FILE_SCOPE f64 dqn_time_now_in_ms(); //////////////////////////////////////////////////////////////////////////////// // PCG (Permuted Congruential Generator) Random Number Generator //////////////////////////////////////////////////////////////////////////////// -typedef struct DqntRandPCGState +typedef struct DqnRandPCGState { u64 state[2]; -} DqntRandPCGState; +} DqnRandPCGState; // Initialise the random number generator using a seed. If not given it is // automatically created by using rdtsc. The generator is not valid until it's // been seeded. -DQNT_FILE_SCOPE void dqnt_rnd_pcg_init_with_seed(DqntRandPCGState *pcg, u32 seed); -DQNT_FILE_SCOPE void dqnt_rnd_pcg_init(DqntRandPCGState *pcg); +DQN_FILE_SCOPE void dqn_rnd_pcg_init_with_seed(DqnRandPCGState *pcg, u32 seed); +DQN_FILE_SCOPE void dqn_rnd_pcg_init(DqnRandPCGState *pcg); // Returns a random number N between [0, 0xFFFFFFFF] -DQNT_FILE_SCOPE u32 dqnt_rnd_pcg_next (DqntRandPCGState *pcg); +DQN_FILE_SCOPE u32 dqn_rnd_pcg_next (DqnRandPCGState *pcg); // Returns a random float N between [0.0, 1.0f] -DQNT_FILE_SCOPE f32 dqnt_rnd_pcg_nextf(DqntRandPCGState *pcg); +DQN_FILE_SCOPE f32 dqn_rnd_pcg_nextf(DqnRandPCGState *pcg); // Returns a random integer N between [min, max] -DQNT_FILE_SCOPE i32 dqnt_rnd_pcg_range(DqntRandPCGState *pcg, i32 min, i32 max); +DQN_FILE_SCOPE i32 dqn_rnd_pcg_range(DqnRandPCGState *pcg, i32 min, i32 max); -#endif /* DQNT_H */ +#endif /* DQN_H */ //////////////////////////////////////////////////////////////////////////////// // // IMPLEMENTATION // //////////////////////////////////////////////////////////////////////////////// -#ifdef DQNT_IMPLEMENTATION -#undef DQNT_IMPLEMENTATION +#ifdef DQN_IMPLEMENTATION +#undef DQN_IMPLEMENTATION #ifdef _WIN32 - #define DQNT_WIN32 + #define DQN_WIN32 #include "Windows.h" #define WIN32_LEAN_AND_MEAN @@ -300,8 +300,8 @@ DQNT_FILE_SCOPE i32 dqnt_rnd_pcg_range(DqntRandPCGState *pcg, i32 min, i32 max) //////////////////////////////////////////////////////////////////////////////// // DArray - Dynamic Array //////////////////////////////////////////////////////////////////////////////// -#define DQNT_DARRAY_SIGNATURE_INTERNAL 0xAC83DB81 -typedef struct DqntDArrayInternal +#define DQN_DARRAY_SIGNATURE_INTERNAL 0xAC83DB81 +typedef struct DqnDArrayInternal { u32 index; u32 itemSize; @@ -309,20 +309,20 @@ typedef struct DqntDArrayInternal u32 signature; void *data; -} DqntDArrayInternal; +} DqnDArrayInternal; -FILE_SCOPE void *dqnt_darray_init_internal(u32 itemSize, u32 startingCapacity) +FILE_SCOPE void *dqn_darray_init_internal(u32 itemSize, u32 startingCapacity) { if (startingCapacity <= 0 || itemSize == 0) return NULL; - u32 metadataSize = sizeof(DqntDArrayInternal); + u32 metadataSize = sizeof(DqnDArrayInternal); u32 storageSize = itemSize * startingCapacity; void *memory = calloc(1, metadataSize + storageSize); if (!memory) return NULL; - DqntDArrayInternal *array = (DqntDArrayInternal *)memory; - array->signature = DQNT_DARRAY_SIGNATURE_INTERNAL; + DqnDArrayInternal *array = (DqnDArrayInternal *)memory; + array->signature = DQN_DARRAY_SIGNATURE_INTERNAL; array->itemSize = itemSize; array->capacity = startingCapacity; array->data = (u8 *)memory + metadataSize; @@ -330,35 +330,35 @@ FILE_SCOPE void *dqnt_darray_init_internal(u32 itemSize, u32 startingCapacity) return array->data; } -FILE_SCOPE DqntDArrayInternal *dqnt_darray_get_header_internal(void *array) +FILE_SCOPE DqnDArrayInternal *dqn_darray_get_header_internal(void *array) { if (!array) return NULL; - DqntDArrayInternal *result = (DqntDArrayInternal *)((u8 *)array - sizeof(DqntDArrayInternal)); - if (result->signature != DQNT_DARRAY_SIGNATURE_INTERNAL) return 0; + DqnDArrayInternal *result = (DqnDArrayInternal *)((u8 *)array - sizeof(DqnDArrayInternal)); + if (result->signature != DQN_DARRAY_SIGNATURE_INTERNAL) return 0; return result; } -DQNT_FILE_SCOPE u32 dqnt_darray_get_capacity(void *array) +DQN_FILE_SCOPE u32 dqn_darray_get_capacity(void *array) { - DqntDArrayInternal *header = dqnt_darray_get_header_internal(array); + DqnDArrayInternal *header = dqn_darray_get_header_internal(array); if (!header) return 0; return header->capacity; } -DQNT_FILE_SCOPE u32 dqnt_darray_get_num_items(void *array) +DQN_FILE_SCOPE u32 dqn_darray_get_num_items(void *array) { - DqntDArrayInternal *header = dqnt_darray_get_header_internal(array); + DqnDArrayInternal *header = dqn_darray_get_header_internal(array); if (!header) return 0; return header->index; } -bool dqnt_darray_push_internal(void **array, void *element, u32 itemSize) +bool dqn_darray_push_internal(void **array, void *element, u32 itemSize) { if (!element || !array) return false; - DqntDArrayInternal *header = dqnt_darray_get_header_internal(*array); + DqnDArrayInternal *header = dqn_darray_get_header_internal(*array); if (!header || header->itemSize != itemSize) return false; if (header->index >= header->capacity) @@ -367,12 +367,12 @@ bool dqnt_darray_push_internal(void **array, void *element, u32 itemSize) u32 newCapacity = (i32)(header->capacity * GROWTH_FACTOR); if (newCapacity == header->capacity) newCapacity++; - u32 metadataSize = sizeof(DqntDArrayInternal); + u32 metadataSize = sizeof(DqnDArrayInternal); u32 storageSize = header->itemSize * newCapacity; void *newMem = realloc(header, metadataSize + storageSize); if (newMem) { - header = (DqntDArrayInternal *)newMem; + header = (DqnDArrayInternal *)newMem; header->capacity = newCapacity; header->data = (u8 *)newMem + metadataSize; *array = header->data; @@ -384,7 +384,7 @@ bool dqnt_darray_push_internal(void **array, void *element, u32 itemSize) } u32 arrayOffset = header->itemSize * header->index++; - DQNT_ASSERT(header->index <= header->capacity); + DQN_ASSERT(header->index <= header->capacity); u8 *dataPtr = (u8 *)header->data; void *dest = (void *)&dataPtr[arrayOffset]; @@ -394,9 +394,9 @@ bool dqnt_darray_push_internal(void **array, void *element, u32 itemSize) return true; } -DQNT_FILE_SCOPE inline bool dqnt_darray_free(void *array) +DQN_FILE_SCOPE inline bool dqn_darray_free(void *array) { - DqntDArrayInternal *header = dqnt_darray_get_header_internal(array); + DqnDArrayInternal *header = dqn_darray_get_header_internal(array); if (header) { header->index = 0; @@ -416,7 +416,7 @@ DQNT_FILE_SCOPE inline bool dqnt_darray_free(void *array) //////////////////////////////////////////////////////////////////////////////// // Math //////////////////////////////////////////////////////////////////////////////// -DQNT_FILE_SCOPE f32 dqnt_math_lerp(f32 a, f32 t, f32 b) +DQN_FILE_SCOPE f32 dqn_math_lerp(f32 a, f32 t, f32 b) { /* Linear blend between two values. We having a starting point "a", and @@ -434,7 +434,7 @@ DQNT_FILE_SCOPE f32 dqnt_math_lerp(f32 a, f32 t, f32 b) return result; } -DQNT_FILE_SCOPE f32 dqnt_math_sqrtf(f32 a) +DQN_FILE_SCOPE f32 dqn_math_sqrtf(f32 a) { f32 result = sqrtf(a); return result; @@ -443,58 +443,58 @@ DQNT_FILE_SCOPE f32 dqnt_math_sqrtf(f32 a) //////////////////////////////////////////////////////////////////////////////// // Vec2 //////////////////////////////////////////////////////////////////////////////// -DQNT_FILE_SCOPE inline DqntV2 dqnt_v2(f32 x, f32 y) +DQN_FILE_SCOPE inline DqnV2 dqn_v2(f32 x, f32 y) { - DqntV2 result = {}; + DqnV2 result = {}; result.x = x; result.y = y; return result; } -DQNT_FILE_SCOPE inline DqntV2 dqnt_v2i(i32 x, i32 y) +DQN_FILE_SCOPE inline DqnV2 dqn_v2i(i32 x, i32 y) { - DqntV2 result = dqnt_v2((f32)x, (f32)y); + DqnV2 result = dqn_v2((f32)x, (f32)y); return result; } -DQNT_FILE_SCOPE inline DqntV2 dqnt_v2_add(DqntV2 a, DqntV2 b) +DQN_FILE_SCOPE inline DqnV2 dqn_v2_add(DqnV2 a, DqnV2 b) { - DqntV2 result; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + DqnV2 result; + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result.e[i] = a.e[i] + b.e[i]; return result; } -DQNT_FILE_SCOPE inline DqntV2 dqnt_v2_sub(DqntV2 a, DqntV2 b) +DQN_FILE_SCOPE inline DqnV2 dqn_v2_sub(DqnV2 a, DqnV2 b) { - DqntV2 result; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + DqnV2 result; + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result.e[i] = a.e[i] - b.e[i]; return result; } -DQNT_FILE_SCOPE inline DqntV2 dqnt_v2_scale(DqntV2 a, f32 b) +DQN_FILE_SCOPE inline DqnV2 dqn_v2_scale(DqnV2 a, f32 b) { - DqntV2 result; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + DqnV2 result; + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result.e[i] = a.e[i] * b; return result; } -DQNT_FILE_SCOPE inline DqntV2 dqnt_v2_hadamard(DqntV2 a, DqntV2 b) +DQN_FILE_SCOPE inline DqnV2 dqn_v2_hadamard(DqnV2 a, DqnV2 b) { - DqntV2 result; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + DqnV2 result; + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result.e[i] = a.e[i] * b.e[i]; return result; } -DQNT_FILE_SCOPE inline f32 dqnt_v2_dot(DqntV2 a, DqntV2 b) +DQN_FILE_SCOPE inline f32 dqn_v2_dot(DqnV2 a, DqnV2 b) { /* DOT PRODUCT @@ -504,44 +504,44 @@ DQNT_FILE_SCOPE inline f32 dqnt_v2_dot(DqntV2 a, DqntV2 b) |c| |f| */ f32 result = 0; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result += (a.e[i] * b.e[i]); return result; } -DQNT_FILE_SCOPE inline bool dqnt_v2_equals(DqntV2 a, DqntV2 b) +DQN_FILE_SCOPE inline bool dqn_v2_equals(DqnV2 a, DqnV2 b) { bool result = TRUE; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) if (a.e[i] != b.e[i]) result = FALSE; return result; } -DQNT_FILE_SCOPE inline f32 dqnt_v2_length_squared(DqntV2 a, DqntV2 b) +DQN_FILE_SCOPE inline f32 dqn_v2_length_squared(DqnV2 a, DqnV2 b) { f32 x = b.x - a.x; f32 y = b.y - a.y; - f32 result = (DQNT_SQUARED(x) + DQNT_SQUARED(y)); + f32 result = (DQN_SQUARED(x) + DQN_SQUARED(y)); return result; } -DQNT_FILE_SCOPE inline f32 dqnt_v2_length(DqntV2 a, DqntV2 b) +DQN_FILE_SCOPE inline f32 dqn_v2_length(DqnV2 a, DqnV2 b) { - f32 lengthSq = dqnt_v2_length_squared(a, b); - f32 result = dqnt_math_sqrtf(lengthSq); + f32 lengthSq = dqn_v2_length_squared(a, b); + f32 result = dqn_math_sqrtf(lengthSq); return result; } -DQNT_FILE_SCOPE inline DqntV2 dqnt_v2_normalise(DqntV2 a) +DQN_FILE_SCOPE inline DqnV2 dqn_v2_normalise(DqnV2 a) { - f32 magnitude = dqnt_v2_length(dqnt_v2(0, 0), a); - DqntV2 result = dqnt_v2(a.x, a.y); - result = dqnt_v2_scale(a, 1 / magnitude); + f32 magnitude = dqn_v2_length(dqn_v2(0, 0), a); + DqnV2 result = dqn_v2(a.x, a.y); + result = dqn_v2_scale(a, 1 / magnitude); return result; } -DQNT_FILE_SCOPE inline bool dqnt_v2_overlaps(DqntV2 a, DqntV2 b) +DQN_FILE_SCOPE inline bool dqn_v2_overlaps(DqnV2 a, DqnV2 b) { bool result = false; @@ -550,7 +550,7 @@ DQNT_FILE_SCOPE inline bool dqnt_v2_overlaps(DqntV2 a, DqntV2 b) if (lenOfA > lenOfB) { - DqntV2 tmp = a; + DqnV2 tmp = a; a = b; b = tmp; } @@ -564,21 +564,21 @@ DQNT_FILE_SCOPE inline bool dqnt_v2_overlaps(DqntV2 a, DqntV2 b) return result; } -DQNT_FILE_SCOPE inline DqntV2 dqnt_v2_perpendicular(DqntV2 a) +DQN_FILE_SCOPE inline DqnV2 dqn_v2_perpendicular(DqnV2 a) { - DqntV2 result = {a.y, -a.x}; + DqnV2 result = {a.y, -a.x}; return result; } -DQNT_FILE_SCOPE DqntV2 dqnt_v2_constrain_to_ratio(DqntV2 dim, DqntV2 ratio) +DQN_FILE_SCOPE DqnV2 dqn_v2_constrain_to_ratio(DqnV2 dim, DqnV2 ratio) { - DqntV2 result = {}; + DqnV2 result = {}; f32 numRatioIncrementsToWidth = (f32)(dim.w / ratio.w); f32 numRatioIncrementsToHeight = (f32)(dim.h / ratio.h); f32 leastIncrementsToSide = - DQNT_MIN(numRatioIncrementsToHeight, numRatioIncrementsToWidth); + DQN_MIN(numRatioIncrementsToHeight, numRatioIncrementsToWidth); result.w = (f32)(ratio.w * leastIncrementsToSide); result.h = (f32)(ratio.h * leastIncrementsToSide); @@ -588,58 +588,58 @@ DQNT_FILE_SCOPE DqntV2 dqnt_v2_constrain_to_ratio(DqntV2 dim, DqntV2 ratio) //////////////////////////////////////////////////////////////////////////////// // Vec3 //////////////////////////////////////////////////////////////////////////////// -DQNT_FILE_SCOPE inline DqntV3 dqnt_v3(f32 x, f32 y, f32 z) +DQN_FILE_SCOPE inline DqnV3 dqn_v3(f32 x, f32 y, f32 z) { - DqntV3 result = {}; + DqnV3 result = {}; result.x = x; result.y = y; result.z = z; return result; } -DQNT_FILE_SCOPE inline DqntV3 dqnt_v3i(i32 x, i32 y, i32 z) +DQN_FILE_SCOPE inline DqnV3 dqn_v3i(i32 x, i32 y, i32 z) { - DqntV3 result = dqnt_v3((f32)x, (f32)y, (f32)z); + DqnV3 result = dqn_v3((f32)x, (f32)y, (f32)z); return result; } -DQNT_FILE_SCOPE inline DqntV3 dqnt_v3_add(DqntV3 a, DqntV3 b) +DQN_FILE_SCOPE inline DqnV3 dqn_v3_add(DqnV3 a, DqnV3 b) { - DqntV3 result; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + DqnV3 result; + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result.e[i] = a.e[i] + b.e[i]; return result; } -DQNT_FILE_SCOPE inline DqntV3 dqnt_v3_sub(DqntV3 a, DqntV3 b) +DQN_FILE_SCOPE inline DqnV3 dqn_v3_sub(DqnV3 a, DqnV3 b) { - DqntV3 result; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + DqnV3 result; + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result.e[i] = a.e[i] - b.e[i]; return result; } -DQNT_FILE_SCOPE inline DqntV3 dqnt_v3_scale(DqntV3 a, f32 b) +DQN_FILE_SCOPE inline DqnV3 dqn_v3_scale(DqnV3 a, f32 b) { - DqntV3 result; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + DqnV3 result; + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result.e[i] = a.e[i] * b; return result; } -DQNT_FILE_SCOPE inline DqntV3 dqnt_v3_hadamard(DqntV3 a, DqntV3 b) +DQN_FILE_SCOPE inline DqnV3 dqn_v3_hadamard(DqnV3 a, DqnV3 b) { - DqntV3 result; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + DqnV3 result; + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result.e[i] = a.e[i] * b.e[i]; return result; } -DQNT_FILE_SCOPE inline f32 dqnt_v3_dot(DqntV3 a, DqntV3 b) +DQN_FILE_SCOPE inline f32 dqn_v3_dot(DqnV3 a, DqnV3 b) { /* DOT PRODUCT @@ -649,21 +649,21 @@ DQNT_FILE_SCOPE inline f32 dqnt_v3_dot(DqntV3 a, DqntV3 b) |c| |f| */ f32 result = 0; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result += (a.e[i] * b.e[i]); return result; } -DQNT_FILE_SCOPE inline bool dqnt_v3_equals(DqntV3 a, DqntV3 b) +DQN_FILE_SCOPE inline bool dqn_v3_equals(DqnV3 a, DqnV3 b) { bool result = TRUE; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) if (a.e[i] != b.e[i]) result = FALSE; return result; } -DQNT_FILE_SCOPE inline DqntV3 dqnt_v3_cross(DqntV3 a, DqntV3 b) +DQN_FILE_SCOPE inline DqnV3 dqn_v3_cross(DqnV3 a, DqnV3 b) { /* CROSS PRODUCT @@ -672,7 +672,7 @@ DQNT_FILE_SCOPE inline DqntV3 dqnt_v3_cross(DqntV3 a, DqntV3 b) |b| x |e| = |cd - af| |c| |f| |ae - be| */ - DqntV3 result = {}; + DqnV3 result = {}; result.e[0] = (a.e[1] * b.e[2]) - (a.e[2] * b.e[1]); result.e[1] = (a.e[2] * b.e[0]) - (a.e[0] * b.e[2]); result.e[2] = (a.e[0] * b.e[1]) - (a.e[1] * b.e[0]); @@ -682,54 +682,54 @@ DQNT_FILE_SCOPE inline DqntV3 dqnt_v3_cross(DqntV3 a, DqntV3 b) //////////////////////////////////////////////////////////////////////////////// // Vec4 //////////////////////////////////////////////////////////////////////////////// -DQNT_FILE_SCOPE inline DqntV4 dqnt_v4(f32 x, f32 y, f32 z, f32 w) +DQN_FILE_SCOPE inline DqnV4 dqn_v4(f32 x, f32 y, f32 z, f32 w) { - DqntV4 result = {x, y, z, w}; + DqnV4 result = {x, y, z, w}; return result; } -DQNT_FILE_SCOPE inline DqntV4 dqnt_v4i(i32 x, i32 y, i32 z, i32 w) { - DqntV4 result = dqnt_v4((f32)x, (f32)y, (f32)z, (f32)w); +DQN_FILE_SCOPE inline DqnV4 dqn_v4i(i32 x, i32 y, i32 z, i32 w) { + DqnV4 result = dqn_v4((f32)x, (f32)y, (f32)z, (f32)w); return result; } -DQNT_FILE_SCOPE inline DqntV4 dqnt_v4_add(DqntV4 a, DqntV4 b) +DQN_FILE_SCOPE inline DqnV4 dqn_v4_add(DqnV4 a, DqnV4 b) { - DqntV4 result; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + DqnV4 result; + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result.e[i] = a.e[i] + b.e[i]; return result; } -DQNT_FILE_SCOPE inline DqntV4 dqnt_v4_sub(DqntV4 a, DqntV4 b) +DQN_FILE_SCOPE inline DqnV4 dqn_v4_sub(DqnV4 a, DqnV4 b) { - DqntV4 result; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + DqnV4 result; + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result.e[i] = a.e[i] - b.e[i]; return result; } -DQNT_FILE_SCOPE inline DqntV4 dqnt_v4_scale(DqntV4 a, f32 b) +DQN_FILE_SCOPE inline DqnV4 dqn_v4_scale(DqnV4 a, f32 b) { - DqntV4 result; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + DqnV4 result; + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result.e[i] = a.e[i] * b; return result; } -DQNT_FILE_SCOPE inline DqntV4 dqnt_v4_hadamard(DqntV4 a, DqntV4 b) +DQN_FILE_SCOPE inline DqnV4 dqn_v4_hadamard(DqnV4 a, DqnV4 b) { - DqntV4 result; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + DqnV4 result; + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result.e[i] = a.e[i] * b.e[i]; return result; } -DQNT_FILE_SCOPE inline f32 dqnt_v4_dot(DqntV4 a, DqntV4 b) +DQN_FILE_SCOPE inline f32 dqn_v4_dot(DqnV4 a, DqnV4 b) { /* DOT PRODUCT @@ -739,16 +739,16 @@ DQNT_FILE_SCOPE inline f32 dqnt_v4_dot(DqntV4 a, DqntV4 b) |c| |f| */ f32 result = 0; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) result += (a.e[i] * b.e[i]); return result; } -DQNT_FILE_SCOPE inline bool dqnt_v4_equals(DqntV4 a, DqntV4 b) +DQN_FILE_SCOPE inline bool dqn_v4_equals(DqnV4 a, DqnV4 b) { bool result = TRUE; - for (i32 i = 0; i < DQNT_ARRAY_COUNT(a.e); i++) + for (i32 i = 0; i < DQN_ARRAY_COUNT(a.e); i++) if (a.e[i] != b.e[i]) result = FALSE; return result; } @@ -756,9 +756,9 @@ DQNT_FILE_SCOPE inline bool dqnt_v4_equals(DqntV4 a, DqntV4 b) //////////////////////////////////////////////////////////////////////////////// // 4D Matrix Mat4 //////////////////////////////////////////////////////////////////////////////// -DQNT_FILE_SCOPE inline DqntMat4 dqnt_mat4_identity() +DQN_FILE_SCOPE inline DqnMat4 dqn_mat4_identity() { - DqntMat4 result = {0}; + DqnMat4 result = {0}; result.e[0][0] = 1; result.e[1][1] = 1; result.e[2][2] = 1; @@ -766,10 +766,10 @@ DQNT_FILE_SCOPE inline DqntMat4 dqnt_mat4_identity() return result; } -DQNT_FILE_SCOPE inline DqntMat4 -dqnt_mat4_ortho(f32 left, f32 right, f32 bottom, f32 top, f32 zNear, f32 zFar) +DQN_FILE_SCOPE inline DqnMat4 +dqn_mat4_ortho(f32 left, f32 right, f32 bottom, f32 top, f32 zNear, f32 zFar) { - DqntMat4 result = dqnt_mat4_identity(); + DqnMat4 result = dqn_mat4_identity(); result.e[0][0] = +2.0f / (right - left); result.e[1][1] = +2.0f / (top - bottom); result.e[2][2] = -2.0f / (zFar - zNear); @@ -781,41 +781,41 @@ dqnt_mat4_ortho(f32 left, f32 right, f32 bottom, f32 top, f32 zNear, f32 zFar) return result; } -DQNT_FILE_SCOPE inline DqntMat4 dqnt_mat4_translate(f32 x, f32 y, f32 z) +DQN_FILE_SCOPE inline DqnMat4 dqn_mat4_translate(f32 x, f32 y, f32 z) { - DqntMat4 result = dqnt_mat4_identity(); + DqnMat4 result = dqn_mat4_identity(); result.e[3][0] = x; result.e[3][1] = y; result.e[3][2] = z; return result; } -DQNT_FILE_SCOPE inline DqntMat4 dqnt_mat4_rotate(f32 radians, f32 x, f32 y, f32 z) +DQN_FILE_SCOPE inline DqnMat4 dqn_mat4_rotate(f32 radians, f32 x, f32 y, f32 z) { - DqntMat4 result = dqnt_mat4_identity(); + DqnMat4 result = dqn_mat4_identity(); f32 sinVal = sinf(radians); f32 cosVal = cosf(radians); - result.e[0][0] = (cosVal + (DQNT_SQUARED(x) * (1.0f - cosVal))); + result.e[0][0] = (cosVal + (DQN_SQUARED(x) * (1.0f - cosVal))); result.e[0][1] = ((y * z * (1.0f - cosVal)) + (z * sinVal)); result.e[0][2] = ((z * x * (1.0f - cosVal)) - (y * sinVal)); result.e[1][0] = ((x * y * (1.0f - cosVal)) - (z * sinVal)); - result.e[1][1] = (cosVal + (DQNT_SQUARED(y) * (1.0f - cosVal))); + result.e[1][1] = (cosVal + (DQN_SQUARED(y) * (1.0f - cosVal))); result.e[1][2] = ((z * y * (1.0f - cosVal)) + (x * sinVal)); result.e[2][0] = ((x * z * (1.0f - cosVal)) + (y * sinVal)); result.e[2][1] = ((y * z * (1.0f - cosVal)) - (x * sinVal)); - result.e[2][2] = (cosVal + (DQNT_SQUARED(z) * (1.0f - cosVal))); + result.e[2][2] = (cosVal + (DQN_SQUARED(z) * (1.0f - cosVal))); result.e[3][3] = 1; return result; } -DQNT_FILE_SCOPE inline DqntMat4 dqnt_mat4_scale(f32 x, f32 y, f32 z) +DQN_FILE_SCOPE inline DqnMat4 dqn_mat4_scale(f32 x, f32 y, f32 z) { - DqntMat4 result = {0}; + DqnMat4 result = {0}; result.e[0][0] = x; result.e[1][1] = y; result.e[2][2] = z; @@ -823,9 +823,9 @@ DQNT_FILE_SCOPE inline DqntMat4 dqnt_mat4_scale(f32 x, f32 y, f32 z) return result; } -DQNT_FILE_SCOPE inline DqntMat4 dqnt_mat4_mul(DqntMat4 a, DqntMat4 b) +DQN_FILE_SCOPE inline DqnMat4 dqn_mat4_mul(DqnMat4 a, DqnMat4 b) { - DqntMat4 result = {0}; + DqnMat4 result = {0}; for (i32 j = 0; j < 4; j++) { for (i32 i = 0; i < 4; i++) { result.e[j][i] = a.e[0][i] * b.e[j][0] @@ -838,9 +838,9 @@ DQNT_FILE_SCOPE inline DqntMat4 dqnt_mat4_mul(DqntMat4 a, DqntMat4 b) return result; } -DQNT_FILE_SCOPE inline DqntV4 dqnt_mat4_mul_vec4(DqntMat4 a, DqntV4 b) +DQN_FILE_SCOPE inline DqnV4 dqn_mat4_mul_vec4(DqnMat4 a, DqnV4 b) { - DqntV4 result = {0}; + DqnV4 result = {0}; result.x = (a.e[0][0] * b.x) + (a.e[1][0] * b.y) + (a.e[2][0] * b.z) + (a.e[3][0] * b.w); @@ -857,48 +857,48 @@ DQNT_FILE_SCOPE inline DqntV4 dqnt_mat4_mul_vec4(DqntMat4 a, DqntV4 b) //////////////////////////////////////////////////////////////////////////////// // Rect //////////////////////////////////////////////////////////////////////////////// -DQNT_FILE_SCOPE inline DqntRect dqnt_rect(DqntV2 origin, DqntV2 size) +DQN_FILE_SCOPE inline DqnRect dqn_rect(DqnV2 origin, DqnV2 size) { - DqntRect result = {}; + DqnRect result = {}; result.min = origin; - result.max = dqnt_v2_add(origin, size); + result.max = dqn_v2_add(origin, size); return result; } -DQNT_FILE_SCOPE inline void dqnt_rect_get_size_2f(DqntRect rect, f32 *width, f32 *height) +DQN_FILE_SCOPE inline void dqn_rect_get_size_2f(DqnRect rect, f32 *width, f32 *height) { - *width = DQNT_ABS(rect.max.x - rect.min.x); - *height = DQNT_ABS(rect.max.y - rect.min.y); + *width = DQN_ABS(rect.max.x - rect.min.x); + *height = DQN_ABS(rect.max.y - rect.min.y); } -DQNT_FILE_SCOPE inline DqntV2 dqnt_rect_get_size_v2(DqntRect rect) +DQN_FILE_SCOPE inline DqnV2 dqn_rect_get_size_v2(DqnRect rect) { - f32 width = DQNT_ABS(rect.max.x - rect.min.x); - f32 height = DQNT_ABS(rect.max.y - rect.min.y); - DqntV2 result = dqnt_v2(width, height); + f32 width = DQN_ABS(rect.max.x - rect.min.x); + f32 height = DQN_ABS(rect.max.y - rect.min.y); + DqnV2 result = dqn_v2(width, height); return result; } -DQNT_FILE_SCOPE inline DqntV2 dqnt_rect_get_centre(DqntRect rect) +DQN_FILE_SCOPE inline DqnV2 dqn_rect_get_centre(DqnRect rect) { f32 sumX = rect.min.x + rect.max.x; f32 sumY = rect.min.y + rect.max.y; - DqntV2 result = dqnt_v2_scale(dqnt_v2(sumX, sumY), 0.5f); + DqnV2 result = dqn_v2_scale(dqn_v2(sumX, sumY), 0.5f); return result; } -DQNT_FILE_SCOPE inline DqntRect dqnt_rect_move(DqntRect rect, DqntV2 shift) +DQN_FILE_SCOPE inline DqnRect dqn_rect_move(DqnRect rect, DqnV2 shift) { - DqntRect result = {0}; - result.min = dqnt_v2_add(rect.min, shift); - result.max = dqnt_v2_add(rect.max, shift); + DqnRect result = {0}; + result.min = dqn_v2_add(rect.min, shift); + result.max = dqn_v2_add(rect.max, shift); return result; } -DQNT_FILE_SCOPE inline bool dqnt_rect_contains_p(DqntRect rect, DqntV2 p) +DQN_FILE_SCOPE inline bool dqn_rect_contains_p(DqnRect rect, DqnV2 p) { bool outsideOfRectX = false; if (p.x < rect.min.x || p.x > rect.max.w) @@ -916,25 +916,25 @@ DQNT_FILE_SCOPE inline bool dqnt_rect_contains_p(DqntRect rect, DqntV2 p) //////////////////////////////////////////////////////////////////////////////// // String Operations //////////////////////////////////////////////////////////////////////////////// -DQNT_FILE_SCOPE bool dqnt_char_is_digit(char c) +DQN_FILE_SCOPE bool dqn_char_is_digit(char c) { if (c >= '0' && c <= '9') return true; return false; } -DQNT_FILE_SCOPE bool dqnt_char_is_alpha(char c) +DQN_FILE_SCOPE bool dqn_char_is_alpha(char c) { if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) return true; return false; } -DQNT_FILE_SCOPE bool dqnt_char_is_alphanum(char c) +DQN_FILE_SCOPE bool dqn_char_is_alphanum(char c) { - if (dqnt_char_is_alpha(c) || dqnt_char_is_digit(c)) return true; + if (dqn_char_is_alpha(c) || dqn_char_is_digit(c)) return true; return false; } -DQNT_FILE_SCOPE i32 dqnt_strcmp(const char *a, const char *b) +DQN_FILE_SCOPE i32 dqn_strcmp(const char *a, const char *b) { if (!a && !b) return -1; if (!a) return -1; @@ -950,7 +950,7 @@ DQNT_FILE_SCOPE i32 dqnt_strcmp(const char *a, const char *b) return (((*a) < (*b)) ? -1 : 1); } -DQNT_FILE_SCOPE i32 dqnt_strlen(const char *a) +DQN_FILE_SCOPE i32 dqn_strlen(const char *a) { i32 result = 0; while (a && a[result]) result++; @@ -958,7 +958,7 @@ DQNT_FILE_SCOPE i32 dqnt_strlen(const char *a) return result; } -DQNT_FILE_SCOPE char *dqnt_strncpy(char *dest, const char *src, i32 numChars) +DQN_FILE_SCOPE char *dqn_strncpy(char *dest, const char *src, i32 numChars) { if (!dest) return NULL; if (!src) return dest; @@ -969,7 +969,7 @@ DQNT_FILE_SCOPE char *dqnt_strncpy(char *dest, const char *src, i32 numChars) return dest; } -DQNT_FILE_SCOPE bool dqnt_str_reverse(char *buf, const i32 bufSize) +DQN_FILE_SCOPE bool dqn_str_reverse(char *buf, const i32 bufSize) { if (!buf) return false; i32 mid = bufSize / 2; @@ -984,7 +984,7 @@ DQNT_FILE_SCOPE bool dqnt_str_reverse(char *buf, const i32 bufSize) return true; } -DQNT_FILE_SCOPE i32 dqnt_str_to_i32(char *const buf, const i32 bufSize) +DQN_FILE_SCOPE i32 dqn_str_to_i32(char *const buf, const i32 bufSize) { if (!buf || bufSize == 0) return 0; @@ -995,7 +995,7 @@ DQNT_FILE_SCOPE i32 dqnt_str_to_i32(char *const buf, const i32 bufSize) if (buf[index] == '-') isNegative = true; index++; } - else if (!dqnt_char_is_digit(buf[index])) + else if (!dqn_char_is_digit(buf[index])) { return 0; } @@ -1003,7 +1003,7 @@ DQNT_FILE_SCOPE i32 dqnt_str_to_i32(char *const buf, const i32 bufSize) i32 result = 0; for (i32 i = index; i < bufSize; i++) { - if (dqnt_char_is_digit(buf[i])) + if (dqn_char_is_digit(buf[i])) { result *= 10; result += (buf[i] - '0'); @@ -1019,7 +1019,7 @@ DQNT_FILE_SCOPE i32 dqnt_str_to_i32(char *const buf, const i32 bufSize) return result; } -DQNT_FILE_SCOPE void dqnt_i32_to_str(i32 value, char *buf, i32 bufSize) +DQN_FILE_SCOPE void dqn_i32_to_str(i32 value, char *buf, i32 bufSize) { if (!buf || bufSize == 0) return; @@ -1036,7 +1036,7 @@ DQNT_FILE_SCOPE void dqnt_i32_to_str(i32 value, char *buf, i32 bufSize) if (negative) buf[charIndex++] = '-'; - i32 val = DQNT_ABS(value); + i32 val = DQN_ABS(value); while (val != 0 && charIndex < bufSize) { i32 rem = val % 10; @@ -1048,11 +1048,11 @@ DQNT_FILE_SCOPE void dqnt_i32_to_str(i32 value, char *buf, i32 bufSize) // from the second character, so we don't put the negative sign at the end if (negative) { - dqnt_str_reverse(buf + 1, charIndex - 1); + dqn_str_reverse(buf + 1, charIndex - 1); } else { - dqnt_str_reverse(buf, charIndex); + dqn_str_reverse(buf, charIndex); } } @@ -1076,7 +1076,7 @@ DQNT_FILE_SCOPE void dqnt_i32_to_str(i32 value, char *buf, i32 bufSize) The UCS code values 0xd800–0xdfff (UTF-16 surrogates) as well as 0xfffe and 0xffff (UCS noncharacters) should not appear in conforming UTF-8 streams. */ -DQNT_FILE_SCOPE u32 dqnt_ucs_to_utf8(u32 *dest, u32 character) +DQN_FILE_SCOPE u32 dqn_ucs_to_utf8(u32 *dest, u32 character) { if (!dest) return 0; @@ -1145,7 +1145,7 @@ DQNT_FILE_SCOPE u32 dqnt_ucs_to_utf8(u32 *dest, u32 character) return 0; } -DQNT_FILE_SCOPE u32 dqnt_utf8_to_ucs(u32 *dest, u32 character) +DQN_FILE_SCOPE u32 dqn_utf8_to_ucs(u32 *dest, u32 character) { if (!dest) return 0; @@ -1221,24 +1221,24 @@ DQNT_FILE_SCOPE u32 dqnt_utf8_to_ucs(u32 *dest, u32 character) //////////////////////////////////////////////////////////////////////////////// // File Operations //////////////////////////////////////////////////////////////////////////////// -#ifdef DQNT_WIN32 - #define DQNT_WIN32_ERROR_BOX(text, title) MessageBoxA(NULL, text, title, MB_OK); +#ifdef DQN_WIN32 + #define DQN_WIN32_ERROR_BOX(text, title) MessageBoxA(NULL, text, title, MB_OK); -FILE_SCOPE bool dqnt_win32_utf8_to_wchar_internal(char *in, wchar_t *out, +FILE_SCOPE bool dqn_win32_utf8_to_wchar_internal(char *in, wchar_t *out, i32 outLen) { u32 result = MultiByteToWideChar(CP_UTF8, 0, in, -1, out, outLen-1); if (result == 0xFFFD || 0) { - DQNT_WIN32_ERROR_BOX("WideCharToMultiByte() failed.", NULL); + DQN_WIN32_ERROR_BOX("WideCharToMultiByte() failed.", NULL); return false; } return true; } -FILE_SCOPE bool dqnt_win32_wchar_to_utf8_internal(wchar_t *in, char *out, +FILE_SCOPE bool dqn_win32_wchar_to_utf8_internal(wchar_t *in, char *out, i32 outLen) { u32 result = @@ -1246,7 +1246,7 @@ FILE_SCOPE bool dqnt_win32_wchar_to_utf8_internal(wchar_t *in, char *out, if (result == 0xFFFD || 0) { - DQNT_WIN32_ERROR_BOX("WideCharToMultiByte() failed.", NULL); + DQN_WIN32_ERROR_BOX("WideCharToMultiByte() failed.", NULL); return false; } @@ -1254,28 +1254,28 @@ FILE_SCOPE bool dqnt_win32_wchar_to_utf8_internal(wchar_t *in, char *out, } #endif -DQNT_FILE_SCOPE bool dqnt_file_open(char *const path, DqntFile *file) +DQN_FILE_SCOPE bool dqn_file_open(char *const path, DqnFile *file) { if (!file || !path) return false; -#ifdef DQNT_WIN32 +#ifdef DQN_WIN32 wchar_t widePath[MAX_PATH] = {}; - dqnt_win32_utf8_to_wchar_internal(path, widePath, - DQNT_ARRAY_COUNT(widePath)); + dqn_win32_utf8_to_wchar_internal(path, widePath, + DQN_ARRAY_COUNT(widePath)); HANDLE handle = CreateFileW(widePath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (handle == INVALID_HANDLE_VALUE) { - DQNT_WIN32_ERROR_BOX("CreateFile() failed.", NULL); + DQN_WIN32_ERROR_BOX("CreateFile() failed.", NULL); return false; } LARGE_INTEGER size; if (GetFileSizeEx(handle, &size) == 0) { - DQNT_WIN32_ERROR_BOX("GetFileSizeEx() failed.", NULL); + DQN_WIN32_ERROR_BOX("GetFileSizeEx() failed.", NULL); return false; } @@ -1289,10 +1289,10 @@ DQNT_FILE_SCOPE bool dqnt_file_open(char *const path, DqntFile *file) return true; } -DQNT_FILE_SCOPE u32 dqnt_file_read(DqntFile file, u8 *buffer, u32 numBytesToRead) +DQN_FILE_SCOPE u32 dqn_file_read(DqnFile file, u8 *buffer, u32 numBytesToRead) { u32 numBytesRead = 0; -#ifdef DQNT_WIN32 +#ifdef DQN_WIN32 if (file.handle && buffer) { DWORD bytesRead = 0; @@ -1304,7 +1304,7 @@ DQNT_FILE_SCOPE u32 dqnt_file_read(DqntFile file, u8 *buffer, u32 numBytesToRead // TODO(doyle): 0 also means it is completing async, but still valid if (result == 0) { - DQNT_WIN32_ERROR_BOX("ReadFile() failed.", NULL); + DQN_WIN32_ERROR_BOX("ReadFile() failed.", NULL); } numBytesRead = (u32)bytesRead; @@ -1314,9 +1314,9 @@ DQNT_FILE_SCOPE u32 dqnt_file_read(DqntFile file, u8 *buffer, u32 numBytesToRead return numBytesRead; } -DQNT_FILE_SCOPE inline void dqnt_file_close(DqntFile *file) +DQN_FILE_SCOPE inline void dqn_file_close(DqnFile *file) { -#ifdef DQNT_WIN32 +#ifdef DQN_WIN32 if (file && file->handle) { CloseHandle(file->handle); @@ -1326,14 +1326,14 @@ DQNT_FILE_SCOPE inline void dqnt_file_close(DqntFile *file) #endif } -DQNT_FILE_SCOPE char **dqnt_dir_read(char *dir, u32 *numFiles) +DQN_FILE_SCOPE char **dqn_dir_read(char *dir, u32 *numFiles) { if (!dir) return NULL; -#ifdef DQNT_WIN32 +#ifdef DQN_WIN32 u32 currNumFiles = 0; wchar_t wideDir[MAX_PATH] = {}; - dqnt_win32_utf8_to_wchar_internal(dir, wideDir, DQNT_ARRAY_COUNT(wideDir)); + dqn_win32_utf8_to_wchar_internal(dir, wideDir, DQN_ARRAY_COUNT(wideDir)); // Enumerate number of files first { @@ -1341,7 +1341,7 @@ DQNT_FILE_SCOPE char **dqnt_dir_read(char *dir, u32 *numFiles) HANDLE findHandle = FindFirstFileW(wideDir, &findData); if (findHandle == INVALID_HANDLE_VALUE) { - DQNT_WIN32_ERROR_BOX("FindFirstFile() failed.", NULL); + DQN_WIN32_ERROR_BOX("FindFirstFile() failed.", NULL); return NULL; } @@ -1357,7 +1357,7 @@ DQNT_FILE_SCOPE char **dqnt_dir_read(char *dir, u32 *numFiles) HANDLE findHandle = FindFirstFileW(wideDir, &initFind); if (findHandle == INVALID_HANDLE_VALUE) { - DQNT_WIN32_ERROR_BOX("FindFirstFile() failed.", NULL); + DQN_WIN32_ERROR_BOX("FindFirstFile() failed.", NULL); return NULL; } @@ -1365,7 +1365,7 @@ DQNT_FILE_SCOPE char **dqnt_dir_read(char *dir, u32 *numFiles) char **list = (char **)calloc(1, sizeof(*list) * (currNumFiles)); if (!list) { - DQNT_WIN32_ERROR_BOX("calloc() failed.", NULL); + DQN_WIN32_ERROR_BOX("calloc() failed.", NULL); return NULL; } @@ -1379,7 +1379,7 @@ DQNT_FILE_SCOPE char **dqnt_dir_read(char *dir, u32 *numFiles) free(list[j]); } - DQNT_WIN32_ERROR_BOX("calloc() failed.", NULL); + DQN_WIN32_ERROR_BOX("calloc() failed.", NULL); return NULL; } } @@ -1388,7 +1388,7 @@ DQNT_FILE_SCOPE char **dqnt_dir_read(char *dir, u32 *numFiles) WIN32_FIND_DATAW findData = {}; while (FindNextFileW(findHandle, &findData) != 0) { - dqnt_win32_wchar_to_utf8_internal( + dqn_win32_wchar_to_utf8_internal( findData.cFileName, list[listIndex++], MAX_PATH); } @@ -1400,7 +1400,7 @@ DQNT_FILE_SCOPE char **dqnt_dir_read(char *dir, u32 *numFiles) #endif } -DQNT_FILE_SCOPE inline void dqnt_dir_read_free(char **fileList, u32 numFiles) +DQN_FILE_SCOPE inline void dqn_dir_read_free(char **fileList, u32 numFiles) { if (fileList) { @@ -1417,14 +1417,14 @@ DQNT_FILE_SCOPE inline void dqnt_dir_read_free(char **fileList, u32 numFiles) //////////////////////////////////////////////////////////////////////////////// // Timer //////////////////////////////////////////////////////////////////////////////// -#ifdef DQNT_WIN32 -inline FILE_SCOPE f64 dqnt_win32_query_perf_counter_time_in_s_internal() +#ifdef DQN_WIN32 +inline FILE_SCOPE f64 dqn_win32_query_perf_counter_time_in_s_internal() { LOCAL_PERSIST LARGE_INTEGER queryPerformanceFrequency = {}; if (queryPerformanceFrequency.QuadPart == 0) { QueryPerformanceFrequency(&queryPerformanceFrequency); - DQNT_ASSERT(queryPerformanceFrequency.QuadPart != 0); + DQN_ASSERT(queryPerformanceFrequency.QuadPart != 0); } LARGE_INTEGER qpcResult; @@ -1437,19 +1437,19 @@ inline FILE_SCOPE f64 dqnt_win32_query_perf_counter_time_in_s_internal() } #endif -f64 dqnt_time_now_in_s() +f64 dqn_time_now_in_s() { #ifdef _WIN32 - return dqnt_win32_query_perf_counter_time_in_s_internal(); + return dqn_win32_query_perf_counter_time_in_s_internal(); #else - DQNT_ASSERT(DQNT_INVALID_CODE_PATH); + DQN_ASSERT(DQN_INVALID_CODE_PATH); return 0; #endif }; -f64 dqnt_time_now_in_ms() +f64 dqn_time_now_in_ms() { - return dqnt_time_now_in_s() * 1000.0f; + return dqn_time_now_in_s() * 1000.0f; } //////////////////////////////////////////////////////////////////////////////// @@ -1460,7 +1460,7 @@ f64 dqnt_time_now_in_ms() // Convert a randomized u32 value to a float value x in the range 0.0f <= x // < 1.0f. Contributed by Jonatan Hedborg -FILE_SCOPE f32 dqnt_rnd_f32_normalized_from_u32_internal(u32 value) +FILE_SCOPE f32 dqn_rnd_f32_normalized_from_u32_internal(u32 value) { u32 exponent = 127; u32 mantissa = value >> 9; @@ -1469,7 +1469,7 @@ FILE_SCOPE f32 dqnt_rnd_f32_normalized_from_u32_internal(u32 value) return fresult - 1.0f; } -FILE_SCOPE u64 dqnt_rnd_murmur3_avalanche64_internal(u64 h) +FILE_SCOPE u64 dqn_rnd_murmur3_avalanche64_internal(u64 h) { h ^= h >> 33; h *= 0xff51afd7ed558ccd; @@ -1479,7 +1479,7 @@ FILE_SCOPE u64 dqnt_rnd_murmur3_avalanche64_internal(u64 h) return h; } -FILE_SCOPE u32 dqnt_rnd_make_seed_internal() +FILE_SCOPE u32 dqn_rnd_make_seed_internal() { #ifdef _WIN32 __int64 numClockCycles = __rdtsc(); @@ -1490,24 +1490,24 @@ FILE_SCOPE u32 dqnt_rnd_make_seed_internal() #endif } -DQNT_FILE_SCOPE void dqnt_rnd_pcg_init_with_seed(DqntRandPCGState *pcg, u32 seed) +DQN_FILE_SCOPE void dqn_rnd_pcg_init_with_seed(DqnRandPCGState *pcg, u32 seed) { u64 value = (((u64)seed) << 1ULL) | 1ULL; - value = dqnt_rnd_murmur3_avalanche64_internal(value); + value = dqn_rnd_murmur3_avalanche64_internal(value); pcg->state[0] = 0U; pcg->state[1] = (value << 1ULL) | 1ULL; - dqnt_rnd_pcg_next(pcg); - pcg->state[0] += dqnt_rnd_murmur3_avalanche64_internal(value); - dqnt_rnd_pcg_next(pcg); + dqn_rnd_pcg_next(pcg); + pcg->state[0] += dqn_rnd_murmur3_avalanche64_internal(value); + dqn_rnd_pcg_next(pcg); } -DQNT_FILE_SCOPE void dqnt_rnd_pcg_init(DqntRandPCGState *pcg) +DQN_FILE_SCOPE void dqn_rnd_pcg_init(DqnRandPCGState *pcg) { - u32 seed = dqnt_rnd_make_seed_internal(); - dqnt_rnd_pcg_init_with_seed(pcg, seed); + u32 seed = dqn_rnd_make_seed_internal(); + dqn_rnd_pcg_init_with_seed(pcg, seed); } -DQNT_FILE_SCOPE u32 dqnt_rnd_pcg_next(DqntRandPCGState *pcg) +DQN_FILE_SCOPE u32 dqn_rnd_pcg_next(DqnRandPCGState *pcg) { u64 oldstate = pcg->state[0]; pcg->state[0] = oldstate * 0x5851f42d4c957f2dULL + pcg->state[1]; @@ -1516,17 +1516,17 @@ DQNT_FILE_SCOPE u32 dqnt_rnd_pcg_next(DqntRandPCGState *pcg) return (xorshifted >> rot) | (xorshifted << ((-(i32)rot) & 31)); } -DQNT_FILE_SCOPE f32 dqnt_rnd_pcg_nextf(DqntRandPCGState *pcg) +DQN_FILE_SCOPE f32 dqn_rnd_pcg_nextf(DqnRandPCGState *pcg) { - return dqnt_rnd_f32_normalized_from_u32_internal(dqnt_rnd_pcg_next(pcg)); + return dqn_rnd_f32_normalized_from_u32_internal(dqn_rnd_pcg_next(pcg)); } -DQNT_FILE_SCOPE i32 dqnt_rnd_pcg_range(DqntRandPCGState *pcg, i32 min, i32 max) +DQN_FILE_SCOPE i32 dqn_rnd_pcg_range(DqnRandPCGState *pcg, i32 min, i32 max) { i32 const range = (max - min) + 1; if (range <= 0) return min; - i32 const value = (i32)(dqnt_rnd_pcg_nextf(pcg) * range); + i32 const value = (i32)(dqn_rnd_pcg_nextf(pcg) * range); return min + value; } -#endif /* DQNT_IMPLEMENTATION */ +#endif /* DQN_IMPLEMENTATION */ diff --git a/dqn_unit_test.cpp b/dqn_unit_test.cpp new file mode 100644 index 0000000..2d88b87 --- /dev/null +++ b/dqn_unit_test.cpp @@ -0,0 +1,699 @@ +#define DQN_IMPLEMENTATION +#include "dqn.h" + +#include "stdio.h" + +void dqn_strings_test() +{ + { // Char Checks + DQN_ASSERT(dqn_char_is_alpha('a') == true); + DQN_ASSERT(dqn_char_is_alpha('A') == true); + DQN_ASSERT(dqn_char_is_alpha('0') == false); + DQN_ASSERT(dqn_char_is_alpha('@') == false); + DQN_ASSERT(dqn_char_is_alpha(' ') == false); + DQN_ASSERT(dqn_char_is_alpha('\n') == false); + + DQN_ASSERT(dqn_char_is_digit('1') == true); + DQN_ASSERT(dqn_char_is_digit('n') == false); + DQN_ASSERT(dqn_char_is_digit('N') == false); + DQN_ASSERT(dqn_char_is_digit('*') == false); + DQN_ASSERT(dqn_char_is_digit(' ') == false); + DQN_ASSERT(dqn_char_is_digit('\n') == false); + + DQN_ASSERT(dqn_char_is_alphanum('1') == true); + DQN_ASSERT(dqn_char_is_alphanum('a') == true); + DQN_ASSERT(dqn_char_is_alphanum('A') == true); + DQN_ASSERT(dqn_char_is_alphanum('*') == false); + DQN_ASSERT(dqn_char_is_alphanum(' ') == false); + DQN_ASSERT(dqn_char_is_alphanum('\n') == false); + + printf("dqn_strings_test(): char_checks: Completed successfully\n"); + } + + // String Checks + { + // strcmp + { + char *a = "str_a"; + + // Check simple compares + { + DQN_ASSERT(dqn_strcmp(a, "str_a") == +0); + DQN_ASSERT(dqn_strcmp(a, "str_b") == -1); + DQN_ASSERT(dqn_strcmp("str_b", a) == +1); + DQN_ASSERT(dqn_strcmp(a, "") == +1); + DQN_ASSERT(dqn_strcmp("", "") == 0); + + // NOTE: Check that the string has not been trashed. + DQN_ASSERT(dqn_strcmp(a, "str_a") == +0); + } + + // Check ops against null + { + DQN_ASSERT(dqn_strcmp(NULL, NULL) != +0); + DQN_ASSERT(dqn_strcmp(a, NULL) != +0); + DQN_ASSERT(dqn_strcmp(NULL, a) != +0); + } + + printf("dqn_strings_test(): strcmp: Completed successfully\n"); + } + + // strlen + { + char *a = "str_a"; + DQN_ASSERT(dqn_strlen(a) == 5); + DQN_ASSERT(dqn_strlen("") == 0); + DQN_ASSERT(dqn_strlen(" a ") == 6); + DQN_ASSERT(dqn_strlen("a\n") == 2); + + // NOTE: Check that the string has not been trashed. + DQN_ASSERT(dqn_strcmp(a, "str_a") == 0); + + DQN_ASSERT(dqn_strlen(NULL) == 0); + + printf("dqn_strings_test(): strlen: Completed successfully\n"); + } + + // strncpy + { + { + char *a = "str_a"; + char b[10] = {}; + // Check copy into empty array + { + char *result = dqn_strncpy(b, a, dqn_strlen(a)); + DQN_ASSERT(dqn_strcmp(b, "str_a") == 0); + DQN_ASSERT(dqn_strcmp(a, "str_a") == 0); + DQN_ASSERT(dqn_strcmp(result, "str_a") == 0); + DQN_ASSERT(dqn_strlen(result) == 5); + } + + // Check copy into array offset, overlap with old results + { + char *newResult = dqn_strncpy(&b[1], a, dqn_strlen(a)); + DQN_ASSERT(dqn_strcmp(newResult, "str_a") == 0); + DQN_ASSERT(dqn_strlen(newResult) == 5); + + DQN_ASSERT(dqn_strcmp(a, "str_a") == 0); + DQN_ASSERT(dqn_strlen(a) == 5); + + DQN_ASSERT(dqn_strcmp(b, "sstr_a") == 0); + DQN_ASSERT(dqn_strlen(b) == 6); + } + } + + // Check strncpy with NULL pointers + { + DQN_ASSERT(dqn_strncpy(NULL, NULL, 5) == NULL); + + char *a = "str"; + char *result = dqn_strncpy(a, NULL, 5); + + DQN_ASSERT(dqn_strcmp(a, "str") == 0); + DQN_ASSERT(dqn_strcmp(result, "str") == 0); + DQN_ASSERT(dqn_strcmp(result, a) == 0); + } + + // Check strncpy with 0 chars to copy + { + char *a = "str"; + char *b = "ing"; + + char *result = dqn_strncpy(a, b, 0); + DQN_ASSERT(dqn_strcmp(a, "str") == 0); + DQN_ASSERT(dqn_strcmp(b, "ing") == 0); + DQN_ASSERT(dqn_strcmp(result, "str") == 0); + } + + printf("dqn_strings_test(): strncpy: Completed successfully\n"); + } + + // str_reverse + { + // Basic reverse operations + { + char a[] = "aba"; + DQN_ASSERT(dqn_str_reverse(a, dqn_strlen(a)) == true); + DQN_ASSERT(dqn_strcmp(a, "aba") == 0); + + DQN_ASSERT(dqn_str_reverse(a, 2) == true); + DQN_ASSERT(dqn_strcmp(a, "baa") == 0); + + DQN_ASSERT(dqn_str_reverse(a, dqn_strlen(a)) == true); + DQN_ASSERT(dqn_strcmp(a, "aab") == 0); + + DQN_ASSERT(dqn_str_reverse(&a[1], 2) == true); + DQN_ASSERT(dqn_strcmp(a, "aba") == 0); + + DQN_ASSERT(dqn_str_reverse(a, 0) == true); + DQN_ASSERT(dqn_strcmp(a, "aba") == 0); + } + + // Try reverse empty string + { + char a[] = ""; + DQN_ASSERT(dqn_str_reverse(a, dqn_strlen(a)) == true); + DQN_ASSERT(dqn_strcmp(a, "") == 0); + } + + // Try reverse single char string + { + char a[] = "a"; + DQN_ASSERT(dqn_str_reverse(a, dqn_strlen(a)) == true); + DQN_ASSERT(dqn_strcmp(a, "a") == 0); + + DQN_ASSERT(dqn_str_reverse(a, 0) == true); + DQN_ASSERT(dqn_strcmp(a, "a") == 0); + } + + printf( + "dqn_strings_test(): str_reverse: Completed successfully\n"); + } + + // str_to_i32 + { + char *a = "123"; + DQN_ASSERT(dqn_str_to_i32(a, dqn_strlen(a)) == 123); + + char *b = "-123"; + DQN_ASSERT(dqn_str_to_i32(b, dqn_strlen(b)) == -123); + DQN_ASSERT(dqn_str_to_i32(b, 1) == 0); + DQN_ASSERT(dqn_str_to_i32(&b[1], dqn_strlen(&b[1])) == 123); + + char *c = "-0"; + DQN_ASSERT(dqn_str_to_i32(c, dqn_strlen(c)) == 0); + + char *d = "+123"; + DQN_ASSERT(dqn_str_to_i32(d, dqn_strlen(d)) == 123); + DQN_ASSERT(dqn_str_to_i32(&d[1], dqn_strlen(&d[1])) == 123); + + printf("dqn_strings_test(): str_to_i32: Completed successfully\n"); + } + + // i32_to_str + { + char a[DQN_I32_TO_STR_MAX_BUF_SIZE] = {}; + dqn_i32_to_str(+100, a, DQN_ARRAY_COUNT(a)); + DQN_ASSERT(dqn_strcmp(a, "100") == 0); + + char b[DQN_I32_TO_STR_MAX_BUF_SIZE] = {}; + dqn_i32_to_str(-100, b, DQN_ARRAY_COUNT(b)); + DQN_ASSERT(dqn_strcmp(b, "-100") == 0); + + char c[DQN_I32_TO_STR_MAX_BUF_SIZE] = {}; + dqn_i32_to_str(0, c, DQN_ARRAY_COUNT(c)); + DQN_ASSERT(dqn_strcmp(c, "0") == 0); + + printf("dqn_strings_test(): str_to_i32: Completed successfully\n"); + } + } + + // UCS <-> UTF8 Checks + { + // Test ascii characters + { + u32 codepoint = '@'; + u32 string[1] = {}; + + u32 bytesUsed = dqn_ucs_to_utf8(&string[0], codepoint); + DQN_ASSERT(bytesUsed == 1); + DQN_ASSERT(string[0] == '@'); + + bytesUsed = dqn_utf8_to_ucs(&string[0], codepoint); + DQN_ASSERT(string[0] >= 0 && string[0] < 0x80); + DQN_ASSERT(bytesUsed == 1); + } + + // Test 2 byte characters + { + u32 codepoint = 0x278; + u32 string[1] = {}; + + u32 bytesUsed = dqn_ucs_to_utf8(&string[0], codepoint); + DQN_ASSERT(bytesUsed == 2); + DQN_ASSERT(string[0] == 0xC9B8); + + bytesUsed = dqn_utf8_to_ucs(&string[0], string[0]); + DQN_ASSERT(string[0] == codepoint); + DQN_ASSERT(bytesUsed == 2); + } + + // Test 3 byte characters + { + u32 codepoint = 0x0A0A; + u32 string[1] = {}; + + u32 bytesUsed = dqn_ucs_to_utf8(&string[0], codepoint); + DQN_ASSERT(bytesUsed == 3); + DQN_ASSERT(string[0] == 0xE0A88A); + + bytesUsed = dqn_utf8_to_ucs(&string[0], string[0]); + DQN_ASSERT(string[0] == codepoint); + DQN_ASSERT(bytesUsed == 3); + } + + // Test 4 byte characters + { + u32 codepoint = 0x10912; + u32 string[1] = {}; + u32 bytesUsed = dqn_ucs_to_utf8(&string[0], codepoint); + + DQN_ASSERT(bytesUsed == 4); + DQN_ASSERT(string[0] == 0xF090A492); + + bytesUsed = dqn_utf8_to_ucs(&string[0], string[0]); + DQN_ASSERT(string[0] == codepoint); + DQN_ASSERT(bytesUsed == 4); + } + + { + u32 codepoint = 0x10912; + u32 bytesUsed = dqn_ucs_to_utf8(NULL, codepoint); + DQN_ASSERT(bytesUsed == 0); + + bytesUsed = dqn_utf8_to_ucs(NULL, codepoint); + DQN_ASSERT(bytesUsed == 0); + } + + printf("dqn_strings_test(): ucs <-> utf8: Completed successfully\n"); + } + + printf("dqn_strings_test(): Completed successfully\n"); +} + +#include "Windows.h" +#define WIN32_LEAN_AND_MEAN +void dqn_other_test() +{ + { // Test Win32 Sleep + // NOTE: Win32 Sleep is not granular to a certain point so sleep excessively + u32 sleepInMs = 1000; + f64 startInMs = dqn_time_now_in_ms(); + Sleep(sleepInMs); + f64 endInMs = dqn_time_now_in_ms(); + + DQN_ASSERT(startInMs < endInMs); + printf("dqn_other_test(): time_now: Completed successfully\n"); + } + printf("dqn_other_test(): Completed successfully\n"); +} + +void dqn_random_test() { + + DqnRandPCGState pcg; + dqn_rnd_pcg_init(&pcg); + for (i32 i = 0; i < 10; i++) + { + i32 min = -100; + i32 max = 100000; + i32 result = dqn_rnd_pcg_range(&pcg, min, max); + DQN_ASSERT(result >= min && result <= max) + + f32 randF32 = dqn_rnd_pcg_nextf(&pcg); + DQN_ASSERT(randF32 >= 0.0f && randF32 <= 1.0f); + printf("dqn_random_test(): rnd_pcg: Completed successfully\n"); + } + + printf("dqn_random_test(): Completed successfully\n"); +} + +void dqn_math_test() +{ + { // Lerp + { + f32 start = 10; + f32 t = 0.5f; + f32 end = 20; + DQN_ASSERT(dqn_math_lerp(start, t, end) == 15); + } + + { + f32 start = 10; + f32 t = 2.0f; + f32 end = 20; + DQN_ASSERT(dqn_math_lerp(start, t, end) == 30); + } + + printf("dqn_math_test(): lerp: Completed successfully\n"); + } + + { // sqrtf + DQN_ASSERT(dqn_math_sqrtf(4.0f) == 2.0f); + printf("dqn_math_test(): sqrtf: Completed successfully\n"); + } + + printf("dqn_math_test(): Completed successfully\n"); +} + +void dqn_vec_test() +{ + { // V2 + + // V2 Creating + { + DqnV2 vec = dqn_v2(5.5f, 5.0f); + DQN_ASSERT(vec.x == 5.5f && vec.y == 5.0f); + DQN_ASSERT(vec.w == 5.5f && vec.h == 5.0f); + } + + // V2i Creating + { + DqnV2 vec = dqn_v2i(3, 5); + DQN_ASSERT(vec.x == 3 && vec.y == 5.0f); + DQN_ASSERT(vec.w == 3 && vec.h == 5.0f); + } + + // V2 Arithmetic + { + DqnV2 vecA = dqn_v2(5, 10); + DqnV2 vecB = dqn_v2i(2, 3); + DQN_ASSERT(dqn_v2_equals(vecA, vecB) == false); + DQN_ASSERT(dqn_v2_equals(vecA, dqn_v2(5, 10)) == true); + DQN_ASSERT(dqn_v2_equals(vecB, dqn_v2(2, 3)) == true); + + DqnV2 result = dqn_v2_add(vecA, dqn_v2(5, 10)); + DQN_ASSERT(dqn_v2_equals(result, dqn_v2(10, 20)) == true); + + result = dqn_v2_sub(result, dqn_v2(5, 10)); + DQN_ASSERT(dqn_v2_equals(result, dqn_v2(5, 10)) == true); + + result = dqn_v2_scale(result, 5); + DQN_ASSERT(dqn_v2_equals(result, dqn_v2(25, 50)) == true); + + result = dqn_v2_hadamard(result, dqn_v2(10, 0.5f)); + DQN_ASSERT(dqn_v2_equals(result, dqn_v2(250, 25)) == true); + + f32 dotResult = dqn_v2_dot(dqn_v2(5, 10), dqn_v2(3, 4)); + DQN_ASSERT(dotResult == 55); + } + + // V2 Properties + { + DqnV2 a = dqn_v2(0, 0); + DqnV2 b = dqn_v2(3, 4); + + f32 lengthSq = dqn_v2_length_squared(a, b); + DQN_ASSERT(lengthSq == 25); + + f32 length = dqn_v2_length(a, b); + DQN_ASSERT(length == 5); + + DqnV2 normalised = dqn_v2_normalise(b); + DQN_ASSERT(normalised.x == (b.x / 5.0f)); + DQN_ASSERT(normalised.y == (b.y / 5.0f)); + + DqnV2 c = dqn_v2(3.5f, 8.0f); + DQN_ASSERT(dqn_v2_overlaps(b, c) == true); + DQN_ASSERT(dqn_v2_overlaps(b, a) == false); + + DqnV2 d = dqn_v2_perpendicular(c); + DQN_ASSERT(dqn_v2_dot(c, d) == 0); + } + + { // constrain_to_ratio + DqnV2 ratio = dqn_v2(16, 9); + DqnV2 dim = dqn_v2(2000, 1080); + DqnV2 result = dqn_v2_constrain_to_ratio(dim, ratio); + DQN_ASSERT(result.w == 1920 && result.h == 1080); + } + + printf("dqn_vec_test(): vec2: Completed successfully\n"); + } + + { // V3 + + // V3i Creating + { + DqnV3 vec = dqn_v3(5.5f, 5.0f, 5.875f); + DQN_ASSERT(vec.x == 5.5f && vec.y == 5.0f && vec.z == 5.875f); + DQN_ASSERT(vec.r == 5.5f && vec.g == 5.0f && vec.b == 5.875f); + } + + // V3i Creating + { + DqnV3 vec = dqn_v3i(3, 4, 5); + DQN_ASSERT(vec.x == 3 && vec.y == 4 && vec.z == 5); + DQN_ASSERT(vec.r == 3 && vec.g == 4 && vec.b == 5); + } + + // V3 Arithmetic + { + DqnV3 vecA = dqn_v3(5, 10, 15); + DqnV3 vecB = dqn_v3(2, 3, 6); + DQN_ASSERT(dqn_v3_equals(vecA, vecB) == false); + DQN_ASSERT(dqn_v3_equals(vecA, dqn_v3(5, 10, 15)) == true); + DQN_ASSERT(dqn_v3_equals(vecB, dqn_v3(2, 3, 6)) == true); + + DqnV3 result = dqn_v3_add(vecA, dqn_v3(5, 10, 15)); + DQN_ASSERT(dqn_v3_equals(result, dqn_v3(10, 20, 30)) == true); + + result = dqn_v3_sub(result, dqn_v3(5, 10, 15)); + DQN_ASSERT(dqn_v3_equals(result, dqn_v3(5, 10, 15)) == true); + + result = dqn_v3_scale(result, 5); + DQN_ASSERT(dqn_v3_equals(result, dqn_v3(25, 50, 75)) == true); + + result = dqn_v3_hadamard(result, dqn_v3(10.0f, 0.5f, 10.0f)); + DQN_ASSERT(dqn_v3_equals(result, dqn_v3(250, 25, 750)) == true); + + f32 dotResult = dqn_v3_dot(dqn_v3(5, 10, 2), dqn_v3(3, 4, 6)); + DQN_ASSERT(dotResult == 67); + + DqnV3 cross = dqn_v3_cross(vecA, vecB); + DQN_ASSERT(dqn_v3_equals(cross, dqn_v3(15, 0, -5)) == true); + } + + printf("dqn_vec_test(): vec3: Completed successfully\n"); + } + + { // V4 + + // V4 Creating + { + DqnV4 vec = dqn_v4(5.5f, 5.0f, 5.875f, 5.928f); + DQN_ASSERT(vec.x == 5.5f && vec.y == 5.0f && vec.z == 5.875f && vec.w == 5.928f); + DQN_ASSERT(vec.r == 5.5f && vec.g == 5.0f && vec.b == 5.875f && vec.a == 5.928f); + } + + // V4i Creating + { + DqnV4 vec = dqn_v4i(3, 4, 5, 6); + DQN_ASSERT(vec.x == 3 && vec.y == 4 && vec.z == 5 && vec.w == 6); + DQN_ASSERT(vec.r == 3 && vec.g == 4 && vec.b == 5 && vec.a == 6); + } + + // V4 Arithmetic + { + DqnV4 vecA = dqn_v4(5, 10, 15, 20); + DqnV4 vecB = dqn_v4i(2, 3, 6, 8); + DQN_ASSERT(dqn_v4_equals(vecA, vecB) == false); + DQN_ASSERT(dqn_v4_equals(vecA, dqn_v4(5, 10, 15, 20)) == true); + DQN_ASSERT(dqn_v4_equals(vecB, dqn_v4(2, 3, 6, 8)) == true); + + DqnV4 result = dqn_v4_add(vecA, dqn_v4(5, 10, 15, 20)); + DQN_ASSERT(dqn_v4_equals(result, dqn_v4(10, 20, 30, 40)) == true); + + result = dqn_v4_sub(result, dqn_v4(5, 10, 15, 20)); + DQN_ASSERT(dqn_v4_equals(result, dqn_v4(5, 10, 15, 20)) == true); + + result = dqn_v4_scale(result, 5); + DQN_ASSERT(dqn_v4_equals(result, dqn_v4(25, 50, 75, 100)) == true); + + result = dqn_v4_hadamard(result, dqn_v4(10, 0.5f, 10, 0.25f)); + DQN_ASSERT(dqn_v4_equals(result, dqn_v4(250, 25, 750, 25)) == true); + + f32 dotResult = dqn_v4_dot(dqn_v4(5, 10, 2, 8), dqn_v4(3, 4, 6, 5)); + DQN_ASSERT(dotResult == 107); + } + + printf("dqn_vec_test(): vec4: Completed successfully\n"); + } + + // Rect + { + DqnRect rect = dqn_rect(dqn_v2(-10, -10), dqn_v2(20, 20)); + DQN_ASSERT(dqn_v2_equals(rect.min, dqn_v2(-10, -10))); + DQN_ASSERT(dqn_v2_equals(rect.max, dqn_v2(10, 10))); + + f32 width, height; + dqn_rect_get_size_2f(rect, &width, &height); + DQN_ASSERT(width == 20); + DQN_ASSERT(height == 20); + + DqnV2 dim = dqn_rect_get_size_v2(rect); + DQN_ASSERT(dqn_v2_equals(dim, dqn_v2(20, 20))); + + DqnV2 rectCenter = dqn_rect_get_centre(rect); + DQN_ASSERT(dqn_v2_equals(rectCenter, dqn_v2(0, 0))); + + // Test shifting rect + DqnRect shiftedRect = dqn_rect_move(rect, dqn_v2(10, 0)); + DQN_ASSERT(dqn_v2_equals(shiftedRect.min, dqn_v2(0, -10))); + DQN_ASSERT(dqn_v2_equals(shiftedRect.max, dqn_v2(20, 10))); + + dqn_rect_get_size_2f(shiftedRect, &width, &height); + DQN_ASSERT(width == 20); + DQN_ASSERT(height == 20); + + dim = dqn_rect_get_size_v2(shiftedRect); + DQN_ASSERT(dqn_v2_equals(dim, dqn_v2(20, 20))); + + // Test rect contains p + DqnV2 inP = dqn_v2(5, 5); + DqnV2 outP = dqn_v2(100, 100); + DQN_ASSERT(dqn_rect_contains_p(shiftedRect, inP)); + DQN_ASSERT(!dqn_rect_contains_p(shiftedRect, outP)); + + printf("dqn_vec_test(): rect: Completed successfully\n"); + } + + printf("dqn_vec_test(): Completed successfully\n"); +} + +void dqn_darray_test() +{ + { + DqnV2 *vecDArray = DQN_DARRAY_INIT(DqnV2, 1); + DQN_ASSERT(vecDArray); + DQN_ASSERT(dqn_darray_get_capacity(vecDArray) == 1); + DQN_ASSERT(dqn_darray_get_num_items(vecDArray) == 0); + + // Test basic insert + { + DqnV2 va = dqn_v2(5, 10); + DQN_ASSERT(DQN_DARRAY_PUSH(&vecDArray, &va)); + + DqnV2 vb = vecDArray[0]; + DQN_ASSERT(dqn_v2_equals(va, vb)); + + DQN_ASSERT(dqn_darray_get_capacity(vecDArray) == 1); + DQN_ASSERT(dqn_darray_get_num_items(vecDArray) == 1); + + DqnV2 *empty = NULL; + DQN_ASSERT(DQN_DARRAY_PUSH(NULL, empty) == false); + DQN_ASSERT(DQN_DARRAY_PUSH(NULL, &va) == false); + DQN_ASSERT(DQN_DARRAY_PUSH(&vecDArray, empty) == false); + } + + // Test array resizing and freeing + { + DqnV2 va = dqn_v2(10, 15); + DQN_DARRAY_PUSH(&vecDArray, &va); + + DqnV2 vb = vecDArray[0]; + DQN_ASSERT(dqn_v2_equals(va, vb) == false); + + vb = vecDArray[1]; + DQN_ASSERT(dqn_v2_equals(va, vb) == true); + + DQN_ASSERT(dqn_darray_get_capacity(vecDArray) == 2); + DQN_ASSERT(dqn_darray_get_num_items(vecDArray) == 2); + + DQN_DARRAY_PUSH(&vecDArray, &va); + DQN_ASSERT(dqn_darray_get_capacity(vecDArray) == 3); + DQN_ASSERT(dqn_darray_get_num_items(vecDArray) == 3); + + DQN_DARRAY_PUSH(&vecDArray, &va); + DQN_ASSERT(dqn_darray_get_capacity(vecDArray) == 4); + DQN_ASSERT(dqn_darray_get_num_items(vecDArray) == 4); + + DQN_DARRAY_PUSH(&vecDArray, &va); + DQN_ASSERT(dqn_darray_get_capacity(vecDArray) == 5); + DQN_ASSERT(dqn_darray_get_num_items(vecDArray) == 5); + + DQN_DARRAY_PUSH(&vecDArray, &va); + DQN_ASSERT(dqn_darray_get_capacity(vecDArray) == 6); + DQN_ASSERT(dqn_darray_get_num_items(vecDArray) == 6); + + DQN_DARRAY_PUSH(&vecDArray, &va); + DQN_ASSERT(dqn_darray_get_capacity(vecDArray) == 7); + DQN_ASSERT(dqn_darray_get_num_items(vecDArray) == 7); + + DQN_DARRAY_PUSH(&vecDArray, &va); + DQN_ASSERT(dqn_darray_get_capacity(vecDArray) == 8); + DQN_ASSERT(dqn_darray_get_num_items(vecDArray) == 8); + + DQN_DARRAY_PUSH(&vecDArray, &va); + DQN_ASSERT(dqn_darray_get_capacity(vecDArray) == 9); + DQN_ASSERT(dqn_darray_get_num_items(vecDArray) == 9); + + DQN_DARRAY_PUSH(&vecDArray, &va); + DQN_ASSERT(dqn_darray_get_capacity(vecDArray) == 10); + DQN_ASSERT(dqn_darray_get_num_items(vecDArray) == 10); + + DQN_DARRAY_PUSH(&vecDArray, &va); + DQN_ASSERT(dqn_darray_get_capacity(vecDArray) == 12); + DQN_ASSERT(dqn_darray_get_num_items(vecDArray) == 11); + + DqnV2 vc = dqn_v2(90, 100); + DQN_DARRAY_PUSH(&vecDArray, &vc); + DQN_ASSERT(dqn_darray_get_capacity(vecDArray) == 12); + DQN_ASSERT(dqn_darray_get_num_items(vecDArray) == 12); + DQN_ASSERT(dqn_v2_equals(vc, vecDArray[11])); + + DQN_ASSERT(dqn_darray_free(vecDArray) == true); + } + } + + { + f32 *array = DQN_DARRAY_INIT(f32, 1); + DQN_ASSERT(array); + DQN_ASSERT(dqn_darray_get_capacity(array) == 1); + DQN_ASSERT(dqn_darray_get_num_items(array) == 0); + + f32 *empty = NULL; + DQN_ASSERT(DQN_DARRAY_PUSH(NULL, empty) == false); + DQN_ASSERT(DQN_DARRAY_PUSH(&array, empty) == false); + } + + printf("dqn_darray_test(): Completed successfully\n"); +} + +void dqn_file_test() +{ + // File i/o + { + DqnFile file = {}; + DQN_ASSERT(dqn_file_open(".clang-format", &file)); + DQN_ASSERT(file.size == 1320); + + u8 *buffer = (u8 *)calloc(1, (size_t)file.size * sizeof(u8)); + DQN_ASSERT(dqn_file_read(file, buffer, (u32)file.size) == file.size); + free(buffer); + + dqn_file_close(&file); + DQN_ASSERT(!file.handle && file.size == 0); + + printf("dqn_file_test(): file_io: Completed successfully\n"); + } + + { + u32 numFiles; + char **filelist = dqn_dir_read("*", &numFiles); + printf("dqn_file_test(): dir_read: Display read files\n"); + + for (u32 i = 0; i < numFiles; i++) + printf("dqn_file_test(): dir_read: %s\n", filelist[i]); + + dqn_dir_read_free(filelist, numFiles); + printf("dqn_file_test(): dir_read: Completed successfully\n"); + } + + printf("dqn_file_test(): Completed successfully\n"); +} + +int main(void) +{ + dqn_strings_test(); + dqn_random_test(); + dqn_math_test(); + dqn_vec_test(); + dqn_other_test(); + dqn_darray_test(); + dqn_file_test(); + + printf("\nPress 'Enter' Key to Exit\n"); + getchar(); + + return 0; +} diff --git a/dqnt_unit_test.cpp b/dqnt_unit_test.cpp deleted file mode 100644 index fb138c1..0000000 --- a/dqnt_unit_test.cpp +++ /dev/null @@ -1,699 +0,0 @@ -#define DQNT_IMPLEMENTATION -#include "dqnt.h" - -#include "stdio.h" - -void dqnt_strings_test() -{ - { // Char Checks - DQNT_ASSERT(dqnt_char_is_alpha('a') == true); - DQNT_ASSERT(dqnt_char_is_alpha('A') == true); - DQNT_ASSERT(dqnt_char_is_alpha('0') == false); - DQNT_ASSERT(dqnt_char_is_alpha('@') == false); - DQNT_ASSERT(dqnt_char_is_alpha(' ') == false); - DQNT_ASSERT(dqnt_char_is_alpha('\n') == false); - - DQNT_ASSERT(dqnt_char_is_digit('1') == true); - DQNT_ASSERT(dqnt_char_is_digit('n') == false); - DQNT_ASSERT(dqnt_char_is_digit('N') == false); - DQNT_ASSERT(dqnt_char_is_digit('*') == false); - DQNT_ASSERT(dqnt_char_is_digit(' ') == false); - DQNT_ASSERT(dqnt_char_is_digit('\n') == false); - - DQNT_ASSERT(dqnt_char_is_alphanum('1') == true); - DQNT_ASSERT(dqnt_char_is_alphanum('a') == true); - DQNT_ASSERT(dqnt_char_is_alphanum('A') == true); - DQNT_ASSERT(dqnt_char_is_alphanum('*') == false); - DQNT_ASSERT(dqnt_char_is_alphanum(' ') == false); - DQNT_ASSERT(dqnt_char_is_alphanum('\n') == false); - - printf("dqnt_strings_test(): char_checks: Completed successfully\n"); - } - - // String Checks - { - // strcmp - { - char *a = "str_a"; - - // Check simple compares - { - DQNT_ASSERT(dqnt_strcmp(a, "str_a") == +0); - DQNT_ASSERT(dqnt_strcmp(a, "str_b") == -1); - DQNT_ASSERT(dqnt_strcmp("str_b", a) == +1); - DQNT_ASSERT(dqnt_strcmp(a, "") == +1); - DQNT_ASSERT(dqnt_strcmp("", "") == 0); - - // NOTE: Check that the string has not been trashed. - DQNT_ASSERT(dqnt_strcmp(a, "str_a") == +0); - } - - // Check ops against null - { - DQNT_ASSERT(dqnt_strcmp(NULL, NULL) != +0); - DQNT_ASSERT(dqnt_strcmp(a, NULL) != +0); - DQNT_ASSERT(dqnt_strcmp(NULL, a) != +0); - } - - printf("dqnt_strings_test(): strcmp: Completed successfully\n"); - } - - // strlen - { - char *a = "str_a"; - DQNT_ASSERT(dqnt_strlen(a) == 5); - DQNT_ASSERT(dqnt_strlen("") == 0); - DQNT_ASSERT(dqnt_strlen(" a ") == 6); - DQNT_ASSERT(dqnt_strlen("a\n") == 2); - - // NOTE: Check that the string has not been trashed. - DQNT_ASSERT(dqnt_strcmp(a, "str_a") == 0); - - DQNT_ASSERT(dqnt_strlen(NULL) == 0); - - printf("dqnt_strings_test(): strlen: Completed successfully\n"); - } - - // strncpy - { - { - char *a = "str_a"; - char b[10] = {}; - // Check copy into empty array - { - char *result = dqnt_strncpy(b, a, dqnt_strlen(a)); - DQNT_ASSERT(dqnt_strcmp(b, "str_a") == 0); - DQNT_ASSERT(dqnt_strcmp(a, "str_a") == 0); - DQNT_ASSERT(dqnt_strcmp(result, "str_a") == 0); - DQNT_ASSERT(dqnt_strlen(result) == 5); - } - - // Check copy into array offset, overlap with old results - { - char *newResult = dqnt_strncpy(&b[1], a, dqnt_strlen(a)); - DQNT_ASSERT(dqnt_strcmp(newResult, "str_a") == 0); - DQNT_ASSERT(dqnt_strlen(newResult) == 5); - - DQNT_ASSERT(dqnt_strcmp(a, "str_a") == 0); - DQNT_ASSERT(dqnt_strlen(a) == 5); - - DQNT_ASSERT(dqnt_strcmp(b, "sstr_a") == 0); - DQNT_ASSERT(dqnt_strlen(b) == 6); - } - } - - // Check strncpy with NULL pointers - { - DQNT_ASSERT(dqnt_strncpy(NULL, NULL, 5) == NULL); - - char *a = "str"; - char *result = dqnt_strncpy(a, NULL, 5); - - DQNT_ASSERT(dqnt_strcmp(a, "str") == 0); - DQNT_ASSERT(dqnt_strcmp(result, "str") == 0); - DQNT_ASSERT(dqnt_strcmp(result, a) == 0); - } - - // Check strncpy with 0 chars to copy - { - char *a = "str"; - char *b = "ing"; - - char *result = dqnt_strncpy(a, b, 0); - DQNT_ASSERT(dqnt_strcmp(a, "str") == 0); - DQNT_ASSERT(dqnt_strcmp(b, "ing") == 0); - DQNT_ASSERT(dqnt_strcmp(result, "str") == 0); - } - - printf("dqnt_strings_test(): strncpy: Completed successfully\n"); - } - - // str_reverse - { - // Basic reverse operations - { - char a[] = "aba"; - DQNT_ASSERT(dqnt_str_reverse(a, dqnt_strlen(a)) == true); - DQNT_ASSERT(dqnt_strcmp(a, "aba") == 0); - - DQNT_ASSERT(dqnt_str_reverse(a, 2) == true); - DQNT_ASSERT(dqnt_strcmp(a, "baa") == 0); - - DQNT_ASSERT(dqnt_str_reverse(a, dqnt_strlen(a)) == true); - DQNT_ASSERT(dqnt_strcmp(a, "aab") == 0); - - DQNT_ASSERT(dqnt_str_reverse(&a[1], 2) == true); - DQNT_ASSERT(dqnt_strcmp(a, "aba") == 0); - - DQNT_ASSERT(dqnt_str_reverse(a, 0) == true); - DQNT_ASSERT(dqnt_strcmp(a, "aba") == 0); - } - - // Try reverse empty string - { - char a[] = ""; - DQNT_ASSERT(dqnt_str_reverse(a, dqnt_strlen(a)) == true); - DQNT_ASSERT(dqnt_strcmp(a, "") == 0); - } - - // Try reverse single char string - { - char a[] = "a"; - DQNT_ASSERT(dqnt_str_reverse(a, dqnt_strlen(a)) == true); - DQNT_ASSERT(dqnt_strcmp(a, "a") == 0); - - DQNT_ASSERT(dqnt_str_reverse(a, 0) == true); - DQNT_ASSERT(dqnt_strcmp(a, "a") == 0); - } - - printf( - "dqnt_strings_test(): str_reverse: Completed successfully\n"); - } - - // str_to_i32 - { - char *a = "123"; - DQNT_ASSERT(dqnt_str_to_i32(a, dqnt_strlen(a)) == 123); - - char *b = "-123"; - DQNT_ASSERT(dqnt_str_to_i32(b, dqnt_strlen(b)) == -123); - DQNT_ASSERT(dqnt_str_to_i32(b, 1) == 0); - DQNT_ASSERT(dqnt_str_to_i32(&b[1], dqnt_strlen(&b[1])) == 123); - - char *c = "-0"; - DQNT_ASSERT(dqnt_str_to_i32(c, dqnt_strlen(c)) == 0); - - char *d = "+123"; - DQNT_ASSERT(dqnt_str_to_i32(d, dqnt_strlen(d)) == 123); - DQNT_ASSERT(dqnt_str_to_i32(&d[1], dqnt_strlen(&d[1])) == 123); - - printf("dqnt_strings_test(): str_to_i32: Completed successfully\n"); - } - - // i32_to_str - { - char a[DQNT_I32_TO_STR_MAX_BUF_SIZE] = {}; - dqnt_i32_to_str(+100, a, DQNT_ARRAY_COUNT(a)); - DQNT_ASSERT(dqnt_strcmp(a, "100") == 0); - - char b[DQNT_I32_TO_STR_MAX_BUF_SIZE] = {}; - dqnt_i32_to_str(-100, b, DQNT_ARRAY_COUNT(b)); - DQNT_ASSERT(dqnt_strcmp(b, "-100") == 0); - - char c[DQNT_I32_TO_STR_MAX_BUF_SIZE] = {}; - dqnt_i32_to_str(0, c, DQNT_ARRAY_COUNT(c)); - DQNT_ASSERT(dqnt_strcmp(c, "0") == 0); - - printf("dqnt_strings_test(): str_to_i32: Completed successfully\n"); - } - } - - // UCS <-> UTF8 Checks - { - // Test ascii characters - { - u32 codepoint = '@'; - u32 string[1] = {}; - - u32 bytesUsed = dqnt_ucs_to_utf8(&string[0], codepoint); - DQNT_ASSERT(bytesUsed == 1); - DQNT_ASSERT(string[0] == '@'); - - bytesUsed = dqnt_utf8_to_ucs(&string[0], codepoint); - DQNT_ASSERT(string[0] >= 0 && string[0] < 0x80); - DQNT_ASSERT(bytesUsed == 1); - } - - // Test 2 byte characters - { - u32 codepoint = 0x278; - u32 string[1] = {}; - - u32 bytesUsed = dqnt_ucs_to_utf8(&string[0], codepoint); - DQNT_ASSERT(bytesUsed == 2); - DQNT_ASSERT(string[0] == 0xC9B8); - - bytesUsed = dqnt_utf8_to_ucs(&string[0], string[0]); - DQNT_ASSERT(string[0] == codepoint); - DQNT_ASSERT(bytesUsed == 2); - } - - // Test 3 byte characters - { - u32 codepoint = 0x0A0A; - u32 string[1] = {}; - - u32 bytesUsed = dqnt_ucs_to_utf8(&string[0], codepoint); - DQNT_ASSERT(bytesUsed == 3); - DQNT_ASSERT(string[0] == 0xE0A88A); - - bytesUsed = dqnt_utf8_to_ucs(&string[0], string[0]); - DQNT_ASSERT(string[0] == codepoint); - DQNT_ASSERT(bytesUsed == 3); - } - - // Test 4 byte characters - { - u32 codepoint = 0x10912; - u32 string[1] = {}; - u32 bytesUsed = dqnt_ucs_to_utf8(&string[0], codepoint); - - DQNT_ASSERT(bytesUsed == 4); - DQNT_ASSERT(string[0] == 0xF090A492); - - bytesUsed = dqnt_utf8_to_ucs(&string[0], string[0]); - DQNT_ASSERT(string[0] == codepoint); - DQNT_ASSERT(bytesUsed == 4); - } - - { - u32 codepoint = 0x10912; - u32 bytesUsed = dqnt_ucs_to_utf8(NULL, codepoint); - DQNT_ASSERT(bytesUsed == 0); - - bytesUsed = dqnt_utf8_to_ucs(NULL, codepoint); - DQNT_ASSERT(bytesUsed == 0); - } - - printf("dqnt_strings_test(): ucs <-> utf8: Completed successfully\n"); - } - - printf("dqnt_strings_test(): Completed successfully\n"); -} - -#include "Windows.h" -#define WIN32_LEAN_AND_MEAN -void dqnt_other_test() -{ - { // Test Win32 Sleep - // NOTE: Win32 Sleep is not granular to a certain point so sleep excessively - u32 sleepInMs = 1000; - f64 startInMs = dqnt_time_now_in_ms(); - Sleep(sleepInMs); - f64 endInMs = dqnt_time_now_in_ms(); - - DQNT_ASSERT(startInMs < endInMs); - printf("dqnt_other_test(): time_now: Completed successfully\n"); - } - printf("dqnt_other_test(): Completed successfully\n"); -} - -void dqnt_random_test() { - - DqntRandPCGState pcg; - dqnt_rnd_pcg_init(&pcg); - for (i32 i = 0; i < 10; i++) - { - i32 min = -100; - i32 max = 100000; - i32 result = dqnt_rnd_pcg_range(&pcg, min, max); - DQNT_ASSERT(result >= min && result <= max) - - f32 randF32 = dqnt_rnd_pcg_nextf(&pcg); - DQNT_ASSERT(randF32 >= 0.0f && randF32 <= 1.0f); - printf("dqnt_random_test(): rnd_pcg: Completed successfully\n"); - } - - printf("dqnt_random_test(): Completed successfully\n"); -} - -void dqnt_math_test() -{ - { // Lerp - { - f32 start = 10; - f32 t = 0.5f; - f32 end = 20; - DQNT_ASSERT(dqnt_math_lerp(start, t, end) == 15); - } - - { - f32 start = 10; - f32 t = 2.0f; - f32 end = 20; - DQNT_ASSERT(dqnt_math_lerp(start, t, end) == 30); - } - - printf("dqnt_math_test(): lerp: Completed successfully\n"); - } - - { // sqrtf - DQNT_ASSERT(dqnt_math_sqrtf(4.0f) == 2.0f); - printf("dqnt_math_test(): sqrtf: Completed successfully\n"); - } - - printf("dqnt_math_test(): Completed successfully\n"); -} - -void dqnt_vec_test() -{ - { // V2 - - // V2 Creating - { - DqntV2 vec = dqnt_v2(5.5f, 5.0f); - DQNT_ASSERT(vec.x == 5.5f && vec.y == 5.0f); - DQNT_ASSERT(vec.w == 5.5f && vec.h == 5.0f); - } - - // V2i Creating - { - DqntV2 vec = dqnt_v2i(3, 5); - DQNT_ASSERT(vec.x == 3 && vec.y == 5.0f); - DQNT_ASSERT(vec.w == 3 && vec.h == 5.0f); - } - - // V2 Arithmetic - { - DqntV2 vecA = dqnt_v2(5, 10); - DqntV2 vecB = dqnt_v2i(2, 3); - DQNT_ASSERT(dqnt_v2_equals(vecA, vecB) == false); - DQNT_ASSERT(dqnt_v2_equals(vecA, dqnt_v2(5, 10)) == true); - DQNT_ASSERT(dqnt_v2_equals(vecB, dqnt_v2(2, 3)) == true); - - DqntV2 result = dqnt_v2_add(vecA, dqnt_v2(5, 10)); - DQNT_ASSERT(dqnt_v2_equals(result, dqnt_v2(10, 20)) == true); - - result = dqnt_v2_sub(result, dqnt_v2(5, 10)); - DQNT_ASSERT(dqnt_v2_equals(result, dqnt_v2(5, 10)) == true); - - result = dqnt_v2_scale(result, 5); - DQNT_ASSERT(dqnt_v2_equals(result, dqnt_v2(25, 50)) == true); - - result = dqnt_v2_hadamard(result, dqnt_v2(10, 0.5f)); - DQNT_ASSERT(dqnt_v2_equals(result, dqnt_v2(250, 25)) == true); - - f32 dotResult = dqnt_v2_dot(dqnt_v2(5, 10), dqnt_v2(3, 4)); - DQNT_ASSERT(dotResult == 55); - } - - // V2 Properties - { - DqntV2 a = dqnt_v2(0, 0); - DqntV2 b = dqnt_v2(3, 4); - - f32 lengthSq = dqnt_v2_length_squared(a, b); - DQNT_ASSERT(lengthSq == 25); - - f32 length = dqnt_v2_length(a, b); - DQNT_ASSERT(length == 5); - - DqntV2 normalised = dqnt_v2_normalise(b); - DQNT_ASSERT(normalised.x == (b.x / 5.0f)); - DQNT_ASSERT(normalised.y == (b.y / 5.0f)); - - DqntV2 c = dqnt_v2(3.5f, 8.0f); - DQNT_ASSERT(dqnt_v2_overlaps(b, c) == true); - DQNT_ASSERT(dqnt_v2_overlaps(b, a) == false); - - DqntV2 d = dqnt_v2_perpendicular(c); - DQNT_ASSERT(dqnt_v2_dot(c, d) == 0); - } - - { // constrain_to_ratio - DqntV2 ratio = dqnt_v2(16, 9); - DqntV2 dim = dqnt_v2(2000, 1080); - DqntV2 result = dqnt_v2_constrain_to_ratio(dim, ratio); - DQNT_ASSERT(result.w == 1920 && result.h == 1080); - } - - printf("dqnt_vec_test(): vec2: Completed successfully\n"); - } - - { // V3 - - // V3i Creating - { - DqntV3 vec = dqnt_v3(5.5f, 5.0f, 5.875f); - DQNT_ASSERT(vec.x == 5.5f && vec.y == 5.0f && vec.z == 5.875f); - DQNT_ASSERT(vec.r == 5.5f && vec.g == 5.0f && vec.b == 5.875f); - } - - // V3i Creating - { - DqntV3 vec = dqnt_v3i(3, 4, 5); - DQNT_ASSERT(vec.x == 3 && vec.y == 4 && vec.z == 5); - DQNT_ASSERT(vec.r == 3 && vec.g == 4 && vec.b == 5); - } - - // V3 Arithmetic - { - DqntV3 vecA = dqnt_v3(5, 10, 15); - DqntV3 vecB = dqnt_v3(2, 3, 6); - DQNT_ASSERT(dqnt_v3_equals(vecA, vecB) == false); - DQNT_ASSERT(dqnt_v3_equals(vecA, dqnt_v3(5, 10, 15)) == true); - DQNT_ASSERT(dqnt_v3_equals(vecB, dqnt_v3(2, 3, 6)) == true); - - DqntV3 result = dqnt_v3_add(vecA, dqnt_v3(5, 10, 15)); - DQNT_ASSERT(dqnt_v3_equals(result, dqnt_v3(10, 20, 30)) == true); - - result = dqnt_v3_sub(result, dqnt_v3(5, 10, 15)); - DQNT_ASSERT(dqnt_v3_equals(result, dqnt_v3(5, 10, 15)) == true); - - result = dqnt_v3_scale(result, 5); - DQNT_ASSERT(dqnt_v3_equals(result, dqnt_v3(25, 50, 75)) == true); - - result = dqnt_v3_hadamard(result, dqnt_v3(10.0f, 0.5f, 10.0f)); - DQNT_ASSERT(dqnt_v3_equals(result, dqnt_v3(250, 25, 750)) == true); - - f32 dotResult = dqnt_v3_dot(dqnt_v3(5, 10, 2), dqnt_v3(3, 4, 6)); - DQNT_ASSERT(dotResult == 67); - - DqntV3 cross = dqnt_v3_cross(vecA, vecB); - DQNT_ASSERT(dqnt_v3_equals(cross, dqnt_v3(15, 0, -5)) == true); - } - - printf("dqnt_vec_test(): vec3: Completed successfully\n"); - } - - { // V4 - - // V4 Creating - { - DqntV4 vec = dqnt_v4(5.5f, 5.0f, 5.875f, 5.928f); - DQNT_ASSERT(vec.x == 5.5f && vec.y == 5.0f && vec.z == 5.875f && vec.w == 5.928f); - DQNT_ASSERT(vec.r == 5.5f && vec.g == 5.0f && vec.b == 5.875f && vec.a == 5.928f); - } - - // V4i Creating - { - DqntV4 vec = dqnt_v4i(3, 4, 5, 6); - DQNT_ASSERT(vec.x == 3 && vec.y == 4 && vec.z == 5 && vec.w == 6); - DQNT_ASSERT(vec.r == 3 && vec.g == 4 && vec.b == 5 && vec.a == 6); - } - - // V4 Arithmetic - { - DqntV4 vecA = dqnt_v4(5, 10, 15, 20); - DqntV4 vecB = dqnt_v4i(2, 3, 6, 8); - DQNT_ASSERT(dqnt_v4_equals(vecA, vecB) == false); - DQNT_ASSERT(dqnt_v4_equals(vecA, dqnt_v4(5, 10, 15, 20)) == true); - DQNT_ASSERT(dqnt_v4_equals(vecB, dqnt_v4(2, 3, 6, 8)) == true); - - DqntV4 result = dqnt_v4_add(vecA, dqnt_v4(5, 10, 15, 20)); - DQNT_ASSERT(dqnt_v4_equals(result, dqnt_v4(10, 20, 30, 40)) == true); - - result = dqnt_v4_sub(result, dqnt_v4(5, 10, 15, 20)); - DQNT_ASSERT(dqnt_v4_equals(result, dqnt_v4(5, 10, 15, 20)) == true); - - result = dqnt_v4_scale(result, 5); - DQNT_ASSERT(dqnt_v4_equals(result, dqnt_v4(25, 50, 75, 100)) == true); - - result = dqnt_v4_hadamard(result, dqnt_v4(10, 0.5f, 10, 0.25f)); - DQNT_ASSERT(dqnt_v4_equals(result, dqnt_v4(250, 25, 750, 25)) == true); - - f32 dotResult = dqnt_v4_dot(dqnt_v4(5, 10, 2, 8), dqnt_v4(3, 4, 6, 5)); - DQNT_ASSERT(dotResult == 107); - } - - printf("dqnt_vec_test(): vec4: Completed successfully\n"); - } - - // Rect - { - DqntRect rect = dqnt_rect(dqnt_v2(-10, -10), dqnt_v2(20, 20)); - DQNT_ASSERT(dqnt_v2_equals(rect.min, dqnt_v2(-10, -10))); - DQNT_ASSERT(dqnt_v2_equals(rect.max, dqnt_v2(10, 10))); - - f32 width, height; - dqnt_rect_get_size_2f(rect, &width, &height); - DQNT_ASSERT(width == 20); - DQNT_ASSERT(height == 20); - - DqntV2 dim = dqnt_rect_get_size_v2(rect); - DQNT_ASSERT(dqnt_v2_equals(dim, dqnt_v2(20, 20))); - - DqntV2 rectCenter = dqnt_rect_get_centre(rect); - DQNT_ASSERT(dqnt_v2_equals(rectCenter, dqnt_v2(0, 0))); - - // Test shifting rect - DqntRect shiftedRect = dqnt_rect_move(rect, dqnt_v2(10, 0)); - DQNT_ASSERT(dqnt_v2_equals(shiftedRect.min, dqnt_v2(0, -10))); - DQNT_ASSERT(dqnt_v2_equals(shiftedRect.max, dqnt_v2(20, 10))); - - dqnt_rect_get_size_2f(shiftedRect, &width, &height); - DQNT_ASSERT(width == 20); - DQNT_ASSERT(height == 20); - - dim = dqnt_rect_get_size_v2(shiftedRect); - DQNT_ASSERT(dqnt_v2_equals(dim, dqnt_v2(20, 20))); - - // Test rect contains p - DqntV2 inP = dqnt_v2(5, 5); - DqntV2 outP = dqnt_v2(100, 100); - DQNT_ASSERT(dqnt_rect_contains_p(shiftedRect, inP)); - DQNT_ASSERT(!dqnt_rect_contains_p(shiftedRect, outP)); - - printf("dqnt_vec_test(): rect: Completed successfully\n"); - } - - printf("dqnt_vec_test(): Completed successfully\n"); -} - -void dqnt_darray_test() -{ - { - DqntV2 *vecDArray = DQNT_DARRAY_INIT(DqntV2, 1); - DQNT_ASSERT(vecDArray); - DQNT_ASSERT(dqnt_darray_get_capacity(vecDArray) == 1); - DQNT_ASSERT(dqnt_darray_get_num_items(vecDArray) == 0); - - // Test basic insert - { - DqntV2 va = dqnt_v2(5, 10); - DQNT_ASSERT(DQNT_DARRAY_PUSH(&vecDArray, &va)); - - DqntV2 vb = vecDArray[0]; - DQNT_ASSERT(dqnt_v2_equals(va, vb)); - - DQNT_ASSERT(dqnt_darray_get_capacity(vecDArray) == 1); - DQNT_ASSERT(dqnt_darray_get_num_items(vecDArray) == 1); - - DqntV2 *empty = NULL; - DQNT_ASSERT(DQNT_DARRAY_PUSH(NULL, empty) == false); - DQNT_ASSERT(DQNT_DARRAY_PUSH(NULL, &va) == false); - DQNT_ASSERT(DQNT_DARRAY_PUSH(&vecDArray, empty) == false); - } - - // Test array resizing and freeing - { - DqntV2 va = dqnt_v2(10, 15); - DQNT_DARRAY_PUSH(&vecDArray, &va); - - DqntV2 vb = vecDArray[0]; - DQNT_ASSERT(dqnt_v2_equals(va, vb) == false); - - vb = vecDArray[1]; - DQNT_ASSERT(dqnt_v2_equals(va, vb) == true); - - DQNT_ASSERT(dqnt_darray_get_capacity(vecDArray) == 2); - DQNT_ASSERT(dqnt_darray_get_num_items(vecDArray) == 2); - - DQNT_DARRAY_PUSH(&vecDArray, &va); - DQNT_ASSERT(dqnt_darray_get_capacity(vecDArray) == 3); - DQNT_ASSERT(dqnt_darray_get_num_items(vecDArray) == 3); - - DQNT_DARRAY_PUSH(&vecDArray, &va); - DQNT_ASSERT(dqnt_darray_get_capacity(vecDArray) == 4); - DQNT_ASSERT(dqnt_darray_get_num_items(vecDArray) == 4); - - DQNT_DARRAY_PUSH(&vecDArray, &va); - DQNT_ASSERT(dqnt_darray_get_capacity(vecDArray) == 5); - DQNT_ASSERT(dqnt_darray_get_num_items(vecDArray) == 5); - - DQNT_DARRAY_PUSH(&vecDArray, &va); - DQNT_ASSERT(dqnt_darray_get_capacity(vecDArray) == 6); - DQNT_ASSERT(dqnt_darray_get_num_items(vecDArray) == 6); - - DQNT_DARRAY_PUSH(&vecDArray, &va); - DQNT_ASSERT(dqnt_darray_get_capacity(vecDArray) == 7); - DQNT_ASSERT(dqnt_darray_get_num_items(vecDArray) == 7); - - DQNT_DARRAY_PUSH(&vecDArray, &va); - DQNT_ASSERT(dqnt_darray_get_capacity(vecDArray) == 8); - DQNT_ASSERT(dqnt_darray_get_num_items(vecDArray) == 8); - - DQNT_DARRAY_PUSH(&vecDArray, &va); - DQNT_ASSERT(dqnt_darray_get_capacity(vecDArray) == 9); - DQNT_ASSERT(dqnt_darray_get_num_items(vecDArray) == 9); - - DQNT_DARRAY_PUSH(&vecDArray, &va); - DQNT_ASSERT(dqnt_darray_get_capacity(vecDArray) == 10); - DQNT_ASSERT(dqnt_darray_get_num_items(vecDArray) == 10); - - DQNT_DARRAY_PUSH(&vecDArray, &va); - DQNT_ASSERT(dqnt_darray_get_capacity(vecDArray) == 12); - DQNT_ASSERT(dqnt_darray_get_num_items(vecDArray) == 11); - - DqntV2 vc = dqnt_v2(90, 100); - DQNT_DARRAY_PUSH(&vecDArray, &vc); - DQNT_ASSERT(dqnt_darray_get_capacity(vecDArray) == 12); - DQNT_ASSERT(dqnt_darray_get_num_items(vecDArray) == 12); - DQNT_ASSERT(dqnt_v2_equals(vc, vecDArray[11])); - - DQNT_ASSERT(dqnt_darray_free(vecDArray) == true); - } - } - - { - f32 *array = DQNT_DARRAY_INIT(f32, 1); - DQNT_ASSERT(array); - DQNT_ASSERT(dqnt_darray_get_capacity(array) == 1); - DQNT_ASSERT(dqnt_darray_get_num_items(array) == 0); - - f32 *empty = NULL; - DQNT_ASSERT(DQNT_DARRAY_PUSH(NULL, empty) == false); - DQNT_ASSERT(DQNT_DARRAY_PUSH(&array, empty) == false); - } - - printf("dqnt_darray_test(): Completed successfully\n"); -} - -void dqnt_file_test() -{ - // File i/o - { - DqntFile file = {}; - DQNT_ASSERT(dqnt_file_open(".clang-format", &file)); - DQNT_ASSERT(file.size == 1320); - - u8 *buffer = (u8 *)calloc(1, (size_t)file.size * sizeof(u8)); - DQNT_ASSERT(dqnt_file_read(file, buffer, (u32)file.size) == file.size); - free(buffer); - - dqnt_file_close(&file); - DQNT_ASSERT(!file.handle && file.size == 0); - - printf("dqnt_file_test(): file_io: Completed successfully\n"); - } - - { - u32 numFiles; - char **filelist = dqnt_dir_read("*", &numFiles); - printf("dqnt_file_test(): dir_read: Display read files\n"); - - for (u32 i = 0; i < numFiles; i++) - printf("dqnt_file_test(): dir_read: %s\n", filelist[i]); - - dqnt_dir_read_free(filelist, numFiles); - printf("dqnt_file_test(): dir_read: Completed successfully\n"); - } - - printf("dqnt_file_test(): Completed successfully\n"); -} - -int main(void) -{ - dqnt_strings_test(); - dqnt_random_test(); - dqnt_math_test(); - dqnt_vec_test(); - dqnt_other_test(); - dqnt_darray_test(); - dqnt_file_test(); - - printf("\nPress 'Enter' Key to Exit\n"); - getchar(); - - return 0; -} diff --git a/misc/dqnt_unit_test.sln b/misc/dqn_unit_test.sln similarity index 91% rename from misc/dqnt_unit_test.sln rename to misc/dqn_unit_test.sln index f9702af..0e4141a 100644 --- a/misc/dqnt_unit_test.sln +++ b/misc/dqn_unit_test.sln @@ -6,9 +6,9 @@ MinimumVisualStudioVersion = 10.0.40219.1 Project("{911E67C6-3D85-4FCE-B560-20A9C3E3FF48}") = "dqnt_unit_test", "..\bin\dqnt_unit_test.exe", "{87785192-6F49-4F85-AA0D-F0AFA5CCCDDA}" ProjectSection(DebuggerProjectSystem) = preProject PortSupplier = 00000000-0000-0000-0000-000000000000 - Executable = C:\git\dqnt\bin\dqnt_unit_test.exe + Executable = C:\git\dqn\bin\dqn_unit_test.exe RemoteMachine = THAI-PC - StartingDirectory = C:\git\dqnt + StartingDirectory = C:\git\dqn Environment = Default LaunchingEngine = 00000000-0000-0000-0000-000000000000 UseLegacyDebugEngines = No @@ -21,7 +21,7 @@ Global Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {87785192-6F49-4F85-AA0D-F0AFA5CCCDDA}.Release|x86.ActiveCfg = Release|x86 + {87785192-6F49-4F85-AA0D-F0AFA5CCCDDA}.Release|x86.ActiveCfg = Release EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE