From 817f431377c7f1545621ff597c018b133651e991 Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Thu, 14 Aug 2008 17:19:13 +0000 Subject: moved out e_system, e_detect, vmath and math to a little base library --- default.bam | 2 +- src/base/detect.h | 115 +++ src/base/math.hpp | 61 ++ src/base/system.c | 971 ++++++++++++++++++++++++++ src/base/system.h | 881 +++++++++++++++++++++++ src/base/vmath.hpp | 196 ++++++ src/engine/client/ec_client.c | 2 +- src/engine/client/ec_font.c | 2 +- src/engine/client/ec_gfx.c | 2 +- src/engine/client/ec_inp.c | 2 +- src/engine/client/ec_snd.c | 2 +- src/engine/client/ec_srvbrowse.c | 2 +- src/engine/e_compression.c | 3 +- src/engine/e_config.c | 2 +- src/engine/e_console.c | 2 +- src/engine/e_datafile.c | 2 +- src/engine/e_detect.h | 115 --- src/engine/e_engine.c | 3 +- src/engine/e_huffman.c | 9 +- src/engine/e_if_other.h | 2 +- src/engine/e_linereader.h | 2 +- src/engine/e_map.c | 2 +- src/engine/e_memheap.c | 2 +- src/engine/e_network.c | 10 +- src/engine/e_packer.c | 4 +- src/engine/e_protocol.h | 2 +- src/engine/e_ringbuffer.c | 2 +- src/engine/e_snapshot.h | 2 +- src/engine/e_system.c | 971 -------------------------- src/engine/e_system.h | 881 ----------------------- src/engine/external/glfw/lib/x11/x11_window.c | 2 +- src/engine/server/es_register.c | 2 +- src/engine/server/es_server.c | 3 +- src/game/client/gc_client.cpp | 4 +- src/game/client/gc_client.hpp | 3 +- src/game/client/gc_console.cpp | 3 +- src/game/client/gc_map_image.cpp | 3 +- src/game/client/gc_menu.cpp | 7 +- src/game/client/gc_render.cpp | 4 +- src/game/client/gc_render.hpp | 3 +- src/game/client/gc_render_map.cpp | 2 +- src/game/client/gc_render_obj.cpp | 4 +- src/game/client/gc_skin.cpp | 6 +- src/game/client/gc_skin.hpp | 2 +- src/game/client/gc_ui.cpp | 3 +- src/game/editor/ed_editor.cpp | 3 +- src/game/editor/ed_editor.hpp | 5 +- src/game/editor/ed_layer_quads.cpp | 3 +- src/game/editor/ed_layer_tiles.cpp | 3 +- src/game/g_collision.cpp | 7 +- src/game/g_collision.hpp | 3 +- src/game/g_game.hpp | 5 +- src/game/g_math.hpp | 61 -- src/game/g_vmath.hpp | 198 ------ src/game/server/gs_server.cpp | 4 +- src/mastersrv/mastersrv.cpp | 2 +- src/tools/crapnet.cpp | 2 +- src/tools/fake_server.c | 2 +- src/tools/packetgen.c | 2 +- 59 files changed, 2313 insertions(+), 2292 deletions(-) create mode 100644 src/base/detect.h create mode 100644 src/base/math.hpp create mode 100644 src/base/system.c create mode 100644 src/base/system.h create mode 100644 src/base/vmath.hpp delete mode 100644 src/engine/e_detect.h delete mode 100644 src/engine/e_system.c delete mode 100644 src/engine/e_system.h delete mode 100644 src/game/g_math.hpp delete mode 100644 src/game/g_vmath.hpp diff --git a/default.bam b/default.bam index f79b20ae..2d10baf2 100644 --- a/default.bam +++ b/default.bam @@ -295,7 +295,7 @@ function build(settings) end end - engine = Compile(engine_settings, Collect("src/engine/*.c")) + engine = Compile(engine_settings, Collect("src/engine/*.c", "src/base/*.c")) client = Compile(client_settings, Collect("src/engine/client/*.c")) server = Compile(server_settings, Collect("src/engine/server/*.c")) diff --git a/src/base/detect.h b/src/base/detect.h new file mode 100644 index 00000000..04cca2d9 --- /dev/null +++ b/src/base/detect.h @@ -0,0 +1,115 @@ +/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ +#ifndef BASELIB_FILE_CONFIG_H +#define BASELIB_FILE_CONFIG_H + +/* + this file detected the family, platform and architecture + to compile for. +*/ + +/* platforms */ + +/* windows Family */ +#if defined(WIN64) || defined(_WIN64) + /* Hmm, is this IA64 or x86-64? */ + #define CONF_FAMILY_WINDOWS 1 + #define CONF_FAMILY_STRING "windows" + #define CONF_PLATFORM_WIN64 1 + #define CONF_PLATFORM_STRING "win64" +#elif defined(WIN32) || defined(_WIN32) || defined(__CYGWIN32__) || defined(__MINGW32__) + #define CONF_FAMILY_WINDOWS 1 + #define CONF_FAMILY_STRING "windows" + #define CONF_PLATFORM_WIN32 1 + #define CONF_PLATFORM_STRING "win32" +#endif + +/* unix family */ +#if defined(__FreeBSD__) + #define CONF_FAMILY_UNIX 1 + #define CONF_FAMILY_STRING "unix" + #define CONF_PLATFORM_FREEBSD 1 + #define CONF_PLATFORM_STRING "freebsd" +#endif + +#if defined(__OpenBSD__) + #define CONF_FAMILY_UNIX 1 + #define CONF_FAMILY_STRING "unix" + #define CONF_PLATFORM_OPENBSD 1 + #define CONF_PLATFORM_STRING "openbsd" +#endif + +#if defined(__LINUX__) || defined(__linux__) + #define CONF_FAMILY_UNIX 1 + #define CONF_FAMILY_STRING "unix" + #define CONF_PLATFORM_LINUX 1 + #define CONF_PLATFORM_STRING "linux" +#endif + +#if defined(MACOSX) || defined(__APPLE__) || defined(__DARWIN__) + #define CONF_FAMILY_UNIX 1 + #define CONF_FAMILY_STRING "unix" + #define CONF_PLATFORM_MACOSX 1 + #define CONF_PLATFORM_STRING "macosx" +#endif + +#if defined(__sun) + #define CONF_FAMILY_UNIX 1 + #define CONF_FAMILY_STRING "unix" + #define CONF_PLATFROM_SOLARIS 1 + #define CONF_PLATFORM_STRING "solaris" +#endif + +/* beos family */ +#if defined(__BeOS) || defined(__BEOS__) + #define CONF_FAMILY_BEOS 1 + #define CONF_FAMILY_STRING "beos" + #define CONF_PLATFORM_BEOS 1 + #define CONF_PLATFORM_STRING "beos" +#endif + + +/* architectures */ +#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(CONF_PLATFORM_WIN32) + #define CONF_ARCH_IA32 1 + #define CONF_ARCH_STRING "ia32" + #define CONF_ARCH_ENDIAN_LITTLE 1 +#endif + +#if defined(__ia64__) + #define CONF_ARCH_IA64 1 + #define CONF_ARCH_STRING "ia64" + #define CONF_ARCH_ENDIAN_LITTLE 1 +#endif + +#if defined(__amd64__) || defined(__x86_64__) + #define CONF_ARCH_AMD64 1 + #define CONF_ARCH_STRING "amd64" + #define CONF_ARCH_ENDIAN_LITTLE 1 +#endif + +#if defined(__powerpc__) || defined(__ppc__) + #define CONF_ARCH_PPC 1 + #define CONF_ARCH_STRING "ppc" + #define CONF_ARCH_ENDIAN_BIG 1 +#endif + +#if defined(__sparc__) + #define CONF_ARCH_SPARC 1 + #define CONF_ARCH_STRING "sparc" + #define CONF_ARCH_ENDIAN_BIG 1 +#endif + + +#ifndef CONF_FAMILY_STRING +#define CONF_FAMILY_STRING "unknown" +#endif + +#ifndef CONF_PLATFORM_STRING +#define CONF_PLATFORM_STRING "unknown" +#endif + +#ifndef CONF_ARCH_STRING +#define CONF_ARCH_STRING "unknown" +#endif + +#endif diff --git a/src/base/math.hpp b/src/base/math.hpp new file mode 100644 index 00000000..5e3f7ede --- /dev/null +++ b/src/base/math.hpp @@ -0,0 +1,61 @@ +/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ +#ifndef BASE_MATH_H +#define BASE_MATH_H + +#include + +template +inline T clamp(T val, T min, T max) +{ + if(val < min) + return min; + if(val > max) + return max; + return val; +} + +inline float sign(float f) +{ + return f<0.0f?-1.0f:1.0f; +} + +template +inline T mix(const T a, const T b, TB amount) +{ + return a + (b-a)*amount; +} + +inline float frandom() { return rand()/(float)(RAND_MAX); } + +// float to fixed +inline int f2fx(float v) { return (int)(v*(float)(1<<10)); } +inline float fx2f(int v) { return v*(1.0f/(1<<10)); } + +class fxp +{ + int value; +public: + void set(int v) { value = v; } + int get() const { return value; } + fxp &operator = (int v) { value = v<<10; return *this; } + fxp &operator = (float v) { value = (int)(v*(float)(1<<10)); return *this; } + operator float() const { return value/(float)(1<<10); } +}; + +class tune_param +{ + int value; +public: + void set(int v) { value = v; } + int get() const { return value; } + tune_param &operator = (int v) { value = (int)(v*100.0f); return *this; } + tune_param &operator = (float v) { value = (int)(v*100.0f); return *this; } + operator float() const { return value/100.0f; } +}; + +const float pi = 3.1415926535897932384626433f; + +template inline T min(T a, T b) { return a inline T max(T a, T b) { return a>b?a:b; } + +#endif // BASE_MATH_H diff --git a/src/base/system.c b/src/base/system.c new file mode 100644 index 00000000..1ac5f296 --- /dev/null +++ b/src/base/system.c @@ -0,0 +1,971 @@ +/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ +#include +#include +#include +#include +#include +#include + +/*#include "detect.h"*/ +#include "system.h" +/*#include "e_console.h"*/ + +#if defined(CONF_FAMILY_UNIX) + #include + #include + + /* unix net includes */ + #include + #include + #include + #include + #include + #include + #include + #include + #include + + #include + #include +#elif defined(CONF_FAMILY_WINDOWS) + #define WIN32_LEAN_AND_MEAN + #define _WIN32_WINNT 0x0501 /* required for mingw to get getaddrinfo to work */ + #include + #include + #include + #include + #include + #include + + #define EWOULDBLOCK WSAEWOULDBLOCK +#else + #error NOT IMPLEMENTED +#endif + +#if defined(__cplusplus) +extern "C" { +#endif + +IOHANDLE io_stdin() { return (IOHANDLE)stdin; } +IOHANDLE io_stdout() { return (IOHANDLE)stdout; } +IOHANDLE io_stderr() { return (IOHANDLE)stderr; } + +static DBG_LOGGER loggers[16]; +static int num_loggers = 0; + +static NETSTATS network_stats = {0}; + +void dbg_logger(DBG_LOGGER logger) +{ + loggers[num_loggers++] = logger; +} + +void dbg_assert_imp(const char *filename, int line, int test, const char *msg) +{ + if(!test) + { + dbg_msg("assert", "%s(%d): %s", filename, line, msg); + dbg_break(); + } +} + +void dbg_break() +{ + *((unsigned*)0) = 0x0; +} + +void dbg_msg(const char *sys, const char *fmt, ...) +{ + va_list args; + char str[1024*4]; + char *msg; + int i, len; + + str_format(str, sizeof(str), "[%08x][%s]: ", (int)time(0), sys); + len = strlen(str); + msg = (char *)str + len; + + va_start(args, fmt); +#if defined(CONF_FAMILY_WINDOWS) + _vsnprintf(msg, sizeof(str)-len, fmt, args); +#else + vsnprintf(msg, sizeof(str)-len, fmt, args); +#endif + va_end(args); + + for(i = 0; i < num_loggers; i++) + loggers[i](str); +} + +static void logger_stdout(const char *line) +{ + printf("%s\n", line); + fflush(stdout); +} + +static void logger_debugger(const char *line) +{ +#if defined(CONF_FAMILY_WINDOWS) + OutputDebugString(line); + OutputDebugString("\n"); +#endif +} + + +IOHANDLE logfile = 0; +static void logger_file(const char *line) +{ + io_write(logfile, line, strlen(line)); + io_write(logfile, "\n", 1); + io_flush(logfile); +} + +void dbg_logger_stdout() { dbg_logger(logger_stdout); } +void dbg_logger_debugger() { dbg_logger(logger_debugger); } +void dbg_logger_file(const char *filename) +{ + logfile = io_open(filename, IOFLAG_WRITE); + if(logfile) + dbg_logger(logger_file); + else + dbg_msg("dbg/logger", "failed to open '%s' for logging", filename); + +} + +/* */ + +int memory_alloced = 0; + +struct memheader +{ + const char *filename; + int line; + int size; + struct memheader *prev; + struct memheader *next; +}; + +struct memtail +{ + int guard; +}; + +static struct memheader *first = 0; + +int mem_allocated() +{ + return memory_alloced; +} + +void *mem_alloc_debug(const char *filename, int line, unsigned size, unsigned alignment) +{ + /* TODO: fix alignment */ + /* TODO: add debugging */ + struct memheader *header = (struct memheader *)malloc(size+sizeof(struct memheader)+sizeof(struct memtail)); + struct memtail *tail = (struct memtail *)(((char*)(header+1))+size); + header->size = size; + header->filename = filename; + header->line = line; + memory_alloced += header->size; + + tail->guard = 0xbaadc0de; + + header->prev = (struct memheader *)0; + header->next = first; + if(first) + first->prev = header; + first = header; + + /*dbg_msg("mem", "++ %p", header+1); */ + return header+1; +} + +void mem_free(void *p) +{ + if(p) + { + struct memheader *header = (struct memheader *)p - 1; + struct memtail *tail = (struct memtail *)(((char*)(header+1))+header->size); + + if(tail->guard != 0xbaadc0de) + dbg_msg("mem", "!! %p", p); + /* dbg_msg("mem", "-- %p", p); */ + memory_alloced -= header->size; + + if(header->prev) + header->prev->next = header->next; + else + first = header->next; + if(header->next) + header->next->prev = header->prev; + + free(header); + } +} + +void mem_debug_dump() +{ + char buf[1024]; + struct memheader *header = first; + IOHANDLE f = io_open("memory.txt", IOFLAG_WRITE); + + while(header) + { + str_format(buf, sizeof(buf), "%s(%d): %d\n", header->filename, header->line, header->size); + io_write(f, buf, strlen(buf)); + header = header->next; + } + + io_close(f); +} + + +void mem_copy(void *dest, const void *source, unsigned size) +{ + memcpy(dest, source, size); +} + +void mem_move(void *dest, const void *source, unsigned size) +{ + memmove(dest, source, size); +} + +void mem_zero(void *block,unsigned size) +{ + memset(block, 0, size); +} + +IOHANDLE io_open(const char *filename, int flags) +{ + if(flags == IOFLAG_READ) + return (IOHANDLE)fopen(filename, "rb"); + if(flags == IOFLAG_WRITE) + return (IOHANDLE)fopen(filename, "wb"); + return 0x0; +} + +unsigned io_read(IOHANDLE io, void *buffer, unsigned size) +{ + return fread(buffer, 1, size, (FILE*)io); +} + +unsigned io_skip(IOHANDLE io, unsigned size) +{ + fseek((FILE*)io, size, SEEK_CUR); + return size; +} + +int io_seek(IOHANDLE io, int offset, int origin) +{ + int real_origin; + + switch(origin) + { + case IOSEEK_START: + real_origin = SEEK_SET; + break; + case IOSEEK_CUR: + real_origin = SEEK_CUR; + break; + case IOSEEK_END: + real_origin = SEEK_END; + } + + return fseek((FILE*)io, offset, origin); +} + +long int io_tell(IOHANDLE io) +{ + return ftell((FILE*)io); +} + +long int io_length(IOHANDLE io) +{ + long int length; + io_seek(io, 0, IOSEEK_END); + length = io_tell(io); + io_seek(io, 0, IOSEEK_START); + return length; +} + +unsigned io_write(IOHANDLE io, const void *buffer, unsigned size) +{ + return fwrite(buffer, 1, size, (FILE*)io); +} + +int io_close(IOHANDLE io) +{ + fclose((FILE*)io); + return 1; +} + +int io_flush(IOHANDLE io) +{ + fflush((FILE*)io); + return 0; +} + +void *thread_create(void (*threadfunc)(void *), void *u) +{ +#if defined(CONF_FAMILY_UNIX) + pthread_t id; + pthread_create(&id, NULL, (void *(*)(void*))threadfunc, u); + return (void*)id; +#elif defined(CONF_FAMILY_WINDOWS) + return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadfunc, u, 0, NULL); +#else + #error not implemented +#endif +} + +void thread_wait(void *thread) +{ +#if defined(CONF_FAMILY_UNIX) + pthread_join((pthread_t)thread, NULL); +#elif defined(CONF_FAMILY_WINDOWS) + WaitForSingleObject((HANDLE)thread, INFINITE); +#else + #error not implemented +#endif +} + +void thread_destroy(void *thread) +{ +#if defined(CONF_FAMILY_UNIX) + void *r = 0; + pthread_join((pthread_t)thread, &r); +#else + /*#error not implemented*/ +#endif +} + +void thread_yield() +{ +#if defined(CONF_FAMILY_UNIX) + sched_yield(); +#elif defined(CONF_FAMILY_WINDOWS) + Sleep(0); +#else + #error not implemented +#endif +} + +void thread_sleep(int milliseconds) +{ +#if defined(CONF_FAMILY_UNIX) + usleep(milliseconds*1000); +#elif defined(CONF_FAMILY_WINDOWS) + Sleep(milliseconds); +#else + #error not implemented +#endif +} + + + + +#if defined(CONF_FAMILY_UNIX) +typedef pthread_mutex_t LOCKINTERNAL; +#elif defined(CONF_FAMILY_WINDOWS) +typedef CRITICAL_SECTION LOCKINTERNAL; +#else + #error not implemented on this platform +#endif + +LOCK lock_create() +{ + LOCKINTERNAL *lock = (LOCKINTERNAL*)mem_alloc(sizeof(LOCKINTERNAL), 4); + +#if defined(CONF_FAMILY_UNIX) + pthread_mutex_init(lock, 0x0); +#elif defined(CONF_FAMILY_WINDOWS) + InitializeCriticalSection((LPCRITICAL_SECTION)lock); +#else + #error not implemented on this platform +#endif + return (LOCK)lock; +} + +void lock_destroy(LOCK lock) +{ +#if defined(CONF_FAMILY_UNIX) + pthread_mutex_destroy((LOCKINTERNAL *)lock); +#elif defined(CONF_FAMILY_WINDOWS) + DeleteCriticalSection((LPCRITICAL_SECTION)lock); +#else + #error not implemented on this platform +#endif + mem_free(lock); +} + +int lock_try(LOCK lock) +{ +#if defined(CONF_FAMILY_UNIX) + return pthread_mutex_trylock((LOCKINTERNAL *)lock); +#elif defined(CONF_FAMILY_WINDOWS) + return TryEnterCriticalSection((LPCRITICAL_SECTION)lock); +#else + #error not implemented on this platform +#endif +} + +void lock_wait(LOCK lock) +{ +#if defined(CONF_FAMILY_UNIX) + pthread_mutex_lock((LOCKINTERNAL *)lock); +#elif defined(CONF_FAMILY_WINDOWS) + EnterCriticalSection((LPCRITICAL_SECTION)lock); +#else + #error not implemented on this platform +#endif +} + +void lock_release(LOCK lock) +{ +#if defined(CONF_FAMILY_UNIX) + pthread_mutex_unlock((LOCKINTERNAL *)lock); +#elif defined(CONF_FAMILY_WINDOWS) + LeaveCriticalSection((LPCRITICAL_SECTION)lock); +#else + #error not implemented on this platform +#endif +} + +/* ----- time ----- */ +int64 time_get() +{ +#if defined(CONF_FAMILY_UNIX) + struct timeval val; + gettimeofday(&val, NULL); + return (int64)val.tv_sec*(int64)1000000+(int64)val.tv_usec; +#elif defined(CONF_FAMILY_WINDOWS) + static int64 last = 0; + int64 t; + QueryPerformanceCounter((PLARGE_INTEGER)&t); + if(tsin_family = AF_INET; + p->sin_port = htons(src->port); + p->sin_addr.s_addr = htonl(src->ip[0]<<24|src->ip[1]<<16|src->ip[2]<<8|src->ip[3]); +} + +static void sockaddr_to_netaddr(const struct sockaddr *src, NETADDR *dst) +{ + /* TODO: IPv6 support */ + unsigned int ip = htonl(((struct sockaddr_in*)src)->sin_addr.s_addr); + dst->type = NETTYPE_IPV4; + dst->port = htons(((struct sockaddr_in*)src)->sin_port); + dst->ip[0] = (unsigned char)((ip>>24)&0xFF); + dst->ip[1] = (unsigned char)((ip>>16)&0xFF); + dst->ip[2] = (unsigned char)((ip>>8)&0xFF); + dst->ip[3] = (unsigned char)(ip&0xFF); +} + +int net_addr_comp(const NETADDR *a, const NETADDR *b) +{ + return mem_comp(a, b, sizeof(NETADDR)); +} + +int net_host_lookup(const char *hostname, NETADDR *addr, int types) +{ + /* TODO: IPv6 support */ + struct addrinfo hints; + struct addrinfo *result; + int e; + + mem_zero(&hints, sizeof(hints)); + hints.ai_family = AF_INET; + + e = getaddrinfo(hostname, NULL, &hints, &result); + if(e != 0 || !result) + return -1; + + sockaddr_to_netaddr(result->ai_addr, addr); + freeaddrinfo(result); + addr->port = 0; + return 0; +} + +NETSOCKET net_udp_create(NETADDR bindaddr) +{ + /* TODO: IPv6 support */ + struct sockaddr addr; + unsigned int mode = 1; + int broadcast = 1; + + /* create socket */ + int sock = socket(AF_INET, SOCK_DGRAM, 0); + if(sock < 0) + return NETSOCKET_INVALID; + + /* bind, we should check for error */ + netaddr_to_sockaddr(&bindaddr, &addr); + if(bind(sock, &addr, sizeof(addr)) != 0) + { + net_udp_close(sock); + return NETSOCKET_INVALID; + } + + /* set non-blocking */ +#if defined(CONF_FAMILY_WINDOWS) + ioctlsocket(sock, FIONBIO, (unsigned long *)&mode); +#else + ioctl(sock, FIONBIO, (unsigned long *)&mode); +#endif + + /* set boardcast */ + setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const char*)&broadcast, sizeof(broadcast)); + + /* return */ + return sock; +} + +int net_udp_send(NETSOCKET sock, const NETADDR *addr, const void *data, int size) +{ + struct sockaddr sa; + int d; + mem_zero(&sa, sizeof(sa)); + netaddr_to_sockaddr(addr, &sa); + d = sendto((int)sock, (const char*)data, size, 0, &sa, sizeof(sa)); + if(d < 0) + dbg_msg("net", "sendto error %d %x", d, d); + network_stats.sent_bytes += size; + network_stats.sent_packets++; + return d; +} + +int net_udp_recv(NETSOCKET sock, NETADDR *addr, void *data, int maxsize) +{ + struct sockaddr from; + int bytes; + socklen_t fromlen = sizeof(struct sockaddr); + bytes = recvfrom(sock, (char*)data, maxsize, 0, &from, &fromlen); + if(bytes > 0) + { + sockaddr_to_netaddr(&from, addr); + network_stats.recv_bytes += bytes; + network_stats.recv_packets++; + return bytes; + } + else if(bytes == 0) + return 0; + return -1; /* error */ +} + +int net_udp_close(NETSOCKET sock) +{ +#if defined(CONF_FAMILY_WINDOWS) + closesocket(sock); +#else + close((int)sock); +#endif + return 0; +} + +NETSOCKET net_tcp_create(const NETADDR *a) +{ + /* TODO: IPv6 support */ + struct sockaddr addr; + + /* create socket */ + int sock = socket(AF_INET, SOCK_STREAM, 0); + if(sock < 0) + return NETSOCKET_INVALID; + + /* bind, we should check for error */ + netaddr_to_sockaddr(a, &addr); + bind(sock, &addr, sizeof(addr)); + + /* return */ + return sock; +} + +int net_tcp_set_non_blocking(NETSOCKET sock) +{ + unsigned int mode = 1; +#if defined(CONF_FAMILY_WINDOWS) + return ioctlsocket(sock, FIONBIO, (unsigned long *)&mode); +#else + return ioctl(sock, FIONBIO, (unsigned long *)&mode); +#endif +} + +int net_tcp_set_blocking(NETSOCKET sock) +{ + unsigned int mode = 0; +#if defined(CONF_FAMILY_WINDOWS) + return ioctlsocket(sock, FIONBIO, (unsigned long *)&mode); +#else + return ioctl(sock, FIONBIO, (unsigned long *)&mode); +#endif +} + +int net_tcp_listen(NETSOCKET sock, int backlog) +{ + return listen(sock, backlog); +} + +int net_tcp_accept(NETSOCKET sock, NETSOCKET *new_sock, NETADDR *a) +{ + int s; + socklen_t sockaddr_len; + struct sockaddr addr; + + sockaddr_len = sizeof(addr); + + s = accept(sock, &addr, &sockaddr_len); + + if (s != -1) + { + sockaddr_to_netaddr(&addr, a); + *new_sock = s; + } + return s; +} + +int net_tcp_connect(NETSOCKET sock, const NETADDR *a) +{ + struct sockaddr addr; + + netaddr_to_sockaddr(a, &addr); + return connect(sock, &addr, sizeof(addr)); +} + +int net_tcp_connect_non_blocking(NETSOCKET sock, const NETADDR *a) +{ + struct sockaddr addr; + int res; + + netaddr_to_sockaddr(a, &addr); + net_tcp_set_non_blocking(sock); + res = connect(sock, &addr, sizeof(addr)); + net_tcp_set_blocking(sock); + + return res; +} + +int net_tcp_send(NETSOCKET sock, const void *data, int size) +{ + int d; + d = send((int)sock, (const char*)data, size, 0); + return d; +} + +int net_tcp_recv(NETSOCKET sock, void *data, int maxsize) +{ + int bytes; + bytes = recv((int)sock, (char*)data, maxsize, 0); + return bytes; +} + +int net_tcp_close(NETSOCKET sock) +{ +#if defined(CONF_FAMILY_WINDOWS) + closesocket(sock); +#else + close((int)sock); +#endif + return 0; +} + +int net_errno() +{ + return errno; +} + +int net_would_block() +{ + return net_errno() == EWOULDBLOCK; +} + +int net_init() +{ +#if defined(CONF_FAMILY_WINDOWS) + WSADATA wsaData; + int err = WSAStartup(MAKEWORD(1, 1), &wsaData); + dbg_assert(err == 0, "network initialization failed."); + return err==0?0:1; +#endif + + return 0; +} + +int fs_listdir(const char *dir, fs_listdir_callback cb, void *user) +{ +#if defined(CONF_FAMILY_WINDOWS) + WIN32_FIND_DATA finddata; + HANDLE handle; + char buffer[1024*2]; + str_format(buffer, sizeof(buffer), "%s/*", dir); + + handle = FindFirstFileA(buffer, &finddata); + + if (handle == INVALID_HANDLE_VALUE) + return 0; + + /* add all the entries */ + do + { + if(finddata.cFileName[0] != '.') + cb(finddata.cFileName, 0, user); + } while (FindNextFileA(handle, &finddata)); + + FindClose(handle); + return 0; +#else + struct dirent *entry; + DIR *d = opendir(dir); + + if(!d) + return 0; + + while((entry = readdir(d)) != NULL) + cb(entry->d_name, 0, user); + + /* close the directory and return */ + closedir(d); + return 0; +#endif +} + +int fs_storage_path(const char *appname, char *path, int max) +{ +#if defined(CONF_FAMILY_WINDOWS) + HRESULT r; + char *home = getenv("APPDATA"); + if(!home) + return -1; + _snprintf(path, max, "%s/%s", home, appname); + return 0; +#else + char *home = getenv("HOME"); + int i; + if(!home) + return -1; + +#if defined(CONF_PLATFORM_MACOSX) + snprintf(path, max, "%s/Library/Application Support/%s", home, appname); +#else + snprintf(path, max, "%s/.%s", home, appname); + for(i = strlen(home)+2; path[i]; i++) + path[i] = tolower(path[i]); +#endif + + return 0; +#endif +} + +int fs_makedir(const char *path) +{ +#if defined(CONF_FAMILY_WINDOWS) + if(_mkdir(path) == 0) + return 0; + if(errno == EEXIST) + return 0; + return -1; +#else + if(mkdir(path, 0755) == 0) + return 0; + if(errno == EEXIST) + return 0; + return -1; +#endif +} + +void swap_endian(void *data, unsigned elem_size, unsigned num) +{ + char *src = (char*) data; + char *dst = src + (elem_size - 1); + + while(num) + { + unsigned n = elem_size>>1; + char tmp; + while(n) + { + tmp = *src; + *src = *dst; + *dst = tmp; + + src++; + dst--; + n--; + } + + src = src + (elem_size>>1); + dst = src + (elem_size - 1); + num--; + } +} + +int net_socket_read_wait(NETSOCKET sock, int time) +{ + struct timeval tv; + fd_set readfds; + + tv.tv_sec = 0; + tv.tv_usec = 1000*time; + + FD_ZERO(&readfds); + FD_SET(sock, &readfds); + + /* don't care about writefds and exceptfds */ + select(sock+1, &readfds, NULL, NULL, &tv); + if(FD_ISSET(sock, &readfds)) + return 1; + return 0; +} + +unsigned time_timestamp() +{ + return time(0); +} + +void str_append(char *dst, const char *src, int dst_size) +{ + int s = strlen(dst); + int i = 0; + while(s < dst_size) + { + dst[s] = src[i]; + if(!src[i]) /* check for null termination */ + break; + s++; + i++; + } + + dst[dst_size-1] = 0; /* assure null termination */ +} + +void str_copy(char *dst, const char *src, int dst_size) +{ + strncpy(dst, src, dst_size); + dst[dst_size-1] = 0; /* assure null termination */ +} + +void str_format(char *buffer, int buffer_size, const char *format, ...) +{ +#if defined(CONF_FAMILY_WINDOWS) + va_list ap; + va_start(ap, format); + _vsnprintf(buffer, buffer_size, format, ap); + va_end(ap); +#else + va_list ap; + va_start(ap, format); + vsnprintf(buffer, buffer_size, format, ap); + va_end(ap); +#endif + + buffer[buffer_size-1] = 0; /* assure null termination */ +} + + + +/* makes sure that the string only contains the characters between 32 and 127 */ +void str_sanitize_strong(char *str_in) +{ + unsigned char *str = (unsigned char *)str_in; + while(*str) + { + *str &= 0x7f; + if(*str < 32) + *str = 32; + str++; + } +} + +/* makes sure that the string only contains the characters between 32 and 255 + \r\n\t */ +void str_sanitize(char *str_in) +{ + unsigned char *str = (unsigned char *)str_in; + while(*str) + { + if(*str < 32 && !(*str == '\r') && !(*str == '\n') && !(*str == '\t')) + *str = ' '; + str++; + } +} + +/* case */ +int str_comp_nocase(const char *a, const char *b) +{ +#if defined(CONF_FAMILY_WINDOWS) + return _stricmp(a,b); +#else + return strcasecmp(a,b); +#endif +} + +const char *str_find_nocase(const char *haystack, const char *needle) +{ + while(*haystack) /* native implementation */ + { + const char *a = haystack; + const char *b = needle; + while(*a && *b && tolower(*a) == tolower(*b)) + { + a++; + b++; + } + if(!(*b)) + return haystack; + haystack++; + } + + return 0; +} + +void str_hex(char *dst, int dst_size, const void *data, int data_size) +{ + static const char hex[] = "0123456789ABCDEF"; + int b; + + for(b = 0; b < data_size && b < dst_size/4-4; b++) + { + dst[b*3] = hex[((const unsigned char *)data)[b]>>4]; + dst[b*3+1] = hex[((const unsigned char *)data)[b]&0xf]; + dst[b*3+2] = ' '; + dst[b*3+3] = 0; + } +} + +int mem_comp(const void *a, const void *b, int size) +{ + return memcmp(a,b,size); +} + +void net_stats(NETSTATS *stats_inout) +{ + *stats_inout = network_stats; +} + +#if defined(__cplusplus) +} +#endif diff --git a/src/base/system.h b/src/base/system.h new file mode 100644 index 00000000..4449907c --- /dev/null +++ b/src/base/system.h @@ -0,0 +1,881 @@ +/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ + +/* + Title: OS Abstraction +*/ + +#ifndef BASE_SYSTEM_H +#define BASE_SYSTEM_H + +#include "detect.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* Group: Debug */ +/* + Function: dbg_assert + Breaks into the debugger based on a test. + + Parameters: + test - Result of the test. + msg - Message that should be printed if the test fails. + + Remarks: + Does nothing in release version of the library. + + See Also: + +*/ +void dbg_assert(int test, const char *msg); +#define dbg_assert(test,msg) dbg_assert_imp(__FILE__, __LINE__, test, msg) +void dbg_assert_imp(const char *filename, int line, int test, const char *msg); + +/* + Function: dbg_break + Breaks into the debugger. + + Remarks: + Does nothing in release version of the library. + + See Also: + +*/ +void dbg_break(); + +/* + Function: dbg_msg + + Prints a debug message. + + Parameters: + sys - A string that describes what system the message belongs to + fmt - A printf styled format string. + + Remarks: + Does nothing in relase version of the library. + + See Also: + +*/ +void dbg_msg(const char *sys, const char *fmt, ...); + +/* Group: Memory */ + +/* + Function: mem_alloc + Allocates memory. + + Parameters: + size - Size of the needed block. + alignment - Alignment for the block. + + Returns: + Returns a pointer to the newly allocated block. Returns a + null pointer if the memory couldn't be allocated. + + Remarks: + - Passing 0 to size will allocated the smallest amount possible + and return a unique pointer. + + See Also: + +*/ +void *mem_alloc_debug(const char *filename, int line, unsigned size, unsigned alignment); +#define mem_alloc(s,a) mem_alloc_debug(__FILE__, __LINE__, (s), (a)) + +/* + Function: mem_free + Frees a block allocated through . + + Remarks: + - In the debug version of the library the function will assert if + a non-valid block is passed, like a null pointer or a block that + isn't allocated. + + See Also: + +*/ +void mem_free(void *block); + +/* + Function: mem_copy + Copies a a memory block. + + Parameters: + dest - Destination. + source - Source to copy. + size - Size of the block to copy. + + Remarks: + - This functions DOES NOT handles cases where source and + destination is overlapping. + + See Also: + +*/ +void mem_copy(void *dest, const void *source, unsigned size); + +/* + Function: mem_move + Copies a a memory block + + Parameters: + dest - Destination + source - Source to copy + size - Size of the block to copy + + Remarks: + - This functions handles cases where source and destination + is overlapping + + See Also: + +*/ +void mem_move(void *dest, const void *source, unsigned size); + +/* + Function: mem_zero + Sets a complete memory block to 0 + + Parameters: + block - Pointer to the block to zero out + size - Size of the block +*/ +void mem_zero(void *block, unsigned size); + +/* + Function: mem_comp + Compares two blocks of memory + + Parameters: + a - First block of data + b - Second block of data + size - Size of the data to compare + + Returns: + <0 - Block a is lesser then block b + 0 - Block a is equal to block b + >0 - Block a is greater then block b +*/ +int mem_comp(const void *a, const void *b, int size); + +/* Group: File IO */ +enum { + IOFLAG_READ = 1, + IOFLAG_WRITE = 2, + IOFLAG_RANDOM = 4, + + IOSEEK_START = 0, + IOSEEK_CUR = 1, + IOSEEK_END = 2 +}; + +typedef struct IOINTERNAL *IOHANDLE; + +/* + Function: io_open + Opens a file. + + Parameters: + filename - File to open. + flags - A set of flags. IOFLAG_READ, IOFLAG_WRITE, IOFLAG_RANDOM. + + Returns: + Returns a handle to the file on success and 0 on failure. + +*/ +IOHANDLE io_open(const char *filename, int flags); + +/* + Function: io_read + Reads data into a buffer from a file. + + Parameters: + io - Handle to the file to read data from. + buffer - Pointer to the buffer that will recive the data. + size - Number of bytes to read from the file. + + Returns: + Number of bytes read. + +*/ +unsigned io_read(IOHANDLE io, void *buffer, unsigned size); + +/* + Function: io_skip + Skips data in a file. + + Parameters: + io - Handle to the file. + size - Number of bytes to skip. + + Returns: + Number of bytes skipped. +*/ +unsigned io_skip(IOHANDLE io, unsigned size); + +/* + Function: io_write + Writes data from a buffer to file. + + Parameters: + io - Handle to the file. + buffer - Pointer to the data that should be written. + size - Number of bytes to write. + + Returns: + Number of bytes written. +*/ +unsigned io_write(IOHANDLE io, const void *buffer, unsigned size); + +/* + Function: io_seek + Seeks to a specified offset in the file. + + Parameters: + io - Handle to the file. + offset - Offset from pos to stop. + origin - Position to start searching from. + + Returns: + Returns 0 on success. +*/ +int io_seek(IOHANDLE io, int offset, int origin); + +/* + Function: io_tell + Gets the current position in the file. + + Parameters: + io - Handle to the file. + + Returns: + Returns the current position. -1L if an error occured. +*/ +long int io_tell(IOHANDLE io); + +/* + Function: io_length + Gets the total length of the file. Resetting cursor to the beginning + + Parameters: + io - Handle to the file. + + Returns: + Returns the total size. -1L if an error occured. +*/ +long int io_length(IOHANDLE io); + +/* + Function: io_close + Closes a file. + + Parameters: + io - Handle to the file. + + Returns: + Returns 0 on success. +*/ +int io_close(IOHANDLE io); + +/* + Function: io_flush + Empties all buffers and writes all pending data. + + Parameters: + io - Handle to the file. + + Returns: + Returns 0 on success. +*/ +int io_flush(IOHANDLE io); + + +/* + Function: io_stdin + Returns an to the standard input. +*/ +IOHANDLE io_stdin(); + +/* + Function: io_stdout + Returns an to the standard output. +*/ +IOHANDLE io_stdout(); + +/* + Function: io_stderr + Returns an to the standard error. +*/ +IOHANDLE io_stderr(); + + +/* Group: Threads */ + +/* + Function: thread_sleep + Suspends the current thread for a given period. + + Parameters: + milliseconds - Number of milliseconds to sleep. +*/ +void thread_sleep(int milliseconds); + +/* + Function: thread_create + Creates a new thread. + + Parameters: + threadfunc - Entry point for the new thread. + user - Pointer to pass to the thread. + +*/ +void *thread_create(void (*threadfunc)(void *), void *user); + +/* + Function: thread_wait + Waits for a thread to be done or destroyed. + + Parameters: + thread - Thread to wait for. +*/ +void thread_wait(void *thread); + +/* + Function: thread_destoy + Destroys a thread. + + Parameters: + thread - Thread to destroy. +*/ +void thread_destroy(void *thread); + +/* + Function: thread_yeild + Yeild the current threads execution slice. +*/ +void thread_yield(); + + +/* Group: Locks */ +typedef void* LOCK; + +LOCK lock_create(); +void lock_destroy(LOCK lock); + +int lock_try(LOCK lock); +void lock_wait(LOCK lock); +void lock_release(LOCK lock); + +/* Group: Timer */ +#ifdef __GNUC__ +/* if compiled with -pedantic-errors it will complain about long + not being a C90 thing. +*/ +__extension__ typedef long long int64; +#else +typedef long long int64; +#endif +/* + Function: time_get + Fetches a sample from a high resolution timer. + + Returns: + Current value of the timer. + + Remarks: + To know how fast the timer is ticking, see . +*/ +int64 time_get(); + +/* + Function: time_freq + Returns the frequency of the high resolution timer. + + Returns: + Returns the frequency of the high resolution timer. +*/ +int64 time_freq(); + +/* + Function: time_timestamp + Retrives the current time as a UNIX timestamp + + Returns: + The time as a UNIX timestamp +*/ +unsigned time_timestamp(); + +/* Group: Network General */ +typedef int NETSOCKET; +enum +{ + NETSOCKET_INVALID = -1, + + NETTYPE_INVALID = 0, + NETTYPE_IPV4 = 1, + NETTYPE_IPV6 = 2, + NETTYPE_ALL = ~0 +}; + +typedef struct +{ + unsigned int type; + unsigned char ip[16]; + unsigned short port; +} NETADDR; + +/* + Function: net_init + Initiates network functionallity. + + Returns: + Returns 0 on success, + + Remarks: + You must call this function before using any other network + functions. +*/ +int net_init(); + +/* + Function: net_host_lookup + Does a hostname lookup by name and fills out the passed + NETADDR struct with the recieved details. + + Returns: + 0 on success. +*/ +int net_host_lookup(const char *hostname, NETADDR *addr, int types); + +/* + Function: net_addr_comp + Compares two network addresses. + + Parameters: + a - Address to compare + b - Address to compare to. + + Returns: + <0 - Address a is lesser then address b + 0 - Address a is equal to address b + >0 - Address a is greater then address b +*/ +int net_addr_comp(const NETADDR *a, const NETADDR *b); + +/* + Function: net_addr_str + Turns a network address into a representive string. + + Parameters: + addr - Address to turn into a string. + string - Buffer to fill with the string. + max_length - Maximum size of the string. + + Remarks: + - The string will always be zero terminated + +*/ +int net_addr_str(const NETADDR *addr, char *string, int max_length); + +/* Group: Network UDP */ + +/* + Function: net_udp_create + Creates a UDP socket and binds it to a port. + + Parameters: + bindaddr - Address to bind the socket to. + + Returns: + On success it returns an handle to the socket. On failure it + returns NETSOCKET_INVALID. +*/ +NETSOCKET net_udp_create(NETADDR bindaddr); + +/* + Function: net_udp_send + Sends a packet over an UDP socket. + + Parameters: + sock - Socket to use. + addr - Where to send the packet. + data - Pointer to the packet data to send. + size - Size of the packet. + + Returns: + On success it returns the number of bytes sent. Returns -1 + on error. +*/ +int net_udp_send(NETSOCKET sock, const NETADDR *addr, const void *data, int size); + +/* + Function: net_udp_recv + Recives a packet over an UDP socket. + + Parameters: + sock - Socket to use. + addr - Pointer to an NETADDR that will recive the address. + data - Pointer to a buffer that will recive the data. + maxsize - Maximum size to recive. + + Returns: + On success it returns the number of bytes recived. Returns -1 + on error. +*/ +int net_udp_recv(NETSOCKET sock, NETADDR *addr, void *data, int maxsize); + +/* + Function: net_udp_close + Closes an UDP socket. + + Parameters: + sock - Socket to close. + + Returns: + Returns 0 on success. -1 on error. +*/ +int net_udp_close(NETSOCKET sock); + + +/* Group: Network TCP */ + +/* + Function: net_tcp_create + Creates a TCP socket. + + Parameters: + bindaddr - Address to bind the socket to. + + Returns: + On success it returns an handle to the socket. On failure it returns NETSOCKET_INVALID. +*/ +NETSOCKET net_tcp_create(const NETADDR *a); + +/* + Function: net_tcp_listen + Makes the socket start listening for new connections. + + Parameters: + sock - Socket to start listen to. + backlog - Size of the queue of incomming connections to keep. + + Returns: + Returns 0 on success. +*/ +int net_tcp_listen(NETSOCKET sock, int backlog); + +/* + Function: net_tcp_accept + Polls a listning socket for a new connection. + + Parameters: + sock - Listning socket to poll. + new_sock - Pointer to a socket to fill in with the new socket. + addr - Pointer to an address that will be filled in the remote address (optional, can be NULL). + + Returns: + Returns a non-negative integer on success. Negative integer on failure. +*/ +int net_tcp_accept(NETSOCKET sock, NETSOCKET *new_sock, NETADDR *addr); + +/* + Function: net_tcp_connect + Connects one socket to another. + + Parameters: + sock - Socket to connect. + addr - Address to connect to. + + Returns: + Returns 0 on success. + +*/ +int net_tcp_connect(NETSOCKET sock, const NETADDR *addr); + +/* + Function: net_tcp_send + Sends data to a TCP stream. + + Parameters: + sock - Socket to send data to. + data - Pointer to the data to send. + size - Size of the data to send. + + Returns: + Number of bytes sent. Negative value on failure. +*/ +int net_tcp_send(NETSOCKET sock, const void *data, int size); + +/* + Function: net_tcp_recv + Recvives data from a TCP stream. + + Parameters: + sock - Socket to recvive data from. + data - Pointer to a buffer to write the data to + max_size - Maximum of data to write to the buffer. + + Returns: + Number of bytes recvived. Negative value on failure. +*/ +int net_tcp_recv(NETSOCKET sock, void *data, int maxsize); + +/* + Function: net_tcp_close + Closes a TCP socket. + + Parameters: + sock - Socket to close. + + Returns: + Returns 0 on success. Negative value on failure. +*/ +int net_tcp_close(NETSOCKET sock); + +/* Group: Strings */ + +/* + Function: str_append + Appends a string to another. + + Parameters: + dst - Pointer to a buffer that contains a string. + src - String to append. + dst_size - Size of the buffer of the dst string. + + Remarks: + - The strings are treated as zero-termineted strings. + - Garantees that dst string will contain zero-termination. +*/ +void str_append(char *dst, const char *src, int dst_size); + +/* + Function: str_copy + Copies a string to another. + + Parameters: + dst - Pointer to a buffer that shall recive the string. + src - String to be copied. + dst_size - Size of the buffer dst. + + Remarks: + - The strings are treated as zero-termineted strings. + - Garantees that dst string will contain zero-termination. +*/ +void str_copy(char *dst, const char *src, int dst_size); + +/* + Function: str_format + Performs printf formating into a buffer. + + Parameters: + buffer - Pointer to the buffer to recive the formated string. + buffer_size - Size of the buffer. + format - printf formating string. + ... - Parameters for the formating. + + Remarks: + - See the C manual for syntax for the printf formating string. + - The strings are treated as zero-termineted strings. + - Garantees that dst string will contain zero-termination. +*/ +void str_format(char *buffer, int buffer_size, const char *format, ...); + +/* + Function: str_sanitize_strong + Replaces all characters below 32 and above 127 with whitespace. + + Parameters: + str - String to sanitize. + + Remarks: + - The strings are treated as zero-termineted strings. +*/ +void str_sanitize_strong(char *str); + +/* + Function: str_sanitize + Replaces all characters below 32 and above 127 with whitespace with + exception to \r, \n and \r. + + Parameters: + str - String to sanitize. + + Remarks: + - The strings are treated as zero-termineted strings. +*/ +void str_sanitize(char *str); + +/* + Function: str_comp_nocase + Compares to strings case insensitive. + + Parameters: + a - String to compare. + b - String to compare. + + Returns: + <0 - String a is lesser then string b + 0 - String a is equal to string b + >0 - String a is greater then string b + + Remarks: + - Only garanted to work with a-z/A-Z. + - The strings are treated as zero-termineted strings. +*/ +int str_comp_nocase(const char *a, const char *b); + +/* + Function: str_find_nocase + Finds a string inside another string case insensitive. + + Parameters: + haystack - String to search in + needle - String to search for + + Returns: + A pointer into haystack where the needle was found. + Returns NULL of needle could not be found. + + Remarks: + - Only garanted to work with a-z/A-Z. + - The strings are treated as zero-termineted strings. +*/ +const char *str_find_nocase(const char *haystack, const char *needle); + + +/* + Function: str_hex + Takes a datablock and generates a hexstring of it. + + Parameters: + dst - Buffer to fill with hex data + dst_size - size of the buffer + data - Data to turn into hex + data - Size of the data + + Remarks: + - The desination buffer will be zero-terminated +*/ +void str_hex(char *dst, int dst_size, const void *data, int data_size); + +/* Group: Filesystem */ + +/* + Function: fs_listdir + Lists the files in a directory + + Parameters: + dir - Directory to list + cb - Callback function to call for each entry + user - Pointer to give to the callback + + Returns: + Always returns 0. +*/ +typedef void (*fs_listdir_callback)(const char *name, int is_dir, void *user); +int fs_listdir(const char *dir, fs_listdir_callback cb, void *user); + +/* + Function: fs_makedir + Creates a directory + + Parameters: + path - Directory to create + + Returns: + Returns 0 on success. Negative value on failure. + + Remarks: + Does not create several directories if needed. "a/b/c" will result + in a failure if b or a does not exist. +*/ +int fs_makedir(const char *path); + +/* + Function: fs_storage_path + Fetches per user configuration directory. + + Returns: + Returns 0 on success. Negative value on failure. + + Remarks: + - Returns ~/.appname on UNIX based systems + - Returns ~/Library/Applications Support/appname on Mac OS X + - Returns %APPDATA%/Appname on Windows based systems +*/ +int fs_storage_path(const char *appname, char *path, int max); + + +/* + Group: Undocumented +*/ + + +/* + Function: net_tcp_connect_non_blocking + + DOCTODO: serp +*/ +int net_tcp_connect_non_blocking(NETSOCKET sock, const NETADDR *a); + +/* + Function: net_tcp_set_non_blocking + + DOCTODO: serp +*/ +int net_tcp_set_non_blocking(NETSOCKET sock); + +/* + Function: net_tcp_set_non_blocking + + DOCTODO: serp +*/ +int net_tcp_set_blocking(NETSOCKET sock); + +/* + Function: net_errno + + DOCTODO: serp +*/ +int net_errno(); + +/* + Function: net_would_block + + DOCTODO: serp +*/ +int net_would_block(); + +int net_socket_read_wait(NETSOCKET sock, int time); + +void mem_debug_dump(); +int mem_allocated(); + +void swap_endian(void *data, unsigned elem_size, unsigned num); + +typedef void (*DBG_LOGGER)(const char *line); +void dbg_logger(DBG_LOGGER logger); +void dbg_logger_stdout(); +void dbg_logger_debugger(); +void dbg_logger_file(const char *filename); + +typedef struct +{ + int sent_packets; + int sent_bytes; + int recv_packets; + int recv_bytes; +} NETSTATS; + + +void net_stats(NETSTATS *stats); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/base/vmath.hpp b/src/base/vmath.hpp new file mode 100644 index 00000000..49dd26d1 --- /dev/null +++ b/src/base/vmath.hpp @@ -0,0 +1,196 @@ +/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ +#ifndef BASE_VMATH_H +#define BASE_VMATH_H + +// ------------------------------------ + +template +class vector2_base +{ +public: + union { T x,u; }; + union { T y,v; }; + + vector2_base() {} + vector2_base(float nx, float ny) + { + x = nx; + y = ny; + } + + vector2_base operator -() const { return vector2_base(-x, -y); } + vector2_base operator -(const vector2_base &v) const { return vector2_base(x-v.x, y-v.y); } + vector2_base operator +(const vector2_base &v) const { return vector2_base(x+v.x, y+v.y); } + vector2_base operator *(const T v) const { return vector2_base(x*v, y*v); } + + const vector2_base &operator =(const vector2_base &v) { x = v.x; y = v.y; return *this; } + + const vector2_base &operator +=(const vector2_base &v) { x += v.x; y += v.y; return *this; } + const vector2_base &operator -=(const vector2_base &v) { x -= v.x; y -= v.y; return *this; } + const vector2_base &operator *=(const T v) { x *= v; y *= v; return *this; } + + bool operator ==(const vector2_base &v) const { return x == v.x && y == v.y; } //TODO: do this with an eps instead + + operator const T* () { return &x; } +}; + + +template +inline T length(const vector2_base &a) +{ + return sqrtf(a.x*a.x + a.y*a.y); +} + +template +inline T distance(const vector2_base a, const vector2_base &b) +{ + return length(a-b); +} + +template +inline T dot(const vector2_base a, const vector2_base &b) +{ + return a.x*b.x + a.y*b.y; +} + +template +inline vector2_base normalize(const vector2_base &v) +{ + T l = (T)(1.0f/sqrtf(v.x*v.x + v.y*v.y)); + return vector2_base(v.x*l, v.y*l); +} + +typedef vector2_base vec2; +typedef vector2_base bvec2; +typedef vector2_base ivec2; + +template +inline vector2_base closest_point_on_line(vector2_base line_point0, vector2_base line_point1, vector2_base target_point) +{ + vector2_base c = target_point - line_point0; + vector2_base v = (line_point1 - line_point0); + v = normalize(v); + T d = length(line_point0-line_point1); + T t = dot(v, c)/d; + return mix(line_point0, line_point1, clamp(t, (T)0, (T)1)); + /* + if (t < 0) t = 0; + if (t > 1.0f) return 1.0f; + return t;*/ +} + +// ------------------------------------ +template +class vector3_base +{ +public: + union { T x,r,h; }; + union { T y,g,s; }; + union { T z,b,v,l; }; + + vector3_base() {} + vector3_base(float nx, float ny, float nz) + { + x = nx; + y = ny; + z = nz; + } + + const vector3_base &operator =(const vector3_base &v) { x = v.x; y = v.y; z = v.z; return *this; } + + vector3_base operator -(const vector3_base &v) const { return vector3_base(x-v.x, y-v.y, z-v.z); } + vector3_base operator -() const { return vector3_base(-x, -y, -z); } + vector3_base operator +(const vector3_base &v) const { return vector3_base(x+v.x, y+v.y, z+v.z); } + vector3_base operator *(const T v) const { return vector3_base(x*v, y*v, z*v); } + vector3_base operator *(const vector3_base &v) const { return vector3_base(x*v.x, y*v.y, z*v.z); } + vector3_base operator /(const T v) const { return vector3_base(x/v, y/v, z/v); } + + const vector3_base &operator +=(const vector3_base &v) { x += v.x; y += v.y; z += v.z; return *this; } + const vector3_base &operator -=(const vector3_base &v) { x -= v.x; y -= v.y; z -= v.z; return *this; } + const vector3_base &operator *=(const T v) { x *= v; y *= v; z *= v; return *this; } + + bool operator ==(const vector3_base &v) const { return x == v.x && y == v.y && z == v.z; } //TODO: do this with an eps instead + + operator const T* () { return &x; } +}; + +template +inline T length(const vector3_base &a) +{ + return sqrtf(a.x*a.x + a.y*a.y + a.z*a.z); +} + +template +inline T distance(const vector3_base &a, const vector3_base &b) +{ + return length(a-b); +} + +template +inline T dot(const vector3_base &a, const vector3_base &b) +{ + return a.x*b.x + a.y*b.y + a.z*b.z; +} + +template +inline vector3_base normalize(const vector3_base &v) +{ + T l = (T)(1.0f/sqrtf(v.x*v.x + v.y*v.y + v.z*v.z)); + return vector3_base(v.x*l, v.y*l, v.z*l); +} + +template +inline vector3_base cross(const vector3_base &a, const vector3_base &b) +{ + return vector3_base( + a.y*b.z - a.z*b.y, + a.z*b.x - a.x*b.z, + a.x*b.y - a.y*b.x); +} + +typedef vector3_base vec3; +typedef vector3_base bvec3; +typedef vector3_base ivec3; + +// ------------------------------------ + +template +class vector4_base +{ +public: + union { T x,r; }; + union { T y,g; }; + union { T z,b; }; + union { T w,a; }; + + vector4_base() {} + vector4_base(float nx, float ny, float nz, float nw) + { + x = nx; + y = ny; + z = nz; + w = nw; + } + + vector4_base operator +(const vector4_base &v) const { return vector4_base(x+v.x, y+v.y, z+v.z, w+v.w); } + vector4_base operator -(const vector4_base &v) const { return vector4_base(x-v.x, y-v.y, z-v.z, w-v.w); } + vector4_base operator -() const { return vector4_base(-x, -y, -z, -w); } + vector4_base operator *(const vector4_base &v) const { return vector4_base(x*v.x, y*v.y, z*v.z, w*v.w); } + vector4_base operator *(const T v) const { return vector4_base(x*v, y*v, z*v, w*v); } + + const vector4_base &operator =(const vector4_base &v) { x = v.x; y = v.y; z = v.z; w = v.w; return *this; } + + const vector4_base &operator +=(const vector4_base &v) { x += v.x; y += v.y; z += v.z; w += v.w; return *this; } + const vector4_base &operator -=(const vector4_base &v) { x -= v.x; y -= v.y; z -= v.z; w -= v.w; return *this; } + const vector4_base &operator *=(const T v) { x *= v; y *= v; z *= v; w *= v; return *this; } + + bool operator ==(const vector4_base &v) const { return x == v.x && y == v.y && z == v.z && w == v.w; } //TODO: do this with an eps instead + + operator const T* () { return &x; } +}; + +typedef vector4_base vec4; +typedef vector4_base bvec4; +typedef vector4_base ivec4; + +#endif diff --git a/src/engine/client/ec_client.c b/src/engine/client/ec_client.c index b8439194..5f213ac6 100644 --- a/src/engine/client/ec_client.c +++ b/src/engine/client/ec_client.c @@ -6,7 +6,7 @@ #include #include -#include +#include #include #include diff --git a/src/engine/client/ec_font.c b/src/engine/client/ec_font.c index 5d5c6df4..af9c612a 100644 --- a/src/engine/client/ec_font.c +++ b/src/engine/client/ec_font.c @@ -2,7 +2,7 @@ #include #include #include -#include +#include #include #include "ec_font.h" diff --git a/src/engine/client/ec_gfx.c b/src/engine/client/ec_gfx.c index f3488196..4f258d11 100644 --- a/src/engine/client/ec_gfx.c +++ b/src/engine/client/ec_gfx.c @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include #include diff --git a/src/engine/client/ec_inp.c b/src/engine/client/ec_inp.c index 56b5fd1f..00468434 100644 --- a/src/engine/client/ec_inp.c +++ b/src/engine/client/ec_inp.c @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include diff --git a/src/engine/client/ec_snd.c b/src/engine/client/ec_snd.c index a3b687a7..f7ab6639 100644 --- a/src/engine/client/ec_snd.c +++ b/src/engine/client/ec_snd.c @@ -1,5 +1,5 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include +#include #include #include diff --git a/src/engine/client/ec_srvbrowse.c b/src/engine/client/ec_srvbrowse.c index 41c6a7a0..07e2d227 100644 --- a/src/engine/client/ec_srvbrowse.c +++ b/src/engine/client/ec_srvbrowse.c @@ -1,5 +1,5 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include +#include #include #include #include diff --git a/src/engine/e_compression.c b/src/engine/e_compression.c index 0a16d0b4..6a534feb 100644 --- a/src/engine/e_compression.c +++ b/src/engine/e_compression.c @@ -1,6 +1,5 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include "e_system.h" -#include +#include /* Format: ESDDDDDD EDDDDDDD EDD... Extended, Data, Sign */ unsigned char *vint_pack(unsigned char *dst, int i) diff --git a/src/engine/e_config.c b/src/engine/e_config.c index 858cec59..426bbd78 100644 --- a/src/engine/e_config.c +++ b/src/engine/e_config.c @@ -4,7 +4,7 @@ #include #include -#include "e_system.h" +#include #include "e_config.h" #include "e_linereader.h" #include "e_engine.h" diff --git a/src/engine/e_console.c b/src/engine/e_console.c index 71631bfa..8dd9dbcd 100644 --- a/src/engine/e_console.c +++ b/src/engine/e_console.c @@ -1,4 +1,4 @@ -#include "e_system.h" +#include #include "e_console.h" #include "e_config.h" #include "e_linereader.h" diff --git a/src/engine/e_datafile.c b/src/engine/e_datafile.c index 59858eda..8f684827 100644 --- a/src/engine/e_datafile.c +++ b/src/engine/e_datafile.c @@ -1,5 +1,5 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include "e_system.h" +#include #include "e_datafile.h" #include diff --git a/src/engine/e_detect.h b/src/engine/e_detect.h deleted file mode 100644 index 04cca2d9..00000000 --- a/src/engine/e_detect.h +++ /dev/null @@ -1,115 +0,0 @@ -/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#ifndef BASELIB_FILE_CONFIG_H -#define BASELIB_FILE_CONFIG_H - -/* - this file detected the family, platform and architecture - to compile for. -*/ - -/* platforms */ - -/* windows Family */ -#if defined(WIN64) || defined(_WIN64) - /* Hmm, is this IA64 or x86-64? */ - #define CONF_FAMILY_WINDOWS 1 - #define CONF_FAMILY_STRING "windows" - #define CONF_PLATFORM_WIN64 1 - #define CONF_PLATFORM_STRING "win64" -#elif defined(WIN32) || defined(_WIN32) || defined(__CYGWIN32__) || defined(__MINGW32__) - #define CONF_FAMILY_WINDOWS 1 - #define CONF_FAMILY_STRING "windows" - #define CONF_PLATFORM_WIN32 1 - #define CONF_PLATFORM_STRING "win32" -#endif - -/* unix family */ -#if defined(__FreeBSD__) - #define CONF_FAMILY_UNIX 1 - #define CONF_FAMILY_STRING "unix" - #define CONF_PLATFORM_FREEBSD 1 - #define CONF_PLATFORM_STRING "freebsd" -#endif - -#if defined(__OpenBSD__) - #define CONF_FAMILY_UNIX 1 - #define CONF_FAMILY_STRING "unix" - #define CONF_PLATFORM_OPENBSD 1 - #define CONF_PLATFORM_STRING "openbsd" -#endif - -#if defined(__LINUX__) || defined(__linux__) - #define CONF_FAMILY_UNIX 1 - #define CONF_FAMILY_STRING "unix" - #define CONF_PLATFORM_LINUX 1 - #define CONF_PLATFORM_STRING "linux" -#endif - -#if defined(MACOSX) || defined(__APPLE__) || defined(__DARWIN__) - #define CONF_FAMILY_UNIX 1 - #define CONF_FAMILY_STRING "unix" - #define CONF_PLATFORM_MACOSX 1 - #define CONF_PLATFORM_STRING "macosx" -#endif - -#if defined(__sun) - #define CONF_FAMILY_UNIX 1 - #define CONF_FAMILY_STRING "unix" - #define CONF_PLATFROM_SOLARIS 1 - #define CONF_PLATFORM_STRING "solaris" -#endif - -/* beos family */ -#if defined(__BeOS) || defined(__BEOS__) - #define CONF_FAMILY_BEOS 1 - #define CONF_FAMILY_STRING "beos" - #define CONF_PLATFORM_BEOS 1 - #define CONF_PLATFORM_STRING "beos" -#endif - - -/* architectures */ -#if defined(i386) || defined(__i386__) || defined(__x86__) || defined(CONF_PLATFORM_WIN32) - #define CONF_ARCH_IA32 1 - #define CONF_ARCH_STRING "ia32" - #define CONF_ARCH_ENDIAN_LITTLE 1 -#endif - -#if defined(__ia64__) - #define CONF_ARCH_IA64 1 - #define CONF_ARCH_STRING "ia64" - #define CONF_ARCH_ENDIAN_LITTLE 1 -#endif - -#if defined(__amd64__) || defined(__x86_64__) - #define CONF_ARCH_AMD64 1 - #define CONF_ARCH_STRING "amd64" - #define CONF_ARCH_ENDIAN_LITTLE 1 -#endif - -#if defined(__powerpc__) || defined(__ppc__) - #define CONF_ARCH_PPC 1 - #define CONF_ARCH_STRING "ppc" - #define CONF_ARCH_ENDIAN_BIG 1 -#endif - -#if defined(__sparc__) - #define CONF_ARCH_SPARC 1 - #define CONF_ARCH_STRING "sparc" - #define CONF_ARCH_ENDIAN_BIG 1 -#endif - - -#ifndef CONF_FAMILY_STRING -#define CONF_FAMILY_STRING "unknown" -#endif - -#ifndef CONF_PLATFORM_STRING -#define CONF_PLATFORM_STRING "unknown" -#endif - -#ifndef CONF_ARCH_STRING -#define CONF_ARCH_STRING "unknown" -#endif - -#endif diff --git a/src/engine/e_engine.c b/src/engine/e_engine.c index f71b7b6e..3c3c1d62 100644 --- a/src/engine/e_engine.c +++ b/src/engine/e_engine.c @@ -3,7 +3,8 @@ #include #include -#include +#include + #include /*#include */ #include diff --git a/src/engine/e_huffman.c b/src/engine/e_huffman.c index f2e58f0c..dfb5c817 100644 --- a/src/engine/e_huffman.c +++ b/src/engine/e_huffman.c @@ -1,11 +1,11 @@ +#include #include #include -#include #include void huffman_init(HUFFSTATE *huff) { - mem_zero(huff, sizeof(huff)); + mem_zero(huff, sizeof(HUFFSTATE)); huff->nodes[0].frequency = 1; huff->nodes[0].symbol_size = -1; huff->num_symbols++; @@ -160,7 +160,10 @@ static void bitio_init(HUFFBITIO *bitio, unsigned char *data) static void bitio_flush(HUFFBITIO *bitio) { - *bitio->data = bitio->current_bits; + if(bitio->num == 8) + *bitio->data = bitio->current_bits << (8-bitio->num); + else + *bitio->data = bitio->current_bits; bitio->data++; bitio->num = 0; bitio->current_bits = 0; diff --git a/src/engine/e_if_other.h b/src/engine/e_if_other.h index 3d43533d..d2726a7b 100644 --- a/src/engine/e_if_other.h +++ b/src/engine/e_if_other.h @@ -6,8 +6,8 @@ Title: Engine Interface */ +#include #include "e_keys.h" -#include "e_system.h" enum { diff --git a/src/engine/e_linereader.h b/src/engine/e_linereader.h index d9d050d0..ca84960a 100644 --- a/src/engine/e_linereader.h +++ b/src/engine/e_linereader.h @@ -1,4 +1,4 @@ -#include "e_system.h" +#include /* buffered stream for reading lines, should perhaps be something smaller */ typedef struct diff --git a/src/engine/e_map.c b/src/engine/e_map.c index 4240225a..c33868d7 100644 --- a/src/engine/e_map.c +++ b/src/engine/e_map.c @@ -1,5 +1,5 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include "e_system.h" +#include #include "e_datafile.h" static DATAFILE *map = 0; diff --git a/src/engine/e_memheap.c b/src/engine/e_memheap.c index a4b9bf37..7589e2c1 100644 --- a/src/engine/e_memheap.c +++ b/src/engine/e_memheap.c @@ -1,5 +1,5 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include "e_system.h" +#include typedef struct CHUNK_t { diff --git a/src/engine/e_network.c b/src/engine/e_network.c index 2eef9006..1e320293 100644 --- a/src/engine/e_network.c +++ b/src/engine/e_network.c @@ -1,8 +1,8 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include -#include +#include + +#include /* strlen */ -#include "e_system.h" #include "e_config.h" #include "e_network.h" #include "e_huffman.h" @@ -134,9 +134,9 @@ static void rb_pop_first(RINGBUFFER *rb) mem_free(rb->first); rb->first = next; if(rb->first) - rb->first->prev = NULL; + rb->first->prev = (void*)0; else - rb->last = NULL; + rb->last = (void*)0; } } diff --git a/src/engine/e_packer.c b/src/engine/e_packer.c index 07b9bc51..aee08aed 100644 --- a/src/engine/e_packer.c +++ b/src/engine/e_packer.c @@ -1,7 +1,7 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include +#include /* rand() */ +#include -#include "e_system.h" #include "e_packer.h" #include "e_compression.h" #include "e_engine.h" diff --git a/src/engine/e_protocol.h b/src/engine/e_protocol.h index 46e1b8c2..93441fe7 100644 --- a/src/engine/e_protocol.h +++ b/src/engine/e_protocol.h @@ -1,5 +1,5 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include "e_system.h" +#include /* Connection diagram - How the initilization works. diff --git a/src/engine/e_ringbuffer.c b/src/engine/e_ringbuffer.c index cfb2926b..281c3751 100644 --- a/src/engine/e_ringbuffer.c +++ b/src/engine/e_ringbuffer.c @@ -1,4 +1,4 @@ -#include +#include enum { diff --git a/src/engine/e_snapshot.h b/src/engine/e_snapshot.h index 9527cb24..f57c95c0 100644 --- a/src/engine/e_snapshot.h +++ b/src/engine/e_snapshot.h @@ -2,7 +2,7 @@ #ifndef ENGINE_SNAPSHOT_H #define ENGINE_SNAPSHOT_H -#include "e_system.h" +#include /* SNAPSHOT */ diff --git a/src/engine/e_system.c b/src/engine/e_system.c deleted file mode 100644 index 1be7ea30..00000000 --- a/src/engine/e_system.c +++ /dev/null @@ -1,971 +0,0 @@ -/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include -#include -#include -#include -#include -#include - -#include "e_detect.h" -#include "e_system.h" -#include "e_console.h" - -#if defined(CONF_FAMILY_UNIX) - #include - #include - - /* unix net includes */ - #include - #include - #include - #include - #include - #include - #include - #include - #include - - #include - #include -#elif defined(CONF_FAMILY_WINDOWS) - #define WIN32_LEAN_AND_MEAN - #define _WIN32_WINNT 0x0501 /* required for mingw to get getaddrinfo to work */ - #include - #include - #include - #include - #include - #include - - #define EWOULDBLOCK WSAEWOULDBLOCK -#else - #error NOT IMPLEMENTED -#endif - -#if defined(__cplusplus) -extern "C" { -#endif - -IOHANDLE io_stdin() { return (IOHANDLE)stdin; } -IOHANDLE io_stdout() { return (IOHANDLE)stdout; } -IOHANDLE io_stderr() { return (IOHANDLE)stderr; } - -static DBG_LOGGER loggers[16]; -static int num_loggers = 0; - -static NETSTATS network_stats = {0}; - -void dbg_logger(DBG_LOGGER logger) -{ - loggers[num_loggers++] = logger; -} - -void dbg_assert_imp(const char *filename, int line, int test, const char *msg) -{ - if(!test) - { - dbg_msg("assert", "%s(%d): %s", filename, line, msg); - dbg_break(); - } -} - -void dbg_break() -{ - *((unsigned*)0) = 0x0; -} - -void dbg_msg(const char *sys, const char *fmt, ...) -{ - va_list args; - char str[1024*4]; - char *msg; - int i, len; - - str_format(str, sizeof(str), "[%08x][%s]: ", (int)time(0), sys); - len = strlen(str); - msg = (char *)str + len; - - va_start(args, fmt); -#if defined(CONF_FAMILY_WINDOWS) - _vsnprintf(msg, sizeof(str)-len, fmt, args); -#else - vsnprintf(msg, sizeof(str)-len, fmt, args); -#endif - va_end(args); - - for(i = 0; i < num_loggers; i++) - loggers[i](str); -} - -static void logger_stdout(const char *line) -{ - printf("%s\n", line); - fflush(stdout); -} - -static void logger_debugger(const char *line) -{ -#if defined(CONF_FAMILY_WINDOWS) - OutputDebugString(line); - OutputDebugString("\n"); -#endif -} - - -IOHANDLE logfile = 0; -static void logger_file(const char *line) -{ - io_write(logfile, line, strlen(line)); - io_write(logfile, "\n", 1); - io_flush(logfile); -} - -void dbg_logger_stdout() { dbg_logger(logger_stdout); } -void dbg_logger_debugger() { dbg_logger(logger_debugger); } -void dbg_logger_file(const char *filename) -{ - logfile = io_open(filename, IOFLAG_WRITE); - if(logfile) - dbg_logger(logger_file); - else - dbg_msg("dbg/logger", "failed to open '%s' for logging", filename); - -} - -/* */ - -int memory_alloced = 0; - -struct memheader -{ - const char *filename; - int line; - int size; - struct memheader *prev; - struct memheader *next; -}; - -struct memtail -{ - int guard; -}; - -static struct memheader *first = 0; - -int mem_allocated() -{ - return memory_alloced; -} - -void *mem_alloc_debug(const char *filename, int line, unsigned size, unsigned alignment) -{ - /* TODO: fix alignment */ - /* TODO: add debugging */ - struct memheader *header = (struct memheader *)malloc(size+sizeof(struct memheader)+sizeof(struct memtail)); - struct memtail *tail = (struct memtail *)(((char*)(header+1))+size); - header->size = size; - header->filename = filename; - header->line = line; - memory_alloced += header->size; - - tail->guard = 0xbaadc0de; - - header->prev = (struct memheader *)0; - header->next = first; - if(first) - first->prev = header; - first = header; - - /*dbg_msg("mem", "++ %p", header+1); */ - return header+1; -} - -void mem_free(void *p) -{ - if(p) - { - struct memheader *header = (struct memheader *)p - 1; - struct memtail *tail = (struct memtail *)(((char*)(header+1))+header->size); - - if(tail->guard != 0xbaadc0de) - dbg_msg("mem", "!! %p", p); - /* dbg_msg("mem", "-- %p", p); */ - memory_alloced -= header->size; - - if(header->prev) - header->prev->next = header->next; - else - first = header->next; - if(header->next) - header->next->prev = header->prev; - - free(header); - } -} - -void mem_debug_dump() -{ - char buf[1024]; - struct memheader *header = first; - IOHANDLE f = io_open("memory.txt", IOFLAG_WRITE); - - while(header) - { - str_format(buf, sizeof(buf), "%s(%d): %d\n", header->filename, header->line, header->size); - io_write(f, buf, strlen(buf)); - header = header->next; - } - - io_close(f); -} - - -void mem_copy(void *dest, const void *source, unsigned size) -{ - memcpy(dest, source, size); -} - -void mem_move(void *dest, const void *source, unsigned size) -{ - memmove(dest, source, size); -} - -void mem_zero(void *block,unsigned size) -{ - memset(block, 0, size); -} - -IOHANDLE io_open(const char *filename, int flags) -{ - if(flags == IOFLAG_READ) - return (IOHANDLE)fopen(filename, "rb"); - if(flags == IOFLAG_WRITE) - return (IOHANDLE)fopen(filename, "wb"); - return 0x0; -} - -unsigned io_read(IOHANDLE io, void *buffer, unsigned size) -{ - return fread(buffer, 1, size, (FILE*)io); -} - -unsigned io_skip(IOHANDLE io, unsigned size) -{ - fseek((FILE*)io, size, SEEK_CUR); - return size; -} - -int io_seek(IOHANDLE io, int offset, int origin) -{ - int real_origin; - - switch(origin) - { - case IOSEEK_START: - real_origin = SEEK_SET; - break; - case IOSEEK_CUR: - real_origin = SEEK_CUR; - break; - case IOSEEK_END: - real_origin = SEEK_END; - } - - return fseek((FILE*)io, offset, origin); -} - -long int io_tell(IOHANDLE io) -{ - return ftell((FILE*)io); -} - -long int io_length(IOHANDLE io) -{ - long int length; - io_seek(io, 0, IOSEEK_END); - length = io_tell(io); - io_seek(io, 0, IOSEEK_START); - return length; -} - -unsigned io_write(IOHANDLE io, const void *buffer, unsigned size) -{ - return fwrite(buffer, 1, size, (FILE*)io); -} - -int io_close(IOHANDLE io) -{ - fclose((FILE*)io); - return 1; -} - -int io_flush(IOHANDLE io) -{ - fflush((FILE*)io); - return 0; -} - -void *thread_create(void (*threadfunc)(void *), void *u) -{ -#if defined(CONF_FAMILY_UNIX) - pthread_t id; - pthread_create(&id, NULL, (void *(*)(void*))threadfunc, u); - return (void*)id; -#elif defined(CONF_FAMILY_WINDOWS) - return CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)threadfunc, u, 0, NULL); -#else - #error not implemented -#endif -} - -void thread_wait(void *thread) -{ -#if defined(CONF_FAMILY_UNIX) - pthread_join((pthread_t)thread, NULL); -#elif defined(CONF_FAMILY_WINDOWS) - WaitForSingleObject((HANDLE)thread, INFINITE); -#else - #error not implemented -#endif -} - -void thread_destroy(void *thread) -{ -#if defined(CONF_FAMILY_UNIX) - void *r = 0; - pthread_join((pthread_t)thread, &r); -#else - /*#error not implemented*/ -#endif -} - -void thread_yield() -{ -#if defined(CONF_FAMILY_UNIX) - sched_yield(); -#elif defined(CONF_FAMILY_WINDOWS) - Sleep(0); -#else - #error not implemented -#endif -} - -void thread_sleep(int milliseconds) -{ -#if defined(CONF_FAMILY_UNIX) - usleep(milliseconds*1000); -#elif defined(CONF_FAMILY_WINDOWS) - Sleep(milliseconds); -#else - #error not implemented -#endif -} - - - - -#if defined(CONF_FAMILY_UNIX) -typedef pthread_mutex_t LOCKINTERNAL; -#elif defined(CONF_FAMILY_WINDOWS) -typedef CRITICAL_SECTION LOCKINTERNAL; -#else - #error not implemented on this platform -#endif - -LOCK lock_create() -{ - LOCKINTERNAL *lock = (LOCKINTERNAL*)mem_alloc(sizeof(LOCKINTERNAL), 4); - -#if defined(CONF_FAMILY_UNIX) - pthread_mutex_init(lock, 0x0); -#elif defined(CONF_FAMILY_WINDOWS) - InitializeCriticalSection((LPCRITICAL_SECTION)lock); -#else - #error not implemented on this platform -#endif - return (LOCK)lock; -} - -void lock_destroy(LOCK lock) -{ -#if defined(CONF_FAMILY_UNIX) - pthread_mutex_destroy((LOCKINTERNAL *)lock); -#elif defined(CONF_FAMILY_WINDOWS) - DeleteCriticalSection((LPCRITICAL_SECTION)lock); -#else - #error not implemented on this platform -#endif - mem_free(lock); -} - -int lock_try(LOCK lock) -{ -#if defined(CONF_FAMILY_UNIX) - return pthread_mutex_trylock((LOCKINTERNAL *)lock); -#elif defined(CONF_FAMILY_WINDOWS) - return TryEnterCriticalSection((LPCRITICAL_SECTION)lock); -#else - #error not implemented on this platform -#endif -} - -void lock_wait(LOCK lock) -{ -#if defined(CONF_FAMILY_UNIX) - pthread_mutex_lock((LOCKINTERNAL *)lock); -#elif defined(CONF_FAMILY_WINDOWS) - EnterCriticalSection((LPCRITICAL_SECTION)lock); -#else - #error not implemented on this platform -#endif -} - -void lock_release(LOCK lock) -{ -#if defined(CONF_FAMILY_UNIX) - pthread_mutex_unlock((LOCKINTERNAL *)lock); -#elif defined(CONF_FAMILY_WINDOWS) - LeaveCriticalSection((LPCRITICAL_SECTION)lock); -#else - #error not implemented on this platform -#endif -} - -/* ----- time ----- */ -int64 time_get() -{ -#if defined(CONF_FAMILY_UNIX) - struct timeval val; - gettimeofday(&val, NULL); - return (int64)val.tv_sec*(int64)1000000+(int64)val.tv_usec; -#elif defined(CONF_FAMILY_WINDOWS) - static int64 last = 0; - int64 t; - QueryPerformanceCounter((PLARGE_INTEGER)&t); - if(tsin_family = AF_INET; - p->sin_port = htons(src->port); - p->sin_addr.s_addr = htonl(src->ip[0]<<24|src->ip[1]<<16|src->ip[2]<<8|src->ip[3]); -} - -static void sockaddr_to_netaddr(const struct sockaddr *src, NETADDR *dst) -{ - /* TODO: IPv6 support */ - unsigned int ip = htonl(((struct sockaddr_in*)src)->sin_addr.s_addr); - dst->type = NETTYPE_IPV4; - dst->port = htons(((struct sockaddr_in*)src)->sin_port); - dst->ip[0] = (unsigned char)((ip>>24)&0xFF); - dst->ip[1] = (unsigned char)((ip>>16)&0xFF); - dst->ip[2] = (unsigned char)((ip>>8)&0xFF); - dst->ip[3] = (unsigned char)(ip&0xFF); -} - -int net_addr_comp(const NETADDR *a, const NETADDR *b) -{ - return mem_comp(a, b, sizeof(NETADDR)); -} - -int net_host_lookup(const char *hostname, NETADDR *addr, int types) -{ - /* TODO: IPv6 support */ - struct addrinfo hints; - struct addrinfo *result; - int e; - - mem_zero(&hints, sizeof(hints)); - hints.ai_family = AF_INET; - - e = getaddrinfo(hostname, NULL, &hints, &result); - if(e != 0 || !result) - return -1; - - sockaddr_to_netaddr(result->ai_addr, addr); - freeaddrinfo(result); - addr->port = 0; - return 0; -} - -NETSOCKET net_udp_create(NETADDR bindaddr) -{ - /* TODO: IPv6 support */ - struct sockaddr addr; - unsigned int mode = 1; - int broadcast = 1; - - /* create socket */ - int sock = socket(AF_INET, SOCK_DGRAM, 0); - if(sock < 0) - return NETSOCKET_INVALID; - - /* bind, we should check for error */ - netaddr_to_sockaddr(&bindaddr, &addr); - if(bind(sock, &addr, sizeof(addr)) != 0) - { - net_udp_close(sock); - return NETSOCKET_INVALID; - } - - /* set non-blocking */ -#if defined(CONF_FAMILY_WINDOWS) - ioctlsocket(sock, FIONBIO, (unsigned long *)&mode); -#else - ioctl(sock, FIONBIO, (unsigned long *)&mode); -#endif - - /* set boardcast */ - setsockopt(sock, SOL_SOCKET, SO_BROADCAST, (const char*)&broadcast, sizeof(broadcast)); - - /* return */ - return sock; -} - -int net_udp_send(NETSOCKET sock, const NETADDR *addr, const void *data, int size) -{ - struct sockaddr sa; - int d; - mem_zero(&sa, sizeof(sa)); - netaddr_to_sockaddr(addr, &sa); - d = sendto((int)sock, (const char*)data, size, 0, &sa, sizeof(sa)); - if(d < 0) - dbg_msg("net", "sendto error %d %x", d, d); - network_stats.sent_bytes += size; - network_stats.sent_packets++; - return d; -} - -int net_udp_recv(NETSOCKET sock, NETADDR *addr, void *data, int maxsize) -{ - struct sockaddr from; - int bytes; - socklen_t fromlen = sizeof(struct sockaddr); - bytes = recvfrom(sock, (char*)data, maxsize, 0, &from, &fromlen); - if(bytes > 0) - { - sockaddr_to_netaddr(&from, addr); - network_stats.recv_bytes += bytes; - network_stats.recv_packets++; - return bytes; - } - else if(bytes == 0) - return 0; - return -1; /* error */ -} - -int net_udp_close(NETSOCKET sock) -{ -#if defined(CONF_FAMILY_WINDOWS) - closesocket(sock); -#else - close((int)sock); -#endif - return 0; -} - -NETSOCKET net_tcp_create(const NETADDR *a) -{ - /* TODO: IPv6 support */ - struct sockaddr addr; - - /* create socket */ - int sock = socket(AF_INET, SOCK_STREAM, 0); - if(sock < 0) - return NETSOCKET_INVALID; - - /* bind, we should check for error */ - netaddr_to_sockaddr(a, &addr); - bind(sock, &addr, sizeof(addr)); - - /* return */ - return sock; -} - -int net_tcp_set_non_blocking(NETSOCKET sock) -{ - unsigned int mode = 1; -#if defined(CONF_FAMILY_WINDOWS) - return ioctlsocket(sock, FIONBIO, (unsigned long *)&mode); -#else - return ioctl(sock, FIONBIO, (unsigned long *)&mode); -#endif -} - -int net_tcp_set_blocking(NETSOCKET sock) -{ - unsigned int mode = 0; -#if defined(CONF_FAMILY_WINDOWS) - return ioctlsocket(sock, FIONBIO, (unsigned long *)&mode); -#else - return ioctl(sock, FIONBIO, (unsigned long *)&mode); -#endif -} - -int net_tcp_listen(NETSOCKET sock, int backlog) -{ - return listen(sock, backlog); -} - -int net_tcp_accept(NETSOCKET sock, NETSOCKET *new_sock, NETADDR *a) -{ - int s; - socklen_t sockaddr_len; - struct sockaddr addr; - - sockaddr_len = sizeof(addr); - - s = accept(sock, &addr, &sockaddr_len); - - if (s != -1) - { - sockaddr_to_netaddr(&addr, a); - *new_sock = s; - } - return s; -} - -int net_tcp_connect(NETSOCKET sock, const NETADDR *a) -{ - struct sockaddr addr; - - netaddr_to_sockaddr(a, &addr); - return connect(sock, &addr, sizeof(addr)); -} - -int net_tcp_connect_non_blocking(NETSOCKET sock, const NETADDR *a) -{ - struct sockaddr addr; - int res; - - netaddr_to_sockaddr(a, &addr); - net_tcp_set_non_blocking(sock); - res = connect(sock, &addr, sizeof(addr)); - net_tcp_set_blocking(sock); - - return res; -} - -int net_tcp_send(NETSOCKET sock, const void *data, int size) -{ - int d; - d = send((int)sock, (const char*)data, size, 0); - return d; -} - -int net_tcp_recv(NETSOCKET sock, void *data, int maxsize) -{ - int bytes; - bytes = recv((int)sock, (char*)data, maxsize, 0); - return bytes; -} - -int net_tcp_close(NETSOCKET sock) -{ -#if defined(CONF_FAMILY_WINDOWS) - closesocket(sock); -#else - close((int)sock); -#endif - return 0; -} - -int net_errno() -{ - return errno; -} - -int net_would_block() -{ - return net_errno() == EWOULDBLOCK; -} - -int net_init() -{ -#if defined(CONF_FAMILY_WINDOWS) - WSADATA wsaData; - int err = WSAStartup(MAKEWORD(1, 1), &wsaData); - dbg_assert(err == 0, "network initialization failed."); - return err==0?0:1; -#endif - - return 0; -} - -int fs_listdir(const char *dir, fs_listdir_callback cb, void *user) -{ -#if defined(CONF_FAMILY_WINDOWS) - WIN32_FIND_DATA finddata; - HANDLE handle; - char buffer[1024*2]; - str_format(buffer, sizeof(buffer), "%s/*", dir); - - handle = FindFirstFileA(buffer, &finddata); - - if (handle == INVALID_HANDLE_VALUE) - return 0; - - /* add all the entries */ - do - { - if(finddata.cFileName[0] != '.') - cb(finddata.cFileName, 0, user); - } while (FindNextFileA(handle, &finddata)); - - FindClose(handle); - return 0; -#else - struct dirent *entry; - DIR *d = opendir(dir); - - if(!d) - return 0; - - while((entry = readdir(d)) != NULL) - cb(entry->d_name, 0, user); - - /* close the directory and return */ - closedir(d); - return 0; -#endif -} - -int fs_storage_path(const char *appname, char *path, int max) -{ -#if defined(CONF_FAMILY_WINDOWS) - HRESULT r; - char *home = getenv("APPDATA"); - if(!home) - return -1; - _snprintf(path, max, "%s/%s", home, appname); - return 0; -#else - char *home = getenv("HOME"); - int i; - if(!home) - return -1; - -#if defined(CONF_PLATFORM_MACOSX) - snprintf(path, max, "%s/Library/Application Support/%s", home, appname); -#else - snprintf(path, max, "%s/.%s", home, appname); - for(i = strlen(home)+2; path[i]; i++) - path[i] = tolower(path[i]); -#endif - - return 0; -#endif -} - -int fs_makedir(const char *path) -{ -#if defined(CONF_FAMILY_WINDOWS) - if(_mkdir(path) == 0) - return 0; - if(errno == EEXIST) - return 0; - return -1; -#else - if(mkdir(path, 0755) == 0) - return 0; - if(errno == EEXIST) - return 0; - return -1; -#endif -} - -void swap_endian(void *data, unsigned elem_size, unsigned num) -{ - char *src = (char*) data; - char *dst = src + (elem_size - 1); - - while(num) - { - unsigned n = elem_size>>1; - char tmp; - while(n) - { - tmp = *src; - *src = *dst; - *dst = tmp; - - src++; - dst--; - n--; - } - - src = src + (elem_size>>1); - dst = src + (elem_size - 1); - num--; - } -} - -int net_socket_read_wait(NETSOCKET sock, int time) -{ - struct timeval tv; - fd_set readfds; - - tv.tv_sec = 0; - tv.tv_usec = 1000*time; - - FD_ZERO(&readfds); - FD_SET(sock, &readfds); - - /* don't care about writefds and exceptfds */ - select(sock+1, &readfds, NULL, NULL, &tv); - if(FD_ISSET(sock, &readfds)) - return 1; - return 0; -} - -unsigned time_timestamp() -{ - return time(0); -} - -void str_append(char *dst, const char *src, int dst_size) -{ - int s = strlen(dst); - int i = 0; - while(s < dst_size) - { - dst[s] = src[i]; - if(!src[i]) /* check for null termination */ - break; - s++; - i++; - } - - dst[dst_size-1] = 0; /* assure null termination */ -} - -void str_copy(char *dst, const char *src, int dst_size) -{ - strncpy(dst, src, dst_size); - dst[dst_size-1] = 0; /* assure null termination */ -} - -void str_format(char *buffer, int buffer_size, const char *format, ...) -{ -#if defined(CONF_FAMILY_WINDOWS) - va_list ap; - va_start(ap, format); - _vsnprintf(buffer, buffer_size, format, ap); - va_end(ap); -#else - va_list ap; - va_start(ap, format); - vsnprintf(buffer, buffer_size, format, ap); - va_end(ap); -#endif - - buffer[buffer_size-1] = 0; /* assure null termination */ -} - - - -/* makes sure that the string only contains the characters between 32 and 127 */ -void str_sanitize_strong(char *str_in) -{ - unsigned char *str = (unsigned char *)str_in; - while(*str) - { - *str &= 0x7f; - if(*str < 32) - *str = 32; - str++; - } -} - -/* makes sure that the string only contains the characters between 32 and 255 + \r\n\t */ -void str_sanitize(char *str_in) -{ - unsigned char *str = (unsigned char *)str_in; - while(*str) - { - if(*str < 32 && !(*str == '\r') && !(*str == '\n') && !(*str == '\t')) - *str = ' '; - str++; - } -} - -/* case */ -int str_comp_nocase(const char *a, const char *b) -{ -#if defined(CONF_FAMILY_WINDOWS) - return _stricmp(a,b); -#else - return strcasecmp(a,b); -#endif -} - -const char *str_find_nocase(const char *haystack, const char *needle) -{ - while(*haystack) /* native implementation */ - { - const char *a = haystack; - const char *b = needle; - while(*a && *b && tolower(*a) == tolower(*b)) - { - a++; - b++; - } - if(!(*b)) - return haystack; - haystack++; - } - - return 0; -} - -void str_hex(char *dst, int dst_size, const void *data, int data_size) -{ - static const char hex[] = "0123456789ABCDEF"; - int b; - - for(b = 0; b < data_size && b < dst_size/4-4; b++) - { - dst[b*3] = hex[((const unsigned char *)data)[b]>>4]; - dst[b*3+1] = hex[((const unsigned char *)data)[b]&0xf]; - dst[b*3+2] = ' '; - dst[b*3+3] = 0; - } -} - -int mem_comp(const void *a, const void *b, int size) -{ - return memcmp(a,b,size); -} - -void net_stats(NETSTATS *stats_inout) -{ - *stats_inout = network_stats; -} - -#if defined(__cplusplus) -} -#endif diff --git a/src/engine/e_system.h b/src/engine/e_system.h deleted file mode 100644 index ac9e7cc6..00000000 --- a/src/engine/e_system.h +++ /dev/null @@ -1,881 +0,0 @@ -/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ - -/* - Title: OS Abstraction -*/ - -#ifndef BASE_SYSTEM_H -#define BASE_SYSTEM_H - -#include "e_detect.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/* Group: Debug */ -/* - Function: dbg_assert - Breaks into the debugger based on a test. - - Parameters: - test - Result of the test. - msg - Message that should be printed if the test fails. - - Remarks: - Does nothing in release version of the library. - - See Also: - -*/ -void dbg_assert(int test, const char *msg); -#define dbg_assert(test,msg) dbg_assert_imp(__FILE__, __LINE__, test, msg) -void dbg_assert_imp(const char *filename, int line, int test, const char *msg); - -/* - Function: dbg_break - Breaks into the debugger. - - Remarks: - Does nothing in release version of the library. - - See Also: - -*/ -void dbg_break(); - -/* - Function: dbg_msg - - Prints a debug message. - - Parameters: - sys - A string that describes what system the message belongs to - fmt - A printf styled format string. - - Remarks: - Does nothing in relase version of the library. - - See Also: - -*/ -void dbg_msg(const char *sys, const char *fmt, ...); - -/* Group: Memory */ - -/* - Function: mem_alloc - Allocates memory. - - Parameters: - size - Size of the needed block. - alignment - Alignment for the block. - - Returns: - Returns a pointer to the newly allocated block. Returns a - null pointer if the memory couldn't be allocated. - - Remarks: - - Passing 0 to size will allocated the smallest amount possible - and return a unique pointer. - - See Also: - -*/ -void *mem_alloc_debug(const char *filename, int line, unsigned size, unsigned alignment); -#define mem_alloc(s,a) mem_alloc_debug(__FILE__, __LINE__, (s), (a)) - -/* - Function: mem_free - Frees a block allocated through . - - Remarks: - - In the debug version of the library the function will assert if - a non-valid block is passed, like a null pointer or a block that - isn't allocated. - - See Also: - -*/ -void mem_free(void *block); - -/* - Function: mem_copy - Copies a a memory block. - - Parameters: - dest - Destination. - source - Source to copy. - size - Size of the block to copy. - - Remarks: - - This functions DOES NOT handles cases where source and - destination is overlapping. - - See Also: - -*/ -void mem_copy(void *dest, const void *source, unsigned size); - -/* - Function: mem_move - Copies a a memory block - - Parameters: - dest - Destination - source - Source to copy - size - Size of the block to copy - - Remarks: - - This functions handles cases where source and destination - is overlapping - - See Also: - -*/ -void mem_move(void *dest, const void *source, unsigned size); - -/* - Function: mem_zero - Sets a complete memory block to 0 - - Parameters: - block - Pointer to the block to zero out - size - Size of the block -*/ -void mem_zero(void *block, unsigned size); - -/* - Function: mem_comp - Compares two blocks of memory - - Parameters: - a - First block of data - b - Second block of data - size - Size of the data to compare - - Returns: - <0 - Block a is lesser then block b - 0 - Block a is equal to block b - >0 - Block a is greater then block b -*/ -int mem_comp(const void *a, const void *b, int size); - -/* Group: File IO */ -enum { - IOFLAG_READ = 1, - IOFLAG_WRITE = 2, - IOFLAG_RANDOM = 4, - - IOSEEK_START = 0, - IOSEEK_CUR = 1, - IOSEEK_END = 2 -}; - -typedef struct IOINTERNAL *IOHANDLE; - -/* - Function: io_open - Opens a file. - - Parameters: - filename - File to open. - flags - A set of flags. IOFLAG_READ, IOFLAG_WRITE, IOFLAG_RANDOM. - - Returns: - Returns a handle to the file on success and 0 on failure. - -*/ -IOHANDLE io_open(const char *filename, int flags); - -/* - Function: io_read - Reads data into a buffer from a file. - - Parameters: - io - Handle to the file to read data from. - buffer - Pointer to the buffer that will recive the data. - size - Number of bytes to read from the file. - - Returns: - Number of bytes read. - -*/ -unsigned io_read(IOHANDLE io, void *buffer, unsigned size); - -/* - Function: io_skip - Skips data in a file. - - Parameters: - io - Handle to the file. - size - Number of bytes to skip. - - Returns: - Number of bytes skipped. -*/ -unsigned io_skip(IOHANDLE io, unsigned size); - -/* - Function: io_write - Writes data from a buffer to file. - - Parameters: - io - Handle to the file. - buffer - Pointer to the data that should be written. - size - Number of bytes to write. - - Returns: - Number of bytes written. -*/ -unsigned io_write(IOHANDLE io, const void *buffer, unsigned size); - -/* - Function: io_seek - Seeks to a specified offset in the file. - - Parameters: - io - Handle to the file. - offset - Offset from pos to stop. - origin - Position to start searching from. - - Returns: - Returns 0 on success. -*/ -int io_seek(IOHANDLE io, int offset, int origin); - -/* - Function: io_tell - Gets the current position in the file. - - Parameters: - io - Handle to the file. - - Returns: - Returns the current position. -1L if an error occured. -*/ -long int io_tell(IOHANDLE io); - -/* - Function: io_length - Gets the total length of the file. Resetting cursor to the beginning - - Parameters: - io - Handle to the file. - - Returns: - Returns the total size. -1L if an error occured. -*/ -long int io_length(IOHANDLE io); - -/* - Function: io_close - Closes a file. - - Parameters: - io - Handle to the file. - - Returns: - Returns 0 on success. -*/ -int io_close(IOHANDLE io); - -/* - Function: io_flush - Empties all buffers and writes all pending data. - - Parameters: - io - Handle to the file. - - Returns: - Returns 0 on success. -*/ -int io_flush(IOHANDLE io); - - -/* - Function: io_stdin - Returns an to the standard input. -*/ -IOHANDLE io_stdin(); - -/* - Function: io_stdout - Returns an to the standard output. -*/ -IOHANDLE io_stdout(); - -/* - Function: io_stderr - Returns an to the standard error. -*/ -IOHANDLE io_stderr(); - - -/* Group: Threads */ - -/* - Function: thread_sleep - Suspends the current thread for a given period. - - Parameters: - milliseconds - Number of milliseconds to sleep. -*/ -void thread_sleep(int milliseconds); - -/* - Function: thread_create - Creates a new thread. - - Parameters: - threadfunc - Entry point for the new thread. - user - Pointer to pass to the thread. - -*/ -void *thread_create(void (*threadfunc)(void *), void *user); - -/* - Function: thread_wait - Waits for a thread to be done or destroyed. - - Parameters: - thread - Thread to wait for. -*/ -void thread_wait(void *thread); - -/* - Function: thread_destoy - Destroys a thread. - - Parameters: - thread - Thread to destroy. -*/ -void thread_destroy(void *thread); - -/* - Function: thread_yeild - Yeild the current threads execution slice. -*/ -void thread_yield(); - - -/* Group: Locks */ -typedef void* LOCK; - -LOCK lock_create(); -void lock_destroy(LOCK lock); - -int lock_try(LOCK lock); -void lock_wait(LOCK lock); -void lock_release(LOCK lock); - -/* Group: Timer */ -#ifdef __GNUC__ -/* if compiled with -pedantic-errors it will complain about long - not being a C90 thing. -*/ -__extension__ typedef long long int64; -#else -typedef long long int64; -#endif -/* - Function: time_get - Fetches a sample from a high resolution timer. - - Returns: - Current value of the timer. - - Remarks: - To know how fast the timer is ticking, see . -*/ -int64 time_get(); - -/* - Function: time_freq - Returns the frequency of the high resolution timer. - - Returns: - Returns the frequency of the high resolution timer. -*/ -int64 time_freq(); - -/* - Function: time_timestamp - Retrives the current time as a UNIX timestamp - - Returns: - The time as a UNIX timestamp -*/ -unsigned time_timestamp(); - -/* Group: Network General */ -typedef int NETSOCKET; -enum -{ - NETSOCKET_INVALID = -1, - - NETTYPE_INVALID = 0, - NETTYPE_IPV4 = 1, - NETTYPE_IPV6 = 2, - NETTYPE_ALL = ~0 -}; - -typedef struct -{ - unsigned int type; - unsigned char ip[16]; - unsigned short port; -} NETADDR; - -/* - Function: net_init - Initiates network functionallity. - - Returns: - Returns 0 on success, - - Remarks: - You must call this function before using any other network - functions. -*/ -int net_init(); - -/* - Function: net_host_lookup - Does a hostname lookup by name and fills out the passed - NETADDR struct with the recieved details. - - Returns: - 0 on success. -*/ -int net_host_lookup(const char *hostname, NETADDR *addr, int types); - -/* - Function: net_addr_comp - Compares two network addresses. - - Parameters: - a - Address to compare - b - Address to compare to. - - Returns: - <0 - Address a is lesser then address b - 0 - Address a is equal to address b - >0 - Address a is greater then address b -*/ -int net_addr_comp(const NETADDR *a, const NETADDR *b); - -/* - Function: net_addr_str - Turns a network address into a representive string. - - Parameters: - addr - Address to turn into a string. - string - Buffer to fill with the string. - max_length - Maximum size of the string. - - Remarks: - - The string will always be zero terminated - -*/ -int net_addr_str(const NETADDR *addr, char *string, int max_length); - -/* Group: Network UDP */ - -/* - Function: net_udp_create - Creates a UDP socket and binds it to a port. - - Parameters: - bindaddr - Address to bind the socket to. - - Returns: - On success it returns an handle to the socket. On failure it - returns NETSOCKET_INVALID. -*/ -NETSOCKET net_udp_create(NETADDR bindaddr); - -/* - Function: net_udp_send - Sends a packet over an UDP socket. - - Parameters: - sock - Socket to use. - addr - Where to send the packet. - data - Pointer to the packet data to send. - size - Size of the packet. - - Returns: - On success it returns the number of bytes sent. Returns -1 - on error. -*/ -int net_udp_send(NETSOCKET sock, const NETADDR *addr, const void *data, int size); - -/* - Function: net_udp_recv - Recives a packet over an UDP socket. - - Parameters: - sock - Socket to use. - addr - Pointer to an NETADDR that will recive the address. - data - Pointer to a buffer that will recive the data. - maxsize - Maximum size to recive. - - Returns: - On success it returns the number of bytes recived. Returns -1 - on error. -*/ -int net_udp_recv(NETSOCKET sock, NETADDR *addr, void *data, int maxsize); - -/* - Function: net_udp_close - Closes an UDP socket. - - Parameters: - sock - Socket to close. - - Returns: - Returns 0 on success. -1 on error. -*/ -int net_udp_close(NETSOCKET sock); - - -/* Group: Network TCP */ - -/* - Function: net_tcp_create - Creates a TCP socket. - - Parameters: - bindaddr - Address to bind the socket to. - - Returns: - On success it returns an handle to the socket. On failure it returns NETSOCKET_INVALID. -*/ -NETSOCKET net_tcp_create(const NETADDR *a); - -/* - Function: net_tcp_listen - Makes the socket start listening for new connections. - - Parameters: - sock - Socket to start listen to. - backlog - Size of the queue of incomming connections to keep. - - Returns: - Returns 0 on success. -*/ -int net_tcp_listen(NETSOCKET sock, int backlog); - -/* - Function: net_tcp_accept - Polls a listning socket for a new connection. - - Parameters: - sock - Listning socket to poll. - new_sock - Pointer to a socket to fill in with the new socket. - addr - Pointer to an address that will be filled in the remote address (optional, can be NULL). - - Returns: - Returns a non-negative integer on success. Negative integer on failure. -*/ -int net_tcp_accept(NETSOCKET sock, NETSOCKET *new_sock, NETADDR *addr); - -/* - Function: net_tcp_connect - Connects one socket to another. - - Parameters: - sock - Socket to connect. - addr - Address to connect to. - - Returns: - Returns 0 on success. - -*/ -int net_tcp_connect(NETSOCKET sock, const NETADDR *addr); - -/* - Function: net_tcp_send - Sends data to a TCP stream. - - Parameters: - sock - Socket to send data to. - data - Pointer to the data to send. - size - Size of the data to send. - - Returns: - Number of bytes sent. Negative value on failure. -*/ -int net_tcp_send(NETSOCKET sock, const void *data, int size); - -/* - Function: net_tcp_recv - Recvives data from a TCP stream. - - Parameters: - sock - Socket to recvive data from. - data - Pointer to a buffer to write the data to - max_size - Maximum of data to write to the buffer. - - Returns: - Number of bytes recvived. Negative value on failure. -*/ -int net_tcp_recv(NETSOCKET sock, void *data, int maxsize); - -/* - Function: net_tcp_close - Closes a TCP socket. - - Parameters: - sock - Socket to close. - - Returns: - Returns 0 on success. Negative value on failure. -*/ -int net_tcp_close(NETSOCKET sock); - -/* Group: Strings */ - -/* - Function: str_append - Appends a string to another. - - Parameters: - dst - Pointer to a buffer that contains a string. - src - String to append. - dst_size - Size of the buffer of the dst string. - - Remarks: - - The strings are treated as zero-termineted strings. - - Garantees that dst string will contain zero-termination. -*/ -void str_append(char *dst, const char *src, int dst_size); - -/* - Function: str_copy - Copies a string to another. - - Parameters: - dst - Pointer to a buffer that shall recive the string. - src - String to be copied. - dst_size - Size of the buffer dst. - - Remarks: - - The strings are treated as zero-termineted strings. - - Garantees that dst string will contain zero-termination. -*/ -void str_copy(char *dst, const char *src, int dst_size); - -/* - Function: str_format - Performs printf formating into a buffer. - - Parameters: - buffer - Pointer to the buffer to recive the formated string. - buffer_size - Size of the buffer. - format - printf formating string. - ... - Parameters for the formating. - - Remarks: - - See the C manual for syntax for the printf formating string. - - The strings are treated as zero-termineted strings. - - Garantees that dst string will contain zero-termination. -*/ -void str_format(char *buffer, int buffer_size, const char *format, ...); - -/* - Function: str_sanitize_strong - Replaces all characters below 32 and above 127 with whitespace. - - Parameters: - str - String to sanitize. - - Remarks: - - The strings are treated as zero-termineted strings. -*/ -void str_sanitize_strong(char *str); - -/* - Function: str_sanitize - Replaces all characters below 32 and above 127 with whitespace with - exception to \r, \n and \r. - - Parameters: - str - String to sanitize. - - Remarks: - - The strings are treated as zero-termineted strings. -*/ -void str_sanitize(char *str); - -/* - Function: str_comp_nocase - Compares to strings case insensitive. - - Parameters: - a - String to compare. - b - String to compare. - - Returns: - <0 - String a is lesser then string b - 0 - String a is equal to string b - >0 - String a is greater then string b - - Remarks: - - Only garanted to work with a-z/A-Z. - - The strings are treated as zero-termineted strings. -*/ -int str_comp_nocase(const char *a, const char *b); - -/* - Function: str_find_nocase - Finds a string inside another string case insensitive. - - Parameters: - haystack - String to search in - needle - String to search for - - Returns: - A pointer into haystack where the needle was found. - Returns NULL of needle could not be found. - - Remarks: - - Only garanted to work with a-z/A-Z. - - The strings are treated as zero-termineted strings. -*/ -const char *str_find_nocase(const char *haystack, const char *needle); - - -/* - Function: str_hex - Takes a datablock and generates a hexstring of it. - - Parameters: - dst - Buffer to fill with hex data - dst_size - size of the buffer - data - Data to turn into hex - data - Size of the data - - Remarks: - - The desination buffer will be zero-terminated -*/ -void str_hex(char *dst, int dst_size, const void *data, int data_size); - -/* Group: Filesystem */ - -/* - Function: fs_listdir - Lists the files in a directory - - Parameters: - dir - Directory to list - cb - Callback function to call for each entry - user - Pointer to give to the callback - - Returns: - Always returns 0. -*/ -typedef void (*fs_listdir_callback)(const char *name, int is_dir, void *user); -int fs_listdir(const char *dir, fs_listdir_callback cb, void *user); - -/* - Function: fs_makedir - Creates a directory - - Parameters: - path - Directory to create - - Returns: - Returns 0 on success. Negative value on failure. - - Remarks: - Does not create several directories if needed. "a/b/c" will result - in a failure if b or a does not exist. -*/ -int fs_makedir(const char *path); - -/* - Function: fs_storage_path - Fetches per user configuration directory. - - Returns: - Returns 0 on success. Negative value on failure. - - Remarks: - - Returns ~/.appname on UNIX based systems - - Returns ~/Library/Applications Support/appname on Mac OS X - - Returns %APPDATA%/Appname on Windows based systems -*/ -int fs_storage_path(const char *appname, char *path, int max); - - -/* - Group: Undocumented -*/ - - -/* - Function: net_tcp_connect_non_blocking - - DOCTODO: serp -*/ -int net_tcp_connect_non_blocking(NETSOCKET sock, const NETADDR *a); - -/* - Function: net_tcp_set_non_blocking - - DOCTODO: serp -*/ -int net_tcp_set_non_blocking(NETSOCKET sock); - -/* - Function: net_tcp_set_non_blocking - - DOCTODO: serp -*/ -int net_tcp_set_blocking(NETSOCKET sock); - -/* - Function: net_errno - - DOCTODO: serp -*/ -int net_errno(); - -/* - Function: net_would_block - - DOCTODO: serp -*/ -int net_would_block(); - -int net_socket_read_wait(NETSOCKET sock, int time); - -void mem_debug_dump(); -int mem_allocated(); - -void swap_endian(void *data, unsigned elem_size, unsigned num); - -typedef void (*DBG_LOGGER)(const char *line); -void dbg_logger(DBG_LOGGER logger); -void dbg_logger_stdout(); -void dbg_logger_debugger(); -void dbg_logger_file(const char *filename); - -typedef struct -{ - int sent_packets; - int sent_bytes; - int recv_packets; - int recv_bytes; -} NETSTATS; - - -void net_stats(NETSTATS *stats); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/engine/external/glfw/lib/x11/x11_window.c b/src/engine/external/glfw/lib/x11/x11_window.c index 056991b4..ed4c42f5 100644 --- a/src/engine/external/glfw/lib/x11/x11_window.c +++ b/src/engine/external/glfw/lib/x11/x11_window.c @@ -1521,7 +1521,7 @@ void _glfwPlatformPollEvents( void ) // or without XSync, but when the GL window is rendered over a slow // network I have noticed bad event syncronisation problems when XSync // is not used, so I decided to use it. - XSync( _glfwLibrary.Dpy, False ); + //XSync( _glfwLibrary.Dpy, False ); // Empty the window event queue while( XPending( _glfwLibrary.Dpy ) ) diff --git a/src/engine/server/es_register.c b/src/engine/server/es_register.c index a85900aa..a51b66b3 100644 --- a/src/engine/server/es_register.c +++ b/src/engine/server/es_register.c @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include diff --git a/src/engine/server/es_server.c b/src/engine/server/es_server.c index 72da4f5d..7ed8712b 100644 --- a/src/engine/server/es_server.c +++ b/src/engine/server/es_server.c @@ -3,7 +3,8 @@ #include #include -#include +#include + #include #include #include diff --git a/src/game/client/gc_client.cpp b/src/game/client/gc_client.cpp index f0195a47..2be38637 100644 --- a/src/game/client/gc_client.cpp +++ b/src/game/client/gc_client.cpp @@ -1,5 +1,6 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include +#include + #include #include #include @@ -13,7 +14,6 @@ extern "C" { #include "../g_game.hpp" #include "../g_version.hpp" #include "../g_layers.hpp" -#include "../g_math.hpp" #include "gc_map_image.hpp" #include "../generated/gc_data.hpp" #include "gc_menu.hpp" diff --git a/src/game/client/gc_client.hpp b/src/game/client/gc_client.hpp index f19766bf..4634aa58 100644 --- a/src/game/client/gc_client.hpp +++ b/src/game/client/gc_client.hpp @@ -1,4 +1,5 @@ -#include +#include + #include #include diff --git a/src/game/client/gc_console.cpp b/src/game/client/gc_console.cpp index 45666a01..d1e517de 100644 --- a/src/game/client/gc_console.cpp +++ b/src/game/client/gc_console.cpp @@ -1,8 +1,9 @@ #include "gc_console.hpp" #include "../generated/gc_data.hpp" +#include + extern "C" { - #include #include #include #include diff --git a/src/game/client/gc_map_image.cpp b/src/game/client/gc_map_image.cpp index 63a57c70..b48b6b1d 100644 --- a/src/game/client/gc_map_image.cpp +++ b/src/game/client/gc_map_image.cpp @@ -1,6 +1,7 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ +#include + #include -#include #include #include #include "gc_map_image.hpp" diff --git a/src/game/client/gc_menu.cpp b/src/game/client/gc_menu.cpp index 3a1e9cee..c867720b 100644 --- a/src/game/client/gc_menu.cpp +++ b/src/game/client/gc_menu.cpp @@ -4,11 +4,12 @@ #include #include -#include -#include +#include +#include +#include + extern "C" { - #include #include #include #include diff --git a/src/game/client/gc_render.cpp b/src/game/client/gc_render.cpp index afb827fb..5a2af697 100644 --- a/src/game/client/gc_render.cpp +++ b/src/game/client/gc_render.cpp @@ -1,10 +1,12 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ #include + +#include + #include #include #include #include -#include #include #include "gc_render.hpp" #include "gc_anim.hpp" diff --git a/src/game/client/gc_render.hpp b/src/game/client/gc_render.hpp index d819c194..faaff094 100644 --- a/src/game/client/gc_render.hpp +++ b/src/game/client/gc_render.hpp @@ -2,7 +2,8 @@ #ifndef GAME_CLIENT_RENDER_H #define GAME_CLIENT_RENDER_H -#include "../g_vmath.hpp" +#include + #include "../g_mapitems.hpp" #include "gc_ui.hpp" diff --git a/src/game/client/gc_render_map.cpp b/src/game/client/gc_render_map.cpp index 90579aa7..bbd9f7b1 100644 --- a/src/game/client/gc_render_map.cpp +++ b/src/game/client/gc_render_map.cpp @@ -1,6 +1,6 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ +#include #include -#include "../g_math.hpp" #include "gc_client.hpp" void render_eval_envelope(ENVPOINT *points, int num_points, int channels, float time, float *result) diff --git a/src/game/client/gc_render_obj.cpp b/src/game/client/gc_render_obj.cpp index daba5ceb..aa64a127 100644 --- a/src/game/client/gc_render_obj.cpp +++ b/src/game/client/gc_render_obj.cpp @@ -1,11 +1,13 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ #include #include + +#include + #include #include #include #include -#include "../g_math.hpp" #include "gc_render.hpp" #include "gc_anim.hpp" #include "gc_client.hpp" diff --git a/src/game/client/gc_skin.cpp b/src/game/client/gc_skin.cpp index 2bd2d4ff..e0aa2c84 100644 --- a/src/game/client/gc_skin.cpp +++ b/src/game/client/gc_skin.cpp @@ -2,10 +2,12 @@ #include #include #include -#include + +#include +#include + #include #include "gc_skin.hpp" -#include "../g_math.hpp" enum { diff --git a/src/game/client/gc_skin.hpp b/src/game/client/gc_skin.hpp index 3a746589..786f6768 100644 --- a/src/game/client/gc_skin.hpp +++ b/src/game/client/gc_skin.hpp @@ -1,5 +1,5 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include "../g_vmath.hpp" +#include // do this better and nicer typedef struct diff --git a/src/game/client/gc_ui.cpp b/src/game/client/gc_ui.cpp index a344d661..e873cc86 100644 --- a/src/game/client/gc_ui.cpp +++ b/src/game/client/gc_ui.cpp @@ -1,5 +1,6 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include +#include + #include #include #include "gc_ui.hpp" diff --git a/src/game/editor/ed_editor.cpp b/src/game/editor/ed_editor.cpp index 731d4f4a..429c88db 100644 --- a/src/game/editor/ed_editor.cpp +++ b/src/game/editor/ed_editor.cpp @@ -1,11 +1,12 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ +#include + #include #include #include extern "C" { - #include #include #include #include diff --git a/src/game/editor/ed_editor.hpp b/src/game/editor/ed_editor.hpp index dbca4a0e..a8e8815b 100644 --- a/src/game/editor/ed_editor.hpp +++ b/src/game/editor/ed_editor.hpp @@ -1,14 +1,15 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ +#include +#include + #include #include #include "array.hpp" #include "../g_mapitems.hpp" -#include "../g_math.hpp" #include "../client/gc_render.hpp" extern "C" { - #include #include #include #include diff --git a/src/game/editor/ed_layer_quads.cpp b/src/game/editor/ed_layer_quads.cpp index 8c8a47a7..a6e49248 100644 --- a/src/game/editor/ed_layer_quads.cpp +++ b/src/game/editor/ed_layer_quads.cpp @@ -1,5 +1,6 @@ +#include + #include "ed_editor.hpp" -#include #include #include diff --git a/src/game/editor/ed_layer_tiles.cpp b/src/game/editor/ed_layer_tiles.cpp index d77f0970..cae47acb 100644 --- a/src/game/editor/ed_layer_tiles.cpp +++ b/src/game/editor/ed_layer_tiles.cpp @@ -1,4 +1,5 @@ -#include +#include + #include #include #include "ed_editor.hpp" diff --git a/src/game/g_collision.cpp b/src/game/g_collision.cpp index b91c0e29..510cfba6 100644 --- a/src/game/g_collision.cpp +++ b/src/game/g_collision.cpp @@ -1,9 +1,10 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include -#include +#include +#include +#include + #include #include -#include #include #include diff --git a/src/game/g_collision.hpp b/src/game/g_collision.hpp index e42764c1..735e26c5 100644 --- a/src/game/g_collision.hpp +++ b/src/game/g_collision.hpp @@ -2,8 +2,7 @@ #ifndef GAME_MAPRES_COL_H #define GAME_MAPRES_COL_H - -#include +#include int col_init(); int col_is_solid(int x, int y); diff --git a/src/game/g_game.hpp b/src/game/g_game.hpp index 61342d76..29c948b9 100644 --- a/src/game/g_game.hpp +++ b/src/game/g_game.hpp @@ -2,10 +2,11 @@ #ifndef GAME_GAME_H #define GAME_GAME_H -#include +#include +#include + #include #include -#include "g_math.hpp" #include "g_collision.hpp" #include diff --git a/src/game/g_math.hpp b/src/game/g_math.hpp deleted file mode 100644 index 5e3f7ede..00000000 --- a/src/game/g_math.hpp +++ /dev/null @@ -1,61 +0,0 @@ -/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#ifndef BASE_MATH_H -#define BASE_MATH_H - -#include - -template -inline T clamp(T val, T min, T max) -{ - if(val < min) - return min; - if(val > max) - return max; - return val; -} - -inline float sign(float f) -{ - return f<0.0f?-1.0f:1.0f; -} - -template -inline T mix(const T a, const T b, TB amount) -{ - return a + (b-a)*amount; -} - -inline float frandom() { return rand()/(float)(RAND_MAX); } - -// float to fixed -inline int f2fx(float v) { return (int)(v*(float)(1<<10)); } -inline float fx2f(int v) { return v*(1.0f/(1<<10)); } - -class fxp -{ - int value; -public: - void set(int v) { value = v; } - int get() const { return value; } - fxp &operator = (int v) { value = v<<10; return *this; } - fxp &operator = (float v) { value = (int)(v*(float)(1<<10)); return *this; } - operator float() const { return value/(float)(1<<10); } -}; - -class tune_param -{ - int value; -public: - void set(int v) { value = v; } - int get() const { return value; } - tune_param &operator = (int v) { value = (int)(v*100.0f); return *this; } - tune_param &operator = (float v) { value = (int)(v*100.0f); return *this; } - operator float() const { return value/100.0f; } -}; - -const float pi = 3.1415926535897932384626433f; - -template inline T min(T a, T b) { return a inline T max(T a, T b) { return a>b?a:b; } - -#endif // BASE_MATH_H diff --git a/src/game/g_vmath.hpp b/src/game/g_vmath.hpp deleted file mode 100644 index ac3e1870..00000000 --- a/src/game/g_vmath.hpp +++ /dev/null @@ -1,198 +0,0 @@ -/* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#ifndef BASE_VMATH_H -#define BASE_VMATH_H - -//#include - -// ------------------------------------ - -template -class vector2_base -{ -public: - union { T x,u; }; - union { T y,v; }; - - vector2_base() {} - vector2_base(float nx, float ny) - { - x = nx; - y = ny; - } - - vector2_base operator -() const { return vector2_base(-x, -y); } - vector2_base operator -(const vector2_base &v) const { return vector2_base(x-v.x, y-v.y); } - vector2_base operator +(const vector2_base &v) const { return vector2_base(x+v.x, y+v.y); } - vector2_base operator *(const T v) const { return vector2_base(x*v, y*v); } - - const vector2_base &operator =(const vector2_base &v) { x = v.x; y = v.y; return *this; } - - const vector2_base &operator +=(const vector2_base &v) { x += v.x; y += v.y; return *this; } - const vector2_base &operator -=(const vector2_base &v) { x -= v.x; y -= v.y; return *this; } - const vector2_base &operator *=(const T v) { x *= v; y *= v; return *this; } - - bool operator ==(const vector2_base &v) const { return x == v.x && y == v.y; } //TODO: do this with an eps instead - - operator const T* () { return &x; } -}; - - -template -inline T length(const vector2_base &a) -{ - return sqrtf(a.x*a.x + a.y*a.y); -} - -template -inline T distance(const vector2_base a, const vector2_base &b) -{ - return length(a-b); -} - -template -inline T dot(const vector2_base a, const vector2_base &b) -{ - return a.x*b.x + a.y*b.y; -} - -template -inline vector2_base normalize(const vector2_base &v) -{ - T l = (T)(1.0f/sqrtf(v.x*v.x + v.y*v.y)); - return vector2_base(v.x*l, v.y*l); -} - -typedef vector2_base vec2; -typedef vector2_base bvec2; -typedef vector2_base ivec2; - -template -inline vector2_base closest_point_on_line(vector2_base line_point0, vector2_base line_point1, vector2_base target_point) -{ - vector2_base c = target_point - line_point0; - vector2_base v = (line_point1 - line_point0); - v = normalize(v); - T d = length(line_point0-line_point1); - T t = dot(v, c)/d; - return mix(line_point0, line_point1, clamp(t, (T)0, (T)1)); - /* - if (t < 0) t = 0; - if (t > 1.0f) return 1.0f; - return t;*/ -} - -// ------------------------------------ -template -class vector3_base -{ -public: - union { T x,r,h; }; - union { T y,g,s; }; - union { T z,b,v,l; }; - - vector3_base() {} - vector3_base(float nx, float ny, float nz) - { - x = nx; - y = ny; - z = nz; - } - - const vector3_base &operator =(const vector3_base &v) { x = v.x; y = v.y; z = v.z; return *this; } - - vector3_base operator -(const vector3_base &v) const { return vector3_base(x-v.x, y-v.y, z-v.z); } - vector3_base operator -() const { return vector3_base(-x, -y, -z); } - vector3_base operator +(const vector3_base &v) const { return vector3_base(x+v.x, y+v.y, z+v.z); } - vector3_base operator *(const T v) const { return vector3_base(x*v, y*v, z*v); } - vector3_base operator *(const vector3_base &v) const { return vector3_base(x*v.x, y*v.y, z*v.z); } - vector3_base operator /(const T v) const { return vector3_base(x/v, y/v, z/v); } - - const vector3_base &operator +=(const vector3_base &v) { x += v.x; y += v.y; z += v.z; return *this; } - const vector3_base &operator -=(const vector3_base &v) { x -= v.x; y -= v.y; z -= v.z; return *this; } - const vector3_base &operator *=(const T v) { x *= v; y *= v; z *= v; return *this; } - - bool operator ==(const vector3_base &v) const { return x == v.x && y == v.y && z == v.z; } //TODO: do this with an eps instead - - operator const T* () { return &x; } -}; - -template -inline T length(const vector3_base &a) -{ - return sqrtf(a.x*a.x + a.y*a.y + a.z*a.z); -} - -template -inline T distance(const vector3_base &a, const vector3_base &b) -{ - return length(a-b); -} - -template -inline T dot(const vector3_base &a, const vector3_base &b) -{ - return a.x*b.x + a.y*b.y + a.z*b.z; -} - -template -inline vector3_base normalize(const vector3_base &v) -{ - T l = (T)(1.0f/sqrtf(v.x*v.x + v.y*v.y + v.z*v.z)); - return vector3_base(v.x*l, v.y*l, v.z*l); -} - -template -inline vector3_base cross(const vector3_base &a, const vector3_base &b) -{ - return vector3_base( - a.y*b.z - a.z*b.y, - a.z*b.x - a.x*b.z, - a.x*b.y - a.y*b.x); -} - -typedef vector3_base vec3; -typedef vector3_base bvec3; -typedef vector3_base ivec3; - -// ------------------------------------ - -template -class vector4_base -{ -public: - union { T x,r; }; - union { T y,g; }; - union { T z,b; }; - union { T w,a; }; - - vector4_base() {} - vector4_base(float nx, float ny, float nz, float nw) - { - x = nx; - y = ny; - z = nz; - w = nw; - } - - vector4_base operator +(const vector4_base &v) const { return vector4_base(x+v.x, y+v.y, z+v.z, w+v.w); } - vector4_base operator -(const vector4_base &v) const { return vector4_base(x-v.x, y-v.y, z-v.z, w-v.w); } - vector4_base operator -() const { return vector4_base(-x, -y, -z, -w); } - vector4_base operator *(const vector4_base &v) const { return vector4_base(x*v.x, y*v.y, z*v.z, w*v.w); } - vector4_base operator *(const T v) const { return vector4_base(x*v, y*v, z*v, w*v); } - - const vector4_base &operator =(const vector4_base &v) { x = v.x; y = v.y; z = v.z; w = v.w; return *this; } - - const vector4_base &operator +=(const vector4_base &v) { x += v.x; y += v.y; z += v.z; w += v.w; return *this; } - const vector4_base &operator -=(const vector4_base &v) { x -= v.x; y -= v.y; z -= v.z; w -= v.w; return *this; } - const vector4_base &operator *=(const T v) { x *= v; y *= v; z *= v; w *= v; return *this; } - - bool operator ==(const vector4_base &v) const { return x == v.x && y == v.y && z == v.z && w == v.w; } //TODO: do this with an eps instead - - operator const T* () { return &x; } -}; - -typedef vector4_base vec4; -typedef vector4_base bvec4; -typedef vector4_base ivec4; - -#endif diff --git a/src/game/server/gs_server.cpp b/src/game/server/gs_server.cpp index a4176003..5af6758b 100644 --- a/src/game/server/gs_server.cpp +++ b/src/game/server/gs_server.cpp @@ -2,12 +2,14 @@ #include #include #include + +#include + #include #include #include #include #include -#include #include "gs_common.hpp" #include "gs_game_ctf.hpp" #include "gs_game_tdm.hpp" diff --git a/src/mastersrv/mastersrv.cpp b/src/mastersrv/mastersrv.cpp index c27bb03c..95d34b20 100644 --- a/src/mastersrv/mastersrv.cpp +++ b/src/mastersrv/mastersrv.cpp @@ -1,8 +1,8 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ #include +#include extern "C" { - #include #include } diff --git a/src/tools/crapnet.cpp b/src/tools/crapnet.cpp index f807bb61..cae2b7eb 100644 --- a/src/tools/crapnet.cpp +++ b/src/tools/crapnet.cpp @@ -1,5 +1,5 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include +#include #include diff --git a/src/tools/fake_server.c b/src/tools/fake_server.c index 763caa73..602617ad 100644 --- a/src/tools/fake_server.c +++ b/src/tools/fake_server.c @@ -1,6 +1,6 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ +#include #include -#include #include #include #include diff --git a/src/tools/packetgen.c b/src/tools/packetgen.c index 9e46c44d..0911c749 100644 --- a/src/tools/packetgen.c +++ b/src/tools/packetgen.c @@ -1,5 +1,5 @@ /* copyright (c) 2007 magnus auvinen, see licence.txt for more info */ -#include +#include enum { NUM_SOCKETS = 64 }; -- cgit 1.4.1