| 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 |
#pragma once |
| 50 |
|
| 51 |
#include <assert.h> |
| 52 |
#include <stdbool.h> |
| 53 |
#include <stdio.h> |
| 54 |
#include <stdlib.h> |
| 55 |
#include <string.h> |
| 56 |
|
| 57 |
#include <SDL.h> |
| 58 |
|
| 59 |
#include "color.h" |
| 60 |
#include "sys_specifics.h" |
| 61 |
|
| 62 |
|
| 63 |
extern bool gTrue; |
| 64 |
extern bool gFalse; |
| 65 |
|
| 66 |
#define STRINGIFY(x) #x |
| 67 |
#define TOSTRING(x) STRINGIFY(x) |
| 68 |
|
| 69 |
#ifdef _MSC_VER |
| 70 |
#define CHALT() __debugbreak() |
| 71 |
#else |
| 72 |
#define CHALT() |
| 73 |
#endif |
| 74 |
|
| 75 |
#define CASSERT(_x, _errmsg) \ |
| 76 |
{ \ |
| 77 |
volatile bool isOk = _x; \ |
| 78 |
if (!isOk) \ |
| 79 |
{ \ |
| 80 |
static char _buf[1024]; \ |
| 81 |
sprintf( \ |
| 82 |
_buf, "In %s %d:%s: " _errmsg " (" #_x ")", __FILE__, \ |
| 83 |
__LINE__, __func__); \ |
| 84 |
CHALT(); \ |
| 85 |
assert(_x); \ |
| 86 |
} \ |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
#define _CCHECKALLOC(_func, _var, _size) \ |
| 92 |
{ \ |
| 93 |
if (_var == NULL) \ |
| 94 |
{ \ |
| 95 |
exit(1); \ |
| 96 |
} \ |
| 97 |
} |
| 98 |
|
| 99 |
#define CMALLOC(_var, _size) \ |
| 100 |
{ \ |
| 101 |
_var = malloc(_size); \ |
| 102 |
_CCHECKALLOC("CMALLOC", _var, (_size)) \ |
| 103 |
} |
| 104 |
#define CCALLOC(_var, _size) \ |
| 105 |
{ \ |
| 106 |
_var = calloc(1, _size); \ |
| 107 |
_CCHECKALLOC("CCALLOC", _var, (_size)) \ |
| 108 |
} |
| 109 |
#define CREALLOC(_var, _size) \ |
| 110 |
{ \ |
| 111 |
_var = realloc(_var, _size); \ |
| 112 |
_CCHECKALLOC("CREALLOC", _var, (_size)) \ |
| 113 |
} |
| 114 |
#define CSTRDUP(_var, _str) \ |
| 115 |
{ \ |
| 116 |
CMALLOC(_var, strlen(_str) + 1); \ |
| 117 |
strcpy(_var, _str); \ |
| 118 |
} |
| 119 |
|
| 120 |
#define CFREE(_var) \ |
| 121 |
{ \ |
| 122 |
free(_var); \ |
| 123 |
} |
| 124 |
|
| 125 |
#define UNUSED(expr) (void)(expr); |
| 126 |
|
| 127 |
#ifndef MAX |
| 128 |
#define MAX(x, y) ((x) > (y) ? (x) : (y)) |
| 129 |
#endif |
| 130 |
#ifndef MIN |
| 131 |
#define MIN(x, y) ((x) < (y) ? (x) : (y)) |
| 132 |
#endif |
| 133 |
#define CLAMP(v, _min, _max) MAX((_min), MIN((_max), (v))) |
| 134 |
#define CLAMP_OPPOSITE(v, _min, _max) \ |
| 135 |
((v) > (_max) ? (_min) : ((v) < (_min) ? (_max) : (v))) |
| 136 |
#define SIGN(x) ((x) != 0 ? (x) / abs(x) : 1) |
| 137 |
#define SQUARED(x) ((x) * (x)) |
| 138 |
#define SWAP(x, y, T) \ |
| 139 |
do \ |
| 140 |
{ \ |
| 141 |
T _tmp = x; \ |
| 142 |
x = y; \ |
| 143 |
y = _tmp; \ |
| 144 |
} while (0) |
| 145 |
#define DIV_ROUND_UP(a, b) (((a) + ((b) - 1)) / (b)) |
| 146 |
|
| 147 |
const char *StrGetFileExt(const char *filename); |
| 148 |
|
| 149 |
void PathGetDirname(char *buf, const char *path); |
| 150 |
|
| 151 |
const char *PathGetBasename(const char *path); |
| 152 |
void PathGetWithoutExtension(char *buf, const char *path); |
| 153 |
void PathGetBasenameWithoutExtension(char *buf, const char *path); |
| 154 |
void RealPath(const char *src, char *dest); |
| 155 |
void RelPath(char *buf, const char *to, const char *from); |
| 156 |
void FixPathSeparator(char *dst, const char *src); |
| 157 |
char *CDogsGetCWD(char *buf); |
| 158 |
void RelPathFromCWD(char *buf, const char *to); |
| 159 |
void GetDataFilePath(char *buf, const char *path); |
| 160 |
|
| 161 |
double Round(double x); |
| 162 |
|
| 163 |
double ToDegrees(double radians); |
| 164 |
|
| 165 |
struct vec2 CalcClosestPointOnLineSegmentToPoint( |
| 166 |
const struct vec2 l1, const struct vec2 l2, const struct vec2 p); |
| 167 |
|
| 168 |
typedef enum |
| 169 |
{ |
| 170 |
INPUT_DEVICE_UNSET, |
| 171 |
INPUT_DEVICE_KEYBOARD, |
| 172 |
INPUT_DEVICE_JOYSTICK, |
| 173 |
|
| 174 |
|
| 175 |
INPUT_DEVICE_AI, |
| 176 |
|
| 177 |
INPUT_DEVICE_COUNT |
| 178 |
} input_device_e; |
| 179 |
|
| 180 |
const char *InputDeviceName(const int d, const int deviceIndex); |
| 181 |
|
| 182 |
typedef enum |
| 183 |
{ |
| 184 |
ALLYCOLLISION_NORMAL, |
| 185 |
ALLYCOLLISION_REPEL, |
| 186 |
ALLYCOLLISION_NONE |
| 187 |
} AllyCollision; |
| 188 |
const char *AllyCollisionStr(int a); |
| 189 |
int StrAllyCollision(const char *str); |
| 190 |
|
| 191 |
char *IntStr(int i); |
| 192 |
char *PercentStr(int p); |
| 193 |
char *Div8Str(int i); |
| 194 |
void CamelToTitle(char *buf, const char *src); |
| 195 |
bool StrStartsWith(const char *str, const char *prefix); |
| 196 |
bool StrEndsWith(const char *str, const char *suffix); |
| 197 |
int Stricmp(const char *a, const char *b); |
| 198 |
int CompareIntsAsc(const void *v1, const void *v2); |
| 199 |
int CompareIntsDesc(const void *v1, const void *v2); |
| 200 |
bool IntsEqual(const void *v1, const void *v2); |
| 201 |
|
| 202 |
|
| 203 |
#define T2S(_type, _str) \ |
| 204 |
case _type: \ |
| 205 |
return _str; |
| 206 |
#define S2T(_type, _str) \ |
| 207 |
if (strcmp(s, _str) == 0) \ |
| 208 |
{ \ |
| 209 |
return _type; \ |
| 210 |
} |
| 211 |
|
| 212 |
#define RAND_INT(_low, _high) \ |
| 213 |
((_low) == (_high) ? (_low) : (_low) + (rand() % ((_high) - (_low)))) |
| 214 |
#define RAND_FLOAT(_low, _high) (float)RAND_DOUBLE(_low, _high) |
| 215 |
#define RAND_DOUBLE(_low, _high) \ |
| 216 |
((_low) + ((double)rand() / RAND_MAX * ((_high) - (_low)))) |
| 217 |
#define RAND_BOOL() (rand() > RAND_MAX / 2) |
| 218 |
|
| 219 |
typedef enum |
| 220 |
{ |
| 221 |
HEAD_PART_HAIR, |
| 222 |
HEAD_PART_FACEHAIR, |
| 223 |
HEAD_PART_HAT, |
| 224 |
HEAD_PART_GLASSES, |
| 225 |
HEAD_PART_COUNT |
| 226 |
} HeadPart; |
| 227 |
|
| 228 |
const char *HeadPartStr(const HeadPart hp); |
| 229 |
|
| 230 |
typedef enum |
| 231 |
{ |
| 232 |
BODY_PART_HEAD, |
| 233 |
BODY_PART_HAIR, |
| 234 |
BODY_PART_FACEHAIR, |
| 235 |
BODY_PART_HAT, |
| 236 |
BODY_PART_GLASSES, |
| 237 |
BODY_PART_BODY, |
| 238 |
BODY_PART_LEGS, |
| 239 |
BODY_PART_GUN_R, |
| 240 |
BODY_PART_GUN_L, |
| 241 |
BODY_PART_COUNT |
| 242 |
} BodyPart; |
| 243 |
|
| 244 |
BodyPart StrBodyPart(const char *s); |
| 245 |
|
| 246 |
typedef enum |
| 247 |
{ |
| 248 |
PLACEMENT_ACCESS_ANY, |
| 249 |
PLACEMENT_ACCESS_LOCKED, |
| 250 |
PLACEMENT_ACCESS_NOT_LOCKED |
| 251 |
} PlacementAccessFlags; |
| 252 |
|
| 253 |
int Pulse256(const int t); |
| 254 |
|
| 255 |
char *ReadFileIntoBuf(const char *path, const char *mode); |
| 256 |
|
| 257 |
SDL_Surface *LoadImgToSurface(const char *path); |