From 85ed3a758a322913d3cae305a2bae40aaafeab88 Mon Sep 17 00:00:00 2001 From: Christoph Urlacher Date: Fri, 27 Feb 2026 12:31:08 +0100 Subject: [PATCH] make windows-compliant - fix BS_thread_pool include error (pulls in windows.h, symbols were redefined) - remove all std::println uses :(, seems like mingw doesn't like those - allow to disable backward from cmake --- CMakeLists.txt | 38 +++++++++++++++++++++++++------------- compile_commands.json | 2 +- include/config.hpp | 2 +- include/physics.hpp | 19 ++++++++++++++++--- include/puzzle.hpp | 30 +++++++++++++++++++----------- src/gui.cpp | 4 ++-- src/input.cpp | 18 +++++++++--------- src/main.cpp | 12 ++++++++++++ src/physics.cpp | 5 ----- src/puzzle.cpp | 6 +++--- src/state.cpp | 34 ++++++++++++++++++++++------------ 11 files changed, 110 insertions(+), 60 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 1309df7..1b6f48f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,7 +16,6 @@ message("-- CMAKE_CXX_FLAGS_DEBUG: ${CMAKE_CXX_FLAGS_DEBUG}") message("-- CMAKE_CXX_FLAGS_RELEASE: ${CMAKE_CXX_FLAGS_RELEASE}") find_package(raylib REQUIRED) -find_package(Backward REQUIRED) # Headers + Sources include_directories(include) @@ -38,10 +37,16 @@ set(SOURCES # Main target add_executable(masssprings ${SOURCES}) target_include_directories(masssprings PUBLIC ${RAYLIB_CPP_INCLUDE_DIR}) -target_link_libraries(masssprings PUBLIC raylib Backward::Backward) +if(NOT DEFINED DISABLE_BACKWARD) + find_package(Backward REQUIRED) + target_link_libraries(masssprings PUBLIC raylib Backward::Backward) + target_compile_definitions(masssprings PUBLIC BACKWARD) +else() + target_link_libraries(masssprings PUBLIC raylib) +endif() # Tracy target -if(USE_TRACY) +if(NOT DEFINED DISABLE_TRACY) include(FetchContent) FetchContent_Declare(tracy GIT_REPOSITORY https://github.com/wolfpld/tracy.git @@ -56,18 +61,25 @@ if(USE_TRACY) add_executable(masssprings_tracy ${SOURCES}) target_include_directories(masssprings_tracy PUBLIC ${RAYLIB_CPP_INCLUDE_DIR}) target_compile_definitions(masssprings_tracy PRIVATE TRACY) - target_link_libraries(masssprings_tracy PUBLIC raylib Backward::Backward TracyClient) + if(NOT DEFINED DISABLE_BACKWARD) + target_link_libraries(masssprings_tracy PUBLIC raylib Backward::Backward TracyClient) + target_compile_definitions(masssprings_tracy PUBLIC BACKWARD) + else() + target_link_libraries(masssprings_tracy PUBLIC raylib TracyClient) + endif() endif() # LTO -include(CheckIPOSupported) -check_ipo_supported(RESULT supported OUTPUT error) -if(supported) - message(STATUS "IPO / LTO enabled") - set_property(TARGET masssprings PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) - if(USE_TRACY) - set_property(TARGET masssprings_tracy PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) +if (NOT WIN32) + include(CheckIPOSupported) + check_ipo_supported(RESULT supported OUTPUT error) + if(supported) + message(STATUS "IPO / LTO enabled") + set_property(TARGET masssprings PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) + if(USE_TRACY) + set_property(TARGET masssprings_tracy PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE) + endif() + else() + message(STATUS "IPO / LTO not supported: <${error}>") endif() -else() - message(STATUS "IPO / LTO not supported: <${error}>") endif() diff --git a/compile_commands.json b/compile_commands.json index 66636ac..fd9db9d 120000 --- a/compile_commands.json +++ b/compile_commands.json @@ -1 +1 @@ -./cmake-build-debug/compile_commands.json \ No newline at end of file +./cmake-build-release/compile_commands.json \ No newline at end of file diff --git a/include/config.hpp b/include/config.hpp index a6a9e02..0b6bceb 100644 --- a/include/config.hpp +++ b/include/config.hpp @@ -4,9 +4,9 @@ #include #define THREADPOOL // Enable physics threadpool -#define BACKWARD // Enable pretty stack traces // Gets set by CMake +// #define BACKWARD // Enable pretty stack traces // #define TRACY // Enable tracy profiling support // Window diff --git a/include/physics.hpp b/include/physics.hpp index 0c3209f..a948e20 100644 --- a/include/physics.hpp +++ b/include/physics.hpp @@ -8,7 +8,6 @@ #include #include #include -#include #include #include #include @@ -17,8 +16,16 @@ #include #ifdef THREADPOOL +#if defined(_WIN32) +#define NOGDI // All GDI defines and routines +#define NOUSER // All USER defines and routines +#endif #define BS_THREAD_POOL_NATIVE_EXTENSIONS #include +#if defined(_WIN32) // raylib uses these names as function parameters +#undef near +#undef far +#endif #endif #ifdef TRACY @@ -78,10 +85,16 @@ public: : threads(std::thread::hardware_concurrency() - 1, SetThreadName) #endif { - std::println("Using Barnes-Hut + Octree repulsion force calculation."); + std::cout << std::format( + "Using Barnes-Hut + Octree repulsion force calculation.") + << std::endl; #ifdef THREADPOOL - std::println("Thread-pool: {} threads.", threads.get_thread_count()); + std::cout << std::format("Thread-pool: {} threads.", + threads.get_thread_count()) + << std::endl; +#else + std::cout << std::format("Thread-pool: Disabled.") << std::endl; #endif }; diff --git a/include/puzzle.hpp b/include/puzzle.hpp index fa0912c..4f2f2bd 100644 --- a/include/puzzle.hpp +++ b/include/puzzle.hpp @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include @@ -40,7 +40,7 @@ public: : x(_x), y(_y), width(_width), height(_height), target(_target), immovable(_immovable) { if (_x < 0 || _x + _width >= 10 || _y < 0 || _y + _height >= 10) { - std::println("Block must fit in a 9x9 board!"); + std::cout << std::format("Block must fit in a 9x9 board!") << std::endl; exit(1); } } @@ -92,11 +92,12 @@ public: } if (_x < 0 || _x + width >= 10 || _y < 0 || _y + height >= 10) { - std::println("Block must fit in a 9x9 board!"); + std::cout << std::format("Block must fit in a 9x9 board!") << std::endl; exit(1); } if (block.length() != 2) { - std::println("Block representation must have length 2!"); + std::cout << std::format("Block representation must have length 2!") + << std::endl; exit(1); } } @@ -186,12 +187,15 @@ public: _height, _target_x, _target_y, std::string(_width * _height * 2, '.'))) { if (_width < 1 || _width > 9 || _height < 1 || _height > 9) { - std::println("State width/height must be in [1, 9]!"); + std::cout << std::format("State width/height must be in [1, 9]!") + << std::endl; exit(1); } if (_target_x < 0 || _target_x >= 9 || _target_y < 0 || _target_y >= 9) { if (_target_x != 9 && _target_y != 9) { - std::println("State target must be within the board bounds!"); + std::cout << std::format( + "State target must be within the board bounds!") + << std::endl; exit(1); } } @@ -209,19 +213,23 @@ public: target_y(std::stoi(_state.substr(4, 1))), restricted(_state.substr(0, 1) == "R"), state(_state) { if (width < 1 || width > 9 || height < 1 || height > 9) { - std::println("State width/height must be in [1, 9]!"); + std::cout << std::format("State width/height must be in [1, 9]!") + << std::endl; exit(1); } if (target_x < 0 || target_x >= 9 || target_y < 0 || target_y >= 9) { if (target_x != 9 && target_y != 9) { - std::println("State target must be within the board bounds!"); + std::cout << std::format( + "State target must be within the board bounds!") + << std::endl; exit(1); } } if (static_cast(_state.length()) != width * height * 2 + prefix) { - std::println( - "State representation must have length width * height * 2 + {}!", - prefix); + std::cout << std::format("State representation must have length width * " + "height * 2 + {}!", + prefix) + << std::endl; exit(1); } } diff --git a/src/gui.cpp b/src/gui.cpp index 57af3a5..b94f994 100644 --- a/src/gui.cpp +++ b/src/gui.cpp @@ -44,7 +44,7 @@ auto Grid::Bounds() const -> Rectangle { auto Grid::Bounds(int _x, int _y, int _width, int _height) const -> Rectangle { if (_x < 0 || _x + _width > columns || _y < 0 || _y + _height > rows) { - std::println("Grid bounds are outside range."); + std::cout << std::format("Grid bounds are outside range.") << std::endl; exit(1); } @@ -71,7 +71,7 @@ auto Grid::SquareBounds(int _x, int _y, int _width, int _height) const // filled if (_x < 0 || _x + _width > columns || _y < 0 || _y + _height > rows) { - std::println("Grid bounds are outside range."); + std::cout << std::format("Grid bounds are outside range.") << std::endl; exit(1); } diff --git a/src/input.cpp b/src/input.cpp index eea0261..71d1796 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -1,8 +1,6 @@ #include "input.hpp" #include "config.hpp" -#include -#include #include #ifdef TRACY @@ -269,16 +267,18 @@ auto InputHandler::MoveBlockWes() -> void { } auto InputHandler::PrintState() const -> void { - std::println("State: \"{}\"", state.current_state.state); + std::cout << std::format("State: \"{}\"", state.current_state.state) + << std::endl; Block sel = state.current_state.GetBlock(sel_x, sel_y); int idx = state.current_state.GetIndex(sel.x, sel.y) - State::prefix; if (sel.IsValid()) { - std::println("Sel: \"{}{}{}{}\"", - state.current_state.state.substr(0, State::prefix), - std::string(idx, '.'), sel.ToString(), - std::string(state.current_state.state.length() - idx - - State::prefix - 2, - '.')); + std::cout << std::format("Sel: \"{}{}{}{}\"", + state.current_state.state.substr(0, State::prefix), + std::string(idx, '.'), sel.ToString(), + std::string(state.current_state.state.length() - + idx - State::prefix - 2, + '.')) + << std::endl; } } diff --git a/src/main.cpp b/src/main.cpp index 9bdf6d8..34d54e3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,6 +36,18 @@ auto main(int argc, char *argv[]) -> int { preset_file = argv[1]; } +#ifdef BACKWARD + std::cout << std::format("Backward stack-traces enabled.") << std::endl; +#else + std::cout << std::format("Backward stack-traces disabled.") << std::endl; +#endif + +#ifdef TRACY + std::cout << std::format("Tracy adapter enabled.") << std::endl; +#else + std::cout << std::format("Tracy adapter disabled.") << std::endl; +#endif + // RayLib window setup SetTraceLogLevel(LOG_ERROR); SetConfigFlags(FLAG_VSYNC_HINT); diff --git a/src/physics.cpp b/src/physics.cpp index 1f328c0..046ba82 100644 --- a/src/physics.cpp +++ b/src/physics.cpp @@ -10,11 +10,6 @@ #include #include -#ifdef THREADPOOL -#define BS_THREAD_POOL_NATIVE_EXTENSIONS -#include -#endif - #ifdef TRACY #include "tracy.hpp" #include diff --git a/src/puzzle.cpp b/src/puzzle.cpp index 0374132..415ea45 100644 --- a/src/puzzle.cpp +++ b/src/puzzle.cpp @@ -1,7 +1,6 @@ #include "puzzle.hpp" #include "config.hpp" -#include #include #ifdef TRACY @@ -389,8 +388,9 @@ auto State::Closure() const } } while (remaining_states.size() > 0); - std::println("State space has size {} with {} transitions.", states.size(), - links.size()); + std::cout << std::format("State space has size {} with {} transitions.", + states.size(), links.size()) + << std::endl; return std::make_pair(states, links); } diff --git a/src/state.cpp b/src/state.cpp index 2a9c773..5ca7c5a 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -4,7 +4,6 @@ #include #include -#include #include #ifdef TRACY @@ -17,7 +16,9 @@ auto StateManager::ParsePresetFile(const std::string &_preset_file) -> bool { std::ifstream file(preset_file); if (!file) { - std::println("Preset file \"{}\" couldn't be loaded.", preset_file); + std::cout << std::format("Preset file \"{}\" couldn't be loaded.", + preset_file) + << std::endl; return false; } @@ -33,7 +34,9 @@ auto StateManager::ParsePresetFile(const std::string &_preset_file) -> bool { } if (preset_lines.size() == 0 || comment_lines.size() != preset_lines.size()) { - std::println("Preset file \"{}\" couldn't be loaded.", preset_file); + std::cout << std::format("Preset file \"{}\" couldn't be loaded.", + preset_file) + << std::endl; return false; } @@ -43,24 +46,29 @@ auto StateManager::ParsePresetFile(const std::string &_preset_file) -> bool { } comments = comment_lines; - std::println("Loaded {} presets from \"{}\".", preset_lines.size(), - preset_file); + std::cout << std::format("Loaded {} presets from \"{}\".", + preset_lines.size(), preset_file) + << std::endl; return true; } auto StateManager::AppendPresetFile(const std::string preset_name) -> void { - std::println("Saving preset \"{}\" to \"{}\"", preset_name, preset_file); + std::cout << std::format("Saving preset \"{}\" to \"{}\"", preset_name, + preset_file) + << std::endl; std::ofstream file(preset_file, std::ios_base::app | std::ios_base::out); if (!file) { - std::println("Preset file \"{}\" couldn't be loaded.", preset_file); + std::cout << std::format("Preset file \"{}\" couldn't be loaded.", + preset_file) + << std::endl; return; } file << "\n# " << preset_name << "\n" << current_state.state << std::flush; - std::println("Refreshing presets..."); + std::cout << std::format("Refreshing presets...") << std::endl; if (ParsePresetFile(preset_file)) { LoadPreset(presets.size() - 1); } @@ -139,7 +147,8 @@ auto StateManager::FillGraph() -> void { // Sanity check. Both values need to be equal // for (const auto &[mass, state] : masses) { - // std::println("Masses: {}, States: {}", mass, states.at(state)); + // std::cout << std::format("Masses: {}, States: {}", mass, + // states.at(state)) << std::endl; // } } @@ -226,8 +235,8 @@ auto StateManager::FindTargetDistances() -> void { target_distances = CalculateDistances(states.size(), springs, targets); - // std::println("Calculated {} distances to {} targets.", - // target_distances.distances.size(), targets.size()); + // std::cout << std::format("Calculated {} distances to {} targets.", + // target_distances.distances.size(), targets.size()) << std::endl; } auto StateManager::FindTargetPath() -> void { @@ -236,7 +245,8 @@ auto StateManager::FindTargetPath() -> void { } winning_path = GetPath(target_distances, CurrentMassIndex()); - // std::println("Nearest target is {} moves away.", winning_path.size()); + // std::cout << std::format("Nearest target is {} moves away.", + // winning_path.size()) << std::endl; } auto StateManager::FindWorstState() -> State {