Files
handmadehero/core.h
T

200 lines
4.1 KiB
C

#ifndef CORE_H
#define CORE_H
#include <stdint.h>
/* ---- Switches ------------------------------------------------------- */
#ifndef BUILD_DEBUG
#define BUILD_DEBUG 1
#endif
#ifndef BUILD_INTERNAL
#define BUILD_INTERNAL 1
#endif
/* ---- Environment Detection ------------------------------------------ */
/* Architecture */
#ifndef ARCHITECTURE_X64
#define ARCHITECTURE_X64 0
#endif
#ifndef ARCHITECTURE_X86
#define ARCHITECTURE_X86 0
#endif
#ifndef ARCHITECTURE_ARM64
#define ARCHITECTURE_ARM64 0
#endif
#ifndef ARCHITECTURE_ARM32
#define ARCHITECTURE_ARM32 0
#endif
#if defined(__x86_64__) || defined(__x86_64) || defined(__amd64__) || defined(__amd64) || defined(_M_AMD64)
#undef ARCHITECTURE_X64
#define ARCHITECTURE_X64 1
#elif defined(i386) || defined(__i386) || defined(__i386__) || defined(_M_I86)
#undef ARCHITECTURE_X86
#define ARCHITECTURE_X86 1
#elif defined(__aarch64__)
#undef ARCHITECTURE_ARM64
#define ARCHITECTURE_ARM64 1
#elif defined(__arm__)
#undef ARCHITECTURE_ARM32
#define ARCHITECTURE_ARM32 1
#else
#error Unknown architecture . . .
#endif
/* OS */
#ifndef OS_WINDOWS
#define OS_WINDOWS 0
#endif
#ifndef OS_MACOS
#define OS_MACOS 0
#endif
#ifndef OS_LINUX
#define OS_LINUX 0
#endif
#if defined(_WIN32)
#undef OS_WINDOWS
#define OS_WINDOWS 1
#elif defined(__APPLE__) && defined(__MACH__)
#undef OS_MACOS
#define OS_MACOS 1
#elif defined(__linux__)
#undef OS_LINUX
#define OS_LINUX 1
#else
#error Unknown operating system . . .
#endif
/* Compiler */
#ifndef COMPILER_MSVC
#define COMPILER_MSVC 0
#endif
#ifndef COMPILER_CLANG
#define COMPILER_CLANG 0
#endif
#ifndef COMPILER_GCC
#define COMPILER_GCC 0
#endif
#if _MSC_VER
#undef COMPILER_MSVC
#define COMPILER_MSVC 1
#elif __clang__
#undef COMPILER_CLANG
#define COMPILER_CLANG 1
#elif __GNUC__
#undef COMPILER_GCC
#define COMPILER_GCC 1
#else
#error Unknown compiler . . .
#endif
/* ---- Types ---------------------------------------------------------- */
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
typedef uint8_t U8;
typedef uint16_t U16;
typedef uint32_t U32;
typedef uint64_t U64;
typedef int8_t S8;
typedef int16_t S16;
typedef int32_t S32;
typedef int64_t S64;
typedef S8 B8;
typedef S16 B16;
typedef S32 B32;
typedef S64 B64;
typedef float F32;
typedef double F64;
/* ---- Units ---------------------------------------------------------- */
#define KB(n) (((U64)(n)) << 10)
#define MB(n) (((U64)(n)) << 20)
#define GB(n) (((U64)(n)) << 30)
#define TB(n) (((U64)(n)) << 40)
/* ---- Clamps, Min/Max ------------------------------------------------ */
#define MIN(A,B) (((A) < (B)) ? (A) : (B))
#define MAX(A,B) (((A) > (B)) ? (A) : (B))
#define CLAMP_TOP(A, X) Min(A, X)
#define CLAMP_BOT(X, B) Max(X, B)
#define CLAMP(A, X, B) (((X) < (A)) ? (A) : ((X) > (B)) ? (B) : (X))
/* ---- Asserts -------------------------------------------------------- */
#if COMPILER_CLANG || COMPILER_GCC
#define TRAP() __builtin_trap()
#elif COMPILER_MSVC
#define TRAP() __debugbreak()
#else
#error Unknow debug break for the compiler . . .
#endif
#define ASSERT_ALWAYS(x) do { if(!(x)) { TRAP(); } } while(0)
#if BUILD_DEBUG
#define ASSERT(x) ASSERT_ALWAYS(x)
#else
#define ASSERT(x) (void)(x)
#endif
/* ---- Helper Macros -------------------------------------------------- */
#define STRINGIFY_(S) #S
#define STRINGIFY(S) STRINGIFY_(S)
#define GLUE_(A, B) A##B
#define GLUE(A, B) GLUE_(A,B)
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#define SWAP(T, a, b) do { T t__ = a; a = b; b = t__; } while(0)
/* ---- Constants ------------------------------------------------------ */
#define PI 3.14159265358979323846
#define MAX_U64 (U64)0xffffffffffffffff
#define MAX_U32 (U32)0xffffffff
#define MAX_U16 (U16)0xffff
#define MAX_U8 (U8)0xff
#define MIN_U64 (U64)0x0
#define MIN_U32 (U32)0x0
#define MIN_U16 (U16)0x0
#define MIN_U8 (U8)0x0
#define MAX_S64 (S64)0x7fffffffffffffff
#define MAX_S32 (S32)0x7fffffff
#define MAX_S16 (S16)0x7fff
#define MAX_S8 (S8)0x7f
#define MIN_S64 (S64)0x8000000000000000
#define MIN_S32 (S32)0x80000000
#define MIN_S16 (S16)0x8000
#define MIN_S8 (S8)0x80
#endif