From d484c6ce655b4a14eaf201fdaf9923764c120070 Mon Sep 17 00:00:00 2001 From: Erin Catto Date: Sat, 9 Dec 2023 21:26:04 -0800 Subject: [PATCH] Xcode fixes (#72) `B2_DEBUG` type conversion --- include/box2d/types.h | 6 ++++++ samples/CMakeLists.txt | 5 +---- samples/collection/sample_events.cpp | 2 +- samples/sample.h | 6 +++--- samples/settings.cpp | 4 ++-- src/CMakeLists.txt | 3 ++- src/block_allocator.c | 4 ++-- src/core.h | 8 ++++---- src/dynamic_tree.c | 2 +- src/graph.c | 4 ++-- src/table.c | 6 +++--- src/timer.c | 4 ++-- test/test_table.c | 4 ++-- 13 files changed, 31 insertions(+), 27 deletions(-) diff --git a/include/box2d/types.h b/include/box2d/types.h index a7052aa0..7a7f7f34 100644 --- a/include/box2d/types.h +++ b/include/box2d/types.h @@ -19,6 +19,12 @@ #define B2_ZERO_INIT {0} #endif +#ifdef NDEBUG +#define B2_DEBUG 0 +#else +#define B2_DEBUG 1 +#endif + #define B2_ARRAY_COUNT(A) (int)(sizeof(A) / sizeof(A[0])) #define B2_MAYBE_UNUSED(x) ((void)(x)) #define B2_NULL_INDEX (-1) diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index e31c3180..0f38612f 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -94,10 +94,7 @@ set_target_properties(samples PROPERTIES target_include_directories(samples PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${JSMN_DIR}) target_link_libraries(samples PUBLIC box2d imgui glfw glad enkiTS) -if (${CMAKE_BUILD_TYPE} STREQUAL Debug) - target_compile_definitions(samples PRIVATE _DEBUG) -endif () - +# target_compile_definitions(samples PRIVATE "$<$:SAMPLES_DEBUG>") # message(STATUS "runtime = ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}") # message(STATUS "binary = ${CMAKE_CURRENT_BINARY_DIR}") diff --git a/samples/collection/sample_events.cpp b/samples/collection/sample_events.cpp index f4ffcf9e..b6b15b43 100644 --- a/samples/collection/sample_events.cpp +++ b/samples/collection/sample_events.cpp @@ -269,7 +269,7 @@ class Sensor : public Sample Ring* ring = (Ring*)b2Body_GetUserData(ringBodyId); if (ring != nullptr && ring->valid) { - int index = ring - m_rings; + int index = (int)(ring - m_rings); assert(0 <= index && index < e_count); // Defer destruction to avoid double destruction and event invalidation (orphaned shape ids) diff --git a/samples/sample.h b/samples/sample.h index 2f92f146..0bc49f09 100644 --- a/samples/sample.h +++ b/samples/sample.h @@ -17,10 +17,10 @@ struct Settings; -#if defined(_DEBUG) -constexpr bool g_sampleDebug = true; -#else +#ifdef NDEBUG constexpr bool g_sampleDebug = false; +#else +constexpr bool g_sampleDebug = true; #endif #define RAND_LIMIT 32767 diff --git a/samples/settings.cpp b/samples/settings.cpp index c5541041..b4c12c08 100644 --- a/samples/settings.cpp +++ b/samples/settings.cpp @@ -23,7 +23,7 @@ static bool ReadFile(char*& data, int& size, const char* filename) } fseek(file, 0, SEEK_END); - size = ftell(file); + size = (int)ftell(file); fseek(file, 0, SEEK_SET); if (size == 0) @@ -102,7 +102,7 @@ void Settings::Load() const char* s = data + tokens[i + 1].start; strncpy(buffer, s, count); char* dummy; - m_sampleIndex = strtol(buffer, &dummy, 10); + m_sampleIndex = (int)strtol(buffer, &dummy, 10); } else if (jsoneq(data, &tokens[i], "drawShapes") == 0) { diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fa3a5de0..dfeb8ebf 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -85,6 +85,7 @@ target_include_directories(box2d ) target_link_libraries(box2d PRIVATE simde) +#target_compile_definitions(box2d PRIVATE "$<$:B2_DEBUG>") message(STATUS "CMake C compiler: ${CMAKE_C_COMPILER_ID}") message(STATUS "CMake C++ compiler: ${CMAKE_CXX_COMPILER_ID}") @@ -96,7 +97,7 @@ if (MSVC) target_compile_options(box2d PRIVATE /W4 /WX /experimental:c11atomics) target_compile_options(box2d PRIVATE /arch:AVX2) # target_compile_options(box2d PRIVATE /arch:SSE2) - # target_compile_definitions(box2d PUBLIC "$<$:B2_ENABLE_ASSERT>") + target_compile_definitions(box2d PUBLIC "$<$:B2_ENABLE_ASSERT>") elseif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") message(STATUS "CMake using Clang-CL") target_compile_options(box2d PRIVATE /W4 /WX /std:c17 /arch:AVX2) diff --git a/src/block_allocator.c b/src/block_allocator.c index e6393445..7f548b94 100644 --- a/src/block_allocator.c +++ b/src/block_allocator.c @@ -153,7 +153,7 @@ void* b2AllocBlock(b2BlockAllocator* allocator, int32_t size) b2Chunk* chunk = allocator->chunks + allocator->chunkCount; chunk->blocks = (b2Block*)b2Alloc(b2_chunkSize); -#if defined(_DEBUG) +#if B2_DEBUG memset(chunk->blocks, 0xcd, b2_chunkSize); #endif int32_t blockSize = b2_blockSizes[index]; @@ -194,7 +194,7 @@ void b2FreeBlock(b2BlockAllocator* allocator, void* p, int32_t size) int32_t index = b2_sizeMap.values[size]; B2_ASSERT(0 <= index && index < b2_blockSizeCount); -#if defined(_DEBUG) +#if B2_DEBUG // Verify the memory address and size is valid. int32_t blockSize = b2_blockSizes[index]; bool found = false; diff --git a/src/core.h b/src/core.h index f4b10183..620a5d68 100644 --- a/src/core.h +++ b/src/core.h @@ -64,7 +64,7 @@ #error Unknown platform #endif -#if defined(_DEBUG) || defined(B2_ENABLE_ASSERT) +#if !defined(NDEBUG) || defined(B2_ENABLE_ASSERT) #define B2_ASSERT(condition) \ do \ { \ @@ -75,8 +75,8 @@ #define B2_ASSERT(...) ((void)0) #endif -#if defined(_DEBUG) -#define B2_VALIDATE 1 -#else +#if defined(NDEBUG) #define B2_VALIDATE 0 +#else +#define B2_VALIDATE 1 #endif diff --git a/src/dynamic_tree.c b/src/dynamic_tree.c index 2cda4307..93a10435 100644 --- a/src/dynamic_tree.c +++ b/src/dynamic_tree.c @@ -530,7 +530,7 @@ static void b2RotateNodes(b2DynamicTree* tree, int32_t iA) if (costCE < bestCost) { bestRotation = b2_rotateCE; - bestCost = costCE; + // bestCost = costCE; } switch (bestRotation) diff --git a/src/graph.c b/src/graph.c index 453b2d80..d8c7322f 100644 --- a/src/graph.c +++ b/src/graph.c @@ -975,7 +975,7 @@ void b2SolverTask(int32_t startIndex, int32_t endIndex, uint32_t threadIndexDont B2_ASSERT(stages[stageIndex].type == b2_stagePrepareJoints); b2ExecuteMainStage(stages + stageIndex, context, syncBits); stageIndex += 1; - jointSyncIndex += 1; + // jointSyncIndex += 1; uint32_t constraintSyncIndex = 1; syncBits = (constraintSyncIndex << 16) | stageIndex; @@ -1054,7 +1054,7 @@ void b2SolverTask(int32_t startIndex, int32_t endIndex, uint32_t threadIndexDont b2ExecuteMainStage(stages + iterStageIndex, context, syncBits); iterStageIndex += 1; } - graphSyncIndex += 1; + // graphSyncIndex += 1; b2ApplyOverflowRestitution(context); } diff --git a/src/table.c b/src/table.c index 69199281..469bc142 100644 --- a/src/table.c +++ b/src/table.c @@ -11,7 +11,7 @@ #include #include -#if _DEBUG +#if B2_DEBUG int32_t g_probeCount; #endif @@ -91,7 +91,7 @@ static inline uint32_t b2KeyHash(uint64_t key) return (uint32_t)h; } -#if _DEBUG +#if B2_DEBUG int32_t g_probeCount; #endif @@ -102,7 +102,7 @@ int32_t b2FindSlot(const b2HashSet* set, uint64_t key, uint32_t hash) const b2SetItem* items = set->items; while (items[index].hash != 0 && items[index].key != key) { -#if _DEBUG +#if B2_DEBUG g_probeCount += 1; #endif index = (index + 1) & (capacity - 1); diff --git a/src/timer.c b/src/timer.c index 8a2b34b2..e06bb5d5 100644 --- a/src/timer.c +++ b/src/timer.c @@ -87,7 +87,7 @@ float b2GetMilliseconds(const b2Timer* timer) struct timeval t; gettimeofday(&t, 0); time_t start_sec = timer->start_sec; - suseconds_t start_usec = timer->start_usec; + suseconds_t start_usec = (suseconds_t)timer->start_usec; // http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html if (t.tv_usec < start_usec) @@ -111,7 +111,7 @@ float b2GetMillisecondsAndReset(b2Timer* timer) struct timeval t; gettimeofday(&t, 0); time_t start_sec = timer->start_sec; - suseconds_t start_usec = timer->start_usec; + suseconds_t start_usec = (suseconds_t)timer->start_usec; // http://www.gnu.org/software/libc/manual/html_node/Elapsed-Time.html if (t.tv_usec < start_usec) diff --git a/test/test_table.c b/test/test_table.c index 80e20bc2..51e9bcaf 100644 --- a/test/test_table.c +++ b/test/test_table.c @@ -54,7 +54,7 @@ int TableTest(void) ENSURE(set.count == (itemCount - removeCount)); -#ifdef _DEBUG +#if B2_DEBUG extern int32_t g_probeCount; g_probeCount = 0; #endif @@ -80,7 +80,7 @@ int TableTest(void) float ms = b2GetMilliseconds(&timer); printf("set: count = %d, b2ContainsKey = %.5f ms, ave = %.5f us\n", itemCount, ms, 1000.0f * ms / itemCount); -#ifdef _DEBUG +#if B2_DEBUG float aveProbeCount = (float)g_probeCount / (float)itemCount; printf("item count = %d, probe count = %d, ave probe count %.2f\n", itemCount, g_probeCount, aveProbeCount); #endif