Skip to content

Commit

Permalink
Fix compile issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurSonzogni committed Jun 11, 2023
1 parent ebb2457 commit b86358c
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 79 deletions.
43 changes: 17 additions & 26 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -33,21 +34,21 @@ 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
)

# SMK is a multimedia framework, however I am using it only to add some sounds.
# 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
)

Expand Down Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)
Expand Down
Binary file added resources/bounce.ogg
Binary file not shown.
15 changes: 9 additions & 6 deletions src/board.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ struct CollisionCallback : public b2QueryCallback {
bool collided = false;
};

// static
b2Vec2 Gravity() {
const float gravity = 140.f;
return b2Vec2(0.F, gravity);
Expand All @@ -36,8 +37,9 @@ b2Vec2 Gravity() {
Board::Board(BoardConfig config,
std::function<void()> win,
std::function<void()> lose)
: config_(config), win_(win), lose_(lose), world_(Gravity()) {
world_.SetContactListener(&contact_listener_);
: config_(config), win_(win), lose_(lose) {
world_ = std::make_unique<b2World>(Gravity());
world_->SetContactListener(&contact_listener_);

InitializeBricks();
PlayBackgroundMusic();
Expand Down Expand Up @@ -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<BrickBase>(world_, x, y, half_width,
bricks_.push_back(std::make_unique<BrickBase>(*world_, x, y, half_width,
half_height, counter));
const size_t bricks = 100;
if (bricks_.size() >= bricks) {
Expand Down Expand Up @@ -122,6 +124,7 @@ bool Board::OnEvent(ftxui::Event event) {
}

void Board::Step() {

// Win condition:
if (bricks_.size() == 0) {
win_();
Expand All @@ -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();
Expand All @@ -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<BallBase>(world_, ShootPosition(),
balls_.push_back(std::make_unique<BallBase>(*world_, ShootPosition(),
shooting_direction_, radius));
}

Expand Down
2 changes: 1 addition & 1 deletion src/board.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ class Board {
void MoveUp();
void MoveBricks();

std::unique_ptr<b2World> world_;
const BoardConfig config_;

std::function<void()> win_;
std::function<void()> lose_;

b2World world_;
ContactListener contact_listener_;

std::vector<Ball> balls_;
Expand Down
26 changes: 11 additions & 15 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <ftxui/component/component.hpp>
#include <ftxui/component/event.hpp>
#include <ftxui/component/loop.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/dom/elements.hpp>
#include <iostream>
Expand Down Expand Up @@ -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<bool> 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) {
Expand Down
2 changes: 1 addition & 1 deletion src/resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
26 changes: 4 additions & 22 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
3 changes: 0 additions & 3 deletions test/catch_main.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion test/game_unittests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <catch2/catch.hpp>
#include <catch2/catch_test_macros.hpp>
#include "game.hpp"

using namespace ftxui;
Expand Down
2 changes: 1 addition & 1 deletion test/intro_unittests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <catch2/catch.hpp>
#include <catch2/catch_test_macros.hpp>
#include "game.hpp"

using namespace ftxui;
Expand Down
2 changes: 1 addition & 1 deletion test/lose_unittests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <catch2/catch.hpp>
#include <catch2/catch_test_macros.hpp>
#include "game.hpp"

using namespace ftxui;
Expand Down
2 changes: 1 addition & 1 deletion test/main_menu_unittests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <catch2/catch.hpp>
#include <catch2/catch_test_macros.hpp>
#include "game.hpp"

using namespace ftxui;
Expand Down
2 changes: 1 addition & 1 deletion test/win_unittests.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <catch2/catch.hpp>
#include <catch2/catch_test_macros.hpp>
#include "game.hpp"

using namespace ftxui;
Expand Down

0 comments on commit b86358c

Please sign in to comment.