From 15c4cf103c4da7808b7173581ead22ab7190632d Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Thu, 14 Nov 2024 18:49:17 +0100 Subject: updated test --- src/test/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) (limited to 'src/test/CMakeLists.txt') diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 0e4eaed..8618ae6 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -2,5 +2,6 @@ target_sources(test_main PUBLIC dummy.cpp # audio.cpp PhysicsTest.cpp + CollisionTest.cpp ) -- cgit v1.2.3 From 690471c7c4536c074a4dca5aab7cc618d47bfb5f Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Wed, 20 Nov 2024 10:43:32 +0100 Subject: merge with maser --- src/crepe/api/LoopManager.cpp | 6 +- src/crepe/api/LoopManager.h | 5 +- src/crepe/api/Script.h | 9 ++- src/crepe/system/CollisionSystem.cpp | 6 +- src/crepe/system/CollisionSystem.h | 2 - src/example/CMakeLists.txt | 2 +- src/example/collision.cpp | 123 ----------------------------------- src/example/game.cpp | 89 +++++++++++++++++++++++++ src/example/particles.cpp | 43 ------------ src/example/physics.cpp | 24 ------- src/example/rendering.cpp | 4 +- src/test/CMakeLists.txt | 2 - src/test/CollisionTest.cpp | 121 ++++++++++++++++------------------ 13 files changed, 165 insertions(+), 271 deletions(-) delete mode 100644 src/example/collision.cpp create mode 100644 src/example/game.cpp delete mode 100644 src/example/particles.cpp delete mode 100644 src/example/physics.cpp (limited to 'src/test/CMakeLists.txt') diff --git a/src/crepe/api/LoopManager.cpp b/src/crepe/api/LoopManager.cpp index a4bc101..586919d 100644 --- a/src/crepe/api/LoopManager.cpp +++ b/src/crepe/api/LoopManager.cpp @@ -35,10 +35,8 @@ void LoopManager::start() { void LoopManager::set_running(bool running) { this->game_running = running; } void LoopManager::fixed_update() { - PhysicsSystem phys; - phys.update(); - CollisionSystem col; - col.update(); + this->get_system().update(); + this->get_system().update(); } void LoopManager::loop() { diff --git a/src/crepe/api/LoopManager.h b/src/crepe/api/LoopManager.h index f6904be..37f13ac 100644 --- a/src/crepe/api/LoopManager.h +++ b/src/crepe/api/LoopManager.h @@ -4,6 +4,7 @@ #include "../ComponentManager.h" #include "../system/System.h" +#include "api/SceneManager.h" namespace crepe { @@ -71,7 +72,9 @@ private: private: //! Component manager instance ComponentManager component_manager{}; - +public: + //! Scene manager instance + SceneManager scene_manager{component_manager}; private: /** * \brief Collection of System instances diff --git a/src/crepe/api/Script.h b/src/crepe/api/Script.h index 2b70379..939d142 100644 --- a/src/crepe/api/Script.h +++ b/src/crepe/api/Script.h @@ -63,7 +63,14 @@ protected: */ template std::vector> get_components() const; - + + /** + * \brief Gets game object id this script is attached to + * + * \returns game object id + */ + game_object_id_t get_game_object_id() const {return this->game_object_id;}; + protected: // NOTE: Script must have a constructor without arguments so the game programmer doesn't need // to manually add `using Script::Script` to their concrete script class. diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index df0ee41..3e73b44 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -18,11 +18,9 @@ using namespace crepe; -CollisionSystem::CollisionSystem() {} - void CollisionSystem::update() { // Get collider components and keep them seperate - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; std::vector> boxcolliders = mgr.get_components_by_type(); std::vector> circlecolliders = mgr.get_components_by_type(); @@ -185,7 +183,7 @@ void CollisionSystem::static_collision_handler(CollisionInfo& info){ } std::vector> CollisionSystem::check_collisions(const std::vector>& boxcolliders, const std::vector>& circlecolliders) { - ComponentManager & mgr = ComponentManager::get_instance(); + ComponentManager & mgr = this->component_manager; std::vector> collisions_ret; // TODO: diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h index 0518b34..d94f6bc 100644 --- a/src/crepe/system/CollisionSystem.h +++ b/src/crepe/system/CollisionSystem.h @@ -70,8 +70,6 @@ public: public: - CollisionSystem(); - //! Updates the collision system by checking for collisions between colliders and handling them. void update() override; diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index d2ea926..2facc4d 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -28,7 +28,7 @@ add_example(proxy) add_example(db) add_example(ecs) add_example(scene_manager) -add_example(collision) +add_example(game) add_example(events) add_example(particles) add_example(gameloop) diff --git a/src/example/collision.cpp b/src/example/collision.cpp deleted file mode 100644 index f51380e..0000000 --- a/src/example/collision.cpp +++ /dev/null @@ -1,123 +0,0 @@ -#include "api/BoxCollider.h" -#include "system/CollisionSystem.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -using namespace crepe; -using namespace std; - -class MyScript : public Script { - static bool oncollision(const CollisionEvent& test) { - std::cout << "test collision: " << test.info.first.collider.game_object_id << std::endl; - return true; - } - void init() { - EventManager::get_instance().subscribe(oncollision, this->parent->game_object_id); - } - void update() { - // Retrieve component from the same GameObject this script is on - - } - - -}; - -int main(int argc, char * argv[]) { - //setup - LoopManager gameloop; - Color color(0, 0, 0, 0); - - double screen_size_width = 640; - double screen_size_height = 480; - double world_collider = 1000; - //define playable world - GameObject World(0, "Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1); - World.add_component(Rigidbody::Data{ - .mass = 0, - .gravity_scale = 0, - .body_type = Rigidbody::BodyType::STATIC, - .constraints = {0, 0, 0}, - .use_gravity = false, - .bounce = false, - .offset = {0,0} - }); - World.add_component(Vector2{0, 0-(screen_size_height/2+world_collider/2)}, world_collider, world_collider);; // Top - World.add_component(Vector2{0, screen_size_height/2+world_collider/2}, world_collider, world_collider); // Bottom - World.add_component(Vector2{0-(screen_size_width/2+world_collider/2), 0}, world_collider, world_collider); // Left - World.add_component(Vector2{screen_size_width/2+world_collider/2, 0}, world_collider, world_collider); // right - - - GameObject game_object1(1, "Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1); - game_object1.add_component(Rigidbody::Data{ - .mass = 1, - .gravity_scale = 0.01, - .body_type = Rigidbody::BodyType::DYNAMIC, - .linear_velocity = {1,0}, - .constraints = {0, 0, 0}, - .use_gravity = true, - .bounce = true, - .elastisity = 1, - .offset = {0,0}, - }); - game_object1.add_component(Vector2{0, 0}, 20, 20); - game_object1.add_component().set_script(); - game_object1.add_component( - make_shared("/home/jaro/crepe/asset/texture/green_square.png"), color, - FlipSettings{true, true}); - game_object1.add_component(Color::get_white()); - - - // GameObject game_object2(2, "Name", "Tag", Vector2{20, 470}, 0, 1); - // game_object2.add_component(Rigidbody::Data{ - // .mass = 1, - // .gravity_scale = 1, - // .body_type = Rigidbody::BodyType::DYNAMIC, - // .linear_velocity = {0,0}, - // .constraints = {0, 0, 0}, - // .use_gravity = false, - // .bounce = false, - // .offset = {0,0}, - // }); - // game_object2.add_component(Vector2{0, 0}, 0, 0); - // game_object2.add_component().set_script(); - // game_object2.add_component( - // make_shared("/home/jaro/crepe/asset/texture/red_square.png"), color, - // FlipSettings{true, true}); - - - crepe::ScriptSystem sys; - // Update all scripts. This should result in MyScript::update being called - sys.update(); - - gameloop.start(); - // auto & render = crepe::RenderSystem::get_instance(); - // auto start = std::chrono::steady_clock::now(); - // while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { - // render.update(); - // } - - - return 0; -} diff --git a/src/example/game.cpp b/src/example/game.cpp new file mode 100644 index 0000000..a9f6103 --- /dev/null +++ b/src/example/game.cpp @@ -0,0 +1,89 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace crepe; + +using namespace std; + +class MyScript : public Script { + static bool oncollision(const CollisionEvent& test) { + std::cout << "test collision: " << test.info.first.collider.game_object_id << std::endl; + return true; + } + void init() { + EventManager::get_instance().subscribe(oncollision, this->get_game_object_id()); + } + void update() { + // Retrieve component from the same GameObject this script is on + + } +}; + +class ConcreteScene1 : public Scene { +public: + using Scene::Scene; + + void load_scene() { + ComponentManager & mgr = this->component_manager; + Color color(0, 0, 0, 0); + + double screen_size_width = 640; + double screen_size_height = 480; + double world_collider = 1000; + //define playable world + GameObject world = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1); + world.add_component(Rigidbody::Data{ + .mass = 0, + .gravity_scale = 0, + .body_type = Rigidbody::BodyType::STATIC, + .constraints = {0, 0, 0}, + .use_gravity = false, + .bounce = false, + .offset = {0,0} + }); + world.add_component(Vector2{0, 0-(screen_size_height/2+world_collider/2)}, world_collider, world_collider);; // Top + world.add_component(Vector2{0, screen_size_height/2+world_collider/2}, world_collider, world_collider); // Bottom + world.add_component(Vector2{0-(screen_size_width/2+world_collider/2), 0}, world_collider, world_collider); // Left + world.add_component(Vector2{screen_size_width/2+world_collider/2, 0}, world_collider, world_collider); // right + + + GameObject game_object1 = mgr.new_object("Name", "Tag", Vector2{screen_size_width/2, screen_size_height/2}, 0, 1); + game_object1.add_component(Rigidbody::Data{ + .mass = 1, + .gravity_scale = 0.01, + .body_type = Rigidbody::BodyType::DYNAMIC, + .linear_velocity = {1,0}, + .constraints = {0, 0, 0}, + .use_gravity = true, + .bounce = true, + .elastisity = 1, + .offset = {0,0}, + }); + game_object1.add_component(Vector2{0, 0}, 20, 20); + game_object1.add_component().set_script(); + game_object1.add_component( + make_shared("/home/jaro/crepe/asset/texture/green_square.png"), color, + FlipSettings{true, true}); + game_object1.add_component(Color::get_white()); + } +}; + +int main(int argc, char * argv[]) { + + LoopManager gameloop; + gameloop.scene_manager.add_scene("scene1"); + gameloop.scene_manager.load_next_scene(); + gameloop.start(); + return 0; +} diff --git a/src/example/particles.cpp b/src/example/particles.cpp deleted file mode 100644 index 3d5f676..0000000 --- a/src/example/particles.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace crepe; -using namespace std; - -int main(int argc, char * argv[]) { - ComponentManager mgr{}; - GameObject game_object = mgr.new_object("", "", Vector2{0, 0}, 0, 0); - Color color(0, 0, 0, 0); - Sprite test_sprite = game_object.add_component( - make_shared("../asset/texture/img.png"), color, FlipSettings{true, true}); - game_object.add_component(ParticleEmitter::Data{ - .position = {0, 0}, - .max_particles = 100, - .emission_rate = 0, - .min_speed = 0, - .max_speed = 0, - .min_angle = 0, - .max_angle = 0, - .begin_lifespan = 0, - .end_lifespan = 0, - .force_over_time = Vector2{0, 0}, - .boundary{ - .width = 0, - .height = 0, - .offset = Vector2{0, 0}, - .reset_on_exit = false, - }, - .sprite = test_sprite, - }); - - return 0; -} diff --git a/src/example/physics.cpp b/src/example/physics.cpp deleted file mode 100644 index ad663a0..0000000 --- a/src/example/physics.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include -#include -#include -#include -#include - -using namespace crepe; -using namespace std; - -int main(int argc, char * argv[]) { - ComponentManager mgr{}; - - GameObject game_object = mgr.new_object("Name", "Tag", Vector2{0, 0}, 0, 0); - game_object.add_component(Rigidbody::Data{ - .mass = 1, - .gravity_scale = 1, - .body_type = Rigidbody::BodyType::DYNAMIC, - .constraints = {0, 0, 0}, - .use_gravity = true, - .bounce = false, - }); - return 0; -} diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index c9e62f1..14ecaa9 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -30,13 +30,13 @@ int main() { // Normal adding components { Color color(0, 0, 0, 0); - obj.add_component(make_shared("../asset/texture/img.png"), color, + obj.add_component(make_shared("/home/jaro/crepe/asset/texture/green_square.png"), color, FlipSettings{false, false}); obj.add_component(Color::get_red()); } { Color color(0, 0, 0, 0); - obj1.add_component(make_shared("../asset/texture/second.png"), color, + obj1.add_component(make_shared("/home/jaro/crepe/asset/texture/green_square.png"), color, FlipSettings{true, true}); } diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index a41d097..f830165 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -1,6 +1,4 @@ target_sources(test_main PUBLIC - dummy.cpp - # audio.cpp CollisionTest.cpp main.cpp PhysicsTest.cpp diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index 3e43479..83564e1 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -1,83 +1,76 @@ -#include "api/BoxCollider.h" -#include "api/CircleCollider.h" -#include "api/Vector2.h" #include #include #include #include #include -#include +#include #include using namespace std; using namespace std::chrono_literals; using namespace crepe; -class CollisionTest : public ::testing::Test { -protected: - GameObject * game_object1; - GameObject * game_object2; - CollisionSystem collision_system; - void SetUp() override { - ComponentManager & mgr = ComponentManager::get_instance(); - mgr.delete_all_components(); - std::vector> transforms - = mgr.get_components_by_id(0); - - // ob 1 - game_object1 = new GameObject(0, "", "", Vector2{0, 0}, 0, 0); - game_object1->add_component(Rigidbody::Data{ - .mass = 1, - .gravity_scale = 1, - .body_type = Rigidbody::BodyType::DYNAMIC, - .max_linear_velocity = Vector2{10, 10}, - .max_angular_velocity = 10, - .constraints = {0, 0, 0}, - .use_gravity = false, - .bounce = false, - }); - - game_object1->add_component(Vector2{0,0},10,10); +class MyScript : public Script { + static bool oncollision(const CollisionEvent& test) { + std::cout << "test collision: " << test.info.first.collider.game_object_id << std::endl; + return true; + } + void init() { + EventManager::get_instance().subscribe(oncollision, this->get_game_object_id()); + } + void update() { + // Retrieve component from the same GameObject this script is on + } +}; + +class PhysicsTest : public ::testing::Test { +public: + ComponentManager component_manager; + PhysicsSystem system{component_manager}; - //ob 2 - game_object2 = new GameObject(1, "", "", Vector2{50, 50}, 0, 0); - game_object2->add_component(Rigidbody::Data{ - .mass = 1, - .gravity_scale = 1, - .body_type = Rigidbody::BodyType::DYNAMIC, - .max_linear_velocity = Vector2{10, 10}, - .max_angular_velocity = 10, - .constraints = {0, 0, 0}, - .use_gravity = false, - .bounce = false, - }); - game_object2->add_component(Vector2{0,0},5); + void SetUp() override { + ComponentManager & mgr = this->component_manager; + vector> transforms + = mgr.get_components_by_id(0); + if (transforms.empty()) { + auto entity = mgr.new_object("", "", Vector2{0, 0}, 0, 0); + entity.add_component(Rigidbody::Data{ + .mass = 1, + .gravity_scale = 1, + .body_type = Rigidbody::BodyType::DYNAMIC, + .max_linear_velocity = Vector2{10, 10}, + .max_angular_velocity = 10, + .constraints = {0, 0}, + .use_gravity = true, + .bounce = false, + }); + } + transforms = mgr.get_components_by_id(0); + Transform & transform = transforms.front().get(); + transform.position.x = 0.0; + transform.position.y = 0.0; + transform.rotation = 0.0; + vector> rigidbodies + = mgr.get_components_by_id(0); + Rigidbody & rigidbody = rigidbodies.front().get(); + rigidbody.data.angular_velocity = 0; + rigidbody.data.linear_velocity.x = 0; + rigidbody.data.linear_velocity.y = 0; } }; -TEST_F(CollisionTest, box_box_collision) { +TEST_F(PhysicsTest, gravity) { Config::get_instance().physics.gravity = 1; - ComponentManager & mgr = ComponentManager::get_instance(); - std::vector> transforms - = mgr.get_components_by_id(0); - Transform & transform = transforms.front().get(); + ComponentManager & mgr = this->component_manager; + vector> transforms = mgr.get_components_by_id(0); + const Transform & transform = transforms.front().get(); ASSERT_FALSE(transforms.empty()); - transform.position = {39,50}; - collision_system.update(); - transform.position = {40,50}; - collision_system.update(); - transform.position = {50,39}; - collision_system.update(); - transform.position = {50,40}; - collision_system.update(); - transform.position = {50,60}; - collision_system.update(); - transform.position = {50,61}; - collision_system.update(); - transform.position = {60,50}; - collision_system.update(); - transform.position = {61,50}; - collision_system.update(); -} + EXPECT_EQ(transform.position.y, 0); + + system.update(); + EXPECT_EQ(transform.position.y, 1); + system.update(); + EXPECT_EQ(transform.position.y, 3); +} -- cgit v1.2.3 From a67c52325bc8cbd264293b9dcc217fc07bfbaf57 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Wed, 20 Nov 2024 18:07:48 +0100 Subject: merge `master` into `loek/collision-system` --- Doxyfile | 1 + contributing.md | 23 +++- makefile | 3 +- src/crepe/api/Color.cpp | 37 ++---- src/crepe/api/Color.h | 49 +++----- src/crepe/api/Sprite.h | 3 +- src/crepe/facade/SDLContext.h | 6 - src/crepe/system/RenderSystem.cpp | 43 +++++-- src/crepe/system/RenderSystem.h | 32 +++-- src/example/CMakeLists.txt | 3 - src/example/components_internal.cpp | 51 -------- src/example/ecs.cpp | 53 -------- src/example/events.cpp | 30 ++--- src/example/game.cpp | 2 +- src/example/rendering.cpp | 10 +- src/example/scene_manager.cpp | 79 ------------ src/makefile | 7 +- src/test/CMakeLists.txt | 2 + src/test/ECSTest.cpp | 236 ++++++++++++++++++++++++++++++++++++ src/test/RenderSystemTest.cpp | 174 ++++++++++++++++++++++++++ src/test/SceneManagerTest.cpp | 122 +++++++++++++++++++ 21 files changed, 667 insertions(+), 299 deletions(-) delete mode 100644 src/example/components_internal.cpp delete mode 100644 src/example/ecs.cpp delete mode 100644 src/example/scene_manager.cpp create mode 100644 src/test/ECSTest.cpp create mode 100644 src/test/RenderSystemTest.cpp create mode 100644 src/test/SceneManagerTest.cpp (limited to 'src/test/CMakeLists.txt') diff --git a/Doxyfile b/Doxyfile index 9328b24..e0a31df 100644 --- a/Doxyfile +++ b/Doxyfile @@ -24,6 +24,7 @@ USE_MDFILE_AS_MAINPAGE = ./readme.md REPEAT_BRIEF = NO INTERNAL_DOCS = YES +EXTRACT_PRIVATE = YES EXTRACT_STATIC = YES HIDE_UNDOC_NAMESPACES = YES HIDE_UNDOC_CLASSES = YES diff --git a/contributing.md b/contributing.md index 5b0c79d..9c95851 100644 --- a/contributing.md +++ b/contributing.md @@ -20,7 +20,7 @@ that you can click on to open them. # Code style - Formatting nitty-gritty is handled by clang-format/clang-tidy (run `make - format` in the root folder of this repository to format all sources files) + format` or `make lint`) -
ASCII only
GoodBad
@@ -798,6 +798,27 @@ that you can click on to open them. resolving merge conflicts when multiple sources were added by different people to the same CMakeLists.txt easier. +## GoogleTest-specific + +- Unit tests are not *required* to follow all code standards +-
+ Private/protected members may be accessed using preprocessor tricks + + + ```cpp + // include unrelated headers before + + #define private public + #define protected public + + // headers included after *will* be affected + ``` +
+- Each test source file defines tests within a single test suite (first + parameter of `TEST()` / `TEST_F()` macro) +- Test source files match their suite name (or test fixture name in the case of + tests that use a fixture) + # Structure - Files are placed in the appropriate directory: diff --git a/makefile b/makefile index dd7c587..c46e8a5 100644 --- a/makefile +++ b/makefile @@ -6,5 +6,6 @@ doxygen: Doxyfile FORCE FMT += $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') format: FORCE clang-format -i $(FMT) - $(MAKE) -C src $@ +lint: FORCE + $(MAKE) -C src $@ diff --git a/src/crepe/api/Color.cpp b/src/crepe/api/Color.cpp index 9e5f187..29bd77a 100644 --- a/src/crepe/api/Color.cpp +++ b/src/crepe/api/Color.cpp @@ -2,32 +2,11 @@ using namespace crepe; -Color Color::white = Color(255, 255, 255, 0); -Color Color::red = Color(255, 0, 0, 0); -Color Color::green = Color(0, 255, 0, 0); -Color Color::blue = Color(0, 0, 255, 0); -Color Color::black = Color(0, 0, 0, 0); -Color Color::cyan = Color(0, 255, 255, 0); -Color Color::yellow = Color(255, 255, 0, 0); -Color Color::magenta = Color(255, 0, 255, 0); - -Color::Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha) { - this->a = alpha; - this->r = red; - this->g = green; - this->b = blue; -}; - -const Color & Color::get_white() { return Color::white; }; - -const Color & Color::get_red() { return Color::red; }; -const Color & Color::get_green() { return Color::green; }; -const Color & Color::get_blue() { return Color::blue; }; - -const Color & Color::get_black() { return Color::black; }; - -const Color & Color::get_cyan() { return Color::cyan; }; - -const Color & Color::get_yellow() { return Color::yellow; }; - -const Color & Color::get_magenta() { return Color::magenta; }; +const Color Color::WHITE{0xff, 0xff, 0xff}; +const Color Color::RED{0xff, 0x00, 0x00}; +const Color Color::GREEN{0x00, 0xff, 0x00}; +const Color Color::BLUE{0x00, 0x00, 0xff}; +const Color Color::BLACK{0x00, 0x00, 0x00}; +const Color Color::CYAN{0x00, 0xff, 0xff}; +const Color Color::YELLOW{0xff, 0xff, 0x00}; +const Color Color::MAGENTA{0xff, 0x00, 0xff}; diff --git a/src/crepe/api/Color.h b/src/crepe/api/Color.h index aa47bf4..84edb5c 100644 --- a/src/crepe/api/Color.h +++ b/src/crepe/api/Color.h @@ -4,41 +4,20 @@ namespace crepe { -// TODO: make Color a struct w/o constructors/destructors -class Color { - - // FIXME: can't these colors be defined as a `static constexpr const Color` - // instead? - -public: - Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t alpha); - static const Color & get_white(); - static const Color & get_red(); - static const Color & get_green(); - static const Color & get_blue(); - static const Color & get_cyan(); - static const Color & get_magenta(); - static const Color & get_yellow(); - static const Color & get_black(); - -private: - // TODO: why are these private!? - uint8_t r; - uint8_t g; - uint8_t b; - uint8_t a; - - static Color white; - static Color red; - static Color green; - static Color blue; - static Color cyan; - static Color magenta; - static Color yellow; - static Color black; - -private: - friend class SDLContext; +struct Color { + uint8_t r = 0x00; + uint8_t g = 0x00; + uint8_t b = 0x00; + uint8_t a = 0xff; + + static const Color WHITE; + static const Color RED; + static const Color GREEN; + static const Color BLUE; + static const Color CYAN; + static const Color MAGENTA; + static const Color YELLOW; + static const Color BLACK; }; } // namespace crepe diff --git a/src/crepe/api/Sprite.h b/src/crepe/api/Sprite.h index 0192793..74a55d4 100644 --- a/src/crepe/api/Sprite.h +++ b/src/crepe/api/Sprite.h @@ -2,8 +2,9 @@ #include +#include "../Component.h" + #include "Color.h" -#include "Component.h" #include "Texture.h" namespace crepe { diff --git a/src/crepe/facade/SDLContext.h b/src/crepe/facade/SDLContext.h index 007092b..652a83e 100644 --- a/src/crepe/facade/SDLContext.h +++ b/src/crepe/facade/SDLContext.h @@ -21,9 +21,6 @@ namespace crepe { // typedef is unusable when crepe is packaged. Wouter will fix this later. typedef SDL_Keycode CREPE_KEYCODES; -class Texture; -class LoopManager; - /** * \class SDLContext * \brief Facade for the SDL library @@ -91,9 +88,6 @@ private: //! Will use the funtions: texture_from_path, get_width,get_height. friend class Texture; - //! Will use the funtions: texture_from_path, get_width,get_height. - friend class Animator; - /** * \brief Loads a texture from a file path. * \param path Path to the image file. diff --git a/src/crepe/system/RenderSystem.cpp b/src/crepe/system/RenderSystem.cpp index fa3d0de..96c5f27 100644 --- a/src/crepe/system/RenderSystem.cpp +++ b/src/crepe/system/RenderSystem.cpp @@ -1,38 +1,59 @@ +#include +#include #include +#include #include #include "../ComponentManager.h" #include "../api/Sprite.h" #include "../api/Transform.h" #include "../facade/SDLContext.h" -#include "../util/Log.h" #include "RenderSystem.h" using namespace crepe; +using namespace std; -void RenderSystem::clear_screen() const { SDLContext::get_instance().clear_screen(); } +void RenderSystem::clear_screen() { this->context.clear_screen(); } -void RenderSystem::present_screen() const { SDLContext::get_instance().present_screen(); } +void RenderSystem::present_screen() { this->context.present_screen(); } void RenderSystem::update_camera() { ComponentManager & mgr = this->component_manager; std::vector> cameras = mgr.get_components_by_type(); + if (cameras.size() == 0) throw std::runtime_error("No cameras in current scene"); + for (Camera & cam : cameras) { - SDLContext::get_instance().camera(cam); - this->curr_cam = &cam; + this->context.camera(cam); + this->curr_cam_ref = &cam; } } -void RenderSystem::render_sprites() const { - ComponentManager & mgr = this->component_manager; - std::vector> sprites = mgr.get_components_by_type(); +bool sorting_comparison(const Sprite & a, const Sprite & b) { + if (a.sorting_in_layer < b.sorting_in_layer) return true; + if (a.sorting_in_layer == b.sorting_in_layer) return a.order_in_layer < b.order_in_layer; + + return false; +} + +std::vector> +RenderSystem::sort(std::vector> & objs) { + + std::vector> sorted_objs(objs); + std::sort(sorted_objs.begin(), sorted_objs.end(), sorting_comparison); + + return sorted_objs; +} + +void RenderSystem::render_sprites() { + ComponentManager & mgr = this->component_manager; + vector> sprites = mgr.get_components_by_type(); + vector> sorted_sprites = this->sort(sprites); - SDLContext & render = SDLContext::get_instance(); - for (const Sprite & sprite : sprites) { + for (const Sprite & sprite : sorted_sprites) { auto transforms = mgr.get_components_by_id(sprite.game_object_id); - render.draw(sprite, transforms[0], *curr_cam); + this->context.draw(sprite, transforms[0], *this->curr_cam_ref); } } diff --git a/src/crepe/system/RenderSystem.h b/src/crepe/system/RenderSystem.h index 87ec494..57b9c73 100644 --- a/src/crepe/system/RenderSystem.h +++ b/src/crepe/system/RenderSystem.h @@ -1,18 +1,23 @@ #pragma once -#include "api/Camera.h" +#include +#include + +#include "facade/SDLContext.h" #include "System.h" namespace crepe { +class Camera; +class Sprite; + /** * \class RenderSystem * \brief Manages rendering operations for all game objects. * - * RenderSystem is responsible for rendering sprites, clearing and presenting the screen, and - * managing the active camera. It functions as a singleton, providing centralized rendering - * services for the application. + * RenderSystem is responsible for rendering, clearing and presenting the screen, and + * managing the active camera. */ class RenderSystem : public System { public: @@ -25,16 +30,25 @@ public: private: //! Clears the screen in preparation for rendering. - void clear_screen() const; + void clear_screen(); //! Presents the rendered frame to the display. - void present_screen() const; + void present_screen(); //! Updates the active camera used for rendering. void update_camera(); //! Renders all active sprites to the screen. - void render_sprites() const; + void render_sprites(); + + /** + * \brief sort a vector sprite objects with + * + * \param objs the vector that will do a sorting algorithm on + * \return returns a sorted reference vector + */ + std::vector> + sort(std::vector> & objs); /** * \todo Include color handling for sprites. @@ -48,8 +62,10 @@ private: private: //! Pointer to the current active camera for rendering - Camera * curr_cam = nullptr; + Camera * curr_cam_ref = nullptr; // TODO: needs a better solution + + SDLContext & context = SDLContext::get_instance(); }; } // namespace crepe diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt index c5cb63f..045f4d4 100644 --- a/src/example/CMakeLists.txt +++ b/src/example/CMakeLists.txt @@ -17,7 +17,6 @@ function(add_example target_name) endfunction() add_example(audio_internal) -# add_example(components_internal) add_example(script) add_example(log) add_example(rendering) @@ -25,8 +24,6 @@ add_example(asset_manager) add_example(savemgr) add_example(proxy) add_example(db) -add_example(ecs) -add_example(scene_manager) add_example(game) add_example(events) add_example(gameloop) diff --git a/src/example/components_internal.cpp b/src/example/components_internal.cpp deleted file mode 100644 index 2a232a9..0000000 --- a/src/example/components_internal.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/** \file - * - * Standalone example for usage of the internal ECS - */ - -#include -#include - -#include -#include - -#include -#include -#include - -#include - -using namespace crepe; -using namespace std; - -#define OBJ_COUNT 100000 - -int main() { - dbg_trace(); - - ComponentManager mgr{}; - - auto start_adding = chrono::high_resolution_clock::now(); - - for (int i = 0; i < OBJ_COUNT; ++i) { - GameObject obj = mgr.new_object("Name", "Tag"); - obj.add_component("test"); - obj.add_component(0, 0, i); - } - - auto stop_adding = chrono::high_resolution_clock::now(); - - auto sprites = mgr.get_components_by_type(); - for (auto sprite : sprites) { - assert(true); - } - - auto stop_looping = chrono::high_resolution_clock::now(); - - auto add_time = chrono::duration_cast(stop_adding - start_adding); - auto loop_time = chrono::duration_cast(stop_looping - stop_adding); - printf("add time: %ldus\n", add_time.count()); - printf("loop time: %ldus\n", loop_time.count()); - - return 0; -} diff --git a/src/example/ecs.cpp b/src/example/ecs.cpp deleted file mode 100644 index d5ba51b..0000000 --- a/src/example/ecs.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include - -#include -#include -#include -#include - -using namespace crepe; -using namespace std; - -int main() { - ComponentManager mgr{}; - - // Create a few GameObjects - try { - GameObject body = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); - GameObject right_leg = mgr.new_object("rightLeg", "person", Vector2{1, 1}, 0, 1); - GameObject left_leg = mgr.new_object("leftLeg", "person", Vector2{1, 1}, 0, 1); - GameObject right_foot = mgr.new_object("rightFoot", "person", Vector2{2, 2}, 0, 1); - GameObject left_foot = mgr.new_object("leftFoot", "person", Vector2{2, 2}, 0, 1); - - // Set the parent of each GameObject - right_foot.set_parent(right_leg); - left_foot.set_parent(left_leg); - right_leg.set_parent(body); - left_leg.set_parent(body); - - // Adding a second Transform component is not allowed and will invoke an exception - body.add_component(Vector2{10, 10}, 0, 1); - } catch (const exception & e) { - cerr << e.what() << endl; - } - - // Get the Metadata and Transform components of each GameObject - vector> metadata = mgr.get_components_by_type(); - vector> transform = mgr.get_components_by_type(); - - // Print the Metadata and Transform components - for (auto & m : metadata) { - cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name - << " Tag: " << m.get().tag << " Parent: " << m.get().parent << " Children: "; - for (auto & c : m.get().children) { - cout << c << " "; - } - cout << endl; - } - for (auto & t : transform) { - cout << "Id: " << t.get().game_object_id << " Position: [" << t.get().position.x - << ", " << t.get().position.y << "]" << endl; - } - - return 0; -} diff --git a/src/example/events.cpp b/src/example/events.cpp index ed519ff..e6d91aa 100644 --- a/src/example/events.cpp +++ b/src/example/events.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include @@ -65,6 +65,10 @@ public: } }; int main() { + EventManager & evmgr = EventManager::get_instance(); + ComponentManager mgr{}; + ScriptSystem sys{mgr}; + // two events to trigger KeyPressEvent key_press; key_press.key = Keycode::A; @@ -74,22 +78,19 @@ int main() { click_event.mouse_x = 100; click_event.mouse_y = 200; // queue events to test queue - EventManager::get_instance().queue_event( - std::move(key_press), 0); - EventManager::get_instance().queue_event( - std::move(click_event), 0); + evmgr.queue_event(std::move(key_press), 0); + evmgr.queue_event(std::move(click_event), 0); { TestKeyListener test_listener; test_listener.set_channel(1); - auto obj = GameObject(0, "name", "tag", Vector2{1.2, 3.4}, 0, 1); + auto obj = mgr.new_object("name", "tag", Vector2{1.2, 3.4}, 0, 1); obj.add_component().set_script(); - ScriptSystem sys; sys.update(); // Trigger the events while `testListener` is in scope - EventManager::get_instance().trigger_event(key_press, 1); - EventManager::get_instance().trigger_event(MouseClickEvent{ + evmgr.trigger_event(key_press, 1); + evmgr.trigger_event(MouseClickEvent{ .mouse_x = 100, .mouse_y = 100, .button = MouseButton::LEFT_MOUSE, @@ -100,14 +101,13 @@ int main() { std::cout << "lambda test" << std::endl; return false; }; - EventManager::get_instance().subscribe( - std::move(event_handler), 0); + evmgr.subscribe(std::move(event_handler), 0); // testing trigger with testListener not in scope (unsubscribed) - EventManager::get_instance().trigger_event(key_press, 0); - EventManager::get_instance().trigger_event(click_event, 0); + evmgr.trigger_event(key_press, 0); + evmgr.trigger_event(click_event, 0); // dispatching queued events - EventManager::get_instance().dispatch_events(); + evmgr.dispatch_events(); - EventManager::get_instance().unsubscribe(event_handler, 0); + evmgr.unsubscribe(event_handler, 0); return EXIT_SUCCESS; } diff --git a/src/example/game.cpp b/src/example/game.cpp index b6a0c31..a557be7 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -74,7 +74,7 @@ public: game_object1.add_component( make_shared("/home/jaro/crepe/asset/texture/green_square.png"), color, FlipSettings{true, true}); - game_object1.add_component(Color::get_white()); + game_object1.add_component(Color::WHITE); } }; diff --git a/src/example/rendering.cpp b/src/example/rendering.cpp index 14ecaa9..9e3c8cc 100644 --- a/src/example/rendering.cpp +++ b/src/example/rendering.cpp @@ -30,13 +30,13 @@ int main() { // Normal adding components { Color color(0, 0, 0, 0); - obj.add_component(make_shared("/home/jaro/crepe/asset/texture/green_square.png"), color, + obj.add_component(make_shared("../asset/texture/green_square.png"), color, FlipSettings{false, false}); - obj.add_component(Color::get_red()); + obj.add_component(Color::RED); } { Color color(0, 0, 0, 0); - obj1.add_component(make_shared("/home/jaro/crepe/asset/texture/green_square.png"), color, + obj1.add_component(make_shared("../asset/texture/green_square.png"), color, FlipSettings{true, true}); } @@ -48,8 +48,12 @@ int main() { } */ + sys.update(); + /* + auto start = std::chrono::steady_clock::now(); while (std::chrono::steady_clock::now() - start < std::chrono::seconds(5)) { sys.update(); } + */ } diff --git a/src/example/scene_manager.cpp b/src/example/scene_manager.cpp deleted file mode 100644 index accec7d..0000000 --- a/src/example/scene_manager.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include - -#include -#include -#include -#include -#include -#include - -using namespace crepe; -using namespace std; - -class ConcreteScene1 : public Scene { -public: - using Scene::Scene; - - void load_scene() { - auto & mgr = this->component_manager; - GameObject object1 = mgr.new_object("scene_1", "tag_scene_1", Vector2{0, 0}, 0, 1); - GameObject object2 = mgr.new_object("scene_1", "tag_scene_1", Vector2{1, 0}, 0, 1); - GameObject object3 = mgr.new_object("scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1); - } -}; - -class ConcreteScene2 : public Scene { -public: - using Scene::Scene; - - void load_scene() { - auto & mgr = this->component_manager; - GameObject object1 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 0}, 0, 1); - GameObject object2 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 1}, 0, 1); - GameObject object3 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 2}, 0, 1); - GameObject object4 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1); - } -}; - -int main() { - ComponentManager component_mgr{}; - SceneManager scene_mgr{component_mgr}; - - // Add the scenes to the scene manager - scene_mgr.add_scene("scene1"); - scene_mgr.add_scene("scene2"); - - // There is no need to call set_next_scene() at the beginnen, because the first scene will be - // automatically set as the next scene - - // Load scene1 (the first scene added) - scene_mgr.load_next_scene(); - - // Get the Metadata components of each GameObject of Scene1 - vector> metadata - = component_mgr.get_components_by_type(); - - cout << "Metadata components of Scene1:" << endl; - // Print the Metadata - for (auto & m : metadata) { - cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name - << " Tag: " << m.get().tag << endl; - } - - // Set scene2 as the next scene - scene_mgr.set_next_scene("scene2"); - // Load scene2 - scene_mgr.load_next_scene(); - - // Get the Metadata components of each GameObject of Scene2 - metadata = component_mgr.get_components_by_type(); - - cout << "Metadata components of Scene2:" << endl; - // Print the Metadata - for (auto & m : metadata) { - cout << "Id: " << m.get().game_object_id << " Name: " << m.get().name - << " Tag: " << m.get().tag << endl; - } - - return 0; -} diff --git a/src/makefile b/src/makefile index 5f80204..a0e8f02 100644 --- a/src/makefile +++ b/src/makefile @@ -1,6 +1,9 @@ .PHONY: FORCE -FMT := $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') format: FORCE - clang-tidy -p build/compile_commands.json --fix-errors $(FMT) + $(MAKE) -C .. $@ + +LINT := $(shell git ls-files '*.c' '*.cpp' '*.h' '*.hpp') +lint: FORCE + clang-tidy -p build/compile_commands.json --fix-errors $(LINT) diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index f830165..14c5123 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -4,5 +4,7 @@ target_sources(test_main PUBLIC PhysicsTest.cpp ScriptTest.cpp ParticleTest.cpp + ECSTest.cpp + SceneManagerTest.cpp ) diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp new file mode 100644 index 0000000..d5a5826 --- /dev/null +++ b/src/test/ECSTest.cpp @@ -0,0 +1,236 @@ +#include + +#define protected public + +#include +#include +#include +#include +#include + +using namespace std; +using namespace crepe; + +class ECSTest : public ::testing::Test { +public: + ComponentManager mgr{}; +}; + +TEST_F(ECSTest, createGameObject) { + GameObject obj = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + + vector> metadata = mgr.get_components_by_type(); + vector> transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 1); + EXPECT_EQ(transform.size(), 1); + + EXPECT_EQ(metadata[0].get().name, "body"); + EXPECT_EQ(metadata[0].get().tag, "person"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + + EXPECT_EQ(transform[0].get().position.x, 0); + EXPECT_EQ(transform[0].get().position.y, 0); + EXPECT_EQ(transform[0].get().rotation, 0); + EXPECT_EQ(transform[0].get().scale, 1); +} + +TEST_F(ECSTest, deleteAllGameObjects) { + GameObject obj0 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + + mgr.delete_all_components(); + + vector> metadata = mgr.get_components_by_type(); + vector> transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 0); + EXPECT_EQ(transform.size(), 0); + + GameObject obj2 = mgr.new_object("body2", "person2", Vector2{1, 0}, 5, 1); + + metadata = mgr.get_components_by_type(); + transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 1); + EXPECT_EQ(transform.size(), 1); + + EXPECT_EQ(metadata[0].get().game_object_id, 0); + EXPECT_EQ(metadata[0].get().name, "body2"); + EXPECT_EQ(metadata[0].get().tag, "person2"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + + EXPECT_EQ(transform[0].get().game_object_id, 0); + EXPECT_EQ(transform[0].get().position.x, 1); + EXPECT_EQ(transform[0].get().position.y, 0); + EXPECT_EQ(transform[0].get().rotation, 5); + EXPECT_EQ(transform[0].get().scale, 1); +} + +TEST_F(ECSTest, deleteGameObject) { + GameObject obj0 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + + mgr.delete_all_components_of_id(0); + + vector> metadata = mgr.get_components_by_type(); + vector> transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 1); + EXPECT_EQ(transform.size(), 1); + + EXPECT_EQ(metadata[0].get().game_object_id, 1); + EXPECT_EQ(metadata[0].get().name, "body"); + EXPECT_EQ(metadata[0].get().tag, "person"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + + EXPECT_EQ(transform[0].get().game_object_id, 1); + EXPECT_EQ(transform[0].get().position.x, 0); + EXPECT_EQ(transform[0].get().position.y, 0); + EXPECT_EQ(transform[0].get().rotation, 0); + EXPECT_EQ(transform[0].get().scale, 1); +} + +TEST_F(ECSTest, manyGameObjects) { + for (int i = 0; i < 5000; i++) { + GameObject obj = mgr.new_object("body", "person", Vector2{0, 0}, 0, i); + } + + vector> metadata = mgr.get_components_by_type(); + vector> transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 5000); + EXPECT_EQ(transform.size(), 5000); + for (int i = 0; i < 5000; i++) { + EXPECT_EQ(metadata[i].get().game_object_id, i); + EXPECT_EQ(metadata[i].get().name, "body"); + EXPECT_EQ(metadata[i].get().tag, "person"); + EXPECT_EQ(metadata[i].get().parent, -1); + EXPECT_EQ(metadata[i].get().children.size(), 0); + + EXPECT_EQ(transform[i].get().game_object_id, i); + EXPECT_EQ(transform[i].get().position.x, 0); + EXPECT_EQ(transform[i].get().position.y, 0); + EXPECT_EQ(transform[i].get().rotation, 0); + EXPECT_EQ(transform[i].get().scale, i); + } + + mgr.delete_components(); + + metadata = mgr.get_components_by_type(); + transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 0); + EXPECT_EQ(transform.size(), 5000); + + for (int i = 0; i < 10000 - 5000; i++) { + string tag = "person" + to_string(i); + GameObject obj = mgr.new_object("body", tag, Vector2{0, 0}, i, 0); + } + + metadata = mgr.get_components_by_type(); + transform = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 10000 - 5000); + EXPECT_EQ(transform.size(), 10000); +} + +TEST_F(ECSTest, getComponentsByID) { + GameObject obj0 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + + vector> metadata = mgr.get_components_by_id(0); + vector> transform = mgr.get_components_by_id(1); + + EXPECT_EQ(metadata.size(), 1); + EXPECT_EQ(transform.size(), 1); + + EXPECT_EQ(metadata[0].get().game_object_id, 0); + EXPECT_EQ(metadata[0].get().name, "body"); + EXPECT_EQ(metadata[0].get().tag, "person"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + + EXPECT_EQ(transform[0].get().game_object_id, 1); + EXPECT_EQ(transform[0].get().position.x, 0); + EXPECT_EQ(transform[0].get().position.y, 0); + EXPECT_EQ(transform[0].get().rotation, 0); + EXPECT_EQ(transform[0].get().scale, 1); +} + +TEST_F(ECSTest, tooMuchComponents) { + try { + GameObject obj0 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + obj0.add_component(Vector2{10, 10}, 0, 1); + } catch (const exception & e) { + EXPECT_EQ(e.what(), + string("Exceeded maximum number of instances for this component type")); + } + + try { + GameObject obj1 = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + obj1.add_component("body", "person"); + } catch (const exception & e) { + EXPECT_EQ(e.what(), + string("Exceeded maximum number of instances for this component type")); + } + + vector> metadata = mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 2); + EXPECT_EQ(metadata[0].get().name, "body"); + EXPECT_EQ(metadata[1].get().name, "body"); +} + +TEST_F(ECSTest, partentChild) { + { + GameObject body = mgr.new_object("body", "person", Vector2{0, 0}, 0, 1); + GameObject right_leg = mgr.new_object("rightLeg", "person", Vector2{1, 1}, 0, 1); + GameObject left_leg = mgr.new_object("leftLeg", "person", Vector2{1, 1}, 0, 1); + GameObject right_foot = mgr.new_object("rightFoot", "person", Vector2{2, 2}, 0, 1); + GameObject left_foot = mgr.new_object("leftFoot", "person", Vector2{2, 2}, 0, 1); + + // Set the parent of each GameObject + right_foot.set_parent(right_leg); + left_foot.set_parent(left_leg); + right_leg.set_parent(body); + left_leg.set_parent(body); + } + + // Get the Metadata and Transform components of each GameObject + vector> metadata = mgr.get_components_by_type(); + + // Check IDs + EXPECT_EQ(metadata[0].get().game_object_id, 0); + EXPECT_EQ(metadata[1].get().game_object_id, 1); + EXPECT_EQ(metadata[2].get().game_object_id, 2); + EXPECT_EQ(metadata[3].get().game_object_id, 3); + EXPECT_EQ(metadata[4].get().game_object_id, 4); + + // Check the parent-child relationships + EXPECT_EQ(metadata[0].get().name, "body"); + EXPECT_EQ(metadata[1].get().name, "rightLeg"); + EXPECT_EQ(metadata[2].get().name, "leftLeg"); + EXPECT_EQ(metadata[3].get().name, "rightFoot"); + EXPECT_EQ(metadata[4].get().name, "leftFoot"); + + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[1].get().parent, 0); + EXPECT_EQ(metadata[2].get().parent, 0); + EXPECT_EQ(metadata[3].get().parent, 1); + EXPECT_EQ(metadata[4].get().parent, 2); + + EXPECT_EQ(metadata[0].get().children.size(), 2); + EXPECT_EQ(metadata[1].get().children.size(), 1); + EXPECT_EQ(metadata[2].get().children.size(), 1); + EXPECT_EQ(metadata[3].get().children.size(), 0); + EXPECT_EQ(metadata[4].get().children.size(), 0); + + EXPECT_EQ(metadata[0].get().children[0], 1); + EXPECT_EQ(metadata[0].get().children[1], 2); + EXPECT_EQ(metadata[1].get().children[0], 3); + EXPECT_EQ(metadata[2].get().children[0], 4); +} diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp new file mode 100644 index 0000000..ac479d3 --- /dev/null +++ b/src/test/RenderSystemTest.cpp @@ -0,0 +1,174 @@ +#include "api/Camera.h" +#include +#include +#include +#include + +#define private public +#define protected public + +#include +#include +#include +#include +#include + +#include + +using namespace std; +using namespace crepe; +using namespace testing; + +class RenderSystemTest : public Test { +public: + ComponentManager mgr{}; + RenderSystem sys{mgr}; + GameObject entity1 = this->mgr.new_object("name"); + GameObject entity2 = this->mgr.new_object("name"); + GameObject entity3 = this->mgr.new_object("name"); + GameObject entity4 = this->mgr.new_object("name"); + + void SetUp() override { + auto & sprite1 + = entity1.add_component(make_shared("../asset/texture/img.png"), + Color(0, 0, 0, 0), FlipSettings{false, false}); + ASSERT_NE(sprite1.sprite_image.get(), nullptr); + sprite1.order_in_layer = 5; + sprite1.sorting_in_layer = 5; + EXPECT_EQ(sprite1.order_in_layer, 5); + EXPECT_EQ(sprite1.sorting_in_layer, 5); + auto & sprite2 + = entity2.add_component(make_shared("../asset/texture/img.png"), + Color(0, 0, 0, 0), FlipSettings{false, false}); + ASSERT_NE(sprite2.sprite_image.get(), nullptr); + sprite2.sorting_in_layer = 2; + sprite2.order_in_layer = 1; + + EXPECT_EQ(sprite2.sorting_in_layer, 2); + EXPECT_EQ(sprite2.order_in_layer, 1); + + auto & sprite3 + = entity3.add_component(make_shared("../asset/texture/img.png"), + Color(0, 0, 0, 0), FlipSettings{false, false}); + ASSERT_NE(sprite3.sprite_image.get(), nullptr); + sprite3.sorting_in_layer = 1; + sprite3.order_in_layer = 2; + + EXPECT_EQ(sprite3.sorting_in_layer, 1); + EXPECT_EQ(sprite3.order_in_layer, 2); + + auto & sprite4 + = entity4.add_component(make_shared("../asset/texture/img.png"), + Color(0, 0, 0, 0), FlipSettings{false, false}); + ASSERT_NE(sprite4.sprite_image.get(), nullptr); + sprite4.sorting_in_layer = 1; + sprite4.order_in_layer = 1; + EXPECT_EQ(sprite4.sorting_in_layer, 1); + EXPECT_EQ(sprite4.order_in_layer, 1); + } +}; + +TEST_F(RenderSystemTest, expected_throws) { + GameObject entity1 = this->mgr.new_object("NAME"); + + // no texture img + EXPECT_ANY_THROW({ + entity1.add_component(make_shared("NO_IMAGE"), Color(0, 0, 0, 0), + FlipSettings{false, false}); + }); + + // No camera + EXPECT_ANY_THROW({ this->sys.update(); }); +} + +TEST_F(RenderSystemTest, make_sprites) {} + +TEST_F(RenderSystemTest, sorting_sprites) { + vector> sprites = this->mgr.get_components_by_type(); + ASSERT_EQ(sprites.size(), 4); + + vector> sorted_sprites = this->sys.sort(sprites); + ASSERT_EQ(sorted_sprites.size(), 4); + + // Expected order after sorting: + // 1. sorting_in_layer: 1, order_in_layer: 1 (entity4) + // 2. sorting_in_layer: 1, order_in_layer: 2 (entity3) + // 3. sorting_in_layer: 2, order_in_layer: 1 (entity2) + // 4. sorting_in_layer: 5, order_in_layer: 5 (entity1) + + EXPECT_EQ(sorted_sprites[0].get().sorting_in_layer, 1); + EXPECT_EQ(sorted_sprites[0].get().order_in_layer, 1); + + EXPECT_EQ(sorted_sprites[1].get().sorting_in_layer, 1); + EXPECT_EQ(sorted_sprites[1].get().order_in_layer, 2); + + EXPECT_EQ(sorted_sprites[2].get().sorting_in_layer, 2); + EXPECT_EQ(sorted_sprites[2].get().order_in_layer, 1); + + EXPECT_EQ(sorted_sprites[3].get().sorting_in_layer, 5); + EXPECT_EQ(sorted_sprites[3].get().order_in_layer, 5); + + for (size_t i = 1; i < sorted_sprites.size(); ++i) { + const Sprite & prev = sorted_sprites[i - 1].get(); + const Sprite & curr = sorted_sprites[i].get(); + + if (prev.sorting_in_layer == curr.sorting_in_layer) { + EXPECT_LE(prev.order_in_layer, curr.order_in_layer); + } else { + EXPECT_LE(prev.sorting_in_layer, curr.sorting_in_layer); + } + } +} + +TEST_F(RenderSystemTest, Update) { + entity1.add_component(Color::WHITE); + { + vector> sprites = this->mgr.get_components_by_type(); + ASSERT_EQ(sprites.size(), 4); + + EXPECT_EQ(sprites[0].get().game_object_id, 0); + EXPECT_EQ(sprites[1].get().game_object_id, 1); + EXPECT_EQ(sprites[2].get().game_object_id, 2); + EXPECT_EQ(sprites[3].get().game_object_id, 3); + } + this->sys.update(); + { + vector> sprites = this->mgr.get_components_by_type(); + ASSERT_EQ(sprites.size(), 4); + + EXPECT_EQ(sprites[0].get().game_object_id, 0); + EXPECT_EQ(sprites[1].get().game_object_id, 1); + EXPECT_EQ(sprites[2].get().game_object_id, 2); + EXPECT_EQ(sprites[3].get().game_object_id, 3); + } +} + +TEST_F(RenderSystemTest, Camera) { + { + auto cameras = this->mgr.get_components_by_type(); + EXPECT_NE(cameras.size(), 1); + } + { + entity1.add_component(Color::WHITE); + auto cameras = this->mgr.get_components_by_type(); + EXPECT_EQ(cameras.size(), 1); + } + + //TODO improve with newer version +} +TEST_F(RenderSystemTest, Color) { + entity1.add_component(Color::WHITE); + auto & sprite = this->mgr.get_components_by_id(entity1.id).front().get(); + ASSERT_NE(sprite.sprite_image.get(), nullptr); + + sprite.color = Color::GREEN; + EXPECT_EQ(sprite.color.r, Color::GREEN.r); + EXPECT_EQ(sprite.color.g, Color::GREEN.g); + EXPECT_EQ(sprite.color.b, Color::GREEN.b); + EXPECT_EQ(sprite.color.a, Color::GREEN.a); + this->sys.update(); + EXPECT_EQ(sprite.color.r, Color::GREEN.r); + EXPECT_EQ(sprite.color.g, Color::GREEN.g); + EXPECT_EQ(sprite.color.b, Color::GREEN.b); + EXPECT_EQ(sprite.color.a, Color::GREEN.a); +} diff --git a/src/test/SceneManagerTest.cpp b/src/test/SceneManagerTest.cpp new file mode 100644 index 0000000..69e1171 --- /dev/null +++ b/src/test/SceneManagerTest.cpp @@ -0,0 +1,122 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace crepe; + +class ConcreteScene1 : public Scene { +public: + using Scene::Scene; + + void load_scene() { + auto & mgr = this->component_manager; + GameObject object1 = mgr.new_object("scene_1", "tag_scene_1", Vector2{0, 0}, 0, 1); + GameObject object2 = mgr.new_object("scene_1", "tag_scene_1", Vector2{1, 0}, 0, 1); + GameObject object3 = mgr.new_object("scene_1", "tag_scene_1", Vector2{2, 0}, 0, 1); + } +}; + +class ConcreteScene2 : public Scene { +public: + using Scene::Scene; + + void load_scene() { + auto & mgr = this->component_manager; + GameObject object1 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 0}, 0, 1); + GameObject object2 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 1}, 0, 1); + GameObject object3 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 2}, 0, 1); + GameObject object4 = mgr.new_object("scene_2", "tag_scene_2", Vector2{0, 3}, 0, 1); + } +}; + +class SceneManagerTest : public ::testing::Test { +public: + ComponentManager component_mgr{}; + SceneManager scene_mgr{component_mgr}; +}; + +TEST_F(SceneManagerTest, loadScene) { + scene_mgr.add_scene("scene1"); + scene_mgr.add_scene("scene2"); + + scene_mgr.load_next_scene(); + + vector> metadata + = component_mgr.get_components_by_type(); + vector> transform + = component_mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 3); + EXPECT_EQ(transform.size(), 3); + + EXPECT_EQ(metadata[0].get().game_object_id, 0); + EXPECT_EQ(metadata[0].get().name, "scene_1"); + EXPECT_EQ(metadata[0].get().tag, "tag_scene_1"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + EXPECT_EQ(transform[0].get().position.x, 0); + EXPECT_EQ(transform[0].get().position.y, 0); + + EXPECT_EQ(metadata[1].get().game_object_id, 1); + EXPECT_EQ(metadata[1].get().name, "scene_1"); + EXPECT_EQ(metadata[1].get().tag, "tag_scene_1"); + EXPECT_EQ(metadata[1].get().parent, -1); + EXPECT_EQ(metadata[1].get().children.size(), 0); + EXPECT_EQ(transform[1].get().position.x, 1); + EXPECT_EQ(transform[1].get().position.y, 0); + + EXPECT_EQ(metadata[2].get().game_object_id, 2); + EXPECT_EQ(metadata[2].get().name, "scene_1"); + EXPECT_EQ(metadata[2].get().tag, "tag_scene_1"); + EXPECT_EQ(metadata[2].get().parent, -1); + EXPECT_EQ(metadata[2].get().children.size(), 0); + EXPECT_EQ(transform[2].get().position.x, 2); + EXPECT_EQ(transform[2].get().position.y, 0); + + scene_mgr.set_next_scene("scene2"); + scene_mgr.load_next_scene(); + + metadata = component_mgr.get_components_by_type(); + transform = component_mgr.get_components_by_type(); + + EXPECT_EQ(metadata.size(), 4); + EXPECT_EQ(transform.size(), 4); + + EXPECT_EQ(metadata[0].get().game_object_id, 0); + EXPECT_EQ(metadata[0].get().name, "scene_2"); + EXPECT_EQ(metadata[0].get().tag, "tag_scene_2"); + EXPECT_EQ(metadata[0].get().parent, -1); + EXPECT_EQ(metadata[0].get().children.size(), 0); + EXPECT_EQ(transform[0].get().position.x, 0); + EXPECT_EQ(transform[0].get().position.y, 0); + + EXPECT_EQ(metadata[1].get().game_object_id, 1); + EXPECT_EQ(metadata[1].get().name, "scene_2"); + EXPECT_EQ(metadata[1].get().tag, "tag_scene_2"); + EXPECT_EQ(metadata[1].get().parent, -1); + EXPECT_EQ(metadata[1].get().children.size(), 0); + EXPECT_EQ(transform[1].get().position.x, 0); + EXPECT_EQ(transform[1].get().position.y, 1); + + EXPECT_EQ(metadata[2].get().game_object_id, 2); + EXPECT_EQ(metadata[2].get().name, "scene_2"); + EXPECT_EQ(metadata[2].get().tag, "tag_scene_2"); + EXPECT_EQ(metadata[2].get().parent, -1); + EXPECT_EQ(metadata[2].get().children.size(), 0); + EXPECT_EQ(transform[2].get().position.x, 0); + EXPECT_EQ(transform[2].get().position.y, 2); + + EXPECT_EQ(metadata[3].get().game_object_id, 3); + EXPECT_EQ(metadata[3].get().name, "scene_2"); + EXPECT_EQ(metadata[3].get().tag, "tag_scene_2"); + EXPECT_EQ(metadata[3].get().parent, -1); + EXPECT_EQ(metadata[3].get().children.size(), 0); + EXPECT_EQ(transform[3].get().position.x, 0); + EXPECT_EQ(transform[3].get().position.y, 3); +} -- cgit v1.2.3 From c341f6a44699d71b8b20e6b814274b30e43514c8 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sat, 23 Nov 2024 22:07:15 +0100 Subject: added profiling test --- src/test/CMakeLists.txt | 27 +++++----- src/test/CollisionTest.cpp | 16 ------ src/test/Profiling.cpp | 128 +++++++++++++++++++++++++++++++++++++++++++++ src/test/main.cpp | 2 +- 4 files changed, 143 insertions(+), 30 deletions(-) create mode 100644 src/test/Profiling.cpp (limited to 'src/test/CMakeLists.txt') diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index c65940b..68fa01c 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -1,16 +1,17 @@ target_sources(test_main PUBLIC - CollisionTest.cpp + # CollisionTest.cpp main.cpp - PhysicsTest.cpp - ScriptTest.cpp - ParticleTest.cpp - AssetTest.cpp - OptionalRefTest.cpp - RenderSystemTest.cpp - EventTest.cpp - ECSTest.cpp - SceneManagerTest.cpp - ValueBrokerTest.cpp - DBTest.cpp - Vector2Test.cpp + # PhysicsTest.cpp + # ScriptTest.cpp + # ParticleTest.cpp + # AssetTest.cpp + # OptionalRefTest.cpp + # RenderSystemTest.cpp + # EventTest.cpp + # ECSTest.cpp + # SceneManagerTest.cpp + # ValueBrokerTest.cpp + # DBTest.cpp + # Vector2Test.cpp + Profiling.cpp ) diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index e36260c..245cced 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -22,22 +22,6 @@ using namespace std::chrono_literals; using namespace crepe; using namespace testing; -// class MyScript : public Script { -// bool oncollision(const CollisionEvent& test) { -// Log::logf("Box {} script on_collision()", test.info.first.collider.game_object_id); -// return true; -// } -// void init() { -// subscribe([this](const CollisionEvent& ev) -> bool { -// return this->oncollision(ev); -// }); -// } -// void update() { -// // Retrieve component from the same GameObject this script is on -// } -// }; - - class CollisionHandler : public Script { public: int box_id; diff --git a/src/test/Profiling.cpp b/src/test/Profiling.cpp new file mode 100644 index 0000000..9929288 --- /dev/null +++ b/src/test/Profiling.cpp @@ -0,0 +1,128 @@ +#include "system/ParticleSystem.h" +#include "system/PhysicsSystem.h" +#include "system/RenderSystem.h" +#include +#include +#include + +#define private public +#define protected public + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace std::chrono_literals; +using namespace crepe; +using namespace testing; + +class CollisionHandler : public Script { +public: + int box_id; + + CollisionHandler(int box_id) { + this->box_id = box_id; + } + + bool on_collision(const CollisionEvent& ev) { + // test load? + return true; + } + + void init() { + subscribe([this](const CollisionEvent& ev) -> bool { + return this->on_collision(ev); + }); + } + void update() { + // Retrieve component from the same GameObject this script is on + } +}; + +class Profiling : public Test { +public: + ComponentManager mgr; + // Add system used for profling tests + CollisionSystem collision_sys{mgr}; + PhysicsSystem physics_sys{mgr}; + ParticleSystem particle_sys{mgr}; + RenderSystem render_sys{mgr}; + ScriptSystem script_sys{mgr}; + + // Store individual function timings + std::map timings; + + // Min and max gameobject that should and can be created + int min_gameobject_count = 100; + int max_gameobject_count = 100; + + void SetUp() override { + + GameObject do_not_use = mgr.new_object("DO_NOT_USE","",{0,0}); + do_not_use.add_component(Color::WHITE); + // initialize systems here: + //calls init + script_sys.update(); + //creates window + render_sys.update(); + } + + // Helper function to time an update call and store its duration + template + long long time_function(const std::string& name, Func&& func) { + auto start = std::chrono::steady_clock::now(); + func(); + auto end = std::chrono::steady_clock::now(); + auto duration = std::chrono::duration_cast(end - start).count(); + timings[name] = duration; // Store the duration in microseconds + return duration; // Return the duration in microseconds + } + + // Run and profile all systems, return the total time in milliseconds + long long run_all_systems() { + long long total_microseconds = 0; + total_microseconds += time_function("PhysicsSystem", [&]() { physics_sys.update(); }); + total_microseconds += time_function("CollisionSystem", [&]() { collision_sys.update(); }); + total_microseconds += time_function("ParticleSystem", [&]() { particle_sys.update(); }); + total_microseconds += time_function("RenderSystem", [&]() { render_sys.update(); }); + return total_microseconds; + } + + // Print timings of all functions + void log_timings(long long total_time,int game_object_count) const { + std::stringstream ss; + ss << std::endl <<"Function timings:\n"; // Starting with a header + for (const auto& [name, duration] : timings) { + ss << name << " took " << duration / 1000.0 << " ms (" << duration << " µs). " << std::endl; + } + ss << "Total time: " << total_time / 1000.0 << "ms (" << total_time << " µs)" << std::endl; + ss << "Amount of gameobjects: " << game_object_count << std::endl; + // Use GTest INFO macro to print the accumulated log without extra newlines + GTEST_LOG_(INFO) << ss.str(); + } +}; + +TEST_F(Profiling, Profiling_example) { + int game_object_count = 0; + long long total_time = 0; + while (total_time < 16000) { + game_object_count++; + { + //define gameobject used for testing + GameObject gameobject = mgr.new_object("gameobject","",{0,0}); + } + total_time = run_all_systems(); + if(game_object_count >= max_gameobject_count) break; + } + log_timings(total_time,game_object_count); + EXPECT_GE(game_object_count, min_gameobject_count); +} diff --git a/src/test/main.cpp b/src/test/main.cpp index 241015d..ec293a6 100644 --- a/src/test/main.cpp +++ b/src/test/main.cpp @@ -9,7 +9,7 @@ int main(int argc, char ** argv) { InitGoogleTest(&argc, argv); auto & cfg = Config::get_instance(); - cfg.log.level = Log::Level::ERROR; + cfg.log.level = Log::Level::DEBUG; return RUN_ALL_TESTS(); } -- cgit v1.2.3 From 7566fa3d50093935315598aabcb0dc0a9e7cca57 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Sun, 1 Dec 2024 22:07:05 +0100 Subject: removed struct and renamed structs --- src/crepe/system/CollisionSystem.cpp | 100 ++++++++++++++-------------- src/crepe/system/CollisionSystem.h | 41 +++++------- src/test/CMakeLists.txt | 4 +- src/test/CollisionTest.cpp | 124 +++++++++++++++++------------------ src/test/Profiling.cpp | 2 +- 5 files changed, 134 insertions(+), 137 deletions(-) (limited to 'src/test/CMakeLists.txt') diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 6f2c39d..362378d 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -28,19 +28,19 @@ void CollisionSystem::update() { std::vector> boxcolliders = mgr.get_components_by_type(); std::vector> circlecolliders = mgr.get_components_by_type(); - std::vector all_colliders; + std::vector all_colliders; // Add BoxCollider references for (auto& box : boxcolliders) { - all_colliders.push_back(collider_stor{box}); + all_colliders.push_back(collider_variant{box}); } // Add CircleCollider references for (auto& circle : circlecolliders) { - all_colliders.push_back(collider_stor{circle}); + all_colliders.push_back(collider_variant{circle}); } // Check between all colliders if there is a collision - std::vector> collided = check_collisions(all_colliders); + std::vector> collided = check_collisions(all_colliders); // For both objects call the collision handler for (auto& collision_pair : collided) { @@ -49,30 +49,30 @@ void CollisionSystem::update() { } } -void CollisionSystem::collision_handler_request(CollidedInfoStor& data1,CollidedInfoStor& data2){ +void CollisionSystem::collision_handler_request(CollisionInternal& data1,CollisionInternal& data2){ - ColliderStorType type = check_collider_type(data1.collider,data2.collider); - std::pair move_back_data = collision_handler(data1,data2,type); + CollisionInternalType type = check_collider_type(data1.collider,data2.collider); + std::pair resolution_data = collision_handler(data1,data2,type); OptionalRef collider1; OptionalRef collider2; switch (type) { - case ColliderStorType::BOX_BOX:{ + case CollisionInternalType::BOX_BOX:{ collider1 = std::get>(data1.collider); collider2 = std::get>(data2.collider); break; } - case ColliderStorType::BOX_CIRCLE:{ + case CollisionInternalType::BOX_CIRCLE:{ collider1 = std::get>(data1.collider); collider2 = std::get>(data2.collider); break; } - case ColliderStorType::CIRCLE_BOX:{ + case CollisionInternalType::CIRCLE_BOX:{ collider1 = std::get>(data1.collider); collider2 = std::get>(data2.collider); break; } - case ColliderStorType::CIRCLE_CIRCLE:{ + case CollisionInternalType::CIRCLE_CIRCLE:{ collider1 = std::get>(data1.collider); collider2 = std::get>(data2.collider); break; @@ -81,10 +81,14 @@ void CollisionSystem::collision_handler_request(CollidedInfoStor& data1,Collided // collision info crepe::CollisionSystem::CollisionInfo collision_info{ - .first={ collider1, data1.transform, data1.rigidbody }, - .second={ collider2, data2.transform, data2.rigidbody }, - .move_back_value = move_back_data.first, - .move_back_direction = move_back_data.second, + .first_collider = collider1, + .first_transform = data1.transform, + .first_rigidbody = data1.rigidbody, + .second_collider = collider2, + .second_transform = data2.transform, + .second_rigidbody = data2.rigidbody, + .resolution = resolution_data.first, + .resolution_direction = resolution_data.second, }; // Determine if static needs to be called @@ -92,23 +96,23 @@ void CollisionSystem::collision_handler_request(CollidedInfoStor& data1,Collided } -std::pair CollisionSystem::collision_handler(CollidedInfoStor& data1,CollidedInfoStor& data2,ColliderStorType type) { +std::pair CollisionSystem::collision_handler(CollisionInternal& data1,CollisionInternal& data2,CollisionInternalType type) { vec2 move_back; switch (type) { - case ColliderStorType::BOX_BOX: { + case CollisionInternalType::BOX_BOX: { const BoxCollider & collider1 = std::get>(data1.collider); const BoxCollider & collider2 = std::get>(data2.collider); vec2 collider_pos1 = current_position(collider1.offset, data1.transform, data1.rigidbody); vec2 collider_pos2 = current_position(collider2.offset, data2.transform, data2.rigidbody); move_back = box_box_move_back(collider1,collider2,collider_pos1,collider_pos2); } - case ColliderStorType::BOX_CIRCLE: { + case CollisionInternalType::BOX_CIRCLE: { } - case ColliderStorType::CIRCLE_CIRCLE: { + case CollisionInternalType::CIRCLE_CIRCLE: { } - case ColliderStorType::CIRCLE_BOX: { + case CollisionInternalType::CIRCLE_BOX: { } } @@ -165,43 +169,43 @@ vec2 CollisionSystem::box_box_move_back(const BoxCollider& box_collider1,const B void CollisionSystem::determine_collision_handler(CollisionInfo& info){ // Check rigidbody type for static - if(info.first.rigidbody.data.body_type != Rigidbody::BodyType::STATIC) + if(info.first_rigidbody.data.body_type != Rigidbody::BodyType::STATIC) { // If second body is static perform the static collision handler in this system - if(info.second.rigidbody.data.body_type == Rigidbody::BodyType::STATIC){ + if(info.second_rigidbody.data.body_type == Rigidbody::BodyType::STATIC){ static_collision_handler(info); }; // Call collision event for user CollisionEvent data(info); - EventManager::get_instance().trigger_event(data, info.first.collider.game_object_id); + EventManager::get_instance().trigger_event(data, info.first_collider.game_object_id); } } void CollisionSystem::static_collision_handler(CollisionInfo& info){ // Move object back using calculate move back value - info.first.transform.position += info.move_back_value; + info.first_transform.position += info.resolution; // If bounce is enabled mirror velocity - if(info.first.rigidbody.data.bounce) { - if(info.move_back_direction == Direction::BOTH) + if(info.first_rigidbody.data.bounce) { + if(info.resolution_direction == Direction::BOTH) { - info.first.rigidbody.data.linear_velocity.y = -info.first.rigidbody.data.linear_velocity.y * info.first.rigidbody.data.elastisity; - info.first.rigidbody.data.linear_velocity.x = -info.first.rigidbody.data.linear_velocity.x * info.first.rigidbody.data.elastisity; + info.first_rigidbody.data.linear_velocity.y = -info.first_rigidbody.data.linear_velocity.y * info.first_rigidbody.data.elastisity; + info.first_rigidbody.data.linear_velocity.x = -info.first_rigidbody.data.linear_velocity.x * info.first_rigidbody.data.elastisity; } - else if(info.move_back_direction == Direction::Y_DIRECTION) { - info.first.rigidbody.data.linear_velocity.y = -info.first.rigidbody.data.linear_velocity.y * info.first.rigidbody.data.elastisity; + else if(info.resolution_direction == Direction::Y_DIRECTION) { + info.first_rigidbody.data.linear_velocity.y = -info.first_rigidbody.data.linear_velocity.y * info.first_rigidbody.data.elastisity; } - else if(info.move_back_direction == Direction::X_DIRECTION){ - info.first.rigidbody.data.linear_velocity.x = -info.first.rigidbody.data.linear_velocity.x * info.first.rigidbody.data.elastisity; + else if(info.resolution_direction == Direction::X_DIRECTION){ + info.first_rigidbody.data.linear_velocity.x = -info.first_rigidbody.data.linear_velocity.x * info.first_rigidbody.data.elastisity; } } // Stop movement if bounce is disabled else { - info.first.rigidbody.data.linear_velocity = {0,0}; + info.first_rigidbody.data.linear_velocity = {0,0}; } } -std::vector> CollisionSystem::check_collisions(std::vector & colliders) { +std::vector> CollisionSystem::check_collisions(std::vector & colliders) { // TODO: @@ -233,7 +237,7 @@ std::vector> collisions_ret; + std::vector> collisions_ret; for (size_t i = 0; i < colliders.size(); ++i) { std::visit([&](auto& inner_collider_ref) { if (!inner_collider_ref.get().active) return; @@ -245,11 +249,11 @@ std::vectorfirst.get(), inner_components->second.get()}, - CollidedInfoStor{colliders[j], outer_components->first.get(), outer_components->second.get()} + CollisionInternal{colliders[i], inner_components->first.get(), inner_components->second.get()}, + CollisionInternal{colliders[j], outer_components->first.get(), outer_components->second.get()} ); }, colliders[j]); } @@ -259,45 +263,45 @@ std::vector>(collider1)){ if(std::holds_alternative>(collider2)) { - return ColliderStorType::CIRCLE_CIRCLE; + return CollisionInternalType::CIRCLE_CIRCLE; } else { - return ColliderStorType::CIRCLE_BOX; + return CollisionInternalType::CIRCLE_BOX; } } else { if(std::holds_alternative>(collider2)) { - return ColliderStorType::BOX_CIRCLE; + return CollisionInternalType::BOX_CIRCLE; } else { - return ColliderStorType::BOX_BOX; + return CollisionInternalType::BOX_BOX; } } } -bool CollisionSystem::check_collision(const collider_stor& collider1,std::pair, std::reference_wrapper> components1,const collider_stor& collider2,std::pair, std::reference_wrapper> components2, ColliderStorType type){ +bool CollisionSystem::check_collision(const collider_variant& collider1,std::pair, std::reference_wrapper> components1,const collider_variant& collider2,std::pair, std::reference_wrapper> components2, CollisionInternalType type){ switch (type) { - case ColliderStorType::BOX_BOX: { + case CollisionInternalType::BOX_BOX: { const BoxCollider & box_collider1 = std::get>(collider1); const BoxCollider & box_collider2 = std::get>(collider2); return check_box_box_collision(box_collider1,box_collider2,components1.first.get(),components2.first.get(),components1.second.get(),components2.second.get()); } - case ColliderStorType::BOX_CIRCLE: { + case CollisionInternalType::BOX_CIRCLE: { const BoxCollider & box_collider = std::get>(collider1); const CircleCollider & circle_collider = std::get>(collider2); return check_box_circle_collision(box_collider,circle_collider,components1.first.get(),components2.first.get(),components1.second.get(),components2.second.get()); } - case ColliderStorType::CIRCLE_CIRCLE: { + case CollisionInternalType::CIRCLE_CIRCLE: { const CircleCollider & circle_collider1 = std::get>(collider1); const CircleCollider & circle_collider2 = std::get>(collider2); return check_circle_circle_collision(circle_collider1,circle_collider2,components1.first.get(),components2.first.get(),components1.second.get(),components2.second.get()); } - case ColliderStorType::CIRCLE_BOX: { + case CollisionInternalType::CIRCLE_BOX: { const CircleCollider & circle_collider = std::get>(collider1); const BoxCollider & box_collider = std::get>(collider2); return check_box_circle_collision(box_collider,circle_collider,components1.first.get(),components2.first.get(),components1.second.get(),components2.second.get()); diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h index 6334ba1..f8c7633 100644 --- a/src/crepe/system/CollisionSystem.h +++ b/src/crepe/system/CollisionSystem.h @@ -23,10 +23,10 @@ private: //! A variant type that can hold either a BoxCollider or a CircleCollider. // using collider_stor = std::variant; - using collider_stor = std::variant, std::reference_wrapper>; + using collider_variant = std::variant, std::reference_wrapper>; //! A enum that is used to tell the pair of the collider stor in a std::pair. - enum class ColliderStorType { + enum class CollisionInternalType { BOX_BOX, CIRCLE_CIRCLE, BOX_CIRCLE, @@ -38,9 +38,9 @@ private: * * This structure stores the collider type, its associated transform, and its rigidbody. */ - struct CollidedInfoStor { + struct CollisionInternal { //! Store either BoxCollider or CircleCollider - collider_stor& collider; + collider_variant& collider; Transform& transform; Rigidbody& rigidbody; }; @@ -54,27 +54,20 @@ private: }; public: - /** - * \brief A structure representing the collision information between two colliders. - * - * This structure contains both colliders, their associated transforms and rigidbodies, - * as well as the movement vector to resolve the collision. - */ - struct ColliderInfo { - const Collider& collider; - Transform& transform; - Rigidbody& rigidbody; - }; /** * \brief A structure representing detailed collision information between two colliders. * * This includes the movement data required to resolve the collision. */ struct CollisionInfo{ - ColliderInfo first; - ColliderInfo second; - vec2 move_back_value; - Direction move_back_direction = Direction::NONE; + Collider& first_collider; + Transform& first_transform; + Rigidbody& first_rigidbody; + Collider& second_collider; + Transform& second_transform; + Rigidbody& second_rigidbody; + vec2 resolution; + Direction resolution_direction = Direction::NONE; }; public: @@ -94,7 +87,7 @@ private: //generic * * \return collider pair type. */ - ColliderStorType check_collider_type(const collider_stor& collider1,const collider_stor& collider2); + CollisionInternalType check_collider_type(const collider_variant& collider1,const collider_variant& collider2); /** * \brief Calculates the position of the Collider @@ -122,7 +115,7 @@ private:// handeling * * \return Postion of collider. */ - void collision_handler_request(CollidedInfoStor& data1,CollidedInfoStor& data2); + void collision_handler_request(CollisionInternal& data1,CollisionInternal& data2); /** * \brief Calculates the move back value and direction of the Collision @@ -135,7 +128,7 @@ private:// handeling * * \return Move back value and direction for first gameobject */ - std::pair collision_handler(CollidedInfoStor& data1,CollidedInfoStor& data2 ,ColliderStorType type); + std::pair collision_handler(CollisionInternal& data1,CollisionInternal& data2 ,CollisionInternalType type); /** * \brief Calculates the move back value for box box collision @@ -183,7 +176,7 @@ private: // detection * * \return Move back value and direction for first gameobject */ - std::vector> check_collisions(std::vector & colliders); + std::vector> check_collisions(std::vector & colliders); /** * \brief Calls the correct check collision function. @@ -198,7 +191,7 @@ private: // detection * * \return status of collision */ - bool check_collision(const collider_stor& collider1,std::pair, std::reference_wrapper> components1,const collider_stor& collider2,std::pair, std::reference_wrapper> components2,CollisionSystem::ColliderStorType type); + bool check_collision(const collider_variant& collider1,std::pair, std::reference_wrapper> components1,const collider_variant& collider2,std::pair, std::reference_wrapper> components2,CollisionSystem::CollisionInternalType type); /** * \brief Check collision for box on box collider diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 68fa01c..616e238 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -1,5 +1,5 @@ target_sources(test_main PUBLIC - # CollisionTest.cpp + CollisionTest.cpp main.cpp # PhysicsTest.cpp # ScriptTest.cpp @@ -13,5 +13,5 @@ target_sources(test_main PUBLIC # ValueBrokerTest.cpp # DBTest.cpp # Vector2Test.cpp - Profiling.cpp + # Profiling.cpp ) diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index 245cced..8daf77f 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -114,11 +114,11 @@ TEST_F(CollisionTest, collision_example) { bool collision_happend = false; script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 1); + EXPECT_EQ(ev.info.first_collider.game_object_id, 1); }; script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 2); + EXPECT_EQ(ev.info.first_collider.game_object_id, 2); }; EXPECT_FALSE(collision_happend); collision_sys.update(); @@ -129,17 +129,17 @@ TEST_F(CollisionTest, collision_box_box_dynamic_both_no_velocity) { bool collision_happend = false; script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 1); - EXPECT_EQ(ev.info.move_back_value.x, 10); - EXPECT_EQ(ev.info.move_back_value.y, 10); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::BOTH); + EXPECT_EQ(ev.info.first_collider.game_object_id, 1); + EXPECT_EQ(ev.info.resolution.x, 10); + EXPECT_EQ(ev.info.resolution.y, 10); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::BOTH); }; script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 2); - EXPECT_EQ(ev.info.move_back_value.x, 10); - EXPECT_EQ(ev.info.move_back_value.y, 10); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::BOTH); + EXPECT_EQ(ev.info.first_collider.game_object_id, 2); + EXPECT_EQ(ev.info.resolution.x, 10); + EXPECT_EQ(ev.info.resolution.y, 10); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::BOTH); }; EXPECT_FALSE(collision_happend); Transform & tf = this->mgr.get_components_by_id(1).front().get(); @@ -152,17 +152,17 @@ TEST_F(CollisionTest, collision_box_box_dynamic_x_direction_no_velocity) { bool collision_happend = false; script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 1); - EXPECT_EQ(ev.info.move_back_value.x, -5); - EXPECT_EQ(ev.info.move_back_value.y, 0); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::X_DIRECTION); + EXPECT_EQ(ev.info.first_collider.game_object_id, 1); + EXPECT_EQ(ev.info.resolution.x, -5); + EXPECT_EQ(ev.info.resolution.y, 0); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::X_DIRECTION); }; script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 2); - EXPECT_EQ(ev.info.move_back_value.x, 5); - EXPECT_EQ(ev.info.move_back_value.y, 0); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::X_DIRECTION); + EXPECT_EQ(ev.info.first_collider.game_object_id, 2); + EXPECT_EQ(ev.info.resolution.x, 5); + EXPECT_EQ(ev.info.resolution.y, 0); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::X_DIRECTION); }; EXPECT_FALSE(collision_happend); Transform & tf = this->mgr.get_components_by_id(1).front().get(); @@ -175,17 +175,17 @@ TEST_F(CollisionTest, collision_box_box_dynamic_y_direction_no_velocity) { bool collision_happend = false; script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 1); - EXPECT_EQ(ev.info.move_back_value.x, 0); - EXPECT_EQ(ev.info.move_back_value.y, -5); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::Y_DIRECTION); + EXPECT_EQ(ev.info.first_collider.game_object_id, 1); + EXPECT_EQ(ev.info.resolution.x, 0); + EXPECT_EQ(ev.info.resolution.y, -5); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::Y_DIRECTION); }; script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 2); - EXPECT_EQ(ev.info.move_back_value.x, 0); - EXPECT_EQ(ev.info.move_back_value.y, 5); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::Y_DIRECTION); + EXPECT_EQ(ev.info.first_collider.game_object_id, 2); + EXPECT_EQ(ev.info.resolution.x, 0); + EXPECT_EQ(ev.info.resolution.y, 5); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::Y_DIRECTION); }; EXPECT_FALSE(collision_happend); Transform & tf = this->mgr.get_components_by_id(1).front().get(); @@ -198,17 +198,17 @@ TEST_F(CollisionTest, collision_box_box_dynamic_both) { bool collision_happend = false; script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 1); - EXPECT_EQ(ev.info.move_back_value.x, 10); - EXPECT_EQ(ev.info.move_back_value.y, 10); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::BOTH); + EXPECT_EQ(ev.info.first_collider.game_object_id, 1); + EXPECT_EQ(ev.info.resolution.x, 10); + EXPECT_EQ(ev.info.resolution.y, 10); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::BOTH); }; script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 2); - EXPECT_EQ(ev.info.move_back_value.x, 10); - EXPECT_EQ(ev.info.move_back_value.y, 10); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::BOTH); + EXPECT_EQ(ev.info.first_collider.game_object_id, 2); + EXPECT_EQ(ev.info.resolution.x, 10); + EXPECT_EQ(ev.info.resolution.y, 10); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::BOTH); }; EXPECT_FALSE(collision_happend); Transform & tf = this->mgr.get_components_by_id(1).front().get(); @@ -225,17 +225,17 @@ TEST_F(CollisionTest, collision_box_box_dynamic_x_direction) { bool collision_happend = false; script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 1); - EXPECT_EQ(ev.info.move_back_value.x, -5); - EXPECT_EQ(ev.info.move_back_value.y, -5); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::X_DIRECTION); + EXPECT_EQ(ev.info.first_collider.game_object_id, 1); + EXPECT_EQ(ev.info.resolution.x, -5); + EXPECT_EQ(ev.info.resolution.y, -5); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::X_DIRECTION); }; script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 2); - EXPECT_EQ(ev.info.move_back_value.x, 5); - EXPECT_EQ(ev.info.move_back_value.y, 5); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::X_DIRECTION); + EXPECT_EQ(ev.info.first_collider.game_object_id, 2); + EXPECT_EQ(ev.info.resolution.x, 5); + EXPECT_EQ(ev.info.resolution.y, 5); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::X_DIRECTION); }; EXPECT_FALSE(collision_happend); Transform & tf = this->mgr.get_components_by_id(1).front().get(); @@ -252,17 +252,17 @@ TEST_F(CollisionTest, collision_box_box_dynamic_y_direction) { bool collision_happend = false; script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 1); - EXPECT_EQ(ev.info.move_back_value.x, -5); - EXPECT_EQ(ev.info.move_back_value.y, -5); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::Y_DIRECTION); + EXPECT_EQ(ev.info.first_collider.game_object_id, 1); + EXPECT_EQ(ev.info.resolution.x, -5); + EXPECT_EQ(ev.info.resolution.y, -5); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::Y_DIRECTION); }; script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 2); - EXPECT_EQ(ev.info.move_back_value.x, 5); - EXPECT_EQ(ev.info.move_back_value.y, 5); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::Y_DIRECTION); + EXPECT_EQ(ev.info.first_collider.game_object_id, 2); + EXPECT_EQ(ev.info.resolution.x, 5); + EXPECT_EQ(ev.info.resolution.y, 5); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::Y_DIRECTION); }; EXPECT_FALSE(collision_happend); Transform & tf = this->mgr.get_components_by_id(1).front().get(); @@ -280,10 +280,10 @@ TEST_F(CollisionTest, collision_box_box_static_both) { bool collision_happend = false; script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 1); - EXPECT_EQ(ev.info.move_back_value.x, 10); - EXPECT_EQ(ev.info.move_back_value.y, 10); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::BOTH); + EXPECT_EQ(ev.info.first_collider.game_object_id, 1); + EXPECT_EQ(ev.info.resolution.x, 10); + EXPECT_EQ(ev.info.resolution.y, 10); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::BOTH); }; script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { // is static should not be called @@ -302,10 +302,10 @@ TEST_F(CollisionTest, collision_box_box_static_x_direction) { bool collision_happend = false; script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 1); - EXPECT_EQ(ev.info.move_back_value.x, -5); - EXPECT_EQ(ev.info.move_back_value.y, -5); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::X_DIRECTION); + EXPECT_EQ(ev.info.first_collider.game_object_id, 1); + EXPECT_EQ(ev.info.resolution.x, -5); + EXPECT_EQ(ev.info.resolution.y, -5); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::X_DIRECTION); }; script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { // is static should not be called @@ -326,10 +326,10 @@ TEST_F(CollisionTest, collision_box_box_static_y_direction) { bool collision_happend = false; script_object1_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.first.collider.game_object_id, 1); - EXPECT_EQ(ev.info.move_back_value.x, -5); - EXPECT_EQ(ev.info.move_back_value.y, -5); - EXPECT_EQ(ev.info.move_back_direction, crepe::CollisionSystem::Direction::Y_DIRECTION); + EXPECT_EQ(ev.info.first_collider.game_object_id, 1); + EXPECT_EQ(ev.info.resolution.x, -5); + EXPECT_EQ(ev.info.resolution.y, -5); + EXPECT_EQ(ev.info.resolution_direction, crepe::CollisionSystem::Direction::Y_DIRECTION); }; script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { // is static should not be called diff --git a/src/test/Profiling.cpp b/src/test/Profiling.cpp index 2b03102..2549c57 100644 --- a/src/test/Profiling.cpp +++ b/src/test/Profiling.cpp @@ -36,7 +36,7 @@ List of test cases with component settings/details class TestScript : public Script { bool oncollision(const CollisionEvent& test) { - Log::logf("Box {} script on_collision()", test.info.first.collider.game_object_id); + Log::logf("Box {} script on_collision()", test.info.first_collider.game_object_id); return true; } void init() { -- cgit v1.2.3 From eeb66130e2cb94c94e1748576f98f78ce0f1ee86 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Mon, 2 Dec 2024 17:17:21 +0100 Subject: changed some values because of feedback and merged from master --- src/crepe/Collider.cpp | 2 +- src/crepe/Collider.h | 2 +- src/crepe/api/BoxCollider.cpp | 2 +- src/crepe/api/BoxCollider.h | 2 +- src/crepe/api/CircleCollider.cpp | 2 +- src/crepe/api/CircleCollider.h | 2 +- src/crepe/api/Rigidbody.h | 14 +++++------- src/crepe/system/CollisionSystem.cpp | 18 ++++++++-------- src/crepe/system/CollisionSystem.h | 8 +++---- src/crepe/system/PhysicsSystem.cpp | 10 ++++----- src/test/CMakeLists.txt | 2 +- src/test/CollisionTest.cpp | 5 ----- src/test/Profiling.cpp | 42 +++++++++++++++++++----------------- 13 files changed, 52 insertions(+), 59 deletions(-) (limited to 'src/test/CMakeLists.txt') diff --git a/src/crepe/Collider.cpp b/src/crepe/Collider.cpp index 80a944d..9d94152 100644 --- a/src/crepe/Collider.cpp +++ b/src/crepe/Collider.cpp @@ -2,4 +2,4 @@ using namespace crepe; -Collider::Collider(game_object_id_t id, vec2 offset) : Component(id), offset(offset) {} +Collider::Collider(game_object_id_t id, const vec2& offset) : Component(id), offset(offset) {} diff --git a/src/crepe/Collider.h b/src/crepe/Collider.h index e1624b4..15d3a24 100644 --- a/src/crepe/Collider.h +++ b/src/crepe/Collider.h @@ -7,7 +7,7 @@ namespace crepe { class Collider : public Component { public: - Collider(game_object_id_t id, vec2 offset); + Collider(game_object_id_t id, const vec2& offset); public: //! Offset of the collider relative to rigidbody position diff --git a/src/crepe/api/BoxCollider.cpp b/src/crepe/api/BoxCollider.cpp index 6034837..1069e90 100644 --- a/src/crepe/api/BoxCollider.cpp +++ b/src/crepe/api/BoxCollider.cpp @@ -4,4 +4,4 @@ using namespace crepe; -BoxCollider::BoxCollider(game_object_id_t game_object_id,vec2 offset, float width, float height) : Collider(game_object_id,offset), width(width), height(height) {} +BoxCollider::BoxCollider(game_object_id_t game_object_id,const vec2& offset, float width, float height) : Collider(game_object_id,offset), width(width), height(height) {} diff --git a/src/crepe/api/BoxCollider.h b/src/crepe/api/BoxCollider.h index 2ce1ee8..c83d54a 100644 --- a/src/crepe/api/BoxCollider.h +++ b/src/crepe/api/BoxCollider.h @@ -12,7 +12,7 @@ namespace crepe { */ class BoxCollider : public Collider { public: - BoxCollider(game_object_id_t game_object_id,vec2 offset, float width, float height); + BoxCollider(game_object_id_t game_object_id,const vec2& offset, float width, float height); //! Width of box collider float width; diff --git a/src/crepe/api/CircleCollider.cpp b/src/crepe/api/CircleCollider.cpp index 497ebba..473734e 100644 --- a/src/crepe/api/CircleCollider.cpp +++ b/src/crepe/api/CircleCollider.cpp @@ -3,4 +3,4 @@ using namespace crepe; -CircleCollider::CircleCollider(game_object_id_t game_object_id,vec2 offset, float radius) : Collider(game_object_id,offset), radius(radius) {} +CircleCollider::CircleCollider(game_object_id_t game_object_id,const vec2& offset, float radius) : Collider(game_object_id,offset), radius(radius) {} diff --git a/src/crepe/api/CircleCollider.h b/src/crepe/api/CircleCollider.h index c61f027..bbcc330 100644 --- a/src/crepe/api/CircleCollider.h +++ b/src/crepe/api/CircleCollider.h @@ -14,7 +14,7 @@ namespace crepe { class CircleCollider : public Collider { public: - CircleCollider(game_object_id_t game_object_id,vec2 offset, float radius); + CircleCollider(game_object_id_t game_object_id,const vec2& offset, float radius); //! Radius of the circle collider. float radius; diff --git a/src/crepe/api/Rigidbody.h b/src/crepe/api/Rigidbody.h index 446c5dd..63b1b51 100644 --- a/src/crepe/api/Rigidbody.h +++ b/src/crepe/api/Rigidbody.h @@ -54,7 +54,7 @@ public: //! objects mass float mass = 0.0; //! gravtiy scale - float gravity_scale = 0.0; + float gravity_scale = 0; //! Changes if physics apply BodyType body_type = BodyType::DYNAMIC; //! linear velocity of object @@ -62,20 +62,16 @@ public: //! maximum linear velocity of object vec2 max_linear_velocity = {INFINITY ,INFINITY}; //! linear damping of object - vec2 linear_damping; + vec2 linear_velocity_factor; //! angular velocity of object - float angular_velocity = 0.0; + float angular_velocity = 1; //! max angular velocity of object float max_angular_velocity = INFINITY; //! angular damping of object - float angular_damping = 0.0; + float angular_velocity_factor = 1; //! movements constraints of object PhysicsConstraints constraints; - //! if gravity applies - bool use_gravity = true; - //! if object bounces - bool bounce = false; - //! bounce factor of material + //! bounce factor of material. 0.0 <= means all velocity is lost, 1.0 means it gets the same momentum but the mirrored direction. 0.5 is half of the velocity is saved. float elastisity = 0.0; //! offset of all colliders relative to transform position vec2 offset; diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index 2fe3621..fbb16e8 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -146,10 +146,10 @@ std::pair CollisionSystem::collision_handler(Co resolution.x = data1.rigidbody.data.linear_velocity.x * (resolution.y/data1.rigidbody.data.linear_velocity.y); } - return {resolution,resolution_direction}; + return std::make_pair(resolution,resolution_direction); } -vec2 CollisionSystem::get_box_box_resolution(const BoxCollider& box_collider1,const BoxCollider& box_collider2,vec2 final_position1,vec2 final_position2) const +vec2 CollisionSystem::get_box_box_resolution(const BoxCollider& box_collider1,const BoxCollider& box_collider2,const vec2& final_position1,const vec2& final_position2) const { vec2 resolution; // Default resolution vector vec2 delta = final_position2 - final_position1; @@ -183,7 +183,7 @@ vec2 CollisionSystem::get_box_box_resolution(const BoxCollider& box_collider1,co return resolution; } -vec2 CollisionSystem::get_circle_circle_resolution(const CircleCollider& circle_collider1, const CircleCollider& circle_collider2, vec2 final_position1, vec2 final_position2) const +vec2 CollisionSystem::get_circle_circle_resolution(const CircleCollider& circle_collider1, const CircleCollider& circle_collider2, const vec2& final_position1, const vec2& final_position2) const { vec2 delta = final_position2 - final_position1; @@ -205,7 +205,7 @@ vec2 CollisionSystem::get_circle_circle_resolution(const CircleCollider& circle_ return resolution; } -vec2 CollisionSystem::get_circle_box_resolution(const CircleCollider& circle_collider, const BoxCollider& box_collider, vec2 circle_position, vec2 box_position) const +vec2 CollisionSystem::get_circle_box_resolution(const CircleCollider& circle_collider, const BoxCollider& box_collider, const vec2& circle_position, const vec2& box_position) const { vec2 delta = circle_position - box_position; @@ -252,7 +252,7 @@ void CollisionSystem::static_collision_handler(CollisionInfo& info){ info.first_transform.position += info.resolution; // If bounce is enabled mirror velocity - if(info.first_rigidbody.data.bounce) { + if(info.first_rigidbody.data.elastisity > 0) { if(info.resolution_direction == Direction::BOTH) { info.first_rigidbody.data.linear_velocity.y = -info.first_rigidbody.data.linear_velocity.y * info.first_rigidbody.data.elastisity; @@ -335,12 +335,12 @@ CollisionSystem::get_active_transform_and_rigidbody(game_object_id_t game_object RefVector transforms = this->component_manager.get_components_by_id(game_object_id); if (transforms.empty()) return std::nullopt; + Transform& transform = transforms.front().get(); + if (!transform.active) return std::nullopt; + RefVector rigidbodies = this->component_manager.get_components_by_id(game_object_id); if (rigidbodies.empty()) return std::nullopt; - Transform& transform = transforms.front().get(); - if (!transform.active) return std::nullopt; - Rigidbody& rigidbody = rigidbodies.front().get(); if (!rigidbody.active) return std::nullopt; @@ -453,7 +453,7 @@ bool CollisionSystem::get_circle_circle_collision(const CircleCollider& circle1, return distance_squared <= radius_sum * radius_sum; } -vec2 CollisionSystem::get_current_position(vec2 collider_offset, const Transform& transform, const Rigidbody& rigidbody) const { +vec2 CollisionSystem::get_current_position(const vec2& collider_offset, const Transform& transform, const Rigidbody& rigidbody) const { // Get the rotation in radians float radians1 = transform.rotation * (M_PI / 180.0); diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h index 3f24db1..34c03ae 100644 --- a/src/crepe/system/CollisionSystem.h +++ b/src/crepe/system/CollisionSystem.h @@ -104,7 +104,7 @@ private: * \param rigidbody The Rigidbody of the associated game object. * \return The calculated position of the collider. */ - vec2 get_current_position(vec2 collider_offset, const Transform& transform, const Rigidbody& rigidbody) const; + vec2 get_current_position(const vec2& collider_offset, const Transform& transform, const Rigidbody& rigidbody) const; private: @@ -141,7 +141,7 @@ private: * \param position2 The position of the second BoxCollider. * \return The resolution vector for the collision. */ - vec2 get_box_box_resolution(const BoxCollider& box_collider1,const BoxCollider& box_collider2,vec2 position1,vec2 position2) const; + vec2 get_box_box_resolution(const BoxCollider& box_collider1,const BoxCollider& box_collider2,const vec2& position1,const vec2& position2) const; /** * \brief Calculates the resolution vector for two CircleCollider. @@ -154,7 +154,7 @@ private: * \param position2 The position of the second CircleCollider. * \return The resolution vector for the collision. */ - vec2 get_circle_circle_resolution(const CircleCollider& circle_collider1, const CircleCollider& circle_collider2, vec2 final_position1, vec2 final_position2) const; + vec2 get_circle_circle_resolution(const CircleCollider& circle_collider1, const CircleCollider& circle_collider2, const vec2& final_position1, const vec2& final_position2) const; /** * \brief Calculates the resolution vector for two CircleCollider. @@ -167,7 +167,7 @@ private: * \param box_position The position of the BocCollider. * \return The resolution vector for the collision. */ - vec2 get_circle_box_resolution(const CircleCollider& circle_collider, const BoxCollider& box_collider, vec2 circle_position, vec2 box_position) const; + vec2 get_circle_box_resolution(const CircleCollider& circle_collider, const BoxCollider& box_collider, const vec2& circle_position, const vec2& box_position) const; /** * \brief Determines the appropriate collision handler for a collision. diff --git a/src/crepe/system/PhysicsSystem.cpp b/src/crepe/system/PhysicsSystem.cpp index 514a4b3..8f21727 100644 --- a/src/crepe/system/PhysicsSystem.cpp +++ b/src/crepe/system/PhysicsSystem.cpp @@ -25,17 +25,17 @@ void PhysicsSystem::update() { if (transform.game_object_id == rigidbody.game_object_id) { // Add gravity - if (rigidbody.data.use_gravity) { + if (rigidbody.data.gravity_scale > 0) { rigidbody.data.linear_velocity.y += (rigidbody.data.mass * rigidbody.data.gravity_scale * gravity); } // Add damping - if (rigidbody.data.angular_damping != 0) { - rigidbody.data.angular_velocity *= rigidbody.data.angular_damping; + if (rigidbody.data.angular_velocity_factor != 1 && rigidbody.data.angular_velocity_factor > 0) { + rigidbody.data.angular_velocity *= rigidbody.data.angular_velocity_factor; } - if (rigidbody.data.linear_damping != vec2{0, 0}) { - rigidbody.data.linear_velocity *= rigidbody.data.linear_damping; + if (rigidbody.data.linear_velocity_factor != vec2{1, 1} && rigidbody.data.linear_velocity_factor.x > 0 && rigidbody.data.linear_velocity_factor.y > 0) { + rigidbody.data.linear_velocity *= rigidbody.data.linear_velocity_factor; } // Max velocity check diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 616e238..4555c0b 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -13,5 +13,5 @@ target_sources(test_main PUBLIC # ValueBrokerTest.cpp # DBTest.cpp # Vector2Test.cpp - # Profiling.cpp + Profiling.cpp ) diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index ed40b1b..92ff7ba 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -65,7 +65,6 @@ public: world.add_component(Rigidbody::Data{ // TODO: remove unrelated properties: .body_type = Rigidbody::BodyType::STATIC, - .bounce = false, .offset = {0,0}, }); // Create a box with an inner size of 10x10 units @@ -80,8 +79,6 @@ public: .body_type = Rigidbody::BodyType::DYNAMIC, .linear_velocity = {0,0}, .constraints = {0, 0, 0}, - .use_gravity = true, - .bounce = true, .elastisity = 1, .offset = {0,0}, }); @@ -96,8 +93,6 @@ public: .body_type = Rigidbody::BodyType::DYNAMIC, .linear_velocity = {0,0}, .constraints = {0, 0, 0}, - .use_gravity = true, - .bounce = true, .elastisity = 1, .offset = {0,0}, }); diff --git a/src/test/Profiling.cpp b/src/test/Profiling.cpp index 2549c57..a88bf85 100644 --- a/src/test/Profiling.cpp +++ b/src/test/Profiling.cpp @@ -79,7 +79,8 @@ public: void SetUp() override { GameObject do_not_use = mgr.new_object("DO_NOT_USE","",{0,0}); - do_not_use.add_component(Color::WHITE); + do_not_use.add_component(Color::WHITE, ivec2{1080, 720}, + vec2{2000, 2000}, 1.0f); // initialize systems here: //calls init script_sys.update(); @@ -110,14 +111,19 @@ public: // Print timings of all functions void log_timings() const { - std::stringstream ss; - ss << "\nFunction timings:\n"; - for (const auto& [name, duration] : timings) { - ss << name << " took " << duration.count() / 1000.0 / average << " ms (" << duration.count() / average << " µs).\n"; - } - ss << "Total time: " << this->total_time.count() / 1000.0 / average << " ms (" << this->total_time.count() / average << " µs)\n"; - ss << "Amount of gameobjects: " << game_object_count << "\n"; - GTEST_LOG_(INFO) << ss.str(); + std::string result = "\nFunction timings:\n"; + + for (const auto& [name, duration] : timings) { + result += name + " took " + std::to_string(duration.count() / 1000.0 / average) + " ms (" + + std::to_string(duration.count() / average) + " µs).\n"; + } + + result += "Total time: " + std::to_string(this->total_time.count() / 1000.0 / average) + " ms (" + + std::to_string(this->total_time.count() / average) + " µs)\n"; + + result += "Amount of gameobjects: " + std::to_string(game_object_count) + "\n"; + + GTEST_LOG_(INFO) << result; } void clear_timings() { @@ -158,15 +164,14 @@ TEST_F(Profiling, Profiling_2) { //define gameobject used for testing GameObject gameobject = mgr.new_object("gameobject","",{static_cast(game_object_count*2),0}); gameobject.add_component(Rigidbody::Data{ - .body_type = Rigidbody::BodyType::STATIC, - .use_gravity = false, + .gravity_scale = 0.0, + .body_type = Rigidbody::BodyType::STATIC, }); gameobject.add_component(vec2{0, 0}, 1, 1); gameobject.add_component().set_script(); Color color(0, 0, 0, 0); - gameobject.add_component( - make_shared("asset/texture/green_square.png"), color, - FlipSettings{true, true}); + auto img = Texture("asset/texture/green_square.png"); + Sprite & test_sprite = gameobject.add_component(img, color, Sprite::FlipSettings{false, false}, 1, 1, 500); } this->game_object_count++; @@ -190,17 +195,14 @@ TEST_F(Profiling, Profiling_3) { //define gameobject used for testing GameObject gameobject = mgr.new_object("gameobject","",{static_cast(game_object_count*2),0}); gameobject.add_component(Rigidbody::Data{ + .gravity_scale = 0, .body_type = Rigidbody::BodyType::STATIC, - .use_gravity = false, }); gameobject.add_component(vec2{0, 0}, 1, 1); gameobject.add_component().set_script(); Color color(0, 0, 0, 0); - gameobject.add_component( - make_shared("asset/texture/green_square.png"), color, - FlipSettings{true, true}); - Sprite & test_sprite = gameobject.add_component( - make_shared("asset/texture/img.png"), color, FlipSettings{false, false}); + auto img = Texture("asset/texture/green_square.png"); + Sprite & test_sprite = gameobject.add_component(img, color, Sprite::FlipSettings{false, false}, 1, 1, 500); auto & test = gameobject.add_component(ParticleEmitter::Data{ .max_particles = 10, .emission_rate = 100, -- cgit v1.2.3 From 4bbc27098d5a8907ab0500ad3ccc82283cf95ddf Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Mon, 2 Dec 2024 20:34:00 +0100 Subject: merge fix and review fix --- src/example/game.cpp | 13 ++++--------- src/test/CMakeLists.txt | 24 ++++++++++++------------ src/test/PhysicsTest.cpp | 10 ++++------ src/test/Profiling.cpp | 10 +--------- 4 files changed, 21 insertions(+), 36 deletions(-) (limited to 'src/test/CMakeLists.txt') diff --git a/src/example/game.cpp b/src/example/game.cpp index e851526..c439f5d 100644 --- a/src/example/game.cpp +++ b/src/example/game.cpp @@ -18,7 +18,7 @@ using namespace std; class MyScript : public Script { bool oncollision(const CollisionEvent& test) { - Log::logf("Box {} script on_collision()", test.info.first.collider.game_object_id); + Log::logf("Box {} script on_collision()", test.info.first_collider.game_object_id); return true; } void init() { @@ -50,8 +50,6 @@ public: .gravity_scale = 0, .body_type = Rigidbody::BodyType::STATIC, .constraints = {0, 0, 0}, - .use_gravity = false, - .bounce = false, .offset = {0,0} }); world.add_component(vec2{0, 0-(screen_size_height/2+world_collider/2)}, world_collider, world_collider);; // Top @@ -67,17 +65,14 @@ public: .body_type = Rigidbody::BodyType::DYNAMIC, .linear_velocity = {1,1}, .constraints = {0, 0, 0}, - .use_gravity = true, - .bounce = true, .elastisity = 1, .offset = {0,0}, }); game_object1.add_component(vec2{0, 0}, 20, 20); game_object1.add_component().set_script(); - game_object1.add_component( - make_shared("/home/jaro/crepe/asset/texture/green_square.png"), color, - FlipSettings{true, true}); - game_object1.add_component(Color::WHITE); + auto img = Texture("asset/texture/green_square.png"); + game_object1.add_component(img, color, Sprite::FlipSettings{false, false}, 1, 1, 500); + game_object1.add_component(Color::WHITE, ivec2{1080, 720},vec2{2000, 2000}, 1.0f); } string get_name() const { return "scene1"; } diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 4555c0b..cd61fb7 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -1,17 +1,17 @@ target_sources(test_main PUBLIC CollisionTest.cpp main.cpp - # PhysicsTest.cpp - # ScriptTest.cpp - # ParticleTest.cpp - # AssetTest.cpp - # OptionalRefTest.cpp - # RenderSystemTest.cpp - # EventTest.cpp - # ECSTest.cpp - # SceneManagerTest.cpp - # ValueBrokerTest.cpp - # DBTest.cpp - # Vector2Test.cpp + PhysicsTest.cpp + ScriptTest.cpp + ParticleTest.cpp + AssetTest.cpp + OptionalRefTest.cpp + RenderSystemTest.cpp + EventTest.cpp + ECSTest.cpp + SceneManagerTest.cpp + ValueBrokerTest.cpp + DBTest.cpp + Vector2Test.cpp Profiling.cpp ) diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp index 33b6020..5dd448f 100644 --- a/src/test/PhysicsTest.cpp +++ b/src/test/PhysicsTest.cpp @@ -28,8 +28,6 @@ public: .max_linear_velocity = vec2{10, 10}, .max_angular_velocity = 10, .constraints = {0, 0}, - .use_gravity = true, - .bounce = false, }); } transforms = mgr.get_components_by_id(0); @@ -105,16 +103,16 @@ TEST_F(PhysicsTest, movement) { EXPECT_EQ(transform.position.y, 1); EXPECT_EQ(transform.rotation, 1); - rigidbody.data.linear_damping.x = 0.5; - rigidbody.data.linear_damping.y = 0.5; - rigidbody.data.angular_damping = 0.5; + rigidbody.data.linear_velocity_factor.x = 0.5; + rigidbody.data.linear_velocity_factor.y = 0.5; + rigidbody.data.angular_velocity_factor = 0.5; system.update(); EXPECT_EQ(rigidbody.data.linear_velocity.x, 0.5); EXPECT_EQ(rigidbody.data.linear_velocity.y, 0.5); EXPECT_EQ(rigidbody.data.angular_velocity, 0.5); rigidbody.data.constraints = {1, 1, 0}; - rigidbody.data.angular_damping = 0; + rigidbody.data.angular_velocity_factor = 0; rigidbody.data.max_angular_velocity = 1000; rigidbody.data.angular_velocity = 360; system.update(); diff --git a/src/test/Profiling.cpp b/src/test/Profiling.cpp index a88bf85..e46d5ff 100644 --- a/src/test/Profiling.cpp +++ b/src/test/Profiling.cpp @@ -26,14 +26,6 @@ using namespace std::chrono_literals; using namespace crepe; using namespace testing; - -/* -List of test cases with component settings/details -1. Minimal test creates gameobject without additonal components -2. Minimal 'Complex' gameobject. Has dynamic body without bounce and no collision handler -3. Minimal 'Complex' gameobject. Same as test 2 but with particle emitter -*/ - class TestScript : public Script { bool oncollision(const CollisionEvent& test) { Log::logf("Box {} script on_collision()", test.info.first_collider.game_object_id); @@ -55,7 +47,7 @@ public: // Minimum amount to let test pass const int min_gameobject_count = 100; // Maximum amount to stop test - const int max_gameobject_count = 200; + const int max_gameobject_count = 150; // Amount of times a test runs to calculate average const int average = 5; // Maximum duration to stop test -- cgit v1.2.3 From 3622b63a0d2ce3f1168d2b43d91987076c7c94cd Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Tue, 3 Dec 2024 18:04:05 +0100 Subject: moved active check to update and changed gather of components --- src/crepe/system/CollisionSystem.cpp | 89 ++++++++++++++++++------------------ src/crepe/system/CollisionSystem.h | 6 +-- src/test/CMakeLists.txt | 26 +++++------ 3 files changed, 61 insertions(+), 60 deletions(-) (limited to 'src/test/CMakeLists.txt') diff --git a/src/crepe/system/CollisionSystem.cpp b/src/crepe/system/CollisionSystem.cpp index fbb16e8..de14aed 100644 --- a/src/crepe/system/CollisionSystem.cpp +++ b/src/crepe/system/CollisionSystem.cpp @@ -23,22 +23,44 @@ using namespace crepe; void CollisionSystem::update() { - // Get collider components and keep them seperate + + std::vector all_colliders; ComponentManager & mgr = this->component_manager; - RefVector boxcolliders = mgr.get_components_by_type(); - RefVector circlecolliders = mgr.get_components_by_type(); - - std::vector all_colliders; - // Add BoxCollider references - for (auto& box : boxcolliders) { - all_colliders.push_back(collider_variant{box}); + game_object_id_t id = 0; + RefVector rigidbodies = mgr.get_components_by_type(); + for(Rigidbody& rigidbody : rigidbodies) { + if (!rigidbody.active) continue; + id = rigidbody.game_object_id; + Transform& transform = this->component_manager.get_components_by_id(id).front().get(); + RefVector boxcolliders = mgr.get_components_by_type(); + for (BoxCollider& boxcollider : boxcolliders) { + if(boxcollider.game_object_id != id) continue; + if(!boxcollider.active) continue; + all_colliders.push_back( + { + .id = id, + .collider = collider_variant{boxcollider}, + .transform = transform, + .rigidbody = rigidbody + } + ); + } + RefVector circlecolliders = mgr.get_components_by_type(); + for (CircleCollider& circlecollider : circlecolliders) { + if(circlecollider.game_object_id != id) continue; + if(!circlecollider.active) continue; + all_colliders.push_back( + { + .id = id, + .collider = collider_variant{circlecollider}, + .transform = transform, + .rigidbody = rigidbody + } + ); + } + } - // Add CircleCollider references - for (auto& circle : circlecolliders) { - all_colliders.push_back(collider_variant{circle}); - } - // Check between all colliders if there is a collision std::vector> collided = gather_collisions(all_colliders); @@ -271,7 +293,7 @@ void CollisionSystem::static_collision_handler(CollisionInfo& info){ } } -std::vector> CollisionSystem::gather_collisions(std::vector & colliders) { +std::vector> CollisionSystem::gather_collisions(std::vector & colliders) { // TODO: @@ -285,45 +307,24 @@ std::vector> collisions_ret; - bool active_inner = false; - bool active_outer = false; - game_object_id_t id_inner = -1; - game_object_id_t id_outer = -1; //using visit to visit the variant to access the active and id. for (size_t i = 0; i < colliders.size(); ++i) { - std::visit([&](Collider& inner_collider_ref) { - active_inner = inner_collider_ref.active; - id_inner = inner_collider_ref.game_object_id; - }, colliders[i]); - if(!active_inner) continue; - auto inner_components = get_active_transform_and_rigidbody(id_inner); - if (!inner_components) continue; for (size_t j = i + 1; j < colliders.size(); ++j) { - std::visit([&](Collider& outer_collider_ref) { - active_outer = outer_collider_ref.active; - id_outer = outer_collider_ref.game_object_id; - }, colliders[j]); - if(!active_outer) continue; - if(id_outer == id_inner) continue; - auto outer_components = get_active_transform_and_rigidbody(id_outer); - if (!outer_components) continue; + if(colliders[i].id == colliders[j].id) continue; // Get collision type form variant colliders - CollisionInternalType type = get_collider_type(colliders[i],colliders[j]); + CollisionInternalType type = get_collider_type(colliders[i].collider,colliders[j].collider); if(!get_collision({ - .collider = colliders[i], - .transform = inner_components->first, - .rigidbody = inner_components->second, + .collider = colliders[i].collider, + .transform = colliders[i].transform, + .rigidbody = colliders[i].rigidbody, }, { - .collider = colliders[j], - .transform = outer_components->first, - .rigidbody = outer_components->second, + .collider = colliders[j].collider, + .transform = colliders[j].transform, + .rigidbody = colliders[j].rigidbody, }, type)) continue; - collisions_ret.emplace_back( - CollisionInternal{colliders[i], inner_components->first.get(), inner_components->second.get()}, - CollisionInternal{colliders[j], outer_components->first.get(), outer_components->second.get()} - ); + collisions_ret.emplace_back(colliders[i],colliders[j]); } } diff --git a/src/crepe/system/CollisionSystem.h b/src/crepe/system/CollisionSystem.h index 34c03ae..9e9096c 100644 --- a/src/crepe/system/CollisionSystem.h +++ b/src/crepe/system/CollisionSystem.h @@ -40,8 +40,8 @@ private: * This structure stores the collider type, its associated transform, and its rigidbody. */ struct CollisionInternal { - //! Store either BoxCollider or CircleCollider - collider_variant& collider; + game_object_id_t id = 0; + collider_variant collider; Transform& transform; Rigidbody& rigidbody; }; @@ -196,7 +196,7 @@ private: * \param colliders A collection of all active colliders. * \return A list of collision pairs with their associated data. */ - std::vector> gather_collisions(std::vector & colliders); + std::vector> gather_collisions(std::vector & colliders); /** * \brief Checks for collision between two colliders. diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index cd61fb7..616e238 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -1,17 +1,17 @@ target_sources(test_main PUBLIC CollisionTest.cpp main.cpp - PhysicsTest.cpp - ScriptTest.cpp - ParticleTest.cpp - AssetTest.cpp - OptionalRefTest.cpp - RenderSystemTest.cpp - EventTest.cpp - ECSTest.cpp - SceneManagerTest.cpp - ValueBrokerTest.cpp - DBTest.cpp - Vector2Test.cpp - Profiling.cpp + # PhysicsTest.cpp + # ScriptTest.cpp + # ParticleTest.cpp + # AssetTest.cpp + # OptionalRefTest.cpp + # RenderSystemTest.cpp + # EventTest.cpp + # ECSTest.cpp + # SceneManagerTest.cpp + # ValueBrokerTest.cpp + # DBTest.cpp + # Vector2Test.cpp + # Profiling.cpp ) -- cgit v1.2.3 From e882a9787919b4e804878e5458c535342a718a08 Mon Sep 17 00:00:00 2001 From: JAROWMR Date: Fri, 6 Dec 2024 15:36:54 +0100 Subject: enabled all test expect profiling --- src/test/CMakeLists.txt | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) (limited to 'src/test/CMakeLists.txt') diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index a863598..9a4dfe7 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -1,20 +1,20 @@ target_sources(test_main PUBLIC main.cpp CollisionTest.cpp - # PhysicsTest.cpp - # ScriptTest.cpp - # ParticleTest.cpp - # AssetTest.cpp - # OptionalRefTest.cpp - # RenderSystemTest.cpp - # EventTest.cpp - # ECSTest.cpp - # SceneManagerTest.cpp - # ValueBrokerTest.cpp - # DBTest.cpp - # Vector2Test.cpp - # InputTest.cpp - # ScriptEventTest.cpp - # ScriptSceneTest.cpp - # Profiling.cpp + PhysicsTest.cpp + ScriptTest.cpp + ParticleTest.cpp + AssetTest.cpp + OptionalRefTest.cpp + RenderSystemTest.cpp + EventTest.cpp + ECSTest.cpp + SceneManagerTest.cpp + ValueBrokerTest.cpp + DBTest.cpp + Vector2Test.cpp + InputTest.cpp + ScriptEventTest.cpp + ScriptSceneTest.cpp + # Profiling.cpp #disabled for saving time ) -- cgit v1.2.3 From cfd578dd0b7d5894ff0b0a0796d85cd5e9ae6e56 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Fri, 6 Dec 2024 19:24:15 +0100 Subject: merge #53 --- src/test/CMakeLists.txt | 2 +- src/test/Profiling.cpp | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/test/CMakeLists.txt') diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 9a4dfe7..c9cbac5 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -16,5 +16,5 @@ target_sources(test_main PUBLIC InputTest.cpp ScriptEventTest.cpp ScriptSceneTest.cpp - # Profiling.cpp #disabled for saving time + Profiling.cpp ) diff --git a/src/test/Profiling.cpp b/src/test/Profiling.cpp index 24ac494..bd99614 100644 --- a/src/test/Profiling.cpp +++ b/src/test/Profiling.cpp @@ -41,7 +41,7 @@ class TestScript : public Script { } }; -class Profiling : public Test { +class DISABLED_ProfilingTest : public Test { public: // Config for test // Minimum amount to let test pass @@ -127,7 +127,7 @@ public: } }; -TEST_F(Profiling, Profiling_1) { +TEST_F(DISABLED_ProfilingTest, Profiling_1) { while (this->total_time / this->average < this->duration) { { @@ -150,7 +150,7 @@ TEST_F(Profiling, Profiling_1) { EXPECT_GE(this->game_object_count, this->min_gameobject_count); } -TEST_F(Profiling, Profiling_2) { +TEST_F(DISABLED_ProfilingTest, Profiling_2) { while (this->total_time / this->average < this->duration) { { @@ -184,7 +184,7 @@ TEST_F(Profiling, Profiling_2) { EXPECT_GE(this->game_object_count, this->min_gameobject_count); } -TEST_F(Profiling, Profiling_3) { +TEST_F(DISABLED_ProfilingTest, Profiling_3) { while (this->total_time / this->average < this->duration) { { -- cgit v1.2.3