Debian Codegraph @

src/cdogs/utils.h

raw symbols SCIP
1 /*
2 C-Dogs SDL
3 A port of the legendary (and fun) action/arcade cdogs.
4 Copyright (C) 1995 Ronny Wester
5 Copyright (C) 2003 Jeremy Chin
6 Copyright (C) 2003-2007 Lucas Martin-King
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 This file incorporates work covered by the following copyright and
23 permission notice:
24
25 Copyright (c) 2013-2017, 2019-2025 Cong Xu
26 All rights reserved.
27
28 Redistribution and use in source and binary forms, with or without
29 modification, are permitted provided that the following conditions are met:
30
31 Redistributions of source code must retain the above copyright notice, this
32 list of conditions and the following disclaimer.
33 Redistributions in binary form must reproduce the above copyright notice,
34 this list of conditions and the following disclaimer in the documentation
35 and/or other materials provided with the distribution.
36
37 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
38 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
41 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
42 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
43 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
47 POSSIBILITY OF SUCH DAMAGE.
48 */
49 #pragma once
50
51 #include <assert.h>
52 #include <stdbool.h>
53 #include <stdio.h> /* for stderr */
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 // Global variables so their address can be taken (passed into void * funcs)
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 // Even though malloc(0) may return NULL, we don't account for it for
90 // simplicity and to allow code linters to work better
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 // Given /path/to/file, return file
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 // Fake device used for co-op AI
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 // Helper macros for defining type/str conversion funcs
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; // cranial accessories
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, // place anywhere
249 PLACEMENT_ACCESS_LOCKED, // place in locked rooms
250 PLACEMENT_ACCESS_NOT_LOCKED // don't place in locked rooms
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);