base.h (4142B)
1 // SPDX-FileCopyrightText: 2023 Erin Catto 2 // SPDX-License-Identifier: MIT 3 4 #pragma once 5 6 #include <stdint.h> 7 8 // clang-format off 9 // 10 // Shared library macros 11 #if defined( _MSC_VER ) && defined( box2d_EXPORTS ) 12 // build the Windows DLL 13 #define BOX2D_EXPORT __declspec( dllexport ) 14 #elif defined( _MSC_VER ) && defined( BOX2D_DLL ) 15 // using the Windows DLL 16 #define BOX2D_EXPORT __declspec( dllimport ) 17 #elif defined( box2d_EXPORTS ) 18 // building or using the shared library 19 #define BOX2D_EXPORT __attribute__( ( visibility( "default" ) ) ) 20 #else 21 // static library 22 #define BOX2D_EXPORT 23 #endif 24 25 // C++ macros 26 #ifdef __cplusplus 27 #define B2_API extern "C" BOX2D_EXPORT 28 #define B2_INLINE inline 29 #define B2_LITERAL(T) T 30 #define B2_ZERO_INIT {} 31 #else 32 #define B2_API BOX2D_EXPORT 33 #define B2_INLINE static inline 34 /// Used for C literals like (b2Vec2){1.0f, 2.0f} where C++ requires b2Vec2{1.0f, 2.0f} 35 #define B2_LITERAL(T) (T) 36 #define B2_ZERO_INIT {0} 37 #endif 38 // clang-format on 39 40 /** 41 * @defgroup base Base 42 * Base functionality 43 * @{ 44 */ 45 46 /// Prototype for user allocation function 47 /// @param size the allocation size in bytes 48 /// @param alignment the required alignment, guaranteed to be a power of 2 49 typedef void* b2AllocFcn( unsigned int size, int alignment ); 50 51 /// Prototype for user free function 52 /// @param mem the memory previously allocated through `b2AllocFcn` 53 typedef void b2FreeFcn( void* mem ); 54 55 /// Prototype for the user assert callback. Return 0 to skip the debugger break. 56 typedef int b2AssertFcn( const char* condition, const char* fileName, int lineNumber ); 57 58 /// This allows the user to override the allocation functions. These should be 59 /// set during application startup. 60 B2_API void b2SetAllocator( b2AllocFcn* allocFcn, b2FreeFcn* freeFcn ); 61 62 /// @return the total bytes allocated by Box2D 63 B2_API int b2GetByteCount( void ); 64 65 /// Override the default assert callback 66 /// @param assertFcn a non-null assert callback 67 B2_API void b2SetAssertFcn( b2AssertFcn* assertFcn ); 68 69 // see https://github.com/scottt/debugbreak 70 #if defined( _MSC_VER ) 71 #define B2_BREAKPOINT __debugbreak() 72 #elif defined( __GNUC__ ) || defined( __clang__ ) 73 #define B2_BREAKPOINT __builtin_trap() 74 #else 75 // Unknown compiler 76 #include <assert.h> 77 #define B2_BREAKPOINT assert( 0 ) 78 #endif 79 80 #if !defined( NDEBUG ) || defined( B2_ENABLE_ASSERT ) 81 B2_API int b2InternalAssertFcn( const char* condition, const char* fileName, int lineNumber ); 82 #define B2_ASSERT( condition ) \ 83 do \ 84 { \ 85 if ( !( condition ) && b2InternalAssertFcn( #condition, __FILE__, (int)__LINE__ ) ) \ 86 B2_BREAKPOINT; \ 87 } \ 88 while ( 0 ) 89 #else 90 #define B2_ASSERT( ... ) ( (void)0 ) 91 #endif 92 93 /// Version numbering scheme. 94 /// See https://semver.org/ 95 typedef struct b2Version 96 { 97 /// Significant changes 98 int major; 99 100 /// Incremental changes 101 int minor; 102 103 /// Bug fixes 104 int revision; 105 } b2Version; 106 107 /// Get the current version of Box2D 108 B2_API b2Version b2GetVersion( void ); 109 110 /**@}*/ 111 112 //! @cond 113 114 /// Get the absolute number of system ticks. The value is platform specific. 115 B2_API uint64_t b2GetTicks( void ); 116 117 /// Get the milliseconds passed from an initial tick value. 118 B2_API float b2GetMilliseconds( uint64_t ticks ); 119 120 /// Get the milliseconds passed from an initial tick value. 121 B2_API float b2GetMillisecondsAndReset( uint64_t* ticks ); 122 123 /// Yield to be used in a busy loop. 124 B2_API void b2Yield( void ); 125 126 /// Simple djb2 hash function for determinism testing 127 #define B2_HASH_INIT 5381 128 B2_API uint32_t b2Hash( uint32_t hash, const uint8_t* data, int count ); 129 130 //! @endcond