2006-04-13 20:47:06 +00:00
|
|
|
// Compatibility declarations for things which might not be present in
|
|
|
|
// certain build environments. It also levels the playing field caused
|
|
|
|
// by different platforms.
|
|
|
|
|
2014-11-22 12:32:56 +00:00
|
|
|
#ifndef compat_h_
|
|
|
|
#define compat_h_
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-01-25 10:56:13 +00:00
|
|
|
#include "xs_Float.h"
|
2020-04-11 21:46:42 +00:00
|
|
|
#include "m_alloc.h"
|
2020-06-20 16:17:49 +00:00
|
|
|
#include "intvec.h"
|
2020-08-30 21:34:40 +00:00
|
|
|
#include "m_swap.h"
|
2020-01-25 10:56:13 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
////////// Compiler detection //////////
|
|
|
|
|
2014-10-25 10:17:35 +00:00
|
|
|
#ifdef __GNUC__
|
2014-11-24 08:19:50 +00:00
|
|
|
# define EDUKE32_GCC_PREREQ(major, minor) (major < __GNUC__ || (major == __GNUC__ && minor <= __GNUC_MINOR__))
|
2014-10-25 10:17:35 +00:00
|
|
|
#else
|
|
|
|
# define EDUKE32_GCC_PREREQ(major, minor) 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __clang__
|
2014-11-24 08:19:50 +00:00
|
|
|
# define EDUKE32_CLANG_PREREQ(major, minor) (major < __clang_major__ || (major == __clang_major__ && minor <= __clang_minor__))
|
2014-10-25 10:17:35 +00:00
|
|
|
#else
|
|
|
|
# define EDUKE32_CLANG_PREREQ(major, minor) 0
|
|
|
|
#endif
|
|
|
|
#ifndef __has_builtin
|
|
|
|
# define __has_builtin(x) 0 // Compatibility with non-clang compilers.
|
|
|
|
#endif
|
|
|
|
#ifndef __has_feature
|
|
|
|
# define __has_feature(x) 0 // Compatibility with non-clang compilers.
|
|
|
|
#endif
|
|
|
|
#ifndef __has_extension
|
|
|
|
# define __has_extension __has_feature // Compatibility with pre-3.0 compilers.
|
|
|
|
#endif
|
2017-07-10 13:44:04 +00:00
|
|
|
#ifndef __has_cpp_attribute
|
|
|
|
# define __has_cpp_attribute(x) 0
|
|
|
|
#endif
|
2014-10-25 10:17:35 +00:00
|
|
|
|
2017-06-05 10:05:17 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# define EDUKE32_MSVC_PREREQ(major) ((major) <= (_MSC_VER))
|
2017-10-09 07:37:05 +00:00
|
|
|
# ifdef __cplusplus
|
|
|
|
# define EDUKE32_MSVC_CXX_PREREQ(major) ((major) <= (_MSC_VER))
|
|
|
|
# else
|
|
|
|
# define EDUKE32_MSVC_CXX_PREREQ(major) 0
|
|
|
|
# endif
|
2017-06-05 10:05:17 +00:00
|
|
|
#else
|
|
|
|
# define EDUKE32_MSVC_PREREQ(major) 0
|
2017-10-09 07:37:05 +00:00
|
|
|
# define EDUKE32_MSVC_CXX_PREREQ(major) 0
|
2017-06-05 10:05:17 +00:00
|
|
|
#endif
|
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
|
2017-04-12 08:30:18 +00:00
|
|
|
////////// Language detection //////////
|
|
|
|
|
|
|
|
#if defined __STDC__
|
|
|
|
# if defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L
|
|
|
|
# define CSTD 2011
|
|
|
|
# elif defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
|
|
|
|
# define CSTD 1999
|
|
|
|
# elif defined __STDC_VERSION__ && __STDC_VERSION__ >= 199409L
|
|
|
|
# define CSTD 1994
|
|
|
|
# else
|
|
|
|
# define CSTD 1989
|
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
# define CSTD 0
|
|
|
|
#endif
|
|
|
|
|
2017-10-09 07:37:01 +00:00
|
|
|
#if defined __cplusplus && __cplusplus >= 201703L
|
|
|
|
# define CXXSTD 2017
|
|
|
|
#elif defined __cplusplus && __cplusplus >= 201402L
|
2017-04-12 08:30:18 +00:00
|
|
|
# define CXXSTD 2014
|
|
|
|
#elif defined __cplusplus && __cplusplus >= 201103L
|
|
|
|
# define CXXSTD 2011
|
|
|
|
#elif defined __cplusplus && __cplusplus >= 199711L
|
2020-05-23 13:18:10 +00:00
|
|
|
# define CXXSTD 2014 // Thanks, Microsoft... :?
|
2017-04-12 08:30:18 +00:00
|
|
|
#else
|
|
|
|
# define CXXSTD 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
////////// Language and compiler feature polyfills //////////
|
|
|
|
|
|
|
|
# define EXTERNC
|
|
|
|
|
2017-02-25 08:15:13 +00:00
|
|
|
#ifndef UNREFERENCED_PARAMETER
|
|
|
|
# define UNREFERENCED_PARAMETER(x) (x) = (x)
|
2014-11-30 23:18:57 +00:00
|
|
|
#endif
|
2014-10-29 17:05:15 +00:00
|
|
|
|
2017-06-27 02:24:14 +00:00
|
|
|
#ifndef UNREFERENCED_CONST_PARAMETER
|
|
|
|
# ifdef _MSC_VER
|
2017-07-29 20:39:50 +00:00
|
|
|
# define UNREFERENCED_CONST_PARAMETER(x) ((void)(x))
|
2017-06-27 02:24:14 +00:00
|
|
|
# else
|
|
|
|
# define UNREFERENCED_CONST_PARAMETER(x)
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2010-12-20 16:55:37 +00:00
|
|
|
#if defined __GNUC__ || defined __clang__
|
|
|
|
# define ATTRIBUTE(attrlist) __attribute__(attrlist)
|
|
|
|
#else
|
|
|
|
# define ATTRIBUTE(attrlist)
|
|
|
|
#endif
|
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#if EDUKE32_GCC_PREREQ(4,0)
|
|
|
|
# define WARN_UNUSED_RESULT __attribute__((warn_unused_result))
|
2014-10-25 10:17:35 +00:00
|
|
|
#else
|
2017-02-25 08:15:16 +00:00
|
|
|
# define WARN_UNUSED_RESULT
|
2015-03-02 08:15:28 +00:00
|
|
|
#endif
|
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#if defined _MSC_VER && _MSC_VER < 1800
|
|
|
|
# define inline __inline
|
2014-11-24 08:19:50 +00:00
|
|
|
#endif
|
|
|
|
|
2018-12-23 23:23:50 +00:00
|
|
|
#ifndef MAY_ALIAS
|
|
|
|
# ifdef _MSC_VER
|
|
|
|
# define MAY_ALIAS
|
|
|
|
# else
|
|
|
|
# define MAY_ALIAS __attribute__((may_alias))
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#ifndef FORCE_INLINE
|
2017-02-25 08:15:53 +00:00
|
|
|
# ifdef _MSC_VER
|
|
|
|
# define FORCE_INLINE __forceinline
|
2015-01-25 12:16:10 +00:00
|
|
|
# else
|
2017-02-25 08:15:16 +00:00
|
|
|
# ifdef __GNUC__
|
2017-02-25 08:15:53 +00:00
|
|
|
# define FORCE_INLINE inline __attribute__((always_inline))
|
2017-02-25 08:15:16 +00:00
|
|
|
# else
|
2017-02-25 08:15:53 +00:00
|
|
|
# define FORCE_INLINE inline
|
2014-11-24 08:19:50 +00:00
|
|
|
# endif
|
2014-10-30 04:59:23 +00:00
|
|
|
# endif
|
2008-02-16 22:27:08 +00:00
|
|
|
#endif
|
|
|
|
|
2009-04-26 05:57:42 +00:00
|
|
|
|
2020-01-29 11:36:52 +00:00
|
|
|
#if 0 && defined(__OPTIMIZE__) && (defined __GNUC__ || __has_builtin(__builtin_expect))
|
2017-02-25 08:15:16 +00:00
|
|
|
#define EDUKE32_PREDICT_FALSE(x) __builtin_expect(!!(x),0)
|
|
|
|
#else
|
|
|
|
#define EDUKE32_PREDICT_FALSE(x) (x)
|
2009-04-26 05:57:42 +00:00
|
|
|
#endif
|
|
|
|
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2017-02-25 08:15:58 +00:00
|
|
|
#if EDUKE32_GCC_PREREQ(2,0) || defined _MSC_VER
|
2017-02-25 08:15:16 +00:00
|
|
|
# define EDUKE32_FUNCTION __FUNCTION__
|
2017-04-12 08:30:18 +00:00
|
|
|
#elif CSTD >= 1999 || CXXSTD >= 2011
|
2017-02-25 08:15:58 +00:00
|
|
|
# define EDUKE32_FUNCTION __func__
|
2017-02-25 08:15:16 +00:00
|
|
|
#else
|
|
|
|
# define EDUKE32_FUNCTION "???"
|
|
|
|
#endif
|
2012-03-14 22:31:19 +00:00
|
|
|
|
2017-02-25 08:15:58 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
# define EDUKE32_PRETTY_FUNCTION __FUNCSIG__
|
|
|
|
#elif EDUKE32_GCC_PREREQ(2,0)
|
|
|
|
# define EDUKE32_PRETTY_FUNCTION __PRETTY_FUNCTION__
|
|
|
|
#else
|
|
|
|
# define EDUKE32_PRETTY_FUNCTION EDUKE32_FUNCTION
|
|
|
|
#endif
|
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#ifdef __COUNTER__
|
2017-10-09 07:37:14 +00:00
|
|
|
# define EDUKE32_UNIQUE_SRC_ID __COUNTER__
|
|
|
|
#else
|
|
|
|
# define EDUKE32_UNIQUE_SRC_ID __LINE__
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CXXSTD >= 2017
|
|
|
|
# define EDUKE32_STATIC_ASSERT(cond) static_assert(cond)
|
|
|
|
#elif CXXSTD >= 2011 || CSTD >= 2011 || EDUKE32_MSVC_CXX_PREREQ(1600)
|
|
|
|
# define EDUKE32_STATIC_ASSERT(cond) static_assert(cond, "")
|
2017-02-25 08:15:16 +00:00
|
|
|
#else
|
2017-10-09 07:37:14 +00:00
|
|
|
/* C99 / C++03 static assertions based on source found in LuaJIT's src/lj_def.h. */
|
|
|
|
# define EDUKE32_ASSERT_NAME2(name, line) name ## line
|
|
|
|
# define EDUKE32_ASSERT_NAME(line) EDUKE32_ASSERT_NAME2(eduke32_assert_, line)
|
2017-02-25 08:15:16 +00:00
|
|
|
# define EDUKE32_STATIC_ASSERT(cond) \
|
2017-10-09 07:37:14 +00:00
|
|
|
extern void EDUKE32_ASSERT_NAME(EDUKE32_UNIQUE_SRC_ID)(int STATIC_ASSERTION_FAILED[(cond)?1:-1])
|
2006-04-13 20:47:06 +00:00
|
|
|
#endif
|
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#ifndef FP_OFF
|
|
|
|
# define FP_OFF(__p) ((uintptr_t)(__p))
|
2014-09-30 04:06:32 +00:00
|
|
|
#endif
|
|
|
|
|
2019-04-08 06:27:48 +00:00
|
|
|
|
2017-06-05 10:05:17 +00:00
|
|
|
#if defined __cplusplus && (__cplusplus >= 201103L || __has_feature(cxx_constexpr) || EDUKE32_MSVC_PREREQ(1900))
|
2017-04-12 08:30:23 +00:00
|
|
|
# define HAVE_CONSTEXPR
|
|
|
|
# define CONSTEXPR constexpr
|
|
|
|
#else
|
|
|
|
# define CONSTEXPR
|
|
|
|
#endif
|
|
|
|
|
2017-06-05 10:05:17 +00:00
|
|
|
#if CXXSTD >= 2011 || EDUKE32_MSVC_PREREQ(1700)
|
2017-04-12 08:30:29 +00:00
|
|
|
# define FINAL final
|
|
|
|
#else
|
|
|
|
# define FINAL
|
|
|
|
#endif
|
|
|
|
|
2017-04-12 08:30:23 +00:00
|
|
|
#if CXXSTD >= 2014
|
|
|
|
# define CONSTEXPR_CXX14 CONSTEXPR
|
|
|
|
#else
|
|
|
|
# define CONSTEXPR_CXX14
|
|
|
|
#endif
|
Inreased debugging level for catching oob accesses to 'main' arrays.
If enabled, this makes the following arrays be allocated statically:
spriteext, spritesmooth, sector, wall, sprite, tsprite, while
necessarily disabling the clipshape feature (because it relies on
setting sector/wall to different malloc'd block temporarily).
To compile, pass DEBUGANYWAY=1 in addition to RELEASE=0 to 'make',
and it's really only useful with CC=clang, of course.
git-svn-id: https://svn.eduke32.com/eduke32@2270 1a8010ca-5511-0410-912e-c29ae57300e0
2012-01-19 23:17:34 +00:00
|
|
|
|
2017-10-09 07:37:11 +00:00
|
|
|
#if CXXSTD >= 2011
|
2017-09-23 03:17:29 +00:00
|
|
|
# if __has_cpp_attribute(fallthrough)
|
|
|
|
# define fallthrough__ [[fallthrough]]
|
|
|
|
# elif __has_cpp_attribute(clang::fallthrough)
|
|
|
|
# define fallthrough__ [[clang::fallthrough]]
|
|
|
|
# elif __has_cpp_attribute(gnu::fallthrough)
|
|
|
|
# define fallthrough__ [[gnu::fallthrough]]
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
#ifndef fallthrough__
|
|
|
|
# if !defined __clang__ && EDUKE32_GCC_PREREQ(7,0)
|
|
|
|
# define fallthrough__ __attribute__((fallthrough))
|
|
|
|
# elif defined _MSC_VER
|
|
|
|
# define fallthrough__ __fallthrough
|
|
|
|
# else
|
|
|
|
# define fallthrough__
|
|
|
|
# endif
|
2017-07-10 13:44:04 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2012-03-18 08:50:41 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
////////// Architecture detection //////////
|
|
|
|
|
2020-08-30 21:34:40 +00:00
|
|
|
#ifdef WORDS_BIGENDIAN
|
2006-04-13 20:47:06 +00:00
|
|
|
# define B_LITTLE_ENDIAN 0
|
|
|
|
# define B_BIG_ENDIAN 1
|
2020-08-30 21:34:40 +00:00
|
|
|
#else
|
2006-04-13 20:47:06 +00:00
|
|
|
# define B_LITTLE_ENDIAN 1
|
|
|
|
# define B_BIG_ENDIAN 0
|
|
|
|
#endif
|
|
|
|
|
2015-01-11 04:53:16 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
////////// Standard library headers //////////
|
|
|
|
|
|
|
|
#undef __USE_MINGW_ANSI_STDIO // Workaround for MinGW-w64.
|
2015-01-11 04:53:16 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#ifndef __STDC_FORMAT_MACROS
|
|
|
|
# define __STDC_FORMAT_MACROS
|
|
|
|
#endif
|
|
|
|
#ifndef __STDC_LIMIT_MACROS
|
|
|
|
# define __STDC_LIMIT_MACROS
|
2014-01-27 10:31:39 +00:00
|
|
|
#endif
|
2017-02-25 08:15:20 +00:00
|
|
|
|
2018-03-17 03:26:05 +00:00
|
|
|
#ifndef _USE_MATH_DEFINES
|
|
|
|
# define _USE_MATH_DEFINES
|
|
|
|
#endif
|
|
|
|
|
2019-04-18 17:24:22 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdint.h>
|
2006-08-31 01:54:23 +00:00
|
|
|
|
2017-02-25 08:15:36 +00:00
|
|
|
#include <limits.h>
|
2017-02-25 08:15:16 +00:00
|
|
|
#include <stdarg.h>
|
2018-11-18 18:09:48 +00:00
|
|
|
#include <stddef.h>
|
2019-03-01 08:51:50 +00:00
|
|
|
#ifndef USE_PHYSFS
|
2017-02-25 08:15:16 +00:00
|
|
|
#include <stdio.h>
|
2019-03-01 08:51:50 +00:00
|
|
|
#endif
|
2018-11-18 18:09:48 +00:00
|
|
|
#include <stdlib.h>
|
2017-02-25 08:15:16 +00:00
|
|
|
#include <string.h>
|
2006-08-31 01:54:23 +00:00
|
|
|
|
2019-07-16 09:35:09 +00:00
|
|
|
#if !(defined _WIN32 && defined __clang__)
|
2017-02-25 08:15:36 +00:00
|
|
|
#include <float.h>
|
2019-07-16 09:35:09 +00:00
|
|
|
#endif
|
2018-11-18 18:09:48 +00:00
|
|
|
#include <math.h>
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2018-11-18 18:09:48 +00:00
|
|
|
#include <time.h>
|
2015-05-03 07:03:12 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#include <assert.h>
|
2015-05-03 07:03:48 +00:00
|
|
|
|
2017-04-12 08:30:29 +00:00
|
|
|
#ifdef __cplusplus
|
2019-07-16 09:35:09 +00:00
|
|
|
# include <limits>
|
2017-06-05 10:05:17 +00:00
|
|
|
# if CXXSTD >= 2011 || EDUKE32_MSVC_PREREQ(1800)
|
2018-10-16 06:09:54 +00:00
|
|
|
# include <algorithm>
|
|
|
|
# include <functional>
|
2017-04-12 08:30:29 +00:00
|
|
|
# include <type_traits>
|
2019-04-10 01:00:38 +00:00
|
|
|
// we need this because MSVC does not properly identify C++11 support
|
|
|
|
# define HAVE_CXX11_HEADERS
|
2017-04-12 08:30:29 +00:00
|
|
|
# endif
|
|
|
|
#endif
|
2017-02-25 08:15:16 +00:00
|
|
|
|
|
|
|
////////// Platform headers //////////
|
|
|
|
|
|
|
|
#if !defined __APPLE__ && (!defined EDUKE32_BSD || !__STDC__)
|
|
|
|
# include <malloc.h>
|
2006-04-13 20:47:06 +00:00
|
|
|
#endif
|
|
|
|
|
2019-03-01 08:51:50 +00:00
|
|
|
#ifndef USE_PHYSFS
|
2017-02-25 08:15:16 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
2018-11-18 18:09:48 +00:00
|
|
|
#include <sys/types.h>
|
2015-05-03 07:03:48 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#if defined(_WIN32)
|
2017-02-25 08:15:36 +00:00
|
|
|
# include <direct.h>
|
2018-11-18 18:09:48 +00:00
|
|
|
# include <io.h>
|
2015-05-03 07:03:48 +00:00
|
|
|
#else
|
2017-02-25 08:15:16 +00:00
|
|
|
# include <unistd.h>
|
2015-05-03 07:03:48 +00:00
|
|
|
#endif
|
2019-03-01 08:51:50 +00:00
|
|
|
#endif
|
2014-02-10 11:00:25 +00:00
|
|
|
|
2020-04-11 21:50:43 +00:00
|
|
|
#include "engineerrors.h"
|
2017-02-25 08:15:16 +00:00
|
|
|
|
|
|
|
////////// DEPRECATED: Standard library prefixing //////////
|
|
|
|
|
2020-08-30 21:34:40 +00:00
|
|
|
typedef intptr_t ssize_t;
|
2017-06-24 21:18:12 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
typedef size_t bsize_t;
|
|
|
|
typedef ssize_t bssize_t;
|
|
|
|
|
|
|
|
#define BMAX_PATH 256
|
|
|
|
|
|
|
|
#define Bstrcpy strcpy
|
|
|
|
#define Bstrncpy strncpy
|
|
|
|
#define Bstrcmp strcmp
|
|
|
|
#define Bstrncmp strncmp
|
|
|
|
#define Bstrcat strcat
|
|
|
|
#define Bstrncat strncat
|
|
|
|
#define Bstrlen strlen
|
|
|
|
#define Bstrchr strchr
|
|
|
|
#define Bstrrchr strrchr
|
|
|
|
#define Bstrtol strtol
|
|
|
|
#define Bstrtoul strtoul
|
|
|
|
#define Bstrtod strtod
|
|
|
|
#define Bstrstr strstr
|
|
|
|
#define Bmemcpy memcpy
|
|
|
|
#define Bmemset memset
|
|
|
|
|
|
|
|
|
|
|
|
////////// Standard library wrappers //////////
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
# define Bstrcasecmp _stricmp
|
|
|
|
# define Bstrncasecmp _strnicmp
|
|
|
|
#elif defined(__QNX__)
|
|
|
|
# define Bstrcasecmp stricmp
|
|
|
|
# define Bstrncasecmp strnicmp
|
2011-03-23 17:41:01 +00:00
|
|
|
#else
|
2017-02-25 08:15:16 +00:00
|
|
|
# define Bstrcasecmp strcasecmp
|
|
|
|
# define Bstrncasecmp strncasecmp
|
2011-03-23 17:41:01 +00:00
|
|
|
#endif
|
2013-05-16 21:54:59 +00:00
|
|
|
|
2020-01-25 09:56:30 +00:00
|
|
|
static inline int32_t atoi_safe(const char *str) { return (int32_t)strtoll(str, NULL, 10); }
|
2019-12-07 23:50:27 +00:00
|
|
|
|
|
|
|
#define Batoi(x) atoi_safe(x)
|
2017-02-25 08:15:16 +00:00
|
|
|
#define Batol(str) (strtol(str, NULL, 10))
|
2014-04-12 08:45:08 +00:00
|
|
|
|
2020-01-25 10:56:13 +00:00
|
|
|
static inline int Blrintf(const double x)
|
2017-02-25 08:15:16 +00:00
|
|
|
{
|
2020-01-25 10:56:13 +00:00
|
|
|
return xs_CRoundToInt(x);
|
2017-02-25 08:15:16 +00:00
|
|
|
}
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
#if defined(__arm__)
|
|
|
|
# define Bsqrtf __builtin_sqrtf
|
2015-03-24 00:40:33 +00:00
|
|
|
#else
|
2017-02-25 08:15:16 +00:00
|
|
|
# define Bsqrtf sqrtf
|
2015-03-24 00:40:33 +00:00
|
|
|
#endif
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2014-12-27 18:36:43 +00:00
|
|
|
|
2017-04-12 08:30:29 +00:00
|
|
|
////////// Metaprogramming structs //////////
|
|
|
|
|
|
|
|
# if CXXSTD >= 2014
|
|
|
|
using std::enable_if_t;
|
2019-04-10 01:00:38 +00:00
|
|
|
# elif defined HAVE_CXX11_HEADERS
|
2017-04-12 08:30:29 +00:00
|
|
|
template <bool B, class T = void>
|
|
|
|
using enable_if_t = typename std::enable_if<B, T>::type;
|
2019-04-10 01:00:50 +00:00
|
|
|
# endif
|
|
|
|
|
2017-04-12 08:30:32 +00:00
|
|
|
|
2020-08-30 21:34:40 +00:00
|
|
|
using native_t = intptr_t;
|
2017-04-12 08:30:32 +00:00
|
|
|
|
2018-12-23 23:23:50 +00:00
|
|
|
typedef struct MAY_ALIAS {
|
2014-12-27 18:36:43 +00:00
|
|
|
int32_t x, y;
|
|
|
|
} vec2_t;
|
|
|
|
|
2015-05-27 08:48:07 +00:00
|
|
|
typedef struct {
|
|
|
|
uint32_t x, y;
|
|
|
|
} vec2u_t;
|
|
|
|
|
2014-12-27 18:36:43 +00:00
|
|
|
typedef struct {
|
|
|
|
float x, y;
|
|
|
|
} vec2f_t;
|
|
|
|
|
2019-03-02 23:21:52 +00:00
|
|
|
typedef struct {
|
|
|
|
double x, y;
|
|
|
|
} vec2d_t;
|
|
|
|
|
2019-06-25 11:28:04 +00:00
|
|
|
typedef struct MAY_ALIAS {
|
2019-07-24 01:38:05 +00:00
|
|
|
union {
|
|
|
|
struct { int32_t x, y, z; };
|
|
|
|
vec2_t vec2;
|
|
|
|
};
|
2019-06-25 11:28:04 +00:00
|
|
|
} vec3_t;
|
|
|
|
|
|
|
|
typedef struct MAY_ALIAS {
|
2019-07-24 01:38:05 +00:00
|
|
|
union {
|
|
|
|
struct { int16_t x, y, z; };
|
|
|
|
vec2_16_t vec2;
|
|
|
|
};
|
2019-06-25 11:28:04 +00:00
|
|
|
} vec3_16_t;
|
|
|
|
|
2014-12-27 18:36:43 +00:00
|
|
|
typedef struct {
|
2019-07-24 01:38:05 +00:00
|
|
|
union {
|
|
|
|
struct {
|
|
|
|
union { float x, d; };
|
|
|
|
union { float y, u; };
|
|
|
|
union { float z, v; };
|
|
|
|
};
|
|
|
|
vec2f_t vec2;
|
|
|
|
};
|
2014-12-27 18:36:43 +00:00
|
|
|
} vec3f_t;
|
|
|
|
|
2015-03-24 00:40:33 +00:00
|
|
|
EDUKE32_STATIC_ASSERT(sizeof(vec3f_t) == sizeof(float) * 3);
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
union { double x; double d; };
|
|
|
|
union { double y; double u; };
|
|
|
|
union { double z; double v; };
|
|
|
|
} vec3d_t;
|
|
|
|
|
|
|
|
EDUKE32_STATIC_ASSERT(sizeof(vec3d_t) == sizeof(double) * 3);
|
2014-12-27 18:36:43 +00:00
|
|
|
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2017-02-25 08:16:01 +00:00
|
|
|
////////// Language tricks that depend on size_t //////////
|
|
|
|
|
2019-12-29 15:35:51 +00:00
|
|
|
#include "basics.h"
|
|
|
|
|
|
|
|
# define ARRAY_SIZE(arr) countof(arr)
|
2017-02-25 08:15:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////// Pointer management //////////
|
|
|
|
|
|
|
|
#define DO_FREE_AND_NULL(var) do { \
|
2019-06-25 11:29:08 +00:00
|
|
|
Xfree(var); (var) = NULL; \
|
2017-02-25 08:15:16 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
|
|
|
////////// Data serialization //////////
|
|
|
|
|
2020-08-30 21:34:40 +00:00
|
|
|
inline int32_t B_LITTLE32(int32_t val) { return LittleLong(val); }
|
|
|
|
inline uint32_t B_LITTLE32(uint32_t val) { return LittleLong(val); }
|
|
|
|
inline int32_t B_LITTLE16(int16_t val) { return LittleShort(val); }
|
|
|
|
inline uint32_t B_LITTLE16(uint16_t val) { return LittleShort(val); }
|
2019-04-08 06:27:48 +00:00
|
|
|
|
2017-02-25 08:15:53 +00:00
|
|
|
static FORCE_INLINE void B_BUF32(void * const buf, uint32_t const x) { *(uint32_t *) buf = x; }
|
2018-10-25 23:33:52 +00:00
|
|
|
static FORCE_INLINE CONSTEXPR uint32_t B_UNBUF32(void const * const buf) { return *(uint32_t const *) buf; }
|
2020-08-30 21:34:40 +00:00
|
|
|
static FORCE_INLINE CONSTEXPR uint16_t B_UNBUF16(void const * const buf) { return *(uint16_t const *) buf; }
|
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
|
|
|
|
|
|
|
|
////////// Abstract data operations //////////
|
|
|
|
|
2018-10-25 23:33:52 +00:00
|
|
|
#define ABSTRACT_DECL static FORCE_INLINE WARN_UNUSED_RESULT CONSTEXPR
|
2017-06-27 02:23:46 +00:00
|
|
|
|
2018-10-16 06:09:54 +00:00
|
|
|
template <typename T, typename X, typename Y> ABSTRACT_DECL T clamp(T in, X min, Y max) { return in <= (T) min ? (T) min : (in >= (T) max ? (T) max : in); }
|
|
|
|
template <typename T, typename X, typename Y> ABSTRACT_DECL T clamp2(T in, X min, Y max) { return in >= (T) max ? (T) max : (in <= (T) min ? (T) min : in); }
|
|
|
|
using std::min;
|
|
|
|
using std::max;
|
2017-02-25 08:15:16 +00:00
|
|
|
|
2019-05-19 03:52:54 +00:00
|
|
|
////////// Bitfield manipulation //////////
|
|
|
|
|
2019-11-02 09:45:41 +00:00
|
|
|
#if 0
|
|
|
|
// Behold the probably most useless (de)optimization I ever discovered.
|
|
|
|
// Replacing a simple bit shift with a memory access through a global pointer is surely going to improve matters... >(
|
|
|
|
// Constexpr means shit here if the index is not a constant!
|
2019-09-10 23:35:07 +00:00
|
|
|
static CONSTEXPR const char pow2char[8] = {1,2,4,8,16,32,64,128u};
|
2019-11-02 09:45:41 +00:00
|
|
|
#else
|
|
|
|
// Revert the above to a real bit shift through some C++ operator magic. That saves me from reverting all the code that uses this construct.
|
2019-11-13 21:40:08 +00:00
|
|
|
static struct
|
2019-11-02 09:45:41 +00:00
|
|
|
{
|
|
|
|
constexpr uint8_t operator[](int index) const { return 1 << index; };
|
|
|
|
} pow2char;
|
|
|
|
#endif
|
2019-08-04 02:51:50 +00:00
|
|
|
|
|
|
|
static FORCE_INLINE void bitmap_set(uint8_t *const ptr, int const n) { ptr[n>>3] |= pow2char[n&7]; }
|
|
|
|
static FORCE_INLINE void bitmap_clear(uint8_t *const ptr, int const n) { ptr[n>>3] &= ~pow2char[n&7]; }
|
|
|
|
static FORCE_INLINE CONSTEXPR char bitmap_test(uint8_t const *const ptr, int const n) { return ptr[n>>3] & pow2char[n&7]; }
|
2017-04-12 08:30:35 +00:00
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
////////// Utility functions //////////
|
|
|
|
|
2019-05-19 03:52:54 +00:00
|
|
|
// breadth-first search helpers
|
|
|
|
template <typename T>
|
|
|
|
void bfirst_search_init(T *const list, uint8_t *const bitmap, T *const eltnumptr, int const maxelts, int const firstelt)
|
|
|
|
{
|
|
|
|
Bmemset(bitmap, 0, (maxelts+7)>>3);
|
|
|
|
|
|
|
|
list[0] = firstelt;
|
|
|
|
bitmap_set(bitmap, firstelt);
|
|
|
|
*eltnumptr = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void bfirst_search_try(T *const list, uint8_t *const bitmap, T *const eltnumptr, int const elt)
|
|
|
|
{
|
|
|
|
if (!bitmap_test(bitmap, elt))
|
|
|
|
{
|
|
|
|
bitmap_set(bitmap, elt);
|
|
|
|
list[(*eltnumptr)++] = elt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-06 19:43:38 +00:00
|
|
|
// Copy min(strlen(src)+1, n) characters into dst, always terminate with a NUL.
|
2017-02-25 08:15:53 +00:00
|
|
|
static FORCE_INLINE char *Bstrncpyz(char *dst, const char *src, bsize_t n)
|
2012-11-20 01:33:22 +00:00
|
|
|
{
|
|
|
|
Bstrncpy(dst, src, n);
|
|
|
|
dst[n-1] = 0;
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2017-02-25 08:15:16 +00:00
|
|
|
////////// PANICKING ALLOCATION WRAPPERS //////////
|
|
|
|
|
2014-05-30 00:02:16 +00:00
|
|
|
|
2019-12-26 13:43:44 +00:00
|
|
|
#define Xstrdup(s) (strdup(s))
|
2020-04-11 21:46:42 +00:00
|
|
|
#define Xmalloc(size) (M_Malloc(size))
|
|
|
|
#define Xcalloc(nmemb, size) (M_Calloc(nmemb, size))
|
|
|
|
#define Xrealloc(ptr, size) (M_Realloc(ptr, size))
|
|
|
|
#define Xfree(ptr) (M_Free(ptr))
|
2017-02-25 08:15:16 +00:00
|
|
|
|
|
|
|
////////// Inlined external libraries //////////
|
|
|
|
|
2020-09-01 13:00:35 +00:00
|
|
|
#include "m_fixed.h"
|
2019-12-17 18:37:05 +00:00
|
|
|
#include "vectors.h"
|
|
|
|
|
|
|
|
inline FVector3 GetSoundPos(const vec3_t *pos)
|
|
|
|
{
|
|
|
|
// converts a Build coordinate to a sound system coordinate
|
|
|
|
const float xmul = 1 / 16.f;
|
|
|
|
const float ymul = -1 / 16.f;
|
|
|
|
const float zmul = -1 / 256.f;
|
|
|
|
return { pos->x* xmul, pos->z* zmul, pos->y* ymul };
|
|
|
|
}
|
2017-02-25 08:15:16 +00:00
|
|
|
|
|
|
|
/* End dependence on compat.o object. */
|
|
|
|
|
|
|
|
|
|
|
|
////////// EDuke32-specific features //////////
|
|
|
|
|
|
|
|
#ifndef TRUE
|
|
|
|
# define TRUE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef FALSE
|
|
|
|
# define FALSE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define WITHKPLIB
|
|
|
|
|
2014-11-22 12:32:56 +00:00
|
|
|
#endif // compat_h_
|