2019-11-24 20:45:15 -08:00
|
|
|
/*
|
|
|
|
Copyright (C) 1996-2001 Id Software, Inc.
|
|
|
|
Copyright (C) 2002-2009 John Fitzgibbons and others
|
|
|
|
Copyright (C) 2010-2014 QuakeSpasm developers
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-12-02 07:00:56 -08:00
|
|
|
#ifndef spingle__common_h
|
|
|
|
#define spingle__common_h
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-12-03 10:13:00 -08:00
|
|
|
#include "sdl/endian_sdl.h"
|
2019-12-03 10:12:02 -08:00
|
|
|
|
|
|
|
#define q_minfunc(typ, name) \
|
|
|
|
static inline typ name(typ a, typ b) {return a < b ? a : b;}
|
|
|
|
|
|
|
|
#define q_maxfunc(typ, name) \
|
|
|
|
static inline typ name(typ a, typ b) {return a > b ? a : b;}
|
|
|
|
|
|
|
|
#define CLAMPfunc(typ, name) \
|
|
|
|
static inline typ name(typ min, typ x, typ max) \
|
|
|
|
{ \
|
|
|
|
return x < min ? min : x > max ? max : x; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define GenericTypes(func, name) \
|
|
|
|
func(char, name##chr) \
|
|
|
|
func(byte, name##byt) \
|
|
|
|
func(int16_t, name##i16) \
|
|
|
|
func(int32_t, name##i32) \
|
|
|
|
func(int64_t, name##i64) \
|
|
|
|
func(uint16_t, name##u16) \
|
|
|
|
func(uint32_t, name##u32) \
|
|
|
|
func(uint64_t, name##u64) \
|
|
|
|
func(float, name##f) \
|
|
|
|
func(double, name##d)
|
|
|
|
|
|
|
|
#define GenericDecl(typ, name) , typ: name
|
|
|
|
|
|
|
|
GenericTypes(q_minfunc, q_min)
|
|
|
|
GenericTypes(q_maxfunc, q_max)
|
|
|
|
GenericTypes(CLAMPfunc, CLAMP)
|
|
|
|
|
|
|
|
#define CLAMP(minval, x, maxval) \
|
|
|
|
_Generic((x) GenericTypes(GenericDecl, CLAMP))(minval, x, maxval)
|
|
|
|
|
|
|
|
#define q_min(a, b) _Generic((a) GenericTypes(GenericDecl, q_min))(a, b)
|
|
|
|
#define q_max(a, b) _Generic((a) GenericTypes(GenericDecl, q_max))(a, b)
|
|
|
|
|
|
|
|
#undef q_minfunc
|
|
|
|
#undef q_maxfunc
|
|
|
|
#undef CLAMPfunc
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
typedef struct sizebuf_s
|
|
|
|
{
|
2019-12-03 10:13:00 -08:00
|
|
|
bool allowoverflow; // if false, do a Sys_Error
|
|
|
|
bool overflowed; // set to true if the buffer size failed
|
|
|
|
byte *data;
|
|
|
|
int32_t maxsize;
|
|
|
|
int32_t cursize;
|
2019-11-24 20:45:15 -08:00
|
|
|
} sizebuf_t;
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
void SZ_Alloc(sizebuf_t *buf, int32_t startsize);
|
|
|
|
void SZ_Free(sizebuf_t *buf);
|
|
|
|
void SZ_Clear(sizebuf_t *buf);
|
|
|
|
void *SZ_GetSpace(sizebuf_t *buf, int32_t length);
|
|
|
|
void SZ_Write(sizebuf_t *buf, const void *data, int32_t length);
|
|
|
|
void SZ_Print(sizebuf_t *buf, const char *data); // strcats onto the sizebuf
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
typedef struct link_s
|
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
struct link_s *prev, *next;
|
2019-11-24 20:45:15 -08:00
|
|
|
} link_t;
|
|
|
|
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
void ClearLink(link_t *l);
|
|
|
|
void RemoveLink(link_t *l);
|
|
|
|
void InsertLinkBefore(link_t *l, link_t *before);
|
|
|
|
void InsertLinkAfter(link_t *l, link_t *after);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
2019-12-03 06:24:06 -08:00
|
|
|
static inline int16_t ReadBigShort(byte const *bytes)
|
|
|
|
{
|
|
|
|
int16_t v;
|
|
|
|
v = bytes[1];
|
|
|
|
v |= bytes[0] << 8;
|
|
|
|
return v;
|
|
|
|
}
|
2019-12-02 23:31:56 -08:00
|
|
|
|
2019-12-03 06:24:06 -08:00
|
|
|
static inline int32_t ReadBigLong(byte const *bytes)
|
|
|
|
{
|
|
|
|
int32_t v;
|
|
|
|
v = bytes[3];
|
|
|
|
v |= bytes[2] << 8;
|
|
|
|
v |= bytes[1] << 16;
|
|
|
|
v |= bytes[0] << 24;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float ReadBigFloat(byte const *bytes)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
float f;
|
|
|
|
uint32_t i;
|
|
|
|
} data;
|
|
|
|
data.i = ReadBigLong(bytes);
|
|
|
|
return data.f;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int16_t ReadLittleShort(byte const *bytes)
|
|
|
|
{
|
|
|
|
int16_t v;
|
|
|
|
v = bytes[0];
|
|
|
|
v |= bytes[1] << 8;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int32_t ReadLittleLong(byte const *bytes)
|
|
|
|
{
|
|
|
|
int32_t v;
|
|
|
|
v = bytes[0];
|
|
|
|
v |= bytes[1] << 8;
|
|
|
|
v |= bytes[2] << 16;
|
|
|
|
v |= bytes[3] << 24;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline float ReadLittleFloat(byte const *bytes)
|
|
|
|
{
|
|
|
|
union
|
|
|
|
{
|
|
|
|
float f;
|
|
|
|
uint32_t i;
|
|
|
|
} data;
|
|
|
|
data.i = ReadLittleLong(bytes);
|
|
|
|
return data.f;
|
|
|
|
}
|
2019-12-02 23:31:56 -08:00
|
|
|
|
2019-11-24 20:45:15 -08:00
|
|
|
//============================================================================
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
void MSG_WriteChar(sizebuf_t *sb, int32_t c);
|
|
|
|
void MSG_WriteByte(sizebuf_t *sb, int32_t c);
|
|
|
|
void MSG_WriteShort(sizebuf_t *sb, int32_t c);
|
|
|
|
void MSG_WriteLong(sizebuf_t *sb, int32_t c);
|
|
|
|
void MSG_WriteFloat(sizebuf_t *sb, float f);
|
|
|
|
void MSG_WriteString(sizebuf_t *sb, const char *s);
|
|
|
|
void MSG_WriteCoord(sizebuf_t *sb, float f, uint32_t flags);
|
|
|
|
void MSG_WriteAngle(sizebuf_t *sb, float f, uint32_t flags);
|
|
|
|
void MSG_WriteAngle16(sizebuf_t *sb, float f, uint32_t flags); //johnfitz
|
|
|
|
|
|
|
|
extern int32_t msg_readcount;
|
|
|
|
extern bool msg_badread; // set if a read goes beyond end of message
|
|
|
|
|
|
|
|
void MSG_BeginReading(void);
|
|
|
|
int32_t MSG_ReadChar(void);
|
|
|
|
int32_t MSG_ReadByte(void);
|
|
|
|
int32_t MSG_ReadShort(void);
|
|
|
|
int32_t MSG_ReadLong(void);
|
|
|
|
float MSG_ReadFloat(void);
|
|
|
|
const char *MSG_ReadString(void);
|
|
|
|
|
|
|
|
float MSG_ReadCoord(uint32_t flags);
|
|
|
|
float MSG_ReadAngle(uint32_t flags);
|
|
|
|
float MSG_ReadAngle16(uint32_t flags); //johnfitz
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
void Q_memset(void *dest, int32_t fill, size_t count);
|
|
|
|
void Q_memcpy(void *dest, const void *src, size_t count);
|
|
|
|
int32_t Q_memcmp(const void *m1, const void *m2, size_t count);
|
|
|
|
void Q_strcpy(char *dest, const char *src);
|
|
|
|
void Q_strncpy(char *dest, const char *src, int32_t count);
|
|
|
|
int32_t Q_strlen(const char *str);
|
|
|
|
char *Q_strrchr(const char *s, char c);
|
|
|
|
void Q_strcat(char *dest, const char *src);
|
|
|
|
int32_t Q_strcmp(const char *s1, const char *s2);
|
|
|
|
int32_t Q_strncmp(const char *s1, const char *s2, int32_t count);
|
|
|
|
int32_t Q_atoi(const char *str);
|
|
|
|
float Q_atof(const char *str);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
|
|
|
|
#include "strl_fn.h"
|
|
|
|
|
|
|
|
/* locale-insensitive strcasecmp replacement functions: */
|
2019-11-25 17:40:18 -08:00
|
|
|
extern int32_t q_strcasecmp(const char * s1, const char * s2);
|
|
|
|
extern int32_t q_strncasecmp(const char *s1, const char *s2, size_t n);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
/* locale-insensitive case-insensitive alternative to strstr */
|
|
|
|
extern char *q_strcasestr(const char *haystack, const char *needle);
|
|
|
|
|
|
|
|
/* locale-insensitive strlwr/upr replacement functions: */
|
2019-11-25 17:40:18 -08:00
|
|
|
extern char *q_strlwr(char *str);
|
|
|
|
extern char *q_strupr(char *str);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
/* snprintf, vsnprintf : always use our versions. */
|
2019-11-25 17:40:18 -08:00
|
|
|
extern int32_t q_snprintf(char *str, size_t size, const char *format, ...) FUNC_PRINTF(3, 4);
|
|
|
|
extern int32_t q_vsnprintf(char *str, size_t size, const char *format, va_list args) FUNC_PRINTF(3, 0);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
extern char com_token[1024];
|
|
|
|
extern bool com_eof;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
const char *COM_Parse(const char *data);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
extern int32_t com_argc;
|
|
|
|
extern char **com_argv;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
extern int32_t safemode;
|
2019-11-24 20:45:15 -08:00
|
|
|
/* safe mode: in true, the engine will behave as if one
|
|
|
|
of these arguments were actually on the command line:
|
|
|
|
-nosound, -nocdaudio, -nomidi, -stdvid, -dibonly,
|
|
|
|
-nomouse, -nojoy, -nolan
|
|
|
|
*/
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
int32_t COM_CheckParm(const char *parm);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
void COM_InitArgv(int32_t argc, char **argv);
|
|
|
|
void COM_InitFilesystem(void);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
const char *COM_SkipPath(const char *pathname);
|
|
|
|
void COM_StripExtension(const char *in, char *out, size_t outsize);
|
|
|
|
void COM_FileBase(const char *in, char *out, size_t outsize);
|
|
|
|
void COM_AddExtension(char *path, const char *extension, size_t len);
|
|
|
|
const char *COM_FileGetExtension(const char *in); /* doesn't return NULL */
|
|
|
|
void COM_ExtractExtension(const char *in, char *out, size_t outsize);
|
|
|
|
void COM_CreatePath(char *path);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
char *va(const char *format, ...) FUNC_PRINTF(1, 2);
|
2019-11-24 20:45:15 -08:00
|
|
|
// does a varargs printf into a temp buffer
|
|
|
|
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
// QUAKEFS
|
|
|
|
typedef struct
|
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
char name[MAX_QPATH];
|
|
|
|
int32_t filepos, filelen;
|
2019-11-24 20:45:15 -08:00
|
|
|
} packfile_t;
|
|
|
|
|
|
|
|
typedef struct pack_s
|
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
char filename[MAX_OSPATH];
|
|
|
|
int32_t handle;
|
|
|
|
int32_t numfiles;
|
|
|
|
packfile_t *files;
|
2019-11-24 20:45:15 -08:00
|
|
|
} pack_t;
|
|
|
|
|
|
|
|
typedef struct searchpath_s
|
|
|
|
{
|
2019-11-25 17:40:18 -08:00
|
|
|
uint32_t path_id; // identifier assigned to the game directory
|
|
|
|
// Note that <install_dir>/game1 and
|
|
|
|
// <userdir>/game1 have the same id.
|
|
|
|
char filename[MAX_OSPATH];
|
|
|
|
pack_t *pack; // only one of filename / pack will be used
|
|
|
|
struct searchpath_s *next;
|
2019-11-24 20:45:15 -08:00
|
|
|
} searchpath_t;
|
|
|
|
|
|
|
|
extern searchpath_t *com_searchpaths;
|
|
|
|
extern searchpath_t *com_base_searchpaths;
|
|
|
|
|
2019-11-25 16:49:58 -08:00
|
|
|
extern int32_t com_filesize;
|
2019-11-24 20:45:15 -08:00
|
|
|
struct cache_user_s;
|
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
extern char com_basedir[MAX_OSPATH];
|
|
|
|
extern char com_gamedir[MAX_OSPATH];
|
|
|
|
extern int32_t file_from_pak; // global indicating that file came from a pak
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 17:40:18 -08:00
|
|
|
void COM_WriteFile(const char *filename, const void *data, int32_t len);
|
|
|
|
int32_t COM_OpenFile(const char *filename, int32_t *handle, uint32_t *path_id);
|
|
|
|
int32_t COM_FOpenFile(const char *filename, FILE **file, uint32_t *path_id);
|
|
|
|
bool COM_FileExists(const char *filename, uint32_t *path_id);
|
|
|
|
void COM_CloseFile(int32_t h);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// these procedures open a file using COM_FindFile and loads it into a proper
|
|
|
|
// buffer. the buffer is allocated with a total size of com_filesize + 1. the
|
|
|
|
// procedures differ by their buffer allocation method.
|
2019-11-25 17:40:18 -08:00
|
|
|
byte *COM_LoadStackFile(const char *path, void *buffer, int32_t bufsize,
|
|
|
|
uint32_t *path_id);
|
|
|
|
// uses the specified stack stack buffer with the specified size
|
|
|
|
// of bufsize. if bufsize is too short, uses temp hunk. the bufsize
|
|
|
|
// must include the +1
|
|
|
|
byte *COM_LoadTempFile(const char *path, uint32_t *path_id);
|
|
|
|
// allocates the buffer on the temp hunk.
|
|
|
|
byte *COM_LoadHunkFile(const char *path, uint32_t *path_id);
|
|
|
|
// allocates the buffer on the hunk.
|
|
|
|
byte *COM_LoadZoneFile(const char *path, uint32_t *path_id);
|
|
|
|
// allocates the buffer on the zone.
|
|
|
|
void COM_LoadCacheFile(const char *path, struct cache_user_s *cu,
|
|
|
|
uint32_t *path_id);
|
|
|
|
// uses cache mem for allocating the buffer.
|
|
|
|
byte *COM_LoadMallocFile(const char *path, uint32_t *path_id);
|
|
|
|
// allocates the buffer on the system mem (malloc).
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// Opens the given path directly, ignoring search paths.
|
|
|
|
// Returns NULL on failure, or else a '\0'-terminated malloc'ed buffer.
|
|
|
|
// Loads in "t" mode so CRLF to LF translation is performed on Windows.
|
2019-11-25 17:40:18 -08:00
|
|
|
byte *COM_LoadMallocFile_TextMode_OSPath(const char *path, long *len_out);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-25 16:49:58 -08:00
|
|
|
// Attempts to parse an int32_t, followed by a newline.
|
2019-11-24 20:45:15 -08:00
|
|
|
// Returns advanced buffer position.
|
|
|
|
// Doesn't signal parsing failure, but this is not needed for savegame loading.
|
2019-11-25 16:49:58 -08:00
|
|
|
const char *COM_ParseIntNewline(const char *buffer, int32_t *value);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
|
|
|
// Attempts to parse a float followed by a newline.
|
|
|
|
// Returns advanced buffer position.
|
|
|
|
const char *COM_ParseFloatNewline(const char *buffer, float *value);
|
|
|
|
|
|
|
|
// Parse a string of non-whitespace into com_token, then tries to consume a
|
|
|
|
// newline. Returns advanced buffer position.
|
|
|
|
const char *COM_ParseStringNewline(const char *buffer);
|
|
|
|
|
|
|
|
/* The following FS_*() stdio replacements are necessary if one is
|
|
|
|
* to perform non-sequential reads on files reopened on pak files
|
|
|
|
* because we need the bookkeeping about file start/end positions.
|
|
|
|
* Allocating and filling in the fshandle_t structure is the users'
|
|
|
|
* responsibility when the file is initially opened. */
|
|
|
|
|
|
|
|
typedef struct _fshandle_t
|
|
|
|
{
|
|
|
|
FILE *file;
|
2019-11-25 17:40:18 -08:00
|
|
|
bool pak; /* is the file read from a pak */
|
|
|
|
long start; /* file or data start position */
|
|
|
|
long length; /* file or data size */
|
|
|
|
long pos; /* current position relative to start */
|
2019-11-24 20:45:15 -08:00
|
|
|
} fshandle_t;
|
|
|
|
|
|
|
|
size_t FS_fread(void *ptr, size_t size, size_t nmemb, fshandle_t *fh);
|
2019-11-25 16:49:58 -08:00
|
|
|
int32_t FS_fseek(fshandle_t *fh, long offset, int32_t whence);
|
2019-11-24 20:45:15 -08:00
|
|
|
long FS_ftell(fshandle_t *fh);
|
|
|
|
void FS_rewind(fshandle_t *fh);
|
2019-11-25 16:49:58 -08:00
|
|
|
int32_t FS_feof(fshandle_t *fh);
|
|
|
|
int32_t FS_ferror(fshandle_t *fh);
|
|
|
|
int32_t FS_fclose(fshandle_t *fh);
|
|
|
|
int32_t FS_fgetc(fshandle_t *fh);
|
|
|
|
char *FS_fgets(char *s, int32_t size, fshandle_t *fh);
|
2019-11-25 17:40:18 -08:00
|
|
|
long FS_filelength(fshandle_t *fh);
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-12-02 07:19:02 -08:00
|
|
|
extern bool standard_quake, rogue, hipnotic;
|
2019-11-24 20:45:15 -08:00
|
|
|
|
2019-11-26 17:37:03 -08:00
|
|
|
struct cvar_s;
|
|
|
|
|
2019-12-02 07:00:56 -08:00
|
|
|
#endif
|