Box3D 0.1.0
A 3D physics engine for games
Loading...
Searching...
No Matches
base.h
1// SPDX-FileCopyrightText: 2025 Erin Catto
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include <stdbool.h>
7#include <stdint.h>
8
9// Compile-time options. Edit box3d/config.h, or define BOX3D_USER_CONFIG to
10// point at your own copy.
11#ifdef BOX3D_USER_CONFIG
12#include BOX3D_USER_CONFIG
13#endif
14#include "config.h"
15
16// clang-format off
17//
18// Shared library macros
19// Predefine BOX3D_EXPORT to reuse an existing export/import scheme, for example
20// when compiling Box3D into another shared library.
21#ifndef BOX3D_EXPORT
22#if defined(_WIN32) && defined(box3d_EXPORTS)
23 // build the Windows DLL
24 #define BOX3D_EXPORT __declspec(dllexport)
25#elif defined(_WIN32) && defined(BOX3D_DLL)
26 // using the Windows DLL
27 #define BOX3D_EXPORT __declspec(dllimport)
28#elif defined(box3d_EXPORTS)
29 // building or using the shared library
30 #define BOX3D_EXPORT __attribute__((visibility("default")))
31#else
32 // static library
33 #define BOX3D_EXPORT
34#endif
35#endif
36
37// C++ macros
38#ifdef __cplusplus
39 #define B3_API extern "C" BOX3D_EXPORT
40 #define B3_INLINE inline
41
42#if defined( _MSC_VER )
43 #define B3_FORCE_INLINE __forceinline
44#elif defined( __GNUC__ ) || defined( __clang__ )
45 #define B3_FORCE_INLINE inline __attribute__((always_inline))
46#else
47 #define B3_FORCE_INLINE inline
48#endif
49
50 #define B3_LITERAL(T) T
51 #define B3_ZERO_INIT {}
52#else
53 #define B3_API BOX3D_EXPORT
54 #define B3_INLINE static inline
55
56#if defined( _MSC_VER )
57 #define B3_FORCE_INLINE static __forceinline
58#elif defined( __GNUC__ ) || defined( __clang__ )
59 #define B3_FORCE_INLINE static inline __attribute__((always_inline))
60#else
61 #define B3_FORCE_INLINE static inline
62#endif
63
65 #define B3_LITERAL(T) (T)
66 #define B3_ZERO_INIT {0}
67#endif
68// clang-format on
69
70#if defined( BOX3D_VALIDATE ) && !defined( NDEBUG )
71#define B3_ENABLE_VALIDATION 1
72#else
73#define B3_ENABLE_VALIDATION 0
74#endif
75
81
83#define B3_NULL_INDEX -1
84
88typedef void* b3AllocFcn( int32_t size, int32_t alignment );
89
92typedef void b3FreeFcn( void* mem );
93
95typedef int b3AssertFcn( const char* condition, const char* fileName, int lineNumber );
96
98typedef void b3LogFcn( const char* message );
99
102B3_API void b3SetAllocator( b3AllocFcn* allocFcn, b3FreeFcn* freeFcn );
103
105B3_API int32_t b3GetByteCount( void );
106
109B3_API void b3SetAssertFcn( b3AssertFcn* assertFcn );
110
112#if defined( _MSC_VER )
114#define B3_BREAKPOINT __debugbreak()
115#elif defined( __GNUC__ ) || defined( __clang__ )
116#define B3_BREAKPOINT __builtin_trap()
117#else
119#include <assert.h>
120#define B3_BREAKPOINT assert( 0 )
121#endif
122
123#if !defined( NDEBUG ) || defined( B3_ENABLE_ASSERT )
125B3_API int b3InternalAssert( const char* condition, const char* fileName, int lineNumber );
127#define B3_ASSERT( condition ) \
128 ( (void)( ( !!( condition ) ) || ( b3InternalAssert( #condition, __FILE__, (int)( __LINE__ ) ), 0 ) ) )
129#else
130#define B3_ASSERT( ... ) ( (void)0 )
131#endif
132
133#if B3_ENABLE_VALIDATION
136#define B3_VALIDATE( condition ) B3_ASSERT( condition )
137#else
140#define B3_VALIDATE( ... ) ( (void)0 )
141#endif
142
144B3_API void b3SetLogFcn( b3LogFcn* logFcn );
145
148typedef struct b3Version
149{
151 int major;
152
154 int minor;
155
158} b3Version;
159
161B3_API b3Version b3GetVersion( void );
162
164B3_API bool b3IsDoublePrecision( void );
165
167
169
171B3_API uint64_t b3GetTicks( void );
172
174B3_API float b3GetMilliseconds( uint64_t ticks );
175
177B3_API float b3GetMillisecondsAndReset( uint64_t* ticks );
178
180B3_API void b3Yield( void );
181
183B3_API void b3Sleep( int milliseconds );
184
185// Simple djb2 hash function for determinism testing
186#define B3_HASH_INIT 5381
187B3_API uint32_t b3Hash( uint32_t hash, const uint8_t* data, int count );
188
189// Dump file support functions
190B3_API void b3WriteBinaryFile( void* data, int size, const char* fileName );
191B3_API void* b3ReadBinaryFile( const char* prefix, const char* fileName, int* memSize );
192
int revision
Bug fixes.
Definition base.h:157
int minor
Incremental changes.
Definition base.h:154
int major
Significant changes.
Definition base.h:151
void * b3AllocFcn(int32_t size, int32_t alignment)
Prototype for user allocation function.
Definition base.h:88
void b3FreeFcn(void *mem)
Prototype for user free function.
Definition base.h:92
int b3InternalAssert(const char *condition, const char *fileName, int lineNumber)
Internal assertion handler. Allows for host intervention.
void b3SetAssertFcn(b3AssertFcn *assertFcn)
Override the default assert callback.
void b3SetLogFcn(b3LogFcn *logFcn)
Override the default logging callback.
int32_t b3GetByteCount(void)
Total bytes allocated by Box3D.
b3Version b3GetVersion(void)
Get the current version of Box3D.
bool b3IsDoublePrecision(void)
void b3SetAllocator(b3AllocFcn *allocFcn, b3FreeFcn *freeFcn)
This allows the user to override the allocation functions.
int b3AssertFcn(const char *condition, const char *fileName, int lineNumber)
Prototype for the user assert callback. Return 0 to skip the debugger break.
Definition base.h:95
void b3LogFcn(const char *message)
Prototype for user log callback. Used to log warnings.
Definition base.h:98
Version numbering scheme.
Definition base.h:149