-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutil.c
More file actions
executable file
·147 lines (128 loc) · 3.92 KB
/
util.c
File metadata and controls
executable file
·147 lines (128 loc) · 3.92 KB
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
/* util.c */
#include "util.h"
// Formula is the square root of (x2 - x1)^2 + (y2 - y1)^2
float UtilDistance(Vector2 a, Vector2 b)
{
double first = pow(b.x - a.x, 2);
double second = pow(b.y - a.y, 2);
return sqrt(first + second);
}
// Lerp see https://en.wikipedia.org/wiki/Linear_interpolation
float UtilLerp(float v0, float v1, float t)
{
return (1.0f - t) * v0 + t * v1;
}
float UtilEaseInSine(float A, float B, float v)
{
v = 1.0f - cosf((v * PI) / 2.0f); // easings.net
return (B * v) + (A * (1.0f - v));
}
float UtilEaseInCubic(float A, float B, float v)
{
v = v * v * v;
return (B * v) + (A * (1.0f - v));
}
// Smoothstep see http://sol.gfxile.net/interpolation/
float UtilSmoothstep(float A, float B, float v)
{
v = (v) * (v) * (3.0f - 2.0f * (v));
return (B * v) + (A * (1.0f - v));
}
// Smootherstep see http://sol.gfxile.net/interpolation/
float UtilSmootherstep(float A, float B, float v)
{
v = (v) * (v) * (v) * ((v)*((v) * 6.0f - 15.0f) + 10.0f);
return (B * v) + (A * (1.0f - v));
}
// Normalize is the opposite of lerp. Instead of a range and a factor, we give a range and a value to find out the factor.
float UtilNormalize(float start, float finish, float value)
{
return (value - start) / (finish - start);
}
// MapValue converts a value from the scale [fromMin, fromMax] to a value from the scale [toMin, toMax].
// It’s just the normalize and lerp functions working together.
float UtilMapValue(float value, float fromMin, float fromMax, float toMin, float toMax)
{
return UtilLerp(toMin, toMax, UtilNormalize(fromMin, fromMax, value));
}
// OverlapArea returns the intersection area of two rectangles
float UtilOverlapArea(Rectangle a, Rectangle b)
{
float x = fmaxf(0, fminf(a.x + a.width, b.x + b.width) - fmaxf(a.x, b.x));
float y = fmaxf(0, fminf(a.y + a.height, b.y + b.height) - fmaxf(a.y, b.y));
return x * y;
}
char* UtilOrdToShortString(int ord)
{
static char* ords[14] = {"", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
return ords[ord];
}
char* UtilOrdToLongString(int ord)
{
static char* ords[14] = {"", "Ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"};
return ords[ord];
}
char* UtilSuitToShortString(int suit)
{
static char* suits[4] = {"C", "D", "H", "S"};
return suits[suit];
}
char* UtilSuitToLongString(int suit)
{
static char* suits[4] = {"Club", "Diamond", "Heart", "Spade"};
return suits[suit];
}
/*
djb2
this algorithm (k=33) was first reported by dan bernstein many years ago in comp.lang.c.
another version of this algorithm (now favored by bernstein) uses xor: hash(i) = hash(i - 1) * 33 ^ str[i];
the magic of number 33 (why it works better than many other constants, prime or not) has never been adequately explained.
*/
uint32_t UtilHash(char *str)
{
uint32_t hash = 5381;
while ( *str ) {
// hash = hash * 33 + c;
hash = ((hash << 5) + hash) + *str++;
}
return hash;
}
_Bool UtilRectangleWithinRectangle(Rectangle inner, Rectangle outer)
{
if (inner.x < outer.x || inner.y < outer.y) {
return 0;
}
float innerRight = inner.x + inner.width;
float outerRight = outer.x + outer.width;
if (innerRight > outerRight) {
return 0;
}
float innerBottom = inner.y + inner.height;
float outerBottom = outer.y + outer.height;
if (innerBottom > outerBottom) {
return 0;
}
return 1;
}
Vector2 UtilCenterTextInRectangle(Rectangle r, float width, float height)
{
return (Vector2){
.x = r.x + (r.width / 2.0f) - (width / 2.0f),
.y = r.y + (r.height / 2.0f) - (height / 2.0f),
};
}
Vector2 UtilVector2Add(Vector2 a, Vector2 b)
{
return (Vector2){.x = a.x + b.x, .y = a.y + b.y};
}
char* UtilStrDup(const char *s)
{
if (!s) {
return (char*)0;
}
char *d = malloc(strlen(s)+1);
if (d) {
strcpy(d, s);
}
return d;
}