forked from Jaysmito101/cgl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcgl.h
14063 lines (12409 loc) · 537 KB
/
cgl.h
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
MIT License
Copyright (c) 2023 Jaysmito Mukherjee
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
/** file: cgl.h
*
* CGL is a cross platform C library for game development.
* It is designed to be simple, fast and easy to use.
* It is also designed to be modular, so that you can use
* only the parts you need.
*
* @author Jaysmito Mukherjee
*/
#ifndef CGL_H
#define CGL_H
#if defined(_WIN32) || defined(_WIN64)
#define CGL_WINDOWS
#else
// TODO: seperate linux and macos and android
#define CGL_UNIX
#endif
#ifdef CGL_WASM
#define CGL_EXCLUDE_NETWORKING
#define CGL_EXCLUDES_THREADS
#ifdef CGL_EXCLUDE_WINDOW_API
#undef CGL_EXCLUDE_WINDOW_API
#endif
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
#define CGL_wasm_set_loop_func(func, data) emscripten_set_main_loop_arg((em_arg_callback_func)func, data, 0, true);
#endif
// common
#if 1 // Just to use code folding
// std includes
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <limits.h>
#include <stddef.h>
#include <float.h>
#include <ctype.h>
#include <wchar.h>
#include <wctype.h>
#include <locale.h>
#include <errno.h>
#include <time.h>
#if !(defined(_WIN32) || defined(_WIN64))
#include <unistd.h>
#endif
/*
* CGL types
*/
typedef unsigned char CGL_ubyte;
typedef unsigned short CGL_ushort;
typedef unsigned int CGL_uint;
typedef unsigned long CGL_ulong;
typedef unsigned long long CGL_ulonglong;
typedef char CGL_byte;
typedef short CGL_short;
typedef int CGL_int;
typedef long CGL_long;
typedef long long CGL_longlong;
typedef float CGL_float;
typedef double CGL_double;
typedef long double CGL_longdouble;
typedef size_t CGL_sizei;
typedef bool CGL_bool;
typedef void CGL_void;
#define CGL_TRUE true
#define CGL_FALSE false
#ifndef max
#define max(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; })
#endif
#ifndef min
#define min(a,b) \
({ __typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; })
#endif
#ifdef CGL_LOGGING_ENABLED
#define CGL_LOG(...) CGL_log_internal(__VA_ARGS__)
#else
#define CGL_LOG(...)
#endif // CGL_LOG_ENABLED
struct CGL_context;
typedef struct CGL_context CGL_context;
/** @brief Initializes CGL
*
* This function initializes CGL. It must be called before
* any other CGL function.
*
* @return true if CGL was initialized successfully
*/
bool CGL_init(); // initialize CGL
/** @brief Shuts down CGL
*
* This function shuts down CGL. It must be called after
* all other CGL functions.
*
* @return returns nothing
*/
void CGL_shutdown(); // shutdown CGL
#endif
#ifndef CGL_EXCLUDE_NETWORKING
/*
* The error codes for CGL networking
*/
#define CGL_NET_NO_ERROR 0xAB000
#define CGL_NET_NAME_RESOLUTION_ERROR 0xAB001
#define CGL_NET_INVALID_PARAMATER_ERROR 0xAB002
#define CGL_NET_UNSUPPORTED_ERROR 0xAB003
#define CGL_NET_MEMORY_ERROR 0xAB004
#define CGL_NET_NOT_FOUND_ERROR 0xAB005
struct CGL_net_addrinfo;
typedef struct CGL_net_addrinfo CGL_net_addrinfo;
struct CGL_net_socket;
typedef struct CGL_net_socket CGL_net_socket;
/** @brief Initializes CGL networking system
*
* This function initializes CGL networking system.
* It must be called before any other CGL networking function.
*
* @return true if CGL networking was initialized successfully
*/
bool CGL_net_init();
/** @brief Shuts down CGL networking system
*
* This function shuts down CGL networking system.
* Also frees all the resources allocated by CGL
* networking system.
*
* @return returns nothing
*/
void CGL_net_shutdown();
/** @brief Query system for addrinfo
*
* This function queries the system for addrinfo
* for the given name and port. The addrinfo is
* stored in the CGL_net_addrinfo struct.
*
* @param name the name of the host
* @param port the port of the host
* @param count the number of addrinfo returned (output)
* @return the addrinfo of the host
*/
CGL_net_addrinfo* CGL_net_addrinfo_query(const char* name, const char* port, size_t* count);
/** @brief Destroys the addrinfo
*
* This function destroys the addrinfo and frees
* all the resources allocated by CGL networking
*
* @param infos the addrinfo to destroy *
*/
void CGL_net_addrinfo_destroy(CGL_net_addrinfo* infos);
/** @brief Creates a socket
*
* This function creates a socket object and returns
* it.
*
* NOTE: This must be destroyed using CGL_net_socket_destroy
*
* @return the socket object
*/
CGL_net_socket* CGL_net_socket_create();
/** @brief Connects a socket to a target addrinfo
*
* This function connects the socket to the target
* specified by the addrinfo.
*
* @param socket The socket to connect
* @param target The target addrinfo
* @return true if the socket was connected successfully
*/
bool CGL_net_socket_connect(CGL_net_socket* socket, CGL_net_addrinfo* target);
/** @brief Binds a socket to a target addrinfo
*
* This function binds the socket to the target
* specified by the addrinfo.
*
* @param socket The socket to bind
* @param target The target addrinfo
* @return true if the socket was bound successfully
*/
bool CGL_net_socket_bind(CGL_net_socket* socket, CGL_net_addrinfo* target);
/** @brief Listens for connections on a socket (Server)
*
* This function makes the socket listen for connections.
* This basically sets up the socket as a server.
*
* @param socket The socket to listen on
* @param max_connections The maximum number of connections
* @return true if the socket was listening successfully
*/
bool CGL_net_socket_listen(CGL_net_socket* socket, size_t max_connections);
/** @brief Accepts a connection on a socket (Server)
*
* This function accepts a connection on the socket
* and returns a new socket object for the connection.
* The returned object has a connection established
* with the client and can be used to send and receive
* data packets over the connection.
*
* NOTE: The returned object must be destroyed using CGL_net_socket_close
*
* @param socket The socket to accept a connection on
* @param addrinfo The addrinfo of the socket
* @return the socket object for the connection if successful, NULL otherwise
*/
CGL_net_socket* CGL_net_socket_accept(CGL_net_socket* socket, CGL_net_addrinfo* addrinfo);
/** @brief Destroys a socket
*
* This function destroys the socket and frees
* all the resources allocated for the socket.
* It also releases the internal handles of the socket.
*
* @param socket The socket to destroy
* @return returns nothing
*/
void CGL_net_socket_close(CGL_net_socket* socket);
/** @brief Sends data over a socket
*
* This function sends data over the socket.
*
* @param socket The socket to send data over
* @param buffer The buffer to send
* @param size The size of the buffer
* @param size_sent The size of the data sent (output)
* @return true if the data was sent successfully
*/
bool CGL_net_socket_send(CGL_net_socket* socket, void* buffer, size_t size, size_t* size_sent);
/** @brief Receives data over a socket
*
* This function receives data over the socket.
*
* @param socket The socket to receive data over
* @param buffer The buffer to receive data into
* @param size The size of the buffer
* @param size_recieved The size of the data recieved (output)
* @return true if the data was recieved successfully
*/
bool CGL_net_socket_recv(CGL_net_socket* socket, void* buffer, size_t size, size_t* size_recieved);
/** @brief Shuts down the send side of a socket
*
* This function shuts down the send side of the socket.
*
* @param socket The socket to shutdown
* @return true if the socket was shutdown successfully
*/
bool CGL_net_socket_shutdown_send(CGL_net_socket* socket);
/** @brief Shuts down the receive side of a socket
*
* This function shuts down the receive side of the socket.
*
* @param socket The socket to shutdown
* @return true if the socket was shutdown successfully
*/
bool CGL_net_socket_shutdown_recv(CGL_net_socket* socket);
/** @brief Performs a HTTP request
*
* This function performs a HTTP request.
* It internally allocates sockets, establishes
* connections, sends and receives data.
* This function is blocking.
*
* NOTE: This function is not very stable yet
* and should be avoided in production
* environments.
*
* @param method The HTTP method to use (GET, POST, etc.)
* @param host The host to connect to (Ex: www.google.com)
* @param path The path to request (Ex: /search)
* @param response_buffer The buffer to store the response in (output)
* @param size The size of the buffer (input) and the size of the response (output)
* @param accept The accept header to send
* @param user_agent The user agent header to send
* @param body The request body to send
* @return HTTP status code is successful, 0 otherwise
*/
CGL_int CGL_net_http_request(const char* method, const char* host, const char* path, void* response_buffer, size_t* size, const char* accept, const char* user_agent, const char* body);
/** @brief Performs a HTTP GET request
*
* This function performs a HTTP GET request.
*
* NOTE: This internally calls CGL_net_http_request
*
* @param host The host to connect to (Ex: www.google.com)
* @param path The path to request (Ex: /search)
* @param buffer The buffer to store the response in (output)
* @param size The size of the buffer (input) and the size of the response (output)
* @param accept The accept header to send
* @param user_agent The user agent header to send
* @return HTTP status code is successful, 0 otherwise
*/
CGL_int CGL_net_http_get(const char* host, const char* path, void* buffer, size_t* size, const char* accept, const char* user_agent);
/** @brief Performs a HTTP POST request
*
* This function performs a HTTP POST request.
*
* NOTE: This internally calls CGL_net_http_request
*
* @param host The host to connect to (Ex: www.google.com)
* @param path The path to request (Ex: /search)
* @param buffer The buffer to store the response in (output)
* @param size The size of the buffer (input) and the size of the response (output)
* @param accept The accept header to send
* @param user_agent The user agent header to send
* @param body The request body to send
* @return HTTP status code is successful, 0 otherwise
*/
CGL_int CGL_net_http_post(const char* host, const char* path, void* buffer, size_t* size, const char* accept, const char* user_agent, const char* body);
#ifndef CGL_EXCLUDE_SSL_SOCKET
/** @brief Performs a HTTPS request
*
* NOTE: Not implemented yet
*
* @param method The HTTP method to use (GET, POST, etc.)
* @param host The host to connect to (Ex: www.google.com)
* @param path The path to request (Ex: /search)
* @param response_buffer The buffer to store the response in (output)
* @param size The size of the buffer (input) and the size of the response (output)
* @param accept The accept header to send
* @param user_agent The user agent header to send
* @param body The request body to send
* @return HTTP status code is successful, 0 otherwise
*/
CGL_int CGL_net_https_request(const char* method, const char* host, const char* path, void* response_buffer, size_t* size, const char* accept, const char* user_agent, const char* body);
/** @brief Performs a HTTPS GET request
*
* Performs a HTTPS GET request.
*
* NOTE: This internally calls CGL_net_https_request
*
* @param host The host to connect to (Ex: www.google.com)
* @param path The path to request (Ex: /search)
* @param buffer The buffer to store the response in (output)
* @param size The size of the buffer (input) and the size of the response (output)
* @param accept The accept header to send
* @param user_agent The user agent header to send
* @return HTTP status code is successful, 0 otherwise
*/
CGL_int CGL_net_https_get(const char* host, const char* path, void* buffer, size_t* size, const char* accept, const char* user_agent);
/** @brief Performs a HTTPS POST request
*
* Performs a HTTPS POST request.
*
* NOTE: This internally calls CGL_net_https_request
*
* @param host The host to connect to (Ex: www.google.com)
* @param path The path to request (Ex: /search)
* @param buffer The buffer to store the response in (output)
* @param size The size of the buffer (input) and the size of the response (output)
* @param accept The accept header to send
* @param user_agent The user agent header to send
* @param body The request body to send
* @return HTTP status code is successful, 0 otherwise
*/
CGL_int CGL_net_https_post(const char* host, const char* path, void* buffer, size_t* size, const char* accept, const char* user_agent, const char* body);
struct CGL_net_ssl_socket;
typedef struct CGL_net_ssl_socket CGL_net_ssl_socket;
/** @brief Creates a new SSL socket
*
* This function creates a new SSL socket from
* the given socket.
*
* NOTE: (1) The socket object must be connected before
* calling this function.
* (2) The socket object will automatically be
* destroyed when the SSL socket is destroyed.
* *
* @param socket The socket to create the SSL socket from
* @return The new SSL socket
*/
CGL_net_ssl_socket* CGL_net_ssl_socket_create(CGL_net_socket* socket);
/** @brief Sends data over the SSL socket
*
* This function sends data over the SSL socket.
*
* @param socket The SSL socket to send data over
* @param buffer The buffer to send
* @param size The size of the buffer
* @param size_sent The size of the data sent
* @return CGL_TRUE if successful, CGL_FALSE otherwise
*/
CGL_bool CGL_net_ssl_socket_send(CGL_net_ssl_socket* socket, void* buffer, size_t size, size_t* size_sent);
/** @brief Receives data over the SSL socket
*
* This function receives data over the SSL socket.
*
* @param socket The SSL socket to receive data over
* @param buffer The buffer to store the data in
* @param size The size of the buffer
* @param size_recieved The size of the data recieved
* @return CGL_TRUE if successful, CGL_FALSE otherwise
*/
CGL_bool CGL_net_ssl_socket_recv(CGL_net_ssl_socket* socket, void* buffer, size_t size, size_t* size_recieved);
/** @brief Destroys the SSL socket
*
* This function destroys the SSL socket.
* This also destroys the socket object.
*
* @param socket The SSL socket to destroy
* @return returns nothing
*/
CGL_void CGL_net_ssl_socket_destroy(CGL_net_ssl_socket* soc);
/** @brief Logs the SSL errors
*
* This function logs the SSL errors if any.
*
* @return returns nothing
*/
CGL_void CGL_net_ssl_log_errors();
#endif
#endif
// utils
#if 1
// CGL utils
#ifndef CGL_RAND_GEN_WITH_PROBABILITY_MAX_COUNT
#define CGL_RAND_GEN_WITH_PROBABILITY_MAX_COUNT 100000
#endif
#define CGL_UTILS_FAST_RAND_MAX 32767
void CGL_utils_sleep(const CGL_sizei milis);
CGL_byte* CGL_utils_read_file(const CGL_byte* path, size_t* size); // read file into memory
CGL_sizei CGL_utils_get_file_size(const CGL_byte* path);
CGL_bool CGL_utils_append_file(const CGL_byte* path, const CGL_byte* data, size_t size);
CGL_bool CGL_utils_write_file(const CGL_byte* path, const CGL_byte* data, size_t size); // write data to file
CGL_float CGL_utils_get_time();
void CGL_utils_get_timestamp(char* buffer);
CGL_bool CGL_utils_is_little_endian();
CGL_sizei CGL_utils_get_random_with_probability(CGL_float* probabilities, CGL_sizei count);
void CGL_utils_reverse_bytes(void* data, size_t size);
void CGL_utils_little_endian_to_current(void* data, size_t size);
void CGL_utils_big_endian_to_current(void* data, size_t size);
void CGL_utils_fast_srand(CGL_int seed);
CGL_int CGL_utils_fast_rand();
CGL_ulong CGL_utils_xorshf96();
void CGL_utils_srand31(CGL_uint seed);
CGL_uint CGL_utils_rand31();
#define CGL_utils_is_point_in_rect(px, py, x, y, sx, sy, scx, scy) (bool)((px) >= (x) * (scx) && (px) <= ((x) + (sx)) * (scx) && (py) >= (y) * (scy) && (py) <= ((y) + (sy)) * (scy))
#define CGL_utils_random_float() ((float)rand() / (float)RAND_MAX)
#define CGL_utils_random_float_in_range(min, max) (CGL_utils_random_float() * (max - min) + min)
#define CGL_utils_random_int(min, max) (rand() % (max - min + 1) + min)
#define CGL_utils_random_bool() (rand() % 2)
#define CGL_utils_random_vec2(min, max) (CGL_vec2_init(CGL_utils_random_float() * (max.x - min.x) + min.x, CGL_utils_random_float() * (max.y - min.y) + min.y))
#define CGL_utils_random_vec3(min, max) (CGL_vec3_init(CGL_utils_random_float() * (max.x - min.x) + min.x, CGL_utils_random_float() * (max.y - min.y) + min.y, CGL_utils_random_float() * (max.z - min.z) + min.z))
#define CGL_utils_random_vec4(min, max) (CGL_vec4_init(CGL_utils_random_float() * (max.x - min.x) + min.x, CGL_utils_random_float() * (max.y - min.y) + min.y, CGL_utils_random_float() * (max.z - min.z) + min.z, CGL_utils_random_float() * (max.w - min.w) + min.w))
#define CGL_utils_random_color() (CGL_vec4_init(CGL_utils_random_float(), CGL_utils_random_float(), CGL_utils_random_float(), 1.0f))
#define CGL_utils_clamp(x, minl, maxl) (x < minl ? minl : (x > maxl ? maxl : x))
#define CGL_utils_array_size(array) (sizeof(array) / sizeof(array[0]))
#define CGL_utils_max(a, b) ( ((a) > (b)) ? (a) : (b) )
#define CGL_utils_min(a, b) ( ((a) < (b)) ? (a) : (b) )
#define CGL_utils_mix(x, y, f) (x * f + y * (1.0f - f))
#define CGL_utils_lerp(a, b, t) (a + (b - a) * t)
#define CGL_utils_square(x) ((x) * (x))
#define CGL_utils_cube(x) ((x) * (x) * (x))
CGL_float CGL_utils_sigmoid(CGL_float x);
CGL_float CGL_utils_sigmoid_derivative(CGL_float x);
CGL_float CGL_utils_relu(CGL_float x);
CGL_float CGL_utils_relu_derivative(CGL_float x);
CGL_float CGL_utils_tanh(CGL_float x);
CGL_float CGL_utils_tanh_derivative(CGL_float x);
CGL_float CGL_utils_step(CGL_float x);
CGL_float CGL_utils_step_derivative(CGL_float x);
CGL_float CGL_utils_relu_leaky(CGL_float x);
CGL_float CGL_utils_relu_leaky_derivative(CGL_float x);
CGL_float CGL_utils_relu_smooth(CGL_float x);
CGL_float CGL_utils_relu_smooth_derivative(CGL_float x);
#define CGL_malloc(size) malloc(size)
#define CGL_realloc(ptr, size) realloc(ptr, size)
#define CGL_free(ptr) free(ptr)
#define CGL_exit(code) exit(code)
#define CGL_CONSOLE_COLOR_RESET 0
#define CGL_CONSOLE_COLOR_RED 1
#define CGL_CONSOLE_COLOR_GREEN 2
#define CGL_CONSOLE_COLOR_GRAY 3
#define CGL_CONSOLE_COLOR_BLUE 4
void CGL_console_set_color(uint8_t color);
void CGL_printf_red(const char* format, ...);
void CGL_printf_green(const char* format, ...);
void CGL_printf_gray(const char* format, ...);
void CGL_printf_blue(const char* format, ...);
void CGL_console_progress_bar(CGL_float progress, CGL_int width, CGL_byte* prefix, CGL_byte* suffix, CGL_byte complete_char, CGL_byte incomplete_char);
#endif
#define CGL_LOG_LEVEL_TRACE 0
#define CGL_LOG_LEVEL_INFO 1
#define CGL_LOG_LEVEL_WARN 2
#define CGL_LOG_LEVEL_ERROR 3
#define CGL_LOG_LEVEL_INTERNAL 4
#define CGL_LOGGER_MAX_LOG_FILES 32
#define CGL_LOGGER_LOG_BUFFER_SIZE (1024 * 4)
#ifndef CGL_ENABLE_CONSOLE_LOGGING
#define CGL_ENABLE_CONSOLE_LOGGING true
#endif
#ifndef CGL_DISABLE_LOGGER
struct CGL_logger_context;
typedef struct CGL_logger_context CGL_logger_context;
void CGL_logger_init(bool enable_console_logging);
void CGL_logger_shutdown();
CGL_logger_context* CGL_logger_get_context();
void CGL_logger_set_context(CGL_logger_context* context);
bool CGL_logger_attach_log_file(const char* path);
bool CGL_logger_detach_log_file(const char* path);
void CGL_logger_flush();
void CGL_logger_disable_console_logging();
void CGL_logger_enable_console_logging();
void CGL_logger_log(CGL_int level, const char* log_format, ...);
#define CGL_trace(...) CGL_logger_log(CGL_LOG_LEVEL_TRACE, __VA_ARGS__)
#define CGL_info(...) CGL_logger_log(CGL_LOG_LEVEL_INFO, __VA_ARGS__)
#define CGL_warn(...) CGL_logger_log(CGL_LOG_LEVEL_WARN, __VA_ARGS__)
#define CGL_error(...) CGL_logger_log(CGL_LOG_LEVEL_ERROR, __VA_ARGS__)
#ifndef CGL_DISABLE_INTERNAL_LOGGING
#define CGL_log_internal(...) CGL_logger_log(CGL_LOG_LEVEL_INTERNAL, __VA_ARGS__)
#endif
#else
#define CGL_logger_init(...)
#define CGL_logger_shutdown()
#define CGL_logger_get_context() NULL
#define CGL_logger_set_context(...)
#define CGL_logger_attach_log_file(path)
#define CGL_logger_dettach_log_file(path)
#define CGL_logger_flush()
#define CGL_logger_disable_console_logging()
#define CGL_logger_enable_console_logging()
#define CGL_logger_log(...)
#define CGL_info(...)
#define CGL_trace(...)
#define CGL_warn(...)
#define CGL_error(...)
#define CGL_log_internal(...)
#endif
// math and data structures
#if 1 // Just to use code folding
// data structures
struct CGL_list;
typedef struct CGL_list CGL_list;
CGL_list* CGL_list_create(size_t item_size, size_t initial_capacity);
void CGL_list_destroy(CGL_list* list);
void CGL_list_set_increase_factor(CGL_list* list, CGL_float increase_factor);
float CGL_list_get_increase_factor(CGL_list* list);
size_t CGL_list_get_item_size(CGL_list* list);
size_t CGL_list_get_size(CGL_list* list);
size_t CGL_list_get_capacity(CGL_list* list);
size_t CGL_list_push(CGL_list* list, void* data);
size_t CGL_list_pop(CGL_list* list, void* data);
void* CGL_list_get(CGL_list* list, size_t index, void* data);
void* CGL_list_get_random(CGL_list* list, void* data);
void* CGL_list_set(CGL_list* list, size_t index, void* data);
bool CGL_list_is_empty(CGL_list* list);
size_t CGL_list_find(CGL_list* list, void* data);
void CGL_list_reserve(CGL_list* list, size_t size);
void CGL_list_fill(CGL_list* list, size_t size);
#ifndef CGL_HASHTABLE_MAX_KEY_SIZE
#define CGL_HASHTABLE_MAX_KEY_SIZE 256
#endif
#ifndef CGL_HASHTABLE_ENTRY_STATIC_VALUE_SIZE
#define CGL_HASHTABLE_ENTRY_STATIC_VALUE_SIZE sizeof(uint64_t)
#endif
struct CGL_hashtable_entry;
typedef struct CGL_hashtable_entry CGL_hashtable_entry;
struct CGL_hashtable;
typedef struct CGL_hashtable CGL_hashtable;
struct CGL_hashtable_iterator;
typedef struct CGL_hashtable_iterator CGL_hashtable_iterator;
typedef uint32_t(*CGL_hash_function)(const void*, size_t);
// set key size to 0 if it is a string (it will be auto calculated using strlen)
CGL_hashtable* CGL_hashtable_create(size_t table_size, size_t key_size, size_t initial_capacity);
void CGL_hashtable_set_growth_rate(CGL_hashtable* table, CGL_float rate);
size_t CGL_hashtable_get_size(CGL_hashtable* table);
void CGL_hashtable_destroy(CGL_hashtable* table);
void CGL_hashtable_set(CGL_hashtable* table, const void* key, const void* value, size_t value_size);
size_t CGL_hashtable_get(CGL_hashtable* table, const void* key, void* value);
void* CGL_hashtable_get_ptr(CGL_hashtable* table, const void* key, size_t* value);
bool CGL_hashtable_exists(CGL_hashtable* table, const void* key);
bool CGL_hashtable_remove(CGL_hashtable* table, const void* key);
void CGL_hashtable_set_hash_function(CGL_hashtable* table, CGL_hash_function hash_function);
CGL_hashtable_iterator* CGL_hashtable_iterator_create(CGL_hashtable* table);
void CGL_hashtable_iterator_destroy(CGL_hashtable_iterator* iterator);
void CGL_hashtable_iterator_reset(CGL_hashtable_iterator* iterator);
bool CGL_hashtable_iterator_next(CGL_hashtable_iterator* iterator, void* key, void* data, size_t* size);
bool CGL_hashtable_iterator_curr(CGL_hashtable_iterator* iterator, void* key, void* data, size_t* size);
void* CGL_hashtable_iterator_curr_key(CGL_hashtable_iterator* iterator);
// getter setters for data types
#define CGL_DECLARE_HASHTABLE_GETTER_SETTER(type) \
static type CGL_hashtable_get_##type (CGL_hashtable* table, const void* key)\
{ \
type value; \
CGL_hashtable_get(table, key, &value); \
return value; \
} \
\
static void CGL_hashtable_set_##type (CGL_hashtable* table, const void* key, type value) \
{ \
CGL_hashtable_set(table, key, &value, sizeof(value)); \
}
#ifndef CDL_DONT_DECLARE_HASHTABLE_STD_GETTER_SETTERS
static void CGL_hashtable_set_string (CGL_hashtable* table, const void* key, const char* value)
{
CGL_hashtable_set(table, key, value, strlen(value) + 1);
}
CGL_DECLARE_HASHTABLE_GETTER_SETTER(int8_t);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(int16_t);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(int32_t);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(int64_t);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(uint8_t);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(uint16_t);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(uint32_t);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(uint64_t);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(float);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(double);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(bool);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(int);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(long);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(short);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(char);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(intptr_t);
CGL_DECLARE_HASHTABLE_GETTER_SETTER(uintptr_t);
#endif
// algorithms
uint32_t CGL_utils_crc32(const void* data, size_t size);
uint64_t CGL_utils_crc64(const void* data, size_t size);
void CGL_utils_rot13(const char* data_in, char* data_out);
uint32_t CGL_utils_super_fast_hash(const void* data, size_t size);
// threads
#ifndef CGL_EXCLUDES_THREADS
struct CGL_thread;
typedef struct CGL_thread CGL_thread;
typedef void (*CGL_thread_function)(void*);
struct CGL_mutex;
typedef struct CGL_mutex CGL_mutex;
CGL_thread* CGL_thread_create();
bool CGL_thread_start(CGL_thread* thread, CGL_thread_function function, void* argument);
void CGL_thread_destroy(CGL_thread* thread);
bool CGL_thread_join(CGL_thread* thread);
bool CGL_thread_joinable(CGL_thread* thread);
bool CGL_thread_is_running(CGL_thread* thread);
uintptr_t CGL_thread_get_id(CGL_thread* thread);
CGL_mutex* CGL_mutex_create(bool set);
void CGL_mutex_destroy(CGL_mutex* mutex);
int CGL_mutex_lock(CGL_mutex* mutex, uint64_t timeout);
void CGL_mutex_release(CGL_mutex* mutex);
#endif
// math
struct CGL_vec2
{
CGL_float x;
CGL_float y;
#ifdef __cplusplus
CGL_vec2() : x(0), y(0) {}
CGL_vec2(CGL_float x, CGL_float y) : x(x), y(y) {}
#endif
};
typedef struct CGL_vec2 CGL_vec2;
struct CGL_vec3
{
CGL_float x;
CGL_float y;
CGL_float z;
#ifdef __cplusplus
CGL_vec3() : x(0), y(0), z(0) {}
CGL_vec3(CGL_float x, CGL_float y, CGL_float z) : x(x), y(y), z(z) {}
#endif
};
typedef struct CGL_vec3 CGL_vec3;
struct CGL_vec4
{
CGL_float x;
CGL_float y;
CGL_float z;
CGL_float w;
#ifdef __cplusplus
CGL_vec4() : x(0), y(0), z(0), w(0) {}
CGL_vec4(CGL_float x, CGL_float y, CGL_float z, CGL_float w) : x(x), y(y), z(z), w(w) {}
#endif
};
typedef struct CGL_vec4 CGL_vec4;
typedef struct CGL_vec4 CGL_color;
struct CGL_ivec4
{
CGL_int x;
CGL_int y;
CGL_int z;
CGL_int w;
#ifdef __cplusplus
CGL_ivec4() : x(0), y(0), z(0), w(0) {}
CGL_ivec4(CGL_int x, CGL_int y, CGL_int z, CGL_int w) : x(x), y(y), z(z), w(w) {}
#endif
};
typedef struct CGL_ivec4 CGL_ivec4;
struct CGL_mat3
{
CGL_float m[9];
#ifdef __cplusplus
CGL_mat3() {}
CGL_mat3(CGL_float m0, CGL_float m1, CGL_float m2, CGL_float m3, CGL_float m4, CGL_float m5, CGL_float m6, CGL_float m7, CGL_float m8)
{
m[0] = m0; m[3] = m1; m[6] = m2;
m[1] = m3; m[4] = m4; m[7] = m5;
m[2] = m6; m[5] = m7; m[8] = m8;
}
#endif
};
typedef struct CGL_mat3 CGL_mat3;
struct CGL_mat4
{
CGL_float m[16];
#ifdef __cplusplus
CGL_mat4() {}
CGL_mat4(CGL_float m0, CGL_float m1, CGL_float m2, CGL_float m3,
CGL_float m4, CGL_float m5, CGL_float m6, CGL_float m7,
CGL_float m8, CGL_float m9, CGL_float m10, CGL_float m11,
CGL_float m12, CGL_float m13, CGL_float m14, CGL_float m15)
{
m[0] = m0; m[4] = m1; m[8] = m2; m[12] = m3;
m[1] = m4; m[5] = m5; m[9] = m6; m[13] = m7;
m[2] = m8; m[6] = m9; m[10] = m10; m[14] = m11;
m[3] = m12; m[7] = m13; m[11] = m14; m[15] = m15;
}
#endif
};
typedef struct CGL_mat4 CGL_mat4;
struct CGL_quat
{
CGL_vec3 vec;
CGL_float w;
#ifdef __cplusplus
CGL_quat() : vec(CGL_vec3(0, 0, 0)), w(0) {}
CGL_quat(CGL_float x, CGL_float y, CGL_float z, CGL_float w) : vec(CGL_vec3(x, y, z)), w(w) {}
#endif
};
typedef struct CGL_quat CGL_quat;
// math functions with macros
#define CGL_PI (3.14159265358979323846f)
#define CGL_2PI (6.28318530717958647692f)
#define CGL_PI_2 (1.57079632679489661923f)
#define CGL_E (2.71828182845904523536f)
#define CGL_SQRT2 (1.41421356237309504880f)
#define CGL_SQRT3 (1.73205080756887729352f)
#define CGL_SQRT5 (2.23606797749978969640f)
#define CGL_SQRT6 (2.44948974278317809820f)
#define CGL_SQRT7 (2.64575131106459059050f)
#define CGL_SQRT8 (2.82842712474619009760f)
#define CGL_deg_to_rad(deg) ((deg) * (CGL_PI / 180.0f))
#define CGL_rad_to_deg(rad) ((rad) * (180.0f / CGL_PI))
#define CGL_float_lerp(a, b, t) (((a) * (1.0f - (t))) + ((b) * (t)))
CGL_float CGL_float_quadratic_lerp(CGL_float a, CGL_float b, CGL_float c, CGL_float t);
CGL_float CGL_float_cubic_lerp(CGL_float a, CGL_float b, CGL_float c, CGL_float d, CGL_float t);
#ifdef __cplusplus
#define CGL_vec2_init(x, y) CGL_vec2(x, y)
#else
#define CGL_vec2_init(x, y) ((CGL_vec2){(x), (y)})
#endif
#define CGL_vec2_add(a, b) CGL_vec2_init(a.x + b.x, a.y + b.y)
#define CGL_vec2_add_scaled(a, b, scale) CGL_vec2_init((a).x + (b).x * (scale), (a).y + (b).y * (scale))
#define CGL_vec2_sub(a, b) CGL_vec2_init(a.x - b.x, a.y - b.y)
#define CGL_vec2_mul(a, b) CGL_vec2_init(a.x * b.x, a.y * b.y)
#define CGL_vec2_div(a, b) CGL_vec2_init(a.x / b.x, a.y / b.y)
#define CGL_vec2_scale(a, s) CGL_vec2_init(a.x * (s), a.y * (s))
#define CGL_vec2_dot(a, b) (a.x * b.x + a.y * b.y)
#define CGL_vec2_length(a) sqrtf(a.x * a.x + a.y * a.y)
#define CGL_vec2_normalize(a) { CGL_float __CGL_vector_length##__LINE__ = 1.0f / CGL_vec2_length(a); a.x *= __CGL_vector_length##__LINE__; a.y *= __CGL_vector_length##__LINE__; }
#define CGL_vec2_lerp(a, b, t) CGL_vec2_init(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t)
#define CGL_vec2_min(a, b) CGL_vec2_init(a.x < b.x ? a.x : b.x, a.y < b.y ? a.y : b.y)
#define CGL_vec2_max(a, b) CGL_vec2_init(a.x > b.x ? a.x : b.x, a.y > b.y ? a.y : b.y)
#define CGL_vec2_equal(a, b) (a.x == b.x && a.y == b.y)
#define CGL_vec2_rotate_about_point(a, p, theta) CGL_vec2_init(((a.x - p.x) * cosf(theta) - (a.y - p.y) * sinf(theta) ), ((a.x - p.x) * sinf(theta) + (a.y - p.y) * cosf(theta)))
#define CGL_vec2_centroid_of_triangle(a, b, c) CGL_vec2_init((a.x + b.x + c.x) / 3.0f, (a.y + b.y + c.y) / 3.0f)
#define CGL_vec2_from_angle(theta) CGL_vec2_init(cosf(theta), sinf(theta))
#define CGL_vec2_angle_between(a, b) acosf(CGL_vec2_dot(a, b) / (CGL_vec2_length(a) * CGL_vec2_length(b)))
#define CGL_vec2_angle(a) atan2f(a.y, a.x)
#define CGL_vec2_angle_from_to(a, b) atan2f(b.y - a.y, b.x - a.x)
#define CGL_vec2_reflect(a, n) CGL_vec2_sub(a, CGL_vec2_scale(n, 2.0f * CGL_vec2_dot(a, n)))
#define CGL_vec2_perpendicular(a) CGL_vec2_init(-a.y, a.x)
#define CGL_vec2_create_from_higher_dimension(a) CGL_vec2_init(a.x, a.y)
#define CGL_vec2_elem_get(a, i) ((float*)&a)[i]
#define CGL_vec2_elem_set(a, i, v) (((float*)&a)[i] = v)
#define CGL_vec2_distance(a, b) (CGL_float)(sqrtf(((a).x - (b).x) * ((a).x - (b).x) + ((a).y - (b).y) * ((a).y - (b).y)))
CGL_vec2 CGL_vec2_triple_product(CGL_vec2 a, CGL_vec2 b, CGL_vec2 c);
#ifdef __cplusplus
#define CGL_vec3_init(x, y, z) CGL_vec3(x, y, z)
#else
#define CGL_vec3_init(x, y, z) ((CGL_vec3){(x), (y), (z)})
#endif
#define CGL_vec3_add(a, b) CGL_vec3_init(a.x + b.x, a.y + b.y, a.z + b.z)
#define CGL_vec3_add_scaled(a, b, scale) CGL_vec3_init((a).x + (b).x * (scale), (a).y + (b).y * (scale), (a).z + (b).z * (scale))
#define CGL_vec3_sub(a, b) CGL_vec3_init(a.x - b.x, a.y - b.y, a.z - b.z)
#define CGL_vec3_mul(a, b) CGL_vec3_init(a.x * b.x, a.y * b.y, a.z * b.z)
#define CGL_vec3_div(a, b) CGL_vec3_init(a.x / b.x, a.y / b.y, a.z / b.z)
#define CGL_vec3_scale(a, s) CGL_vec3_init(a.x * (s), a.y * (s), a.z * (s))
#define CGL_vec3_dot(a, b) (a.x * b.x + a.y * b.y + a.z * b.z)
#define CGL_vec3_cross(a, b) CGL_vec3_init(a.y * b.z - a.z * b.y, a.z * b.x - a.x * b.z, a.x * b.y - a.y * b.x)
#define CGL_vec3_length(a) sqrtf(a.x * a.x + a.y * a.y + a.z * a.z)
#define CGL_vec3_distance(a, b) (CGL_float)(sqrtf(((a).x - (b).x) * ((a).x - (b).x) + ((a).y - (b).y) * ((a).y - (b).y) + ((a).z - (b).z) * ((a).z - (b).z)))
#define CGL_vec3_normalize(a) { CGL_float __CGL_vector_length##__LINE__ = 1.0f / CGL_vec3_length(a); a.x *= __CGL_vector_length##__LINE__; a.y *= __CGL_vector_length##__LINE__; a.z *= __CGL_vector_length##__LINE__; }
#define CGL_vec3_lerp(a, b, t) CGL_vec3_init(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t)
#define CGL_vec3_min(a, b) CGL_vec3_init(a.x < b.x ? a.x : b.x, a.y < b.y ? a.y : b.y, a.z < b.z ? a.z : b.z)
#define CGL_vec3_max(a, b) CGL_vec3_init(a.x > b.x ? a.x : b.x, a.y > b.y ? a.y : b.y, a.z > b.z ? a.z : b.z)
#define CGL_vec3_equal(a, b) (a.x == b.x && a.y == b.y && a.z == b.z)
#define CGL_vec3_rotate_x(a, theta) CGL_vec3_init(a.x, a.y * cosf(theta) - a.z * sinf(theta), a.y * sinf(theta) + a.z * cosf(theta))
#define CGL_vec3_rotate_y(v, theta) CGL_vec3_init(v.x * cosf(theta) + v.z * sinf(theta), v.y, -v.x * sinf(theta) + v.z * cosf(theta))
#define CGL_vec3_rotate_z(v, theta) CGL_vec3_init(v.x * cosf(theta) - v.y * sinf(theta), v.x * sinf(theta) + v.y * cosf(theta), v.z)
#define CGL_vec3_centroid_of_triangle(a, b, c) CGL_vec3_init((a.x + b.x + c.x) / 3.0f, (a.y + b.y + c.y) / 3.0f, (a.z + b.z + c.z) / 3.0f)
#define CGL_vec3_from_angle(theta) CGL_vec3_init(cosf(theta), sinf(theta), 0.0f)
#define CGL_vec3_angle_between(a, b) acosf(CGL_vec3_dot(a, b) / (CGL_vec3_length(a) * CGL_vec3_length(b)))
#define CGL_vec3_from_spherical_coordinates(r, theta, phi) CGL_vec3_init(r * sinf(theta) * cosf(phi), r * sinf(theta) * sinf(phi), r * cosf(theta))
#define CGL_vec3_to_spherical_coordinates(a) CGL_vec3_init(sqrtf(a.x * a.x + a.y * a.y + a.z * a.z), acosf(a.z / sqrtf(a.x * a.x + a.y * a.y + a.z * a.z)), atan2f(a.y, a.x))
#define CGL_vec3_from_higher_dimension(a) CGL_vec3_init(a.x, a.y, a.z)
#define CGL_vec3_from_vec2(a, z) CGL_vec3_init(a.x, a.y, z)
#define CGL_vec3_elem_get(a, i) ((float*)&a)[i]
#define CGL_vec3_elem_set(a, i, v) (((float*)&a)[i] = v)
CGL_vec3 CGL_vec3_reflect(CGL_vec3 a, CGL_vec3 n);
CGL_vec3 CGL_vec3_rotate_about_axis(CGL_vec3 v, CGL_vec3 axis, CGL_float theta);
CGL_vec3 CGL_vec3_triple_product(CGL_vec3 a, CGL_vec3 b, CGL_vec3 c);
void CGL_vec3_calculate_orthonormal_basis_from_one_vector(CGL_vec3 a, CGL_vec3* pb, CGL_vec3* pc);
#ifdef __cplusplus
#define CGL_vec4_init(x, y, z, w) CGL_vec4(x, y, z, w)
#else
#define CGL_vec4_init(x, y, z, w) ((CGL_vec4){x, y, z, w})
#endif
#define CGL_vec4_add(a, b) CGL_vec4_init(a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w)
#define CGL_vec4_sub(a, b) CGL_vec4_init(a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w)
#define CGL_vec4_mul(a, b) CGL_vec4_init(a.x * b.x, a.y * b.y, a.z * b.z, a.w * b.w)
#define CGL_vec4_div(a, b) CGL_vec4_init(a.x / b.x, a.y / b.y, a.z / b.z, a.w / b.w)
#define CGL_vec4_scale(a, s) CGL_vec4_init(a.x * s, a.y * s, a.z * s, a.w * s)
#define CGL_vec4_dot(a, b) (a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w)
#define CGL_vec4_lerp(a, b, t) CGL_vec4_init(a.x + (b.x - a.x) * t, a.y + (b.y - a.y) * t, a.z + (b.z - a.z) * t, a.w + (b.w - a.w) * t)
#define CGL_vec4_min(a, b) CGL_vec4_init(a.x < b.x ? a.x : b.x, a.y < b.y ? a.y : b.y, a.z < b.z ? a.z : b.z, a.w < b.w ? a.w : b.w)
#define CGL_vec4_max(a, b) CGL_vec4_init(a.x > b.x ? a.x : b.x, a.y > b.y ? a.y : b.y, a.z > b.z ? a.z : b.z, a.w > b.w ? a.w : b.w)
#define CGL_vec4_equal(a, b) (a.x == b.x && a.y == b.y && a.z == b.z && a.w == b.w)
#define CGL_vec4_normalize(a) { CGL_float __CGL_vector_length##__LINE__ = 1.0f / CGL_vec4_length(a); a.x *= __CGL_vector_length##__LINE__; a.y *= __CGL_vector_length##__LINE__; a.z *= __CGL_vector_length##__LINE__; a.w *= __CGL_vector_length##__LINE__; }
#define CGL_vec4_normalize_vec3(a) { CGL_float __CGL_vector_length##__LINE__ = 1.0f / CGL_vec3_length(a); a.x *= __CGL_vector_length##__LINE__; a.y *= __CGL_vector_length##__LINE__; a.z *= __CGL_vector_length##__LINE__; }
#define CGL_vec4_centroid_of_triangle(a, b, c) CGL_vec4_init((a.x + b.x + c.x) / 3.0f, (a.y + b.y + c.y) / 3.0f, (a.z + b.z + c.z) / 3.0f, (a.w + b.w + c.w) / 3.0f)
#define CGL_vec4_from_vec3(a, w) CGL_vec4_init(a.x, a.y, a.z, w)
#define CGL_vec4_from_vec2(a, z, w) CGL_vec4_init(a.x, a.y, z, w)
#define CGL_vec4_elem_get(a, i) ((float*)&a)[i]
#define CGL_vec4_elem_set(a, i, v) (((float*)&a)[i] = v)
CGL_vec4 CGL_vec4_triple_product(CGL_vec4 a, CGL_vec4 b, CGL_vec4 c);
#ifdef __cplusplus
#define CGL_mat3_init(a, b, c, d, e, f, g, h, i) CGL_mat3(a, b, c, d, e, f, g, h, i)
#else
#define CGL_mat3_init(m00, m01, m02, m10, m11, m12, m20, m21, m22) (CGL_mat3) \
{ \
{ \
m00, m10, m20, \
m01, m11, m21, \
m02, m12, m22 \
} \
}
#endif
#define CGL_mat3_add(a, b) (CGL_mat3){a.m[0] + b.m[0], a.m[1] + b.m[1], a.m[2] + b.m[2], a.m[3] + b.m[3], a.m[4] + b.m[4], a.m[5] + b.m[5], a.m[6] + b.m[6], a.m[7] + b.m[7], a.m[8] + b.m[8]}
#define CGL_mat3_sub(a, b) (CGL_mat3){a.m[0] - b.m[0], a.m[1] - b.m[1], a.m[2] - b.m[2], a.m[3] - b.m[3], a.m[4] - b.m[4], a.m[5] - b.m[5], a.m[6] - b.m[6], a.m[7] - b.m[7], a.m[8] - b.m[8]}
CGL_float CGL_mat3_det(CGL_mat3 a);
CGL_float CGL_mat3_trace(CGL_mat3 a);
CGL_mat3 CGL_mat3_transpose(CGL_mat3 a);
#define CGL_mat3_zero() CGL_mat3_init( \
0.0f, 0.0f, 0.0f, \
0.0f, 0.0f, 0.0f, \
0.0f, 0.0f, 0.0f \
)