-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdlb_types.h
155 lines (139 loc) · 4.78 KB
/
dlb_types.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
#ifndef DLB_TYPES_H
#define DLB_TYPES_H
//------------------------------------------------------------------------------
// Basic type redefinitions
//------------------------------------------------------------------------------
#include <cstdint>
#include <climits>
//#include <stdbool.h>
//#include <assert.h>
//#include <float.h>
//#include <stddef.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 r32;
typedef double r64;
typedef u32 b32;
typedef s8 int8;
typedef s16 int16;
typedef s32 int32;
typedef s64 int64;
typedef u8 uint8;
typedef u16 uint16;
typedef u32 uint32;
typedef u64 uint64;
typedef r32 real32;
typedef r64 real64;
//typedef u32 bool32;
// NOTE: internal and global are relative to translation unit
#if 0
#define local static
#define internal static
#define global static
#endif
// Enums generators
//
// #define MENU_ITEMS(f) \
// f(Main, "Main Menu") \
// f(Audio, "Audio")
// DLB_ENUM_DECL(Menu, MENU_ITEMS);
// DLB_ENUM_DEFS(Menu, MENU_ITEMS);
// STRING(Menu::Main);
//
#define DLB_ENUM(e, ...) e,
#define DLB_ENUM_INT(e, val) e = val,
#define DLB_ENUM_STRINGIFY(e, ...) #e,
#define DLB_ENUM_DESCIFY(e, desc) desc,
#define DLB_ENUM_DECL(t, items_macro) \
enum t { \
items_macro(DLB_ENUM) \
}; \
extern const char *t##Str[]; \
extern const char *t##Desc[];
#define DLB_ENUM_DEFS(t, items_macro) \
const char *t##Str[] = { items_macro(DLB_ENUM_STRINGIFY) }; \
const char *t##Desc[] = { items_macro(DLB_ENUM_DESCIFY) };
#define DLB_ENUM_STR(t, e) t##Str[(int)t::e]
#define DLB_ENUM_DESC(t, e) t##Desc[(int)t::e]
// Useful macros
#define UNUSED(x) ((void)(x))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define ABS(x) (((x) > 0) ? (x) : -(x))
#define CLAMP(x, min, max) (MAX((min), MIN((x), (max))))
#define LERP(a, b, alpha) ((a) + ((b) - (a)) * (alpha))
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) // NOTE: Old name is ARRAY_COUNT
#define FIELD_SIZEOF(type, field) (sizeof(((type *)0)->field)) // NOTE: Old name is SIZEOF_MEMBER
#define FIELD_SIZEOF_ARRAY(type, field) (sizeof(*(((type *)0)->field))) // NOTE: Old name is SIZEOF_MEMBER_ARRAY
#define OFFSETOF(type, field) ((size_t)&(((type *)0)->field))
#define STRING(s) #s
#define CSTR(s) (s), sizeof(s) - 1 // without terminator
#define CSTR0(s) (s), sizeof(s) // with nil terminator
#define IFNULL(a, b) ((a) ? (a) : (b))
#define KB(bytes) (1024 * bytes)
#define MB(bytes) (1024 * KB(bytes))
#define GB(bytes) (1024 * MB(bytes))
// Note: Alignment must be power of 2
#define ALIGN_DOWN(n, a) ((n) & ~((a) - 1))
#define ALIGN_UP(n, a) ALIGN_DOWN((n) + (a) - 1, (a))
#define ALIGN_DOWN_PTR(p, a) ((void *)ALIGN_DOWN((uintptr_t)(p), (a)))
#define ALIGN_UP_PTR(p, a) ((void *)ALIGN_UP((uintptr_t)(p), (a)))
/*
// NOT SAFE TO USE IN WINDOWS 10, CAUSES ENTIRE OS TO HANG
#if defined(_MSC_VER)
#define DLB_DEBUG_BREAK __debugbreak()
#elif defined(__GNUC__) || defined(__clang__)
#define DLB_DEBUG_BREAK __builtin_trap()
#endif
*/
static inline u16 endian_swap_u16(u16 val)
{
return (u16)(((val & 0xFF00) >> 8) |
((val & 0x00FF) << 8));
}
static inline u32 endian_swap_u32(u32 val)
{
return (u32)(((val & 0xFF000000) >> 24) |
((val & 0x00FF0000) >> 8) |
((val & 0x0000FF00) << 8) |
((val & 0x000000FF) << 24));
}
static inline void swap_r32(r32 *a, r32 *b)
{
r32 t = *a;
*a = *b;
*b = t;
}
static inline void swap_int(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
#endif
//------------------------------------------------------------------------------
// HACK: I have no idea why this doesn't work when it's inside of the #include
// guards for unity builds... Defining this multiple times doesn't really hurt
// anything, so... fuck it.
//------------------------------------------------------------------------------
#define DLB_ASSERT_HANDLER(name) \
void name(const char *expr, const char *filename, u32 line)
typedef DLB_ASSERT_HANDLER(dlb_assert_handler_def);
extern dlb_assert_handler_def *dlb_assert_handler;
#define DLB_ASSERT(expr) \
do { \
if (!(expr)) { \
if (dlb_assert_handler) { \
(*dlb_assert_handler)(#expr, __FILE__, __LINE__); \
} \
__debugbreak(); \
exit(-1); \
} \
} while(0)
//------------------------------------------------------------------------------