Skip to content

Commit

Permalink
more conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
erincatto committed Sep 21, 2024
1 parent 4bd25ae commit c1f8317
Show file tree
Hide file tree
Showing 12 changed files with 175 additions and 114 deletions.
70 changes: 49 additions & 21 deletions src/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,19 @@ void b2Array_Resize( void** a, int elementSize, int count );
// Macro based dynamic arrays
// Pros
// - type safe
// - debuggable (visible count and capacity)
// - array data debuggable (visible count and capacity)
// - bounds checking
// - forward declaration
// - simple
// - simple implementation
// - generates functions (like C++ templates)
// - functions have https://en.wikipedia.org/wiki/Sequence_point
// - avoids stretchy buffer dropped pointer update bugs
// Cons
// - macros suck, however all array functions are real, type-safe functions
// - cannot debug
// - breaks code navigation

// Array declaration that doesn't need the type T to be defined
#define B2_DECLARE_ARRAY( T, PREFIX ) \
#define B2_ARRAY_DECLARE( T, PREFIX ) \
typedef struct \
{ \
struct T* data; \
Expand All @@ -82,12 +86,19 @@ void b2Array_Resize( void** a, int elementSize, int count );

// Inline array functions that need the type T to be defined
#define B2_ARRAY_INLINE( T, PREFIX ) \
/* Resize */ \
static inline void PREFIX##Array_Resize( PREFIX##Array* a, int count ) \
{ \
PREFIX##Array_Reserve( a, count ); \
a->count = count; \
} \
/* Get */ \
static inline T* PREFIX##Array_Get( PREFIX##Array* a, int index ) \
{ \
B2_ASSERT( 0 <= index && index < a->count ); \
return a->data + index; \
} \
\
/* Add */ \
static inline T* PREFIX##Array_Add( PREFIX##Array* a ) \
{ \
if ( a->count == a->capacity ) \
Expand All @@ -98,7 +109,7 @@ void b2Array_Resize( void** a, int elementSize, int count );
a->count += 1; \
return a->data + ( a->count - 1 ); \
} \
\
/* Push */ \
static inline void PREFIX##Array_Push( PREFIX##Array* a, T value ) \
{ \
if ( a->count == a->capacity ) \
Expand All @@ -109,13 +120,13 @@ void b2Array_Resize( void** a, int elementSize, int count );
a->data[a->count] = value; \
a->count += 1; \
} \
\
/* Set */ \
static inline void PREFIX##Array_Set( PREFIX##Array* a, int index, T value ) \
{ \
B2_ASSERT( 0 <= index && index < a->count ); \
a->data[index] = value; \
} \
\
/* RemoveSwap */ \
static inline int PREFIX##Array_RemoveSwap( PREFIX##Array* a, int index ) \
{ \
B2_ASSERT( 0 <= index && index < a->count ); \
Expand All @@ -128,22 +139,28 @@ void b2Array_Resize( void** a, int elementSize, int count );
a->count -= 1; \
return movedIndex; \
} \
\
/* Pop */ \
static inline T PREFIX##Array_Pop( PREFIX##Array* a ) \
{ \
B2_ASSERT( a->count > 0 ); \
T value = a->data[a->count - 1]; \
a->count -= 1; \
return value; \
} \
\
/* Clear */ \
static inline void PREFIX##Array_Clear( PREFIX##Array* a ) \
{ \
a->count = 0; \
} \
/* ByteCount */ \
static inline int PREFIX##Array_ByteCount( PREFIX##Array* a ) \
{ \
return (int)( a->capacity * sizeof( T ) ); \
}

// Array implementations to be instantiated in a source file where the type T is known
#define B2_ARRAY_SOURCE( T, PREFIX ) \
/* Create */ \
PREFIX##Array PREFIX##Array_Create( int capacity ) \
{ \
PREFIX##Array a; \
Expand All @@ -152,7 +169,7 @@ void b2Array_Resize( void** a, int elementSize, int count );
a.capacity = capacity; \
return a; \
} \
\
/* Reserve */ \
void PREFIX##Array_Reserve( PREFIX##Array* a, int newCapacity ) \
{ \
if ( newCapacity <= a->capacity ) \
Expand All @@ -162,7 +179,7 @@ void b2Array_Resize( void** a, int elementSize, int count );
a->data = b2GrowAlloc( a->data, a->capacity * sizeof( T ), newCapacity * sizeof( T ) ); \
a->capacity = newCapacity; \
} \
\
/* Destroy */ \
void PREFIX##Array_Destroy( PREFIX##Array* a ) \
{ \
b2Free( a->data, a->capacity * sizeof( T ) ); \
Expand All @@ -174,12 +191,23 @@ void b2Array_Resize( void** a, int elementSize, int count );
B2_DECLARE_ARRAY_NATIVE( int, b2Int );
B2_ARRAY_INLINE( int, b2Int );

B2_DECLARE_ARRAY( b2Body, b2Body );
B2_DECLARE_ARRAY( b2BodySim, b2BodySim );
B2_DECLARE_ARRAY( b2BodyState, b2BodyState );
B2_DECLARE_ARRAY( b2Contact, b2Contact );
B2_DECLARE_ARRAY( b2ContactSim, b2ContactSim );
B2_DECLARE_ARRAY( b2Joint, b2Joint );
B2_DECLARE_ARRAY( b2JointSim, b2JointSim );
B2_DECLARE_ARRAY( b2Island, b2Island );
B2_DECLARE_ARRAY( b2IslandSim, b2IslandSim );
// Declare all the arrays
B2_ARRAY_DECLARE( b2Body, b2Body );
B2_ARRAY_DECLARE( b2BodyMoveEvent, b2BodyMoveEvent );
B2_ARRAY_DECLARE( b2BodySim, b2BodySim );
B2_ARRAY_DECLARE( b2BodyState, b2BodyState );
B2_ARRAY_DECLARE( b2ChainShape, b2ChainShape );
B2_ARRAY_DECLARE( b2Contact, b2Contact );
B2_ARRAY_DECLARE( b2ContactBeginTouchEvent, b2ContactBeginTouchEvent );
B2_ARRAY_DECLARE( b2ContactEndTouchEvent, b2ContactEndTouchEvent );
B2_ARRAY_DECLARE( b2ContactHitEvent, b2ContactHitEvent );
B2_ARRAY_DECLARE( b2ContactSim, b2ContactSim );
B2_ARRAY_DECLARE( b2Island, b2Island );
B2_ARRAY_DECLARE( b2IslandSim, b2IslandSim );
B2_ARRAY_DECLARE( b2Joint, b2Joint );
B2_ARRAY_DECLARE( b2JointSim, b2JointSim );
B2_ARRAY_DECLARE( b2SensorBeginTouchEvent, b2SensorBeginTouchEvent );
B2_ARRAY_DECLARE( b2SensorEndTouchEvent, b2SensorEndTouchEvent );
B2_ARRAY_DECLARE( b2Shape, b2Shape );
B2_ARRAY_DECLARE( b2SolverSet, b2SolverSet );
B2_ARRAY_DECLARE( b2TaskContext, b2TaskContext );
18 changes: 9 additions & 9 deletions src/broad_phase.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ void b2CreateBroadPhase( b2BroadPhase* bp )

bp->proxyCount = 0;
bp->moveSet = b2CreateSet( 16 );
bp->moveArray = b2CreateArray( sizeof( int ), 16 );
bp->moveArray = b2IntArray_Create( 16 );
bp->moveResults = NULL;
bp->movePairs = NULL;
bp->movePairCapacity = 0;
Expand All @@ -55,7 +55,7 @@ void b2DestroyBroadPhase( b2BroadPhase* bp )
}

b2DestroySet( &bp->moveSet );
b2DestroyArray( bp->moveArray, sizeof( int ) );
b2IntArray_Destroy( &bp->moveArray );
b2DestroySet( &bp->pairSet );

memset( bp, 0, sizeof( b2BroadPhase ) );
Expand All @@ -75,12 +75,12 @@ static inline void b2UnBufferMove( b2BroadPhase* bp, int proxyKey )
{
// Purge from move buffer. Linear search.
// todo if I can iterate the move set then I don't need the moveArray
int count = b2Array( bp->moveArray ).count;
int count = bp->moveArray.count;
for ( int i = 0; i < count; ++i )
{
if ( bp->moveArray[i] == proxyKey )
if ( bp->moveArray.data[i] == proxyKey )
{
b2Array_RemoveSwap( bp->moveArray, i );
b2IntArray_RemoveSwap( &bp->moveArray, i );
break;
}
}
Expand All @@ -102,7 +102,7 @@ int b2BroadPhase_CreateProxy( b2BroadPhase* bp, b2BodyType proxyType, b2AABB aab

void b2BroadPhase_DestroyProxy( b2BroadPhase* bp, int proxyKey )
{
B2_ASSERT( b2Array( bp->moveArray ).count == (int)bp->moveSet.count );
B2_ASSERT( bp->moveArray.count == (int)bp->moveSet.count );
b2UnBufferMove( bp, proxyKey );

--bp->proxyCount;
Expand Down Expand Up @@ -292,7 +292,7 @@ void b2FindPairsTask( int startIndex, int endIndex, uint32_t threadIndex, void*
queryContext.moveResult = bp->moveResults + i;
queryContext.moveResult->pairList = NULL;

int proxyKey = bp->moveArray[i];
int proxyKey = bp->moveArray.data[i];
if ( proxyKey == B2_NULL_INDEX )
{
// proxy was destroyed after it moved
Expand Down Expand Up @@ -336,7 +336,7 @@ void b2UpdateBroadPhasePairs( b2World* world )
{
b2BroadPhase* bp = &world->broadPhase;

int moveCount = b2Array( bp->moveArray ).count;
int moveCount = bp->moveArray.count;
B2_ASSERT( moveCount == (int)bp->moveSet.count );

if ( moveCount == 0 )
Expand Down Expand Up @@ -422,7 +422,7 @@ void b2UpdateBroadPhasePairs( b2World* world )
// }

// Reset move buffer
b2Array_Clear( bp->moveArray );
b2IntArray_Clear( &bp->moveArray );
b2ClearSet( &bp->moveSet );

b2FreeStackItem( alloc, bp->movePairs );
Expand Down
4 changes: 2 additions & 2 deletions src/broad_phase.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ typedef struct b2BroadPhase
// todo implement a 32bit hash set for faster lookup
// todo moveSet can grow quite large on the first time step and remain large
b2HashSet moveSet;
int* moveArray;
b2IntArray moveArray;

// These are the results from the pair query and are used to create new contacts
// in deterministic order.
Expand Down Expand Up @@ -78,6 +78,6 @@ static inline void b2BufferMove( b2BroadPhase* bp, int queryProxy )
bool alreadyAdded = b2AddKey( &bp->moveSet, queryProxy + 1 );
if ( alreadyAdded == false )
{
b2Array_Push( bp->moveArray, queryProxy );
b2IntArray_Push( &bp->moveArray, queryProxy );
}
}
3 changes: 3 additions & 0 deletions src/shape.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

#include <stddef.h>

B2_ARRAY_SOURCE( b2ChainShape, b2ChainShape );
B2_ARRAY_SOURCE( b2Shape, b2Shape );

static b2Shape* b2GetShape( b2World* world, b2ShapeId shapeId )
{
int id = shapeId.index1 - 1;
Expand Down
3 changes: 3 additions & 0 deletions src/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,6 @@ b2CastOutput b2RayCastShape( const b2RayCastInput* input, const b2Shape* shape,
b2CastOutput b2ShapeCastShape( const b2ShapeCastInput* input, const b2Shape* shape, b2Transform transform );

b2Transform b2GetOwnerTransform( b2World* world, b2Shape* shape );

B2_ARRAY_INLINE( b2ChainShape, b2ChainShape );
B2_ARRAY_INLINE( b2Shape, b2Shape );
Loading

0 comments on commit c1f8317

Please sign in to comment.