1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
#if !defined(UNGRATEFUL_H)
# define UNGRATEFUL_H
/*
Ungrateful - standard library for game development.
LICENSE:
Copyright (C) 2025 Bogdan Masyutin (bonmas14)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
Bogdan Masyutin - bonmas14@gmail.com
*/
#if defined(__clang__) || defined(__GNUC__)
# define CLANG_COMPILER
# define __TRAP() __builtin_trap()
#elif _MSC_VER >= 1939
# define MSVC_COMPILER
# include <intrin.h>
# define __TRAP() __debugbreak()
#else
# error "Unknown compiler"
#endif
#if defined(__cplusplus)
# define CLITERAL(type) type
#else
# define CLITERAL(type) (type)
#endif
#if defined(_WIN32)
# define OS_WINDOWS
#elif defined(__linux__)
# define OS_LINUX
#else
# error "unknown platform!"
#endif
#define UN_TEMP_SIZE UN_MB(50)
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include <limits.h>
#include <assert.h>
#include <math.h>
#define UNUSED(x) (void)(x)
#define UN_KB(s) ((u64)(s) * 1024LL)
#define UN_MB(s) (UN_KB(s) * 1024LL)
#define UN_GB(s) (UN_MB(s) * 1024LL)
#define UN_MAX(a, b) (a) > (b) ? (a) : (b)
#define UN_MIN(a, b) (a) < (b) ? (a) : (b)
#define UN_CSTR (u8*)
#define UN_STR(cstr) un_string_from_cstring(UN_CSTR cstr)
#define CSTR (char*)
#define UN_CLEAR(var) un_memory_set((void*)&var, 0, sizeof(var))
#if defined(__cplusplus)
extern "C" {
#endif
typedef uint64_t u64;
typedef uint32_t u32;
typedef uint16_t u16;
typedef uint8_t u8;
typedef int64_t s64;
typedef int32_t s32;
typedef int16_t s16;
typedef int8_t s8;
typedef int8_t b32;
typedef int8_t b8;
typedef float f32;
typedef double f64;
#ifndef PI
# define PI 3.14159265358979323846f
#endif
#ifndef EPSILON
# define EPSILON 0.000001f
#endif
#ifndef DEG2RAD
# define DEG2RAD (PI/180.0f)
#endif
#ifndef RAD2DEG
# define RAD2DEG (180.0f/PI)
#endif
#if defined(UN_DOUBLE_PRECISION)
typedef double real;
#else
typedef float real;
#endif // UN_DOUBLE_PRECISION
#ifndef PI_D
# define PI_D 3.14159265358979323846
#endif
#ifndef EPSILON_D
# define EPSILON_D 0.000000000000001
#endif
#ifndef DEG2RAD_D
# define DEG2RAD_D (PI/180.0)
#endif
#ifndef RAD2DEG_D
# define RAD2DEG_D (180.0/PI)
#endif
#define un_vec_unwrap2(vec) vec[0], vec[1]
#define un_vec_unwrap3(vec) vec[0], vec[1], vec[2]
#define un_vec_unwrap4(vec) vec[0], vec[1], vec[2], vec[3]
/* ---- Memory Allocators API ---- */
typedef enum {
UN_ALLOC_MSG_ALLOCATE,
UN_ALLOC_MSG_REALLOCATE,
UN_ALLOC_MSG_FREE,
UN_ALLOC_MSG_SELF_DELETE,
} Allocator_Message;
#define ALLOCATOR_PROC_SIGNATURE(name)\
void *name(void *p, u64 size, Allocator_Message message, void *data)
typedef ALLOCATOR_PROC_SIGNATURE(Allocator_Proc);
typedef struct {
Allocator_Proc *proc;
void *data;
} Allocator;
// extern Allocator un_allocator_create_heap(s64 chunk_size); /* ... for large things */
extern Allocator un_allocator_create_arena(u64 initial_size); /* Grouping allocator, that will recursively grow */
extern Allocator un_allocator_get_standard(void);
extern Allocator un_allocator_get_temporary(void);
extern void *un_memory_alloc(u64 size, Allocator alloc);
extern void *un_memory_realloc(void *ptr, u64 size, Allocator alloc);
extern void *un_memory_free(void *ptr, Allocator alloc);
extern void un_memory_destroy(Allocator *alloc);
extern void un_memory_set(u8 *dest, u8 value, u64 size);
extern void un_memory_copy(u8 *dest, u8 *src, u64 size);
extern void un_memory_move(u8 *dest, u8 *src, u64 size);
extern s32 un_memory_compare(u8 *left, u8 *right, u64 size); /* checks for every byte in arrays for condition: if left is bigger that right. */
/* ---- Generic list structure ---- */
typedef struct {
u64 count;
u64 capacity;
u64 element_size;
Allocator alloc;
void *data;
} List;
extern List un_list_create(u64 start_capacity, u64 element_size, Allocator alloc);
extern void un_list_destroy(List *list);
extern List un_list_clone(List *list, Allocator alloc);
extern b32 un_list_append(List *list, void *data); /* Returns true if succeed. */
extern void *un_list_get(List *list, u64 index);
extern void un_list_remove(List *list, u64 index);
/* ---- no-wide string API ---- */
typedef struct {
u64 size;
u8 *data;
} String;
extern u64 un_string_get_length(u8 *cstring);
extern String un_string_from_cstring(u8* cstring);
extern u8* un_string_to_cstring(String string, Allocator alloc);
extern String un_string_copy(String source, Allocator alloc);
extern s32 un_string_compare(String left, String right);
extern String un_string_concat(String left, String right, Allocator alloc);
extern String un_string_swap(String input, u8 from, u8 to, Allocator alloc);
extern List un_string_split(String input, String pattern, Allocator alloc);
extern String un_string_join(List string_list, String separator, Allocator alloc);
extern String un_string_substring(String input, u64 start, u64 max_size);
extern s64 un_string_index_of(String input, u8 value, u64 skip_count);
extern s64 un_string_index_of_last(String input, u8 value);
/* Simplest formatter, supports:
*
* %% - %
* %c - char (internally u32)
* %u - u64
* %d - s64
* %s - String
* */
extern String un_string_format(Allocator alloc, String buffer, ...);
extern String un_string_vformat(Allocator alloc, String buffer, va_list args);
/* ---- math, and vecmath ---- */
real un_m_lerpr(real a, real b, real t);
f32 un_m_lerpf(f32 a, f32 b, f32 t);
f64 un_m_lerpd(f64 a, f64 b, f64 t);
/* 2d f32 */
void un_m_add2f(f32 *v, f32 *a, f32 *b);
void un_m_sub2f(f32 *v, f32 *a, f32 *b);
void un_m_add_scalar2f(f32 *v, f32 *a, f32 scalar);
void un_m_sub_scalar2f(f32 *v, f32 *a, f32 scalar);
void un_m_mul_scalar2f(f32 *v, f32 *a, f32 scalar);
void un_m_div_scalar2f(f32 *v, f32 *a, f32 scalar);
void un_m_dot2f(f32 *v, f32 *a, f32 *b);
void un_m_hadamard2f(f32 *v, f32 *a, f32 *b);
void un_m_project2f(f32 *v, f32 *a, f32 *onto);
void un_m_reflect2f(f32 *v, f32 *a, f32 *normal);
void un_m_cross2f(f32 *v, f32 *a);
void un_m_normalize2f(f32 *v, f32 *a);
void un_m_magnitude2f(f32 *v, f32 *a);
void un_m_magnitude_sqr2f(f32 *v, f32 *a);
void un_m_distance2f(f32 *v, f32 *a, f32 *b);
void un_m_distance_sqr2f(f32 *v, f32 *a, f32 *b);
/* 3d f32 */
void un_m_add3f(f32 *v, f32 *a, f32 *b);
void un_m_sub3f(f32 *v, f32 *a, f32 *b);
void un_m_add_scalar3f(f32 *v, f32 *a, f32 scalar);
void un_m_sub_scalar3f(f32 *v, f32 *a, f32 scalar);
void un_m_mul_scalar3f(f32 *v, f32 *a, f32 scalar);
void un_m_div_scalar3f(f32 *v, f32 *a, f32 scalar);
void un_m_dot3f(f32 *v, f32 *a, f32 *b);
void un_m_hadamard3f(f32 *v, f32 *a, f32 *b);
void un_m_project3f(f32 *v, f32 *a, f32 *onto);
void un_m_reflect3f(f32 *v, f32 *a, f32 *normal);
void un_m_cross3f(f32 *v, f32 *a, f32 *b);
void un_m_normalize3f(f32 *v, f32 *a);
void un_m_magnitude3f(f32 *v, f32 *a);
void un_m_magnitude_sqr3f(f32 *v, f32 *a);
void un_m_distance3f(f32 *v, f32 *a, f32 *b);
void un_m_distance_sqr3f(f32 *v, f32 *a, f32 *b);
/* 4d f32 */
void un_m_add4f(f32 *v, f32 *a, f32 *b);
void un_m_sub4f(f32 *v, f32 *a, f32 *b);
void un_m_add_scalar4f(f32 *v, f32 *a, f32 scalar);
void un_m_sub_scalar4f(f32 *v, f32 *a, f32 scalar);
void un_m_mul_scalar4f(f32 *v, f32 *a, f32 scalar);
void un_m_div_scalar4f(f32 *v, f32 *a, f32 scalar);
void un_m_dot4f(f32 *v, f32 *a, f32 *b);
void un_m_hadamard4f(f32 *v, f32 *a, f32 *b);
void un_m_project4f(f32 *v, f32 *a, f32 *onto);
void un_m_reflect4f(f32 *v, f32 *a, f32 *normal);
void un_m_normalize4f(f32 *v, f32 *a);
void un_m_magnitude4f(f32 *v, f32 *a);
void un_m_magnitude_sqr4f(f32 *v, f32 *a);
void un_m_distance4f(f32 *v, f32 *a, f32 *b);
void un_m_distance_sqr4f(f32 *v, f32 *a, f32 *b);
/* 2d f64 */
void un_m_add2d(f64 *v, f64 *a, f64 *b);
void un_m_sub2d(f64 *v, f64 *a, f64 *b);
void un_m_add_scalar2d(f64 *v, f64 *a, f64 scalar);
void un_m_sub_scalar2d(f64 *v, f64 *a, f64 scalar);
void un_m_mul_scalar2d(f64 *v, f64 *a, f64 scalar);
void un_m_div_scalar2d(f64 *v, f64 *a, f64 scalar);
void un_m_dot2d(f64 *v, f64 *a, f64 *b);
void un_m_hadamard2d(f64 *v, f64 *a, f64 *b);
void un_m_project2d(f64 *v, f64 *a, f64 *onto);
void un_m_reflect2d(f64 *v, f64 *a, f64 *normal);
void un_m_cross2d(f64 *v, f64 *a);
void un_m_normalize2d(f64 *v, f64 *a);
void un_m_magnitude2d(f64 *v, f64 *a);
void un_m_magnitude_sqr2d(f64 *v, f64 *a);
void un_m_distance2d(f64 *v, f64 *a, f64 *b);
void un_m_distance_sqr2d(f64 *v, f64 *a, f64 *b);
/* 3d f64 */
void un_m_add3d(f64 *v, f64 *a, f64 *b);
void un_m_sub3d(f64 *v, f64 *a, f64 *b);
void un_m_add_scalar3d(f64 *v, f64 *a, f64 scalar);
void un_m_sub_scalar3d(f64 *v, f64 *a, f64 scalar);
void un_m_mul_scalar3d(f64 *v, f64 *a, f64 scalar);
void un_m_div_scalar3d(f64 *v, f64 *a, f64 scalar);
void un_m_dot3d(f64 *v, f64 *a, f64 *b);
void un_m_hadamard3d(f64 *v, f64 *a, f64 *b);
void un_m_project3d(f64 *v, f64 *a, f64 *onto);
void un_m_reflect3d(f64 *v, f64 *a, f64 *normal);
void un_m_cross3d(f64 *v, f64 *a, f64 *b);
void un_m_normalize3d(f64 *v, f64 *a);
void un_m_magnitude3d(f64 *v, f64 *a);
void un_m_magnitude_sqr3d(f64 *v, f64 *a);
void un_m_distance3d(f64 *v, f64 *a, f64 *b);
void un_m_distance_sqr3d(f64 *v, f64 *a, f64 *b);
/* 4d f64 */
void un_m_add4d(f64 *v, f64 *a, f64 *b);
void un_m_sub4d(f64 *v, f64 *a, f64 *b);
void un_m_add_scalar4d(f64 *v, f64 *a, f64 scalar);
void un_m_sub_scalar4d(f64 *v, f64 *a, f64 scalar);
void un_m_mul_scalar4d(f64 *v, f64 *a, f64 scalar);
void un_m_div_scalar4d(f64 *v, f64 *a, f64 scalar);
void un_m_dot4d(f64 *v, f64 *a, f64 *b);
void un_m_hadamard4d(f64 *v, f64 *a, f64 *b);
void un_m_project4d(f64 *v, f64 *a, f64 *onto);
void un_m_reflect4d(f64 *v, f64 *a, f64 *normal);
void un_m_normalize4d(f64 *v, f64 *a);
void un_m_magnitude4d(f64 *v, f64 *a);
void un_m_magnitude_sqr4d(f64 *v, f64 *a);
void un_m_distance4d(f64 *v, f64 *a, f64 *b);
void un_m_distance_sqr4d(f64 *v, f64 *a, f64 *b);
/* 2d real */
void un_m_add2r(real *v, real *a, real *b);
void un_m_sub2r(real *v, real *a, real *b);
void un_m_add_scalar2r(real *v, real *a, real scalar);
void un_m_sub_scalar2r(real *v, real *a, real scalar);
void un_m_mul_scalar2r(real *v, real *a, real scalar);
void un_m_div_scalar2r(real *v, real *a, real scalar);
void un_m_dot2r(real *v, real *a, real *b);
void un_m_hadamard2r(real *v, real *a, real *b);
void un_m_project2r(real *v, real *a, real *onto);
void un_m_reflect2r(real *v, real *a, real *normal);
void un_m_cross2r(real *v, real *a);
void un_m_normalize2r(real *v, real *a);
void un_m_magnitude2r(real *v, real *a);
void un_m_magnitude_sqr2r(real *v, real *a);
void un_m_distance2r(real *v, real *a, real *b);
void un_m_distance_sqr2r(real *v, real *a, real *b);
/* 3d real */
void un_m_add3r(real *v, real *a, real *b);
void un_m_sub3r(real *v, real *a, real *b);
void un_m_add_scalar3r(real *v, real *a, real scalar);
void un_m_sub_scalar3r(real *v, real *a, real scalar);
void un_m_mul_scalar3r(real *v, real *a, real scalar);
void un_m_div_scalar3r(real *v, real *a, real scalar);
void un_m_dot3r(real *v, real *a, real *b);
void un_m_hadamard3r(real *v, real *a, real *b);
void un_m_project3r(real *v, real *a, real *onto);
void un_m_reflect3r(real *v, real *a, real *normal);
void un_m_cross3r(real *v, real *a, real *b);
void un_m_normalize3r(real *v, real *a);
void un_m_magnitude3r(real *v, real *a);
void un_m_magnitude_sqr3r(real *v, real *a);
void un_m_distance3r(real *v, real *a, real *b);
void un_m_distance_sqr3r(real *v, real *a, real *b);
/* 4d real */
void un_m_add4r(real *v, real *a, real *b);
void un_m_sub4r(real *v, real *a, real *b);
void un_m_add_scalar4r(real *v, real *a, real scalar);
void un_m_sub_scalar4r(real *v, real *a, real scalar);
void un_m_mul_scalar4r(real *v, real *a, real scalar);
void un_m_div_scalar4r(real *v, real *a, real scalar);
void un_m_dot4r(real *v, real *a, real *b);
void un_m_hadamard4r(real *v, real *a, real *b);
void un_m_project4r(real *v, real *a, real *onto);
void un_m_reflect4r(real *v, real *a, real *normal);
void un_m_normalize4r(real *v, real *a);
void un_m_magnitude4r(real *v, real *a);
void un_m_magnitude_sqr4r(real *v, real *a);
void un_m_distance4r(real *v, real *a, real *b);
void un_m_distance_sqr4r(real *v, real *a, real *b);
/* ---- splines ---- */
void un_m_bezierr(real *v, real a, real q0, real q1, real b, real t);
void un_m_bezier2r(real *v, real *a, real *q0, real *q1, real *b, real t);
void un_m_bezier3r(real *v, real *a, real *q0, real *q1, real *b, real t);
void un_m_bezier4r(real *v, real *a, real *q0, real *q1, real *b, real t);
void un_m_bezierf(f32 *v, f32 a, f32 q0, f32 q1, f32 b, f32 t);
void un_m_bezier2f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t);
void un_m_bezier3f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t);
void un_m_bezier4f(f32 *v, f32 *a, f32 *q0, f32 *q1, f32 *b, f32 t);
void un_m_bezierd(f64 *v, f64 a, f64 q0, f64 q1, f64 b, f64 t);
void un_m_bezier2d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t);
void un_m_bezier3d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t);
void un_m_bezier4d(f64 *v, f64 *a, f64 *q0, f64 *q1, f64 *b, f64 t);
/* ---- easing ---- */
real un_m_ease_isiner(real t);
real un_m_ease_iosiner(real t);
real un_m_ease_osiner(real t);
real un_m_ease_iquadr(real t);
real un_m_ease_ioquadr(real t);
real un_m_ease_oquadr(real t);
f32 un_m_ease_isinef(f32 t);
f32 un_m_ease_iosinef(f32 t);
f32 un_m_ease_osinef(f32 t);
f32 un_m_ease_iquadf(f32 t);
f32 un_m_ease_ioquadf(f32 t);
f32 un_m_ease_oquadf(f32 t);
f64 un_m_ease_isined(f64 t);
f64 un_m_ease_iosined(f64 t);
f64 un_m_ease_osined(f64 t);
f64 un_m_ease_iquadd(f64 t);
f64 un_m_ease_ioquadd(f64 t);
f64 un_m_ease_oquadd(f64 t);
#if defined(__cplusplus)
}
#endif
#endif // UNGRATEFUL_H
|