]> icculus.org git repositories - divverent/darkplaces.git/blob - common.h
Don't generate tangent vectors when GLSL is off, small improvement to anim caching..
[divverent/darkplaces.git] / common.h
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19 */
20
21 #ifndef COMMON_H
22 #define COMMON_H
23
24 /// many buffers use this size
25 #define MAX_INPUTLINE 16384
26
27
28 /// MSVC has a different name for several standard functions
29 #ifdef WIN32
30 # define strcasecmp _stricmp
31 # define strncasecmp _strnicmp
32 #endif
33
34 // Create our own define for Mac OS X
35 #if defined(__APPLE__) && defined(__MACH__)
36 # define MACOSX
37 #endif
38
39 #ifdef SUNOS
40 #include <sys/file.h>           ///< Needed for FNDELAY
41 #endif
42
43 //============================================================================
44
45 typedef struct sizebuf_s
46 {
47         qboolean        allowoverflow;  ///< if false, do a Sys_Error
48         qboolean        overflowed;             ///< set to true if the buffer size failed
49         unsigned char           *data;
50         int                     maxsize;
51         int                     cursize;
52 } sizebuf_t;
53
54 void SZ_Clear (sizebuf_t *buf);
55 unsigned char *SZ_GetSpace (sizebuf_t *buf, int length);
56 void SZ_Write (sizebuf_t *buf, const unsigned char *data, int length);
57 void SZ_HexDumpToConsole(const sizebuf_t *buf);
58
59 void Com_HexDumpToConsole(const unsigned char *data, int size);
60
61 unsigned short CRC_Block(const unsigned char *data, size_t size);
62 unsigned short CRC_Block_CaseInsensitive(const unsigned char *data, size_t size); // for hash lookup functions that use strcasecmp for comparison
63
64 unsigned char COM_BlockSequenceCRCByteQW(unsigned char *base, int length, int sequence);
65
66 // these are actually md4sum (mdfour.c)
67 unsigned Com_BlockChecksum (void *buffer, int length);
68 void Com_BlockFullChecksum (void *buffer, int len, unsigned char *outbuf);
69
70
71 //============================================================================
72 //                                                      Endianess handling
73 //============================================================================
74
75 // We use BSD-style defines: BYTE_ORDER is defined to either BIG_ENDIAN or LITTLE_ENDIAN
76
77 // Initializations
78 #if !defined(BYTE_ORDER) || !defined(LITTLE_ENDIAN) || !defined(BIG_ENDIAN) || (BYTE_ORDER != LITTLE_ENDIAN && BYTE_ORDER != BIG_ENDIAN)
79 # undef BYTE_ORDER
80 # undef LITTLE_ENDIAN
81 # undef BIG_ENDIAN
82 # define LITTLE_ENDIAN 1234
83 # define BIG_ENDIAN 4321
84 #endif
85
86 // If we still don't know the CPU endianess at this point, we try to guess
87 // normally including sys/types.h includes endian.h for the platform, which defines BYTE_ORDER, LITTLE_ENDIAN, and BIG_ENDIAN, however endian.h is a BSD-ism, and may not be present on all platforms (particularly windows)
88 #ifndef BYTE_ORDER
89 # if defined(WIN32) || defined (__i386) || defined(__amd64)
90 #  define BYTE_ORDER LITTLE_ENDIAN
91 # else
92 #  if defined(SUNOS)
93 #   if defined(__i386) || defined(__amd64)
94 #    define BYTE_ORDER LITTLE_ENDIAN
95 #   else
96 #    define BYTE_ORDER BIG_ENDIAN
97 #   endif
98 #  else
99 #   warning "Unable to determine the CPU endianess. Defaulting to little endian"
100 #   define BYTE_ORDER LITTLE_ENDIAN
101 #  endif
102 # endif
103 #endif
104
105 /*! \name Byte order functions.
106  * @{
107  */
108
109 /// Swaps the byte order of the given short \p l.
110 short ShortSwap (short l);
111
112 /// Swaps the byte order of the given long \p l.
113 int LongSwap (int l);
114
115 /// Swaps the byte order of the given float \p f.
116 float FloatSwap (float f);
117
118 #if BYTE_ORDER == LITTLE_ENDIAN
119 // little endian
120 #define BigShort(l) ShortSwap(l)
121 #define LittleShort(l) (l)
122 #define BigLong(l) LongSwap(l)
123 #define LittleLong(l) (l)
124 #define BigFloat(l) FloatSwap(l)
125 #define LittleFloat(l) (l)
126 #else
127 // big endian
128 #define BigShort(l) (l)
129 #define LittleShort(l) ShortSwap(l)
130 #define BigLong(l) (l)
131 #define LittleLong(l) LongSwap(l)
132 #define BigFloat(l) (l)
133 #define LittleFloat(l) FloatSwap(l)
134 #endif
135
136 /// Extract a big endian long from the given \p buffer.
137 unsigned int BuffBigLong (const unsigned char *buffer);
138
139 /// Extract a big endian short from the given \p buffer.
140 unsigned short BuffBigShort (const unsigned char *buffer);
141
142 /// Extract a little endian long from the given \p buffer.
143 unsigned int BuffLittleLong (const unsigned char *buffer);
144
145 /// Extract a little endian short from the given \p buffer.
146 unsigned short BuffLittleShort (const unsigned char *buffer);
147 //@}
148
149 //============================================================================
150
151 // these versions are purely for internal use, never sent in network protocol
152 // (use Protocol_EnumForNumber and Protocol_NumberToEnum to convert)
153 typedef enum protocolversion_e
154 {
155         PROTOCOL_UNKNOWN,
156         PROTOCOL_DARKPLACES7, ///< added QuakeWorld-style movement protocol to allow more consistent prediction
157         PROTOCOL_DARKPLACES6, ///< various changes
158         PROTOCOL_DARKPLACES5, ///< uses EntityFrame5 entity snapshot encoder/decoder which is based on a Tribes networking article at http://www.garagegames.com/articles/networking1/
159         PROTOCOL_DARKPLACES4, ///< various changes
160         PROTOCOL_DARKPLACES3, ///< uses EntityFrame4 entity snapshot encoder/decoder which is broken, this attempted to do partial snapshot updates on a QuakeWorld-like protocol, but it is broken and impossible to fix
161         PROTOCOL_DARKPLACES2, ///< various changes
162         PROTOCOL_DARKPLACES1, ///< uses EntityFrame entity snapshot encoder/decoder which is a QuakeWorld-like entity snapshot delta compression method
163         PROTOCOL_QUAKEDP, ///< darkplaces extended quake protocol (used by TomazQuake and others), backwards compatible as long as no extended features are used
164         PROTOCOL_NEHAHRAMOVIE, ///< Nehahra movie protocol, a big nasty hack dating back to early days of the Quake Standards Group (but only ever used by neh_gl.exe), this is potentially backwards compatible with quake protocol as long as no extended features are used (but in actuality the neh_gl.exe which wrote this protocol ALWAYS wrote the extended information)
165         PROTOCOL_QUAKE, ///< quake (aka netquake/normalquake/nq) protocol
166         PROTOCOL_QUAKEWORLD, ///< quakeworld protocol
167         PROTOCOL_NEHAHRABJP, ///< same as QUAKEDP but with 16bit modelindex
168         PROTOCOL_NEHAHRABJP2, ///< same as NEHAHRABJP but with 16bit soundindex
169         PROTOCOL_NEHAHRABJP3, ///< same as NEHAHRABJP2 but with some changes
170 }
171 protocolversion_t;
172
173 /*! \name Message IO functions.
174  * Handles byte ordering and avoids alignment errors
175  * @{
176  */
177
178 void MSG_WriteChar (sizebuf_t *sb, int c);
179 void MSG_WriteByte (sizebuf_t *sb, int c);
180 void MSG_WriteShort (sizebuf_t *sb, int c);
181 void MSG_WriteLong (sizebuf_t *sb, int c);
182 void MSG_WriteFloat (sizebuf_t *sb, float f);
183 void MSG_WriteString (sizebuf_t *sb, const char *s);
184 void MSG_WriteUnterminatedString (sizebuf_t *sb, const char *s);
185 void MSG_WriteAngle8i (sizebuf_t *sb, float f);
186 void MSG_WriteAngle16i (sizebuf_t *sb, float f);
187 void MSG_WriteAngle32f (sizebuf_t *sb, float f);
188 void MSG_WriteCoord13i (sizebuf_t *sb, float f);
189 void MSG_WriteCoord16i (sizebuf_t *sb, float f);
190 void MSG_WriteCoord32f (sizebuf_t *sb, float f);
191 void MSG_WriteCoord (sizebuf_t *sb, float f, protocolversion_t protocol);
192 void MSG_WriteVector (sizebuf_t *sb, float *v, protocolversion_t protocol);
193 void MSG_WriteAngle (sizebuf_t *sb, float f, protocolversion_t protocol);
194
195 extern  int                     msg_readcount;
196 extern  qboolean        msg_badread;            // set if a read goes beyond end of message
197
198 void MSG_BeginReading (void);
199 int MSG_ReadLittleShort (void);
200 int MSG_ReadBigShort (void);
201 int MSG_ReadLittleLong (void);
202 int MSG_ReadBigLong (void);
203 float MSG_ReadLittleFloat (void);
204 float MSG_ReadBigFloat (void);
205 char *MSG_ReadString (void);
206 int MSG_ReadBytes (int numbytes, unsigned char *out);
207
208 #define MSG_ReadChar() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (signed char)net_message.data[msg_readcount++])
209 #define MSG_ReadByte() (msg_readcount >= net_message.cursize ? (msg_badread = true, -1) : (unsigned char)net_message.data[msg_readcount++])
210 #define MSG_ReadShort MSG_ReadLittleShort
211 #define MSG_ReadLong MSG_ReadLittleLong
212 #define MSG_ReadFloat MSG_ReadLittleFloat
213
214 float MSG_ReadAngle8i (void);
215 float MSG_ReadAngle16i (void);
216 float MSG_ReadAngle32f (void);
217 float MSG_ReadCoord13i (void);
218 float MSG_ReadCoord16i (void);
219 float MSG_ReadCoord32f (void);
220 float MSG_ReadCoord (protocolversion_t protocol);
221 void MSG_ReadVector (float *v, protocolversion_t protocol);
222 float MSG_ReadAngle (protocolversion_t protocol);
223 //@}
224 //============================================================================
225
226 typedef float (*COM_WordWidthFunc_t) (void *passthrough, const char *w, size_t *length, float maxWidth); // length is updated to the longest fitting string into maxWidth; if maxWidth < 0, all characters are used and length is used as is
227 typedef int (*COM_LineProcessorFunc) (void *passthrough, const char *line, size_t length, float width, qboolean isContination);
228 int COM_Wordwrap(const char *string, size_t length, float continuationSize, float maxWidth, COM_WordWidthFunc_t wordWidth, void *passthroughCW, COM_LineProcessorFunc processLine, void *passthroughPL);
229
230 extern char com_token[MAX_INPUTLINE];
231
232 int COM_ParseToken_Simple(const char **datapointer, qboolean returnnewline, qboolean parsebackslash);
233 int COM_ParseToken_QuakeC(const char **datapointer, qboolean returnnewline);
234 int COM_ParseToken_VM_Tokenize(const char **datapointer, qboolean returnnewline);
235 int COM_ParseToken_Console(const char **datapointer);
236
237 extern int com_argc;
238 extern const char **com_argv;
239
240 int COM_CheckParm (const char *parm);
241 void COM_Init (void);
242 void COM_Shutdown (void);
243 void COM_InitGameType (void);
244
245 char    *va(const char *format, ...) DP_FUNC_PRINTF(1);
246 // does a varargs printf into a temp buffer
247
248
249 // snprintf and vsnprintf are NOT portable. Use their DP counterparts instead
250 #define snprintf DO_NOT_USE_SNPRINTF__USE_DPSNPRINTF
251 #define vsnprintf DO_NOT_USE_VSNPRINTF__USE_DPVSNPRINTF
252
253 // dpsnprintf and dpvsnprintf
254 // return the number of printed characters, excluding the final '\0'
255 // or return -1 if the buffer isn't big enough to contain the entire string.
256 // buffer is ALWAYS null-terminated
257 extern int dpsnprintf (char *buffer, size_t buffersize, const char *format, ...) DP_FUNC_PRINTF(3);
258 extern int dpvsnprintf (char *buffer, size_t buffersize, const char *format, va_list args);
259
260 // A bunch of functions are forbidden for security reasons (and also to please MSVS 2005, for some of them)
261 // LordHavoc: added #undef lines here to avoid warnings in Linux
262 #undef strcat
263 #define strcat DO_NOT_USE_STRCAT__USE_STRLCAT_OR_MEMCPY
264 #undef strncat
265 #define strncat DO_NOT_USE_STRNCAT__USE_STRLCAT_OR_MEMCPY
266 #undef strcpy
267 #define strcpy DO_NOT_USE_STRCPY__USE_STRLCPY_OR_MEMCPY
268 #undef strncpy
269 #define strncpy DO_NOT_USE_STRNCPY__USE_STRLCPY_OR_MEMCPY
270 //#undef sprintf
271 //#define sprintf DO_NOT_USE_SPRINTF__USE_DPSNPRINTF
272
273
274 //============================================================================
275
276 extern  struct cvar_s   registered;
277 extern  struct cvar_s   cmdline;
278
279 typedef enum gamemode_e
280 {
281         GAME_NORMAL,
282         GAME_HIPNOTIC,
283         GAME_ROGUE,
284         GAME_NEHAHRA,
285         GAME_NEXUIZ,
286         GAME_TRANSFUSION,
287         GAME_GOODVSBAD2,
288         GAME_TEU,
289         GAME_BATTLEMECH,
290         GAME_ZYMOTIC,
291         GAME_SETHERAL,
292         GAME_SOM,
293         GAME_TENEBRAE, // full of evil hackery
294         GAME_NEOTERIC,
295         GAME_OPENQUARTZ, //this game sucks
296         GAME_PRYDON,
297         GAME_DELUXEQUAKE,
298         GAME_THEHUNTED,
299         GAME_DEFEATINDETAIL2,
300         GAME_DARSANA,
301         GAME_CONTAGIONTHEORY,
302         GAME_EDU2P,
303         GAME_BLADEMASTER,
304         GAME_PROPHECY,
305         GAME_COUNT
306 }
307 gamemode_t;
308
309 extern gamemode_t gamemode;
310 extern const char *gamename;
311 extern const char *gamedirname1;
312 extern const char *gamedirname2;
313 extern const char *gamescreenshotname;
314 extern const char *gameuserdirname;
315 extern char com_modname[MAX_OSPATH];
316
317 void COM_ToLowerString (const char *in, char *out, size_t size_out);
318 void COM_ToUpperString (const char *in, char *out, size_t size_out);
319 int COM_StringBeginsWith(const char *s, const char *match);
320
321 int COM_ReadAndTokenizeLine(const char **text, char **argv, int maxargc, char *tokenbuf, int tokenbufsize, const char *commentprefix);
322
323 size_t COM_StringLengthNoColors(const char *s, size_t size_s, qboolean *valid);
324 qboolean COM_StringDecolorize(const char *in, size_t size_in, char *out, size_t size_out, qboolean escape_carets);
325 void COM_ToLowerString (const char *in, char *out, size_t size_out);
326 void COM_ToUpperString (const char *in, char *out, size_t size_out);
327
328 typedef struct stringlist_s
329 {
330         /// maxstrings changes as needed, causing reallocation of strings[] array
331         int maxstrings;
332         int numstrings;
333         char **strings;
334 } stringlist_t;
335
336 int matchpattern(const char *in, const char *pattern, int caseinsensitive);
337 int matchpattern_with_separator(const char *in, const char *pattern, int caseinsensitive, const char *separators, qboolean wildcard_least_one);
338 void stringlistinit(stringlist_t *list);
339 void stringlistfreecontents(stringlist_t *list);
340 void stringlistappend(stringlist_t *list, const char *text);
341 void stringlistsort(stringlist_t *list);
342 void listdirectory(stringlist_t *list, const char *basepath, const char *path);
343
344 char *SearchInfostring(const char *infostring, const char *key);
345
346 void InfoString_GetValue(const char *buffer, const char *key, char *value, size_t valuelength);
347 void InfoString_SetValue(char *buffer, size_t bufferlength, const char *key, const char *value);
348 void InfoString_Print(char *buffer);
349
350 // strlcat and strlcpy, from OpenBSD
351 // Most (all?) BSDs already have them
352 #if defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(MACOSX)
353 # define HAVE_STRLCAT 1
354 # define HAVE_STRLCPY 1
355 #endif
356
357 #ifndef HAVE_STRLCAT
358 /*!
359  * Appends src to string dst of size siz (unlike strncat, siz is the
360  * full size of dst, not space left).  At most siz-1 characters
361  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
362  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
363  * If retval >= siz, truncation occurred.
364  */
365 size_t strlcat(char *dst, const char *src, size_t siz);
366 #endif  // #ifndef HAVE_STRLCAT
367
368 #ifndef HAVE_STRLCPY
369 /*!
370  * Copy src to string dst of size siz.  At most siz-1 characters
371  * will be copied.  Always NUL terminates (unless siz == 0).
372  * Returns strlen(src); if retval >= siz, truncation occurred.
373  */
374 size_t strlcpy(char *dst, const char *src, size_t siz);
375
376 #endif  // #ifndef HAVE_STRLCPY
377
378 void FindFraction(double val, int *num, int *denom, int denomMax);
379
380 #endif
381