This repository has been archived by the owner on Dec 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_shared.h
167 lines (140 loc) · 3.62 KB
/
app_shared.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
#include "inttypes.h"
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef float f32;
typedef double f64;
typedef s8 b8; // booleans
typedef s16 b16;
typedef s64 b64;
typedef s32 b32;
#define debug_break() do{if(IsDebuggerPresent()) {fflush(stdout); __debugbreak();}}while(0)
// Usually I enable my asserts for non-shipping builds only
#define assert(Expression) do{ if(!(Expression)) { debug_break(); *((s32 volatile*)0) = 1; ExitProcess(1); }}while(0)
#define break_at(Expression) do{ if (!(Expression)) { debug_break(); } }while(0)
#define get_min(a, b) (((a) > (b)) ? (b) : (a))
#define get_max(a, b) (((a) > (b)) ? (a) : (b))
#define array_count(a) ((sizeof(a))/(sizeof(*a)))
// Assumes 0 initialized memory in 2 places
#define allocate_memory(Bytes) VirtualAlloc(0, Bytes, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE);
struct v2
{
f32 x, y;
};
struct Rect
{
f32 x, y;
f32 w, h;
};
static b32 v2_in_rect(Rect rect, v2 vec)
{
return (vec.x >= rect.x && vec.x < rect.x + rect.w &&
vec.y >= rect.y && vec.y < rect.y + rect.h);
}
static Rect rect_contract(Rect rect, f32 contract)
{
rect.x += contract;
rect.y += contract;
rect.w -= contract*2.f;
rect.h -= contract*2.f;
return rect;
}
static s64 time_perf()
{
LARGE_INTEGER large;
QueryPerformanceCounter(&large);
s64 result = large.QuadPart;
return result;
}
static f32 time_elapsed(s64 recent, s64 old)
{
LARGE_INTEGER perfomance_freq;
QueryPerformanceFrequency(&perfomance_freq);
f32 inv_freq = 1.f / (f32)perfomance_freq.QuadPart;
s64 delta = recent - old;
f32 result = ((f32)delta * inv_freq);
return result;
}
struct Bit_Scan_Result
{
u32 index;
b32 found;
};
static Bit_Scan_Result find_most_significant_bit(u64 value)
{
Bit_Scan_Result result = {};
#if _MSC_VER
unsigned long index;
result.found = _BitScanReverse64(&index, value);
result.index = index;
#else
if (value) {
result.found = true;
result.index = 63 - __builtin_clzll(value);
}
#endif
return result;
}
static Bit_Scan_Result find_most_significant_bit(s64 value) {
return find_most_significant_bit((u64)value);
}
static Bit_Scan_Result find_most_significant_bit(u32 value)
{
Bit_Scan_Result result = {};
#if _MSC_VER
unsigned long index;
result.found = _BitScanReverse(&index, value);
result.index = index;
#else
if (value) {
result.found = true;
result.index = 31 - __builtin_clz(value);
}
#endif
return result;
}
static Bit_Scan_Result find_most_significant_bit(s32 value) {
return find_most_significant_bit((u32)value);
}
static Bit_Scan_Result find_least_significant_bit(u32 value)
{
Bit_Scan_Result result = {};
#if _MSC_VER
unsigned long index;
result.found = _BitScanForward(&index, value);
result.index = index;
#else
if (value) {
result.found = true;
result.index = __builtin_ctz(value);
}
#endif
return result;
}
static Bit_Scan_Result find_least_significant_bit(s32 value) {
return find_least_significant_bit((u32)value);
};
static Bit_Scan_Result
find_least_significant_bit(u64 value)
{
Bit_Scan_Result result = {};
#if _MSC_VER
unsigned long index;
result.found = _BitScanForward64(&index, value);
result.index = index;
#else
if (value) {
result.found = true;
result.index = __builtin_ctzll(value);
}
#endif
return result;
}
static Bit_Scan_Result find_least_significant_bit(s64 value) {
return find_least_significant_bit((u64)value);
};