summaryrefslogtreecommitdiff
path: root/vendors/_.h
blob: d5662dc9e52de69cb674d95e173e90330ee8f5bc (plain)
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
#pragma once
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "input.h"

void wdeinit(void);
[[nodiscard]] void *alloc(size_t size);
void _free(void *ptr);
void *_realloc(void *ptr, size_t size);

#define free _free
#define malloc alloc
#define calloc(X, Y) alloc((X) * (Y))
#define realloc _realloc

// math
#define clamp(X, MIN, MAX) ({ \
	const auto ___x = (X); \
	const auto ___min = (MIN); \
	const auto ___max = (MAX); \
	(___x > ___max) ? ___max : ((___x < ___min) ? ___min : ___x); \
})
#define max(X, Y) ({ \
	const auto ___x = (X); \
	const auto ___y = (Y); \
	(___x > ___y) ? ___x : ___y; \
})
#define min(X, Y) ({ \
	const auto ___x = (X); \
	const auto ___y = (Y); \
	(___x < ___y) ? ___x : ___y; \
})

#define STR(X) #X
#define defer(X) if (atexit(X)) { X(); panic("defer"); }
#define auto __auto_type
#define foreach(E, L) for (__auto_type E = (L); E != NULL; E = E->next)
#define rfor(I, F, T) for (__auto_type I = F; I < T; I++)
#define repeat(I, T) for (__auto_type I = 0u; I < T; I++)
#define with(I, T) auto I = (T); if (I)

// logging
#define pgeneric(PREFIX, ...) fprintf(stderr, \
                                      "\x1b["PREFIX" %s:%s:%d\x1b[0m \t", \
                                      __FILE_NAME__, __FUNCTION__, __LINE__), \
                              fprintf(stderr, __VA_ARGS__), fputc('\n', stderr)
#define plog(...) pgeneric("94mLOG", __VA_ARGS__)
#define perr(...) pgeneric("91mERR", __VA_ARGS__)
#define pwrn(...) pgeneric("93mWRN", __VA_ARGS__)
#define panic(...) perr(__VA_ARGS__), exit(EXIT_FAILURE)
#define assert(X) if (!(X)) panic("assert failed: "STR(X))

// mathematical vectors

#define UNPACK(V) (V).x, (V).y
#define I2(X, Y) (int2){{(X), (Y)}}
#define I2R(X) I2((X), (X))
#define I2Z I2(0, 0)
#define F2(X, Y) (float2){{(X), (Y)}}
#define F2R(X) F2((X), (X))
#define F2Z F2(0.0f, 0.0f)
#define I4(X, Y, Z, W) (int4){{(X), (Y), (Z), (W)}}
#define I4Z I4(0, 0, 0, 0)
#define I21 I2R(1)
#define F21 F2R(1)

typedef union { struct { int x, y; }; int a[2]; } int2;
typedef union { struct { float x, y; }; float a[2]; } float2;
typedef union { struct { int x, y, z, w; }; int a[4]; } int4;

float2 tofloat2(int2 a);
int2 toint2(float2 a);

float2 float2_add(float2 a, float2 b);
float2 float2_sub(float2 a, float2 b);
float2 float2_mul(float2 a, float x);
float2 float2_div(float2 a, float x);
float float2_length(float2 a);
float2 float2_normalize(float2 a);
float float2_angle(float2 a);
int2 float2_round(float2 a);
float2 float2_clamp(float2 min, float2 a, float2 max);
float2 float2_swp(float2 a);
float2 float2_lerp(float2 a, float2 b, float x);

int2 int2_sub(int2 a, int2 b);
int2 int2_add(int2 a, int2 b);
int2 int2_mul(int2 a, float x);
int2 int2_div(int2 a, float x);
int2 int2_abs(int2 a);
int2 int2_swp(int2 a);
int2 int2_inv(int2 a);

int2 int4_xy(int4 a);
int2 int4_zw(int4 a);

#define int2_log(L, A) p##L("%d %d", (A).x, (A).y)
#define float2_log(L, A) p##L("%f %f", (A).x, (A).y)

#include "TZR.h"
#include "config.h"
#include "world.h"