diff --git a/CMakeLists.txt b/CMakeLists.txt index 034c7a0..f509cc1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,16 @@ cmake_minimum_required(VERSION 3.16) option(ENABLE_TESTING "Enable the tests" ON) option(ENABLE_FUZZING "Enable the fuzz tests" OFF) +# Note: by default ENABLE_DEVELOPER_MODE is True +# This means that all analysis (sanitizers, static analysis) +# is enabled and all warnings are treated as errors +# if you want to switch this behavior, change TRUE to FALSE +set(ENABLE_DEVELOPER_MODE FALSE CACHE BOOL "Enable 'developer mode'") + +# Change this to false if you want to disable warnings_as_errors in developer +# mode +set(OPT_WARNINGS_AS_ERRORS_DEVELOPER_DEFAULT FALSE) + # The audio backend do not work with ASAN and seems to be working only on Linux # and WebAssembly. Sorry! if(UNIX AND NOT APPLE) @@ -11,15 +21,6 @@ else() option(ENABLE_AUDIO "Enable audio" OFF) endif() -# Not ideal to use this global variable, but necessary to make sure -# that tooling and projects use the same version -set(CMAKE_CXX_STANDARD 17) - -# strongly encouraged to enable this globally to avoid conflicts between -# -Wpedantic being enabled and -std=c++20 and -std=gnu++20 for example -# when compiling with PCH enabled -set(CMAKE_CXX_EXTENSIONS OFF) - # Setup dependencies: include(FetchContent) set(FETCHCONTENT_UPDATES_DISCONNECTED TRUE) @@ -33,13 +34,13 @@ FetchContent_Declare(_project_options FetchContent_Declare(ftxui GIT_REPOSITORY https://github.com/ArthurSonzogni/ftxui - GIT_TAG aebde943523bb75da79ba45eb5b2e88d39a620e1 + GIT_TAG v4.1.1 GIT_PROGRESS ON ) FetchContent_Declare(box2d GIT_REPOSITORY https://github.com/erincatto/box2d.git - GIT_TAG v2.4.1 + GIT_TAG 411acc32eb6d4f2e96fc70ddbdf01fe5f9b16230 GIT_PROGRESS ON ) @@ -47,7 +48,7 @@ FetchContent_Declare(box2d # TODO(arthursonzogni): Find something lightweight to replace it. FetchContent_Declare(smk GIT_REPOSITORY https://github.com/ArthurSonzogni/smk - GIT_TAG 65ff24ee244f83e019bf6fb36322c070dbf18875 + GIT_TAG d8604bd4414d2257f02cc68891db1cb5ac7475e8 GIT_PROGRESS ON ) @@ -75,7 +76,6 @@ endif() if (EMSCRIPTEN) set(ENABLE_TESTING CACHE INTERNAL "") - set(ENABLE_DEVELOPER_MODE FALSE CACHE BOOL "Enable 'developer mode'") string(APPEND CMAKE_CXX_FLAGS " -s USE_PTHREADS") string(APPEND CMAKE_EXE_LINKER_FLAGS " -s ASYNCIFY") string(APPEND CMAKE_EXE_LINKER_FLAGS " -s PROXY_TO_PTHREAD") @@ -98,16 +98,6 @@ project(TermBreaker HOMEPAGE_URL "https://github.com/ArthurSonzogni/termBreaker" LANGUAGES CXX) -# Note: by default ENABLE_DEVELOPER_MODE is True -# This means that all analysis (sanitizers, static analysis) -# is enabled and all warnings are treated as errors -# if you want to switch this behavior, change TRUE to FALSE -set(ENABLE_DEVELOPER_MODE TRUE CACHE BOOL "Enable 'developer mode'") - -# Change this to false if you want to disable warnings_as_errors in developer -# mode -set(OPT_WARNINGS_AS_ERRORS_DEVELOPER_DEFAULT TRUE) - find_package(Threads) add_library(termBreakerLib STATIC @@ -146,7 +136,6 @@ if (ENABLE_AUDIO) target_link_system_libraries(termBreakerLib PRIVATE smk::smk) endif() - set(GIT_SHA "Unknown" CACHE STRING "SHA this build was generated from") string(SUBSTRING "${GIT_SHA}" 0 8 GIT_SHORT_SHA) @@ -211,13 +200,15 @@ target_link_system_libraries(termBreaker PRIVATE termBreakerLib) if (EMSCRIPTEN) - target_link_options(termBreaker PUBLIC "SHELL: -s TOTAL_MEMORY=67108864") + target_link_options(termBreaker PUBLIC "SHELL: -s TOTAL_MEMORY=256MB") + target_compile_options(termBreaker PUBLIC "-gsource-map") + target_compile_options(termBreaker PUBLIC "--source-map-base=\"https://localhost:8888/\"") foreach(file "index.html" "run_webassembly.py" "resources/background.ogg" - "resources/bounce.wav" + "resources/bounce.ogg" "resources/termBreaker") configure_file(${file} ${file} COPYONLY) endforeach(file) diff --git a/resources/bounce.ogg b/resources/bounce.ogg new file mode 100644 index 0000000..5c1dde2 Binary files /dev/null and b/resources/bounce.ogg differ diff --git a/src/board.cpp b/src/board.cpp index d42e19c..485c77e 100644 --- a/src/board.cpp +++ b/src/board.cpp @@ -26,6 +26,7 @@ struct CollisionCallback : public b2QueryCallback { bool collided = false; }; +// static b2Vec2 Gravity() { const float gravity = 140.f; return b2Vec2(0.F, gravity); @@ -36,8 +37,9 @@ b2Vec2 Gravity() { Board::Board(BoardConfig config, std::function win, std::function lose) - : config_(config), win_(win), lose_(lose), world_(Gravity()) { - world_.SetContactListener(&contact_listener_); + : config_(config), win_(win), lose_(lose) { + world_ = std::make_unique(Gravity()); + world_->SetContactListener(&contact_listener_); InitializeBricks(); PlayBackgroundMusic(); @@ -78,12 +80,12 @@ void Board::InitializeBricks() { continue; CollisionCallback callback; - world_.QueryAABB(&callback, aabb); + world_->QueryAABB(&callback, aabb); if (callback.collided) { continue; } - bricks_.push_back(std::make_unique(world_, x, y, half_width, + bricks_.push_back(std::make_unique(*world_, x, y, half_width, half_height, counter)); const size_t bricks = 100; if (bricks_.size() >= bricks) { @@ -122,6 +124,7 @@ bool Board::OnEvent(ftxui::Event event) { } void Board::Step() { + // Win condition: if (bricks_.size() == 0) { win_(); @@ -141,7 +144,7 @@ void Board::Step() { float timeStep = 1.0f / 60.0f; // NOLINT int32 velocityIterations = 6; // NOLINT int32 positionIterations = 2; // NOLINT - world_.Step(timeStep, velocityIterations, positionIterations); + world_->Step(timeStep, velocityIterations, positionIterations); for (auto& brick : bricks_) { brick->Step(); @@ -164,7 +167,7 @@ void Board::Step() { remaining_balls_to_shoot_ >= 1) { remaining_balls_to_shoot_--; const float radius = 4.F; - balls_.push_back(std::make_unique(world_, ShootPosition(), + balls_.push_back(std::make_unique(*world_, ShootPosition(), shooting_direction_, radius)); } diff --git a/src/board.hpp b/src/board.hpp index eb520f5..2d50bb8 100644 --- a/src/board.hpp +++ b/src/board.hpp @@ -44,12 +44,12 @@ class Board { void MoveUp(); void MoveBricks(); + std::unique_ptr world_; const BoardConfig config_; std::function win_; std::function lose_; - b2World world_; ContactListener contact_listener_; std::vector balls_; diff --git a/src/game.cpp b/src/game.cpp index 094fefe..62000fb 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -45,25 +46,20 @@ void ExecuteBoard(BoardConfig config, lose(); exit(); }; - Board board(config, on_win, on_lose); - // This thread exists to make sure that the event queue has an event to - // process at approximately a rate of 60 FPS - std::atomic refresh_ui_continue = true; - std::thread refresh_ui([&] { - while (refresh_ui_continue) { - using namespace std::chrono_literals; - const auto refresh_time = 1.0s / 60.0; - std::this_thread::sleep_for(refresh_time); - screen.PostEvent(Event::Custom); - } - }); + Board board(config, on_win, on_lose); auto component = GameScreen(board, on_lose, on_quit); - screen.Loop(component); - refresh_ui_continue = false; - refresh_ui.join(); + Loop loop(&screen, component); + + while (!loop.HasQuitted()) { + loop.RunOnce(); + using namespace std::chrono_literals; + const auto refresh_time = 1.0s / 60.0; + std::this_thread::sleep_for(refresh_time); + screen.PostEvent(Event::Custom); + } } void ExecuteWinScreen(int coins) { diff --git a/src/resources.cpp b/src/resources.cpp index cc249f1..9559e09 100644 --- a/src/resources.cpp +++ b/src/resources.cpp @@ -80,7 +80,7 @@ void PlaySound(const smk::SoundBuffer& snd, float volume) { } // namespace void LoadResources() { - sb_boing = smk::SoundBuffer(ResourcePath() + "/bounce.wav"); + sb_boing = smk::SoundBuffer(ResourcePath() + "/bounce.ogg"); sb_background = smk::SoundBuffer(ResourcePath() + "/background.ogg"); } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a884d59..929e747 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -2,41 +2,23 @@ include(CTest) FetchContent_Declare(Catch2 GIT_REPOSITORY https://github.com/catchorg/Catch2.git - GIT_TAG v2.13.1 + GIT_TAG v3.3.2 GIT_PROGRESS ON ) -FetchContent_GetProperties(Catch2) FetchContent_MakeAvailable(Catch2) list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/contrib) - include(Catch) -add_library(catch_main OBJECT catch_main.cpp) -target_link_system_libraries(catch_main - PUBLIC Catch2::Catch2 -) - add_executable(termBreakerTests intro_unittests.cpp win_unittests.cpp main_menu_unittests.cpp game_unittests.cpp ) -target_link_system_libraries(termBreakerTests - PRIVATE catch_main - PRIVATE project_options - PRIVATE project_warnings +target_link_libraries(termBreakerTests + PRIVATE Catch2::Catch2WithMain PRIVATE termBreakerLib ) -# automatically discover tests that are defined in catch based test files you -# can modify the unittests. Set TEST_PREFIX to whatever you want, or use -# different for different binaries -catch_discover_tests(termBreakerTests - TEST_PREFIX "unittests." - REPORTER xml - OUTPUT_DIR . - OUTPUT_PREFIX "unittests." - OUTPUT_SUFFIX .xml -) +catch_discover_tests(termBreakerTests) diff --git a/test/catch_main.cpp b/test/catch_main.cpp deleted file mode 100644 index 28ceb8c..0000000 --- a/test/catch_main.cpp +++ /dev/null @@ -1,3 +0,0 @@ -#define CATCH_CONFIG_MAIN// This tells the catch header to generate a main - -#include diff --git a/test/game_unittests.cpp b/test/game_unittests.cpp index 0afc907..6e4efc3 100644 --- a/test/game_unittests.cpp +++ b/test/game_unittests.cpp @@ -1,4 +1,4 @@ -#include +#include #include "game.hpp" using namespace ftxui; diff --git a/test/intro_unittests.cpp b/test/intro_unittests.cpp index e44df14..fe22fed 100644 --- a/test/intro_unittests.cpp +++ b/test/intro_unittests.cpp @@ -1,4 +1,4 @@ -#include +#include #include "game.hpp" using namespace ftxui; diff --git a/test/lose_unittests.cpp b/test/lose_unittests.cpp index bb18a9c..2be176c 100644 --- a/test/lose_unittests.cpp +++ b/test/lose_unittests.cpp @@ -1,4 +1,4 @@ -#include +#include #include "game.hpp" using namespace ftxui; diff --git a/test/main_menu_unittests.cpp b/test/main_menu_unittests.cpp index c7a9fb3..028dfdd 100644 --- a/test/main_menu_unittests.cpp +++ b/test/main_menu_unittests.cpp @@ -1,4 +1,4 @@ -#include +#include #include "game.hpp" using namespace ftxui; diff --git a/test/win_unittests.cpp b/test/win_unittests.cpp index d32d4e6..44127dd 100644 --- a/test/win_unittests.cpp +++ b/test/win_unittests.cpp @@ -1,4 +1,4 @@ -#include +#include #include "game.hpp" using namespace ftxui;