Skip to content

Commit

Permalink
feat(box2d): add a minimal box2d system (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
Milerius authored Oct 2, 2019
1 parent f4b043d commit b1ace12
Show file tree
Hide file tree
Showing 8 changed files with 183 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,17 @@ jobs:
if: runner.os == 'Linux' || runner.os == 'macOS'
run: |
if [[ "${{matrix.name}}" == "ubuntu-18-04-emcc-latest-debug" ]]; then
cmake -B build -S . -G Ninja -DCMAKE_BUILD_TYPE=${ANTARA_BUILD_TYPE} -DANTARA_BUILD_EXAMPLES=ON -DUSE_LUA_ANTARA_WRAPPER=ON -DANTARA_BUILD_UNIT_TESTS=ON -DCMAKE_TOOLCHAIN_FILE=emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CXX_COMPILER=$CXX -DCMAKE_C_COMPILER=$CC
cmake -B build -S . -G Ninja -DCMAKE_BUILD_TYPE=${ANTARA_BUILD_TYPE} -DANTARA_BUILD_EXAMPLES=ON -DUSE_LUA_ANTARA_WRAPPER=ON -DANTARA_BUILD_UNIT_TESTS=ON -DUSE_BOX2D_ANTARA_WRAPPER=ON -DCMAKE_TOOLCHAIN_FILE=emscripten/cmake/Modules/Platform/Emscripten.cmake -DCMAKE_CXX_COMPILER=$CXX -DCMAKE_C_COMPILER=$CC
else
cmake -B build -S . -G Ninja -DCMAKE_BUILD_TYPE=${ANTARA_BUILD_TYPE} -DANTARA_BUILD_EXAMPLES=ON -DUSE_LUA_ANTARA_WRAPPER=ON -DANTARA_BUILD_UNIT_TESTS=ON -DUSE_SFML_ANTARA_WRAPPER=ON
cmake -B build -S . -G Ninja -DCMAKE_BUILD_TYPE=${ANTARA_BUILD_TYPE} -DANTARA_BUILD_EXAMPLES=ON -DUSE_LUA_ANTARA_WRAPPER=ON -DANTARA_BUILD_UNIT_TESTS=ON -DUSE_SFML_ANTARA_WRAPPER=ON -DUSE_BOX2D_ANTARA_WRAPPER=ON
fi
- name: Configure (Windows)
env:
ANTARA_BUILD_TYPE: ${{matrix.cmake_build_type}}
if: runner.os == 'Windows'
run: |
cmake -B build -S . -G Ninja -DCMAKE_BUILD_TYPE=${ANTARA_BUILD_TYPE} -DANTARA_BUILD_EXAMPLES=ON -DUSE_LUA_ANTARA_WRAPPER=ON -DANTARA_BUILD_UNIT_TESTS=ON -DUSE_SFML_ANTARA_WRAPPER=ON
cmake -B build -S . -G Ninja -DCMAKE_BUILD_TYPE=${ANTARA_BUILD_TYPE} -DANTARA_BUILD_EXAMPLES=ON -DUSE_LUA_ANTARA_WRAPPER=ON -DANTARA_BUILD_UNIT_TESTS=ON -DUSE_SFML_ANTARA_WRAPPER=ON -DUSE_BOX2D_ANTARA_WRAPPER=ON
- name: TSan (Linux)
if: matrix.name == 'ubuntu-18-04-clang-8-debug-tsan'
Expand Down
18 changes: 18 additions & 0 deletions cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ FetchContent_Declare(
URL https://github.com/veselink1/refl-cpp/archive/v0.5.2.zip
)

if (USE_BOX2D_ANTARA_WRAPPER)
FetchContent_Declare(
box2d
URL https://github.com/erincatto/Box2D/archive/master.zip
)
endif()

if (USE_LUA_ANTARA_WRAPPER)
FetchContent_Declare(
lua
Expand All @@ -69,6 +76,17 @@ if (USE_SFML_ANTARA_WRAPPER)
FetchContent_MakeAvailable(SFML)
endif ()

if (USE_BOX2D_ANTARA_WRAPPER)
FetchContent_MakeAvailable(box2d)
add_library(Box2D STATIC)
file(GLOB_RECURSE BOX2D_SOURCES ${box2d_SOURCE_DIR}/Box2D/*.cpp)
message(STATUS "box2d sources: -> ${BOX2D_SOURCES}")
target_sources(Box2D PRIVATE ${BOX2D_SOURCES})
target_include_directories(Box2D PUBLIC ${box2d_SOURCE_DIR})
target_compile_features(Box2D PRIVATE cxx_std_11)
add_library(antara::box2d_wrapper ALIAS Box2D)
endif()

if (USE_LUA_ANTARA_WRAPPER)
FetchContent_MakeAvailable(lua sol2)
add_library(lua_lib STATIC)
Expand Down
4 changes: 4 additions & 0 deletions modules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ if (USE_LUA_ANTARA_WRAPPER)
add_subdirectory(lua)
endif()

if (USE_BOX2D_ANTARA_WRAPPER)
add_subdirectory(box2d)
endif()

if (USE_SFML_ANTARA_WRAPPER)
add_subdirectory(sfml)
endif()
36 changes: 36 additions & 0 deletions modules/box2d/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## shared sources between the module and his unit tests
add_library(antara_box2d_shared_sources STATIC)
target_sources(antara_box2d_shared_sources PRIVATE
antara/gaming/box2d/box2d.system.cpp)
target_include_directories(antara_box2d_shared_sources PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(antara_box2d_shared_sources PUBLIC antara::default_settings antara::box2d_wrapper antara::ecs antara::timer antara::core)
add_library(antara::box2d ALIAS antara_box2d_shared_sources)

if (ANTARA_BUILD_UNIT_TESTS)
##! antara box2d tests
add_executable(antara_box2d_tests)
target_sources(antara_box2d_tests PUBLIC
antara/gaming/box2d/antara.box2d.tests.cpp
antara/gaming/box2d/antara.box2d.system.tests.cpp)
target_link_libraries(antara_box2d_tests PRIVATE doctest PUBLIC antara::box2d)
set_target_properties(antara_box2d_tests
PROPERTIES
RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/unit_tests"
)
target_enable_coverage(antara_box2d_tests)
target_enable_tsan(antara_box2d_tests)
target_enable_asan(antara_box2d_tests)
target_enable_ubsan(antara_box2d_tests)

if (EMSCRIPTEN)
message(STATUS "Emscripten detected")
if (ENABLE_HTML_COMPILATION)
message(STATUS "Html compilation enabled")
set_target_properties(antara_box2d_tests PROPERTIES LINK_FLAGS "-s FORCE_FILESYSTEM=1 -s EXIT_RUNTIME=1"
SUFFIX ".html")
else ()
message(STATUS "Local js compilation")
set_target_properties(antara_box2d_tests PROPERTIES LINK_FLAGS "-s FORCE_FILESYSTEM=1 -s NODERAWFS=1 -s EXIT_RUNTIME=1")
endif ()
endif ()
endif ()
32 changes: 32 additions & 0 deletions modules/box2d/antara/gaming/box2d/antara.box2d.system.tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#include <doctest/doctest.h>
#include "antara/gaming/box2d/box2d.system.hpp"

namespace antara::gaming::box2d::tests
{
TEST_SUITE ("box2d system tests suite")
{
entt::registry registry;
entt::dispatcher dispatcher;
box2d_system system{registry, dispatcher};
TEST_CASE ("update system")
{
system.update();
}
}
}
18 changes: 18 additions & 0 deletions modules/box2d/antara/gaming/box2d/antara.box2d.tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>
31 changes: 31 additions & 0 deletions modules/box2d/antara/gaming/box2d/box2d.system.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#include "antara/gaming/timer/time.step.hpp"
#include "antara/gaming/box2d/box2d.system.hpp"

namespace antara::gaming::box2d
{
box2d_system::box2d_system(entt::registry &registry, entt::dispatcher &dispatcher) noexcept : system(registry, dispatcher)
{

}

void box2d_system::update() noexcept
{
world_.Step(antara::gaming::timer::time_step::get_fixed_delta_time(), 8, 3);
}
}
41 changes: 41 additions & 0 deletions modules/box2d/antara/gaming/box2d/box2d.system.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/******************************************************************************
* Copyright © 2013-2019 The Komodo Platform Developers. *
* *
* See the AUTHORS, DEVELOPER-AGREEMENT and LICENSE files at *
* the top-level directory of this distribution for the individual copyright *
* holder information and the developer policies on copyright and licensing. *
* *
* Unless otherwise agreed in a custom licensing agreement, no part of the *
* Komodo Platform software, including this file may be copied, modified, *
* propagated or distributed except according to the terms contained in the *
* LICENSE file *
* *
* Removal or modification of this copyright notice is prohibited. *
* *
******************************************************************************/

#pragma once

#include <Box2D/Box2D.h>
#include <entt/entity/registry.hpp>
#include <entt/signal/dispatcher.hpp>
#include "antara/gaming/core/safe.refl.hpp"
#include "antara/gaming/ecs/system.hpp"

namespace antara::gaming::box2d
{
class box2d_system final : public ecs::logic_update_system<box2d_system>
{
public:
//! Constructors
box2d_system(entt::registry &registry, entt::dispatcher &dispatcher) noexcept;

void update() noexcept final;

private:
b2Vec2 gravity_{0.f, 9.8f};
b2World world_{gravity_};
};
}

REFL_AUTO(type(antara::gaming::box2d::box2d_system));

0 comments on commit b1ace12

Please sign in to comment.