hldsp/nanoquake.h

204 lines
4.5 KiB
C

/*
nanoquake.h -- common quake engine defintions as single-header library
Copyright (C) 1997 id Software
Copyright (C) 2024 Uncle Mike, Alibek Omarov
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 3 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.
*/
#ifndef NANOQUAKE_H
#define NANOQUAKE_H
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <time.h>
#ifdef NANOQUAKE_IMPL
#define EXTERN
#else // NANOQUAKE_IMPL
#define EXTERN extern
#endif
#define Assert assert
//
// mathlib
//
#define Q_min( x, y ) (( x ) < ( y ) ? ( x ) : ( y ))
#define Q_max( x, y ) (( x ) > ( y ) ? ( x ) : ( y ))
#define bound( min, val, max ) Q_min( Q_max( min, val ), max )
#define SetBits( x, y ) ((x) |= (y))
#define ClearBits( x, y ) ((x) &= ~(y))
#define FBitSet( x, y ) ((x) & (y))
#define ARRAYSIZE( array ) ( sizeof( array ) / sizeof( array[0] ))
//
// common
//
#ifndef __cplusplus
typedef enum { false, true } qboolean;
#else
typedef int qboolean;
#endif
typedef unsigned char byte;
typedef off_t fs_offset_t;
#define Z_Free free
#define Z_Calloc( x ) calloc( 1, x )
#define Mem_Malloc( x, y ) malloc( y )
#define Con_Printf printf
#define Con_DPrintf printf
#define Con_Reportf printf
#define Q_atof atof
#define Q_atoi atoi
#define COM_RandomLong( min, max ) ( min + rand() % ( max - min ))
#define Q_strncmp strncmp
#define S_NOTE "Note: "
#define S_WARN "Warning: "
#define S_ERROR "Error: "
//
// cvars
//
typedef struct cvar_s
{
const char *name;
char *string;
int flags;
float value;
struct cvar_s *next;
} cvar_t;
#define CVAR_SENTINEL 0xdeadbeef
#define CVAR_DEFINE( cv, cvname, cvstr, cvflags, cvdesc ) \
cvar_t cv = { (char*)cvname, (char*)cvstr, cvflags, 0.0f, (void *)CVAR_SENTINEL }
#define CVAR_DEFINE_AUTO( cv, cvstr, cvflags, cvdesc ) \
CVAR_DEFINE( cv, #cv, cvstr, cvflags, cvdesc )
#define FCVAR_ARCHIVE (1 << 0)
#define FCVAR_CHANGED (1 << 13)
static inline void Cvar_DirectSet( cvar_t *cv, const char *value )
{
free( cv->string );
cv->string = strdup( value );
cv->value = atoi( cv->string );
cv->flags |= FCVAR_CHANGED;
}
static inline void Cvar_DirectSetValue( cvar_t *cv, float value )
{
char str[32];
snprintf( str, sizeof( str ), "%f", value );
free( cv->string );
cv->string = strdup( str );
cv->value = value;
cv->flags |= FCVAR_CHANGED;
}
static inline void Cvar_RegisterVariable( cvar_t *cv )
{
cv->string = strdup( cv->string );
cv->value = atoi( cv->string );
}
//
// cmds
//
#define Cmd_Argc() cmd_argc
#define Cmd_Argv( i ) cmd_argv[i]
EXTERN int cmd_argc;
EXTERN char **cmd_argv;
static inline void Cmd_ExecCommand( void (*func)(), int argc, char **argv )
{
cmd_argc = argc;
cmd_argv = argv;
func();
}
static inline void Cmd_AddCommand( const char *cmd, void *func, const char *desc )
{
}
static inline void Cmd_RemoveCommand( const char *cmd )
{
}
//
// time
//
static inline double Sys_DoubleTime( void )
{
struct timespec ts;
#if __irix__
clock_gettime( CLOCK_SGI_CYCLE, &ts );
#else
clock_gettime( CLOCK_MONOTONIC, &ts );
#endif
return (double) ts.tv_sec + (double) ts.tv_nsec/1000000000.0;
}
//
// sound
//
#define SOUND_11k (11025)
#define CLIP( x ) (( x ) > 32760 ? 32760 : (( x ) < -32760 ? -32760 : ( x )))
typedef struct
{
int left;
int right;
} portable_samplepair_t;
typedef struct
{
int waterlevel;
} listener_t;
typedef enum
{
WF_UNKNOWN = 0,
WF_PCMDATA,
WF_MPGDATA,
WF_TOTALCOUNT, // must be last
} sndformat_t;
typedef struct sndlib_s
{
// current sound state
int type; // sound type
int rate; // num samples per second (e.g. 11025 - 11 khz)
int width; // resolution - bum bits divided by 8 (8 bit is 1, 16 bit is 2)
int channels; // num channels (1 - mono, 2 - stereo)
int loopstart; // start looping from
uint samples; // total samplecount in sound
uint flags; // additional sound flags
size_t size; // sound unpacked size (for bounds checking)
byte *wav; // sound pointer (see sound_type for details)
byte *tempbuffer; // for convert operations
int cmd_flags;
} sndlib_t;
EXTERN sndlib_t sound;
EXTERN listener_t s_listener;
qboolean Sound_LoadWAV( const char *name, const byte *buffer, fs_offset_t filesize );
#endif // NANOQUAKE_H