diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2025-01-11 21:32:30 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2025-01-11 21:32:30 +0100 |
commit | a6803980f1e74ecf1abb007b7c77f00d2cd92c43 (patch) | |
tree | 425ca961b27117d6e5d5fa0ae5cfca93351e0b33 /src/test | |
parent | 6bc0025e4c24ed6659d993f3469c10615fb0e273 (diff) | |
parent | 525636bb2158ecea68ebb9d6b8d2dc722524c5e5 (diff) |
merge master into loek/doxygen
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/AssetTest.cpp | 6 | ||||
-rw-r--r-- | src/test/AudioTest.cpp | 43 | ||||
-rw-r--r-- | src/test/CMakeLists.txt | 3 | ||||
-rw-r--r-- | src/test/CollisionTest.cpp | 197 | ||||
-rw-r--r-- | src/test/DBTest.cpp | 1 | ||||
-rw-r--r-- | src/test/ECSTest.cpp | 80 | ||||
-rw-r--r-- | src/test/EventTest.cpp | 70 | ||||
-rw-r--r-- | src/test/InputTest.cpp | 169 | ||||
-rw-r--r-- | src/test/LoopManagerTest.cpp | 6 | ||||
-rw-r--r-- | src/test/LoopTimerTest.cpp | 2 | ||||
-rw-r--r-- | src/test/ParticleTest.cpp | 146 | ||||
-rw-r--r-- | src/test/PhysicsTest.cpp | 28 | ||||
-rw-r--r-- | src/test/Profiling.cpp | 112 | ||||
-rw-r--r-- | src/test/RenderSystemTest.cpp | 98 | ||||
-rw-r--r-- | src/test/ReplayManagerTest.cpp | 38 | ||||
-rw-r--r-- | src/test/ResourceManagerTest.cpp | 6 | ||||
-rw-r--r-- | src/test/SaveManagerTest.cpp | 4 | ||||
-rw-r--r-- | src/test/SceneManagerTest.cpp | 20 | ||||
-rw-r--r-- | src/test/ScriptEventTest.cpp | 4 | ||||
-rw-r--r-- | src/test/ScriptSaveManagerTest.cpp | 4 | ||||
-rw-r--r-- | src/test/ScriptSceneTest.cpp | 2 | ||||
-rw-r--r-- | src/test/ScriptTest.cpp | 38 | ||||
-rw-r--r-- | src/test/ScriptTest.h | 14 | ||||
-rw-r--r-- | src/test/ValueBrokerTest.cpp | 4 | ||||
-rw-r--r-- | src/test/Vector2Test.cpp | 10 |
25 files changed, 637 insertions, 468 deletions
diff --git a/src/test/AssetTest.cpp b/src/test/AssetTest.cpp index 93fd6a9..f41e9de 100644 --- a/src/test/AssetTest.cpp +++ b/src/test/AssetTest.cpp @@ -7,15 +7,15 @@ using namespace std; using namespace crepe; using namespace testing; -TEST(AssetTest, Existant) { ASSERT_NO_THROW(Asset{"asset/texture/img.png"}); } +TEST(AssetTest, Existant) { ASSERT_NO_THROW(Asset {"asset/texture/img.png"}); } -TEST(AssetTest, Nonexistant) { ASSERT_ANY_THROW(Asset{"asset/nonexistant"}); } +TEST(AssetTest, Nonexistant) { ASSERT_ANY_THROW(Asset {"asset/nonexistant"}); } TEST(AssetTest, Rootless) { Config & cfg = Config::get_instance(); cfg.asset.root_pattern.clear(); string arbitrary = "\\/this is / /../passed through as-is"; - Asset asset{arbitrary}; + Asset asset {arbitrary}; ASSERT_EQ(arbitrary, asset.get_path()); } diff --git a/src/test/AudioTest.cpp b/src/test/AudioTest.cpp index 48bba1b..e548221 100644 --- a/src/test/AudioTest.cpp +++ b/src/test/AudioTest.cpp @@ -31,11 +31,11 @@ private: private: Mediator mediator; - ComponentManager component_manager{mediator}; - ResourceManager resource_manager{mediator}; + ComponentManager component_manager {mediator}; + ResourceManager resource_manager {mediator}; public: - TestAudioSystem system{mediator}; + TestAudioSystem system {mediator}; TestSoundContext & context = system.context; private: @@ -50,16 +50,18 @@ TEST_F(AudioTest, Default) { EXPECT_CALL(context, stop(_)).Times(0); EXPECT_CALL(context, set_volume(_, _)).Times(0); EXPECT_CALL(context, set_loop(_, _)).Times(0); - system.update(); + system.fixed_update(); } TEST_F(AudioTest, Play) { - system.update(); + system.fixed_update(); { InSequence seq; EXPECT_CALL(context, play(_)).Times(0); + EXPECT_CALL(context, set_loop(_, _)).Times(0); + EXPECT_CALL(context, set_volume(_, _)).Times(0); component.play(); } @@ -67,12 +69,14 @@ TEST_F(AudioTest, Play) { InSequence seq; EXPECT_CALL(context, play(_)).Times(1); - system.update(); + EXPECT_CALL(context, set_loop(_, _)).Times(1); + EXPECT_CALL(context, set_volume(_, _)).Times(1); + system.fixed_update(); } } TEST_F(AudioTest, Stop) { - system.update(); + system.fixed_update(); { InSequence seq; @@ -85,12 +89,12 @@ TEST_F(AudioTest, Stop) { InSequence seq; EXPECT_CALL(context, stop(_)).Times(1); - system.update(); + system.fixed_update(); } } TEST_F(AudioTest, Volume) { - system.update(); + system.fixed_update(); { InSequence seq; @@ -103,12 +107,12 @@ TEST_F(AudioTest, Volume) { InSequence seq; EXPECT_CALL(context, set_volume(_, component.volume)).Times(1); - system.update(); + system.fixed_update(); } } TEST_F(AudioTest, Looping) { - system.update(); + system.fixed_update(); { InSequence seq; @@ -121,33 +125,36 @@ TEST_F(AudioTest, Looping) { InSequence seq; EXPECT_CALL(context, set_loop(_, component.loop)).Times(1); - system.update(); + system.fixed_update(); } } TEST_F(AudioTest, StopOnDeactivate) { - system.update(); + system.fixed_update(); { InSequence seq; EXPECT_CALL(context, stop(_)).Times(1); component.active = false; - system.update(); + system.fixed_update(); } } TEST_F(AudioTest, PlayOnActive) { component.active = false; component.play_on_awake = true; - system.update(); + system.fixed_update(); { InSequence seq; EXPECT_CALL(context, play(_)).Times(1); + EXPECT_CALL(context, set_loop(_, _)).Times(1); + EXPECT_CALL(context, set_volume(_, _)).Times(1); + component.active = true; - system.update(); + system.fixed_update(); } } @@ -156,6 +163,8 @@ TEST_F(AudioTest, PlayImmediately) { component.play(); EXPECT_CALL(context, play(_)).Times(1); + EXPECT_CALL(context, set_volume(_, _)).Times(1); + EXPECT_CALL(context, set_loop(_, _)).Times(1); - system.update(); + system.fixed_update(); } diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index 11b4ca9..ea92d96 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -15,7 +15,7 @@ target_sources(test_main PUBLIC ValueBrokerTest.cpp DBTest.cpp Vector2Test.cpp - LoopManagerTest.cpp + # LoopManagerTest.cpp LoopTimerTest.cpp InputTest.cpp ScriptEventTest.cpp @@ -24,4 +24,5 @@ target_sources(test_main PUBLIC SaveManagerTest.cpp ScriptSaveManagerTest.cpp ScriptECSTest.cpp + ReplayManagerTest.cpp ) diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp index 2ad65fa..c571c1a 100644 --- a/src/test/CollisionTest.cpp +++ b/src/test/CollisionTest.cpp @@ -39,8 +39,9 @@ public: } void init() { - subscribe<CollisionEvent>( - [this](const CollisionEvent & ev) -> bool { return this->on_collision(ev); }); + subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool { + return this->on_collision(ev); + }); } void update() { // Retrieve component from the same GameObject this script is on @@ -50,10 +51,11 @@ public: class CollisionTest : public Test { public: Mediator m; - EventManager event_mgr{m}; - ComponentManager mgr{m}; - CollisionSystem collision_sys{m}; - ScriptSystem script_sys{m}; + EventManager event_mgr {m}; + ComponentManager mgr {m}; + CollisionSystem collision_sys {m}; + ScriptSystem script_sys {m}; + LoopTimerManager loop_timer {m}; GameObject world = mgr.new_object("world", "", {50, 50}); GameObject game_object1 = mgr.new_object("object1", "", {50, 50}); @@ -63,51 +65,48 @@ public: CollisionHandler * script_object2_ref = nullptr; void SetUp() override { - world.add_component<Rigidbody>(Rigidbody::Data{ + world.add_component<Rigidbody>(Rigidbody::Data { // TODO: remove unrelated properties: .body_type = Rigidbody::BodyType::STATIC, - .offset = {0, 0}, }); // Create a box with an inner size of 10x10 units - world.add_component<BoxCollider>(vec2{0, -100}, vec2{100, 100}); // Top - world.add_component<BoxCollider>(vec2{0, 100}, vec2{100, 100}); // Bottom - world.add_component<BoxCollider>(vec2{-100, 0}, vec2{100, 100}); // Left - world.add_component<BoxCollider>(vec2{100, 0}, vec2{100, 100}); // right + world.add_component<BoxCollider>(vec2 {100, 100}, vec2 {0, -100}); // Top + world.add_component<BoxCollider>(vec2 {100, 100}, vec2 {0, 100}); // Bottom + world.add_component<BoxCollider>(vec2 {100, 100}, vec2 {-100, 0}); // Left + world.add_component<BoxCollider>(vec2 {100, 100}, vec2 {100, 0}); // right - game_object1.add_component<Rigidbody>(Rigidbody::Data{ + game_object1.add_component<Rigidbody>(Rigidbody::Data { .mass = 1, .gravity_scale = 0.01, .body_type = Rigidbody::BodyType::DYNAMIC, .linear_velocity = {0, 0}, .constraints = {0, 0, 0}, - .elastisity_coefficient = 1, - .offset = {0, 0}, + .elasticity_coefficient = 1, .collision_layers = {0}, }); - game_object1.add_component<BoxCollider>(vec2{0, 0}, vec2{10, 10}); + game_object1.add_component<BoxCollider>(vec2 {10, 10}, vec2 {0, 0}); BehaviorScript & script_object1 = game_object1.add_component<BehaviorScript>().set_script<CollisionHandler>(1); script_object1_ref = static_cast<CollisionHandler *>(script_object1.script.get()); ASSERT_NE(script_object1_ref, nullptr); - game_object2.add_component<Rigidbody>(Rigidbody::Data{ + game_object2.add_component<Rigidbody>(Rigidbody::Data { .mass = 1, .gravity_scale = 0.01, .body_type = Rigidbody::BodyType::DYNAMIC, .linear_velocity = {0, 0}, .constraints = {0, 0, 0}, - .elastisity_coefficient = 1, - .offset = {0, 0}, + .elasticity_coefficient = 1, .collision_layers = {0}, }); - game_object2.add_component<BoxCollider>(vec2{0, 0}, vec2{10, 10}); + game_object2.add_component<BoxCollider>(vec2 {10, 10}, vec2 {0, 0}); BehaviorScript & script_object2 = game_object2.add_component<BehaviorScript>().set_script<CollisionHandler>(2); script_object2_ref = static_cast<CollisionHandler *>(script_object2.script.get()); ASSERT_NE(script_object2_ref, nullptr); // Ensure Script::init() is called on all BehaviorScript instances - script_sys.update(); + script_sys.fixed_update(); } }; @@ -115,14 +114,14 @@ 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.this_collider.game_object_id, 1); + EXPECT_EQ(ev.info.self.transform.game_object_id, 1); }; script_object2_ref->test_fn = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; - EXPECT_EQ(ev.info.this_collider.game_object_id, 2); + EXPECT_EQ(ev.info.self.transform.game_object_id, 2); }; EXPECT_FALSE(collision_happend); - collision_sys.update(); + collision_sys.fixed_update(); EXPECT_FALSE(collision_happend); } @@ -130,22 +129,22 @@ 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.this_collider.game_object_id, 1); + EXPECT_EQ(ev.info.self.transform.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.this_collider.game_object_id, 2); - EXPECT_EQ(ev.info.resolution.x, 10); - EXPECT_EQ(ev.info.resolution.y, 10); + EXPECT_EQ(ev.info.self.transform.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<Transform>(1).front().get(); tf.position = {50, 30}; - collision_sys.update(); + collision_sys.fixed_update(); EXPECT_TRUE(collision_happend); } @@ -153,24 +152,26 @@ 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.this_collider.game_object_id, 1); + EXPECT_EQ(ev.info.self.transform.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); + 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.this_collider.game_object_id, 2); + EXPECT_EQ(ev.info.self.transform.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_EQ( + ev.info.resolution_direction, crepe::CollisionSystem::Direction::X_DIRECTION + ); }; EXPECT_FALSE(collision_happend); Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get(); tf.position = {45, 30}; - collision_sys.update(); + collision_sys.fixed_update(); EXPECT_TRUE(collision_happend); } @@ -178,24 +179,26 @@ 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.this_collider.game_object_id, 1); + EXPECT_EQ(ev.info.self.transform.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); + 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.this_collider.game_object_id, 2); + EXPECT_EQ(ev.info.self.transform.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_EQ( + ev.info.resolution_direction, crepe::CollisionSystem::Direction::Y_DIRECTION + ); }; EXPECT_FALSE(collision_happend); Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get(); tf.position = {50, 25}; - collision_sys.update(); + collision_sys.fixed_update(); EXPECT_TRUE(collision_happend); } @@ -203,16 +206,16 @@ 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.this_collider.game_object_id, 1); + EXPECT_EQ(ev.info.self.transform.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.this_collider.game_object_id, 2); - EXPECT_EQ(ev.info.resolution.x, 10); - EXPECT_EQ(ev.info.resolution.y, 10); + EXPECT_EQ(ev.info.self.transform.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); @@ -222,7 +225,7 @@ TEST_F(CollisionTest, collision_box_box_dynamic_both) { rg1.data.linear_velocity = {10, 10}; Rigidbody & rg2 = this->mgr.get_components_by_id<Rigidbody>(2).front().get(); rg2.data.linear_velocity = {10, 10}; - collision_sys.update(); + collision_sys.fixed_update(); EXPECT_TRUE(collision_happend); } @@ -230,19 +233,21 @@ 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.this_collider.game_object_id, 1); + EXPECT_EQ(ev.info.self.transform.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); + 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.this_collider.game_object_id, 2); + EXPECT_EQ(ev.info.self.transform.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_EQ( + ev.info.resolution_direction, crepe::CollisionSystem::Direction::X_DIRECTION + ); }; EXPECT_FALSE(collision_happend); Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get(); @@ -251,7 +256,7 @@ TEST_F(CollisionTest, collision_box_box_dynamic_x_direction) { rg1.data.linear_velocity = {10, 10}; Rigidbody & rg2 = this->mgr.get_components_by_id<Rigidbody>(2).front().get(); rg2.data.linear_velocity = {10, 10}; - collision_sys.update(); + collision_sys.fixed_update(); EXPECT_TRUE(collision_happend); } @@ -259,19 +264,21 @@ 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.this_collider.game_object_id, 1); + EXPECT_EQ(ev.info.self.transform.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); + 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.this_collider.game_object_id, 2); + EXPECT_EQ(ev.info.self.transform.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_EQ( + ev.info.resolution_direction, crepe::CollisionSystem::Direction::Y_DIRECTION + ); }; EXPECT_FALSE(collision_happend); Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get(); @@ -280,7 +287,7 @@ TEST_F(CollisionTest, collision_box_box_dynamic_y_direction) { rg1.data.linear_velocity = {10, 10}; Rigidbody & rg2 = this->mgr.get_components_by_id<Rigidbody>(2).front().get(); rg2.data.linear_velocity = {10, 10}; - collision_sys.update(); + collision_sys.fixed_update(); EXPECT_TRUE(collision_happend); } @@ -288,21 +295,19 @@ 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.this_collider.game_object_id, 1); + EXPECT_EQ(ev.info.self.transform.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 - FAIL(); - }; + script_object2_ref->test_fn + = [&collision_happend](const CollisionEvent & ev) { collision_happend = true; }; EXPECT_FALSE(collision_happend); Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get(); tf.position = {50, 30}; Rigidbody & rg2 = this->mgr.get_components_by_id<Rigidbody>(2).front().get(); rg2.data.body_type = crepe::Rigidbody::BodyType::STATIC; - collision_sys.update(); + collision_sys.fixed_update(); EXPECT_TRUE(collision_happend); } @@ -310,15 +315,16 @@ 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.this_collider.game_object_id, 1); + EXPECT_EQ(ev.info.self.transform.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); + 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 - FAIL(); + //FAIL(); }; EXPECT_FALSE(collision_happend); Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get(); @@ -327,7 +333,7 @@ TEST_F(CollisionTest, collision_box_box_static_x_direction) { rg1.data.linear_velocity = {10, 10}; Rigidbody & rg2 = this->mgr.get_components_by_id<Rigidbody>(2).front().get(); rg2.data.body_type = crepe::Rigidbody::BodyType::STATIC; - collision_sys.update(); + collision_sys.fixed_update(); EXPECT_TRUE(collision_happend); } @@ -335,15 +341,16 @@ 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.this_collider.game_object_id, 1); + EXPECT_EQ(ev.info.self.transform.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); + 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 - FAIL(); + //FAIL(); }; EXPECT_FALSE(collision_happend); Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get(); @@ -352,40 +359,6 @@ TEST_F(CollisionTest, collision_box_box_static_y_direction) { rg1.data.linear_velocity = {10, 10}; Rigidbody & rg2 = this->mgr.get_components_by_id<Rigidbody>(2).front().get(); rg2.data.body_type = crepe::Rigidbody::BodyType::STATIC; - collision_sys.update(); - EXPECT_TRUE(collision_happend); -} - -TEST_F(CollisionTest, collision_box_box_static_multiple) { //todo check visually - bool collision_happend = false; - float offset_value = 0; - float resolution = 0; - script_object1_ref->test_fn = [&](const CollisionEvent & ev) { - collision_happend = true; - EXPECT_EQ(ev.info.this_collider.game_object_id, 1); - EXPECT_EQ(ev.info.this_collider.offset.x, offset_value); - EXPECT_EQ(ev.info.resolution.x, resolution); - }; - script_object2_ref->test_fn = [&](const CollisionEvent & ev) { - // is static should not be called - FAIL(); - }; - EXPECT_FALSE(collision_happend); - Transform & tf = this->mgr.get_components_by_id<Transform>(1).front().get(); - tf.position = {45, 30}; - Rigidbody & rg1 = this->mgr.get_components_by_id<Rigidbody>(1).front().get(); - rg1.data.linear_velocity = {10, 10}; - Rigidbody & rg2 = this->mgr.get_components_by_id<Rigidbody>(2).front().get(); - rg2.data.body_type = crepe::Rigidbody::BodyType::STATIC; - BoxCollider & bxc = this->mgr.get_components_by_id<BoxCollider>(1).front().get(); - bxc.offset = {5, 0}; - this->game_object1.add_component<BoxCollider>(vec2{-5, 0}, vec2{10, 10}); - offset_value = 5; - resolution = 10; - collision_sys.update(); - offset_value = -5; - resolution = 10; - tf.position = {55, 30}; - collision_sys.update(); + collision_sys.fixed_update(); EXPECT_TRUE(collision_happend); } diff --git a/src/test/DBTest.cpp b/src/test/DBTest.cpp index 0c98e10..7f2c339 100644 --- a/src/test/DBTest.cpp +++ b/src/test/DBTest.cpp @@ -35,4 +35,3 @@ TEST_F(DBTest, MultipleKeys) { EXPECT_EQ(db.get("foo"), "foo"); EXPECT_EQ(db.get("bar"), "bar"); } - diff --git a/src/test/ECSTest.cpp b/src/test/ECSTest.cpp index af2b7b0..92436a9 100644 --- a/src/test/ECSTest.cpp +++ b/src/test/ECSTest.cpp @@ -16,7 +16,7 @@ class ECSTest : public ::testing::Test { Mediator m; public: - ComponentManager mgr{m}; + ComponentManager mgr {m}; class TestComponent : public Component { using Component::Component; @@ -24,7 +24,7 @@ public: }; TEST_F(ECSTest, createGameObject) { - GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); + GameObject obj = mgr.new_object("body", "person", vec2 {0, 0}, 0, 1); vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_type<Metadata>(); vector<reference_wrapper<Transform>> transform = mgr.get_components_by_type<Transform>(); @@ -44,8 +44,8 @@ TEST_F(ECSTest, createGameObject) { } TEST_F(ECSTest, deleteAllGameObjects) { - GameObject obj0 = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); - GameObject obj1 = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); + GameObject obj0 = mgr.new_object("body", "person", vec2 {0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("body", "person", vec2 {0, 0}, 0, 1); mgr.delete_all_components(); @@ -55,7 +55,7 @@ TEST_F(ECSTest, deleteAllGameObjects) { EXPECT_EQ(metadata.size(), 0); EXPECT_EQ(transform.size(), 0); - GameObject obj2 = mgr.new_object("body2", "person2", vec2{1, 0}, 5, 1); + GameObject obj2 = mgr.new_object("body2", "person2", vec2 {1, 0}, 5, 1); metadata = mgr.get_components_by_type<Metadata>(); transform = mgr.get_components_by_type<Transform>(); @@ -77,8 +77,8 @@ TEST_F(ECSTest, deleteAllGameObjects) { } TEST_F(ECSTest, deleteGameObject) { - GameObject obj0 = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); - GameObject obj1 = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); + GameObject obj0 = mgr.new_object("body", "person", vec2 {0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("body", "person", vec2 {0, 0}, 0, 1); mgr.delete_all_components_of_id(0); @@ -103,7 +103,7 @@ TEST_F(ECSTest, deleteGameObject) { TEST_F(ECSTest, manyGameObjects) { for (int i = 0; i < 5000; i++) { - GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, i); + GameObject obj = mgr.new_object("body", "person", vec2 {0, 0}, 0, i); } vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_type<Metadata>(); @@ -135,7 +135,7 @@ TEST_F(ECSTest, manyGameObjects) { for (int i = 0; i < 10000 - 5000; i++) { string tag = "person" + to_string(i); - GameObject obj = mgr.new_object("body", tag, vec2{0, 0}, i, 0); + GameObject obj = mgr.new_object("body", tag, vec2 {0, 0}, i, 0); } metadata = mgr.get_components_by_type<Metadata>(); @@ -168,7 +168,7 @@ TEST_F(ECSTest, manyGameObjects) { for (int i = 0; i < 10000; i++) { string name = "body" + to_string(i); - GameObject obj = mgr.new_object(name, "person", vec2{0, 0}, 0, 0); + GameObject obj = mgr.new_object(name, "person", vec2 {0, 0}, 0, 0); } metadata = mgr.get_components_by_type<Metadata>(); @@ -193,8 +193,8 @@ TEST_F(ECSTest, manyGameObjects) { } TEST_F(ECSTest, getComponentsByID) { - GameObject obj0 = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); - GameObject obj1 = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); + GameObject obj0 = mgr.new_object("body", "person", vec2 {0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("body", "person", vec2 {0, 0}, 0, 1); vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_id<Metadata>(0); vector<reference_wrapper<Transform>> transform = mgr.get_components_by_id<Transform>(1); @@ -217,19 +217,21 @@ TEST_F(ECSTest, getComponentsByID) { TEST_F(ECSTest, tooMuchComponents) { try { - GameObject obj0 = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); - obj0.add_component<Transform>(vec2{10, 10}, 0, 1); + GameObject obj0 = mgr.new_object("body", "person", vec2 {0, 0}, 0, 1); + obj0.add_component<Transform>(vec2 {10, 10}, 0, 1); } catch (const exception & e) { - EXPECT_EQ(e.what(), - string("Exceeded maximum number of instances for this component type")); + EXPECT_EQ( + e.what(), string("Exceeded maximum number of instances for this component type") + ); } try { - GameObject obj1 = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("body", "person", vec2 {0, 0}, 0, 1); obj1.add_component<Metadata>("body", "person"); } catch (const exception & e) { - EXPECT_EQ(e.what(), - string("Exceeded maximum number of instances for this component type")); + EXPECT_EQ( + e.what(), string("Exceeded maximum number of instances for this component type") + ); } vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_type<Metadata>(); @@ -241,11 +243,11 @@ TEST_F(ECSTest, tooMuchComponents) { TEST_F(ECSTest, partentChild) { { - GameObject body = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); - GameObject right_leg = mgr.new_object("rightLeg", "person", vec2{1, 1}, 0, 1); - GameObject left_leg = mgr.new_object("leftLeg", "person", vec2{1, 1}, 0, 1); - GameObject right_foot = mgr.new_object("rightFoot", "person", vec2{2, 2}, 0, 1); - GameObject left_foot = mgr.new_object("leftFoot", "person", vec2{2, 2}, 0, 1); + GameObject body = mgr.new_object("body", "person", vec2 {0, 0}, 0, 1); + GameObject right_leg = mgr.new_object("rightLeg", "person", vec2 {1, 1}, 0, 1); + GameObject left_leg = mgr.new_object("leftLeg", "person", vec2 {1, 1}, 0, 1); + GameObject right_foot = mgr.new_object("rightFoot", "person", vec2 {2, 2}, 0, 1); + GameObject left_foot = mgr.new_object("leftFoot", "person", vec2 {2, 2}, 0, 1); // Set the parent of each GameObject right_foot.set_parent(right_leg); @@ -290,10 +292,10 @@ TEST_F(ECSTest, partentChild) { } TEST_F(ECSTest, persistent) { - GameObject obj0 = mgr.new_object("obj0", "obj0", vec2{0, 0}, 0, 1); - GameObject obj1 = mgr.new_object("obj1", "obj1", vec2{0, 0}, 0, 1); + GameObject obj0 = mgr.new_object("obj0", "obj0", vec2 {0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("obj1", "obj1", vec2 {0, 0}, 0, 1); obj1.set_persistent(); - GameObject obj2 = mgr.new_object("obj2", "obj2", vec2{0, 0}, 0, 1); + GameObject obj2 = mgr.new_object("obj2", "obj2", vec2 {0, 0}, 0, 1); vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_type<Metadata>(); vector<reference_wrapper<Transform>> transform = mgr.get_components_by_type<Transform>(); @@ -329,8 +331,8 @@ TEST_F(ECSTest, persistent) { EXPECT_EQ(transform[0].get().position.x, 0); EXPECT_EQ(transform[0].get().position.y, 0); - GameObject obj3 = mgr.new_object("obj3", "obj3", vec2{0, 0}, 0, 5); - GameObject obj4 = mgr.new_object("obj4", "obj4", vec2{0, 0}, 0, 5); + GameObject obj3 = mgr.new_object("obj3", "obj3", vec2 {0, 0}, 0, 5); + GameObject obj4 = mgr.new_object("obj4", "obj4", vec2 {0, 0}, 0, 5); metadata = mgr.get_components_by_type<Metadata>(); transform = mgr.get_components_by_type<Transform>(); @@ -358,10 +360,10 @@ TEST_F(ECSTest, persistent) { } TEST_F(ECSTest, resetPersistent) { - GameObject obj0 = mgr.new_object("obj0", "obj0", vec2{0, 0}, 0, 1); - GameObject obj1 = mgr.new_object("obj1", "obj1", vec2{0, 0}, 0, 1); + GameObject obj0 = mgr.new_object("obj0", "obj0", vec2 {0, 0}, 0, 1); + GameObject obj1 = mgr.new_object("obj1", "obj1", vec2 {0, 0}, 0, 1); obj1.set_persistent(); - GameObject obj2 = mgr.new_object("obj2", "obj2", vec2{0, 0}, 0, 1); + GameObject obj2 = mgr.new_object("obj2", "obj2", vec2 {0, 0}, 0, 1); vector<reference_wrapper<Metadata>> metadata = mgr.get_components_by_type<Metadata>(); vector<reference_wrapper<Transform>> transform = mgr.get_components_by_type<Transform>(); @@ -466,3 +468,17 @@ TEST_F(ECSTest, ComponentsByTag) { EXPECT_EQ(objects.size(), 3); } } + +TEST_F(ECSTest, Snapshot) { + GameObject foo = mgr.new_object("foo"); + + foo.transform.position = {1, 1}; + + ComponentManager::Snapshot snapshot = mgr.save(); + + foo.transform.position = {0, 0}; + + mgr.restore(snapshot); + + EXPECT_EQ(foo.transform.position, (vec2 {1, 1})); +} diff --git a/src/test/EventTest.cpp b/src/test/EventTest.cpp index 82272b5..6105679 100644 --- a/src/test/EventTest.cpp +++ b/src/test/EventTest.cpp @@ -10,7 +10,7 @@ using namespace crepe; class EventManagerTest : public ::testing::Test { protected: Mediator mediator; - EventManager event_mgr{mediator}; + EventManager event_mgr {mediator}; void SetUp() override { // Clear any existing subscriptions or events before each test event_mgr.clear(); @@ -30,33 +30,34 @@ TEST_F(EventManagerTest, EventSubscription) { // Verify subscription (not directly verifiable; test by triggering event) event_mgr.trigger_event<KeyPressEvent>( - KeyPressEvent{ + KeyPressEvent { .repeat = true, .key = Keycode::A, }, - 1); + 1 + ); event_mgr.trigger_event<KeyPressEvent>( - KeyPressEvent{ + KeyPressEvent { .repeat = true, .key = Keycode::A, }, - EventManager::CHANNEL_ALL); + EventManager::CHANNEL_ALL + ); } TEST_F(EventManagerTest, EventManagerTest_trigger_all_channels) { bool triggered = false; EventHandler<MouseClickEvent> mouse_handler = [&](const MouseClickEvent & e) { triggered = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.mouse_pos.x, 100); + EXPECT_EQ(e.mouse_pos.y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; }; event_mgr.subscribe<MouseClickEvent>(mouse_handler, EventManager::CHANNEL_ALL); - MouseClickEvent click_event{ - .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; + MouseClickEvent click_event {.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE}; event_mgr.trigger_event<MouseClickEvent>(click_event, EventManager::CHANNEL_ALL); EXPECT_TRUE(triggered); @@ -66,15 +67,14 @@ TEST_F(EventManagerTest, EventManagerTest_trigger_one_channel) { int test_channel = 1; EventHandler<MouseClickEvent> mouse_handler = [&](const MouseClickEvent & e) { triggered = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.mouse_pos.x, 100); + EXPECT_EQ(e.mouse_pos.y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; }; event_mgr.subscribe<MouseClickEvent>(mouse_handler, test_channel); - MouseClickEvent click_event{ - .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; + MouseClickEvent click_event {.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE}; event_mgr.trigger_event<MouseClickEvent>(click_event, EventManager::CHANNEL_ALL); EXPECT_FALSE(triggered); @@ -90,23 +90,22 @@ TEST_F(EventManagerTest, EventManagerTest_callback_propagation) { // Handlers EventHandler<MouseClickEvent> mouse_handler_true = [&](const MouseClickEvent & e) { triggered_true = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.mouse_pos.x, 100); + EXPECT_EQ(e.mouse_pos.y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return true; // Stops propagation }; EventHandler<MouseClickEvent> mouse_handler_false = [&](const MouseClickEvent & e) { triggered_false = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.mouse_pos.x, 100); + EXPECT_EQ(e.mouse_pos.y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; // Allows propagation }; // Test event - MouseClickEvent click_event{ - .mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}; + MouseClickEvent click_event {.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE}; event_mgr.subscribe<MouseClickEvent>(mouse_handler_true, EventManager::CHANNEL_ALL); event_mgr.subscribe<MouseClickEvent>(mouse_handler_false, EventManager::CHANNEL_ALL); @@ -138,15 +137,15 @@ TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) { int test_channel = 1; EventHandler<MouseClickEvent> mouse_handler1 = [&](const MouseClickEvent & e) { triggered1 = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.mouse_pos.x, 100); + EXPECT_EQ(e.mouse_pos.y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; // Allows propagation }; EventHandler<MouseClickEvent> mouse_handler2 = [&](const MouseClickEvent & e) { triggered2 = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.mouse_pos.x, 100); + EXPECT_EQ(e.mouse_pos.y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; // Allows propagation }; @@ -154,10 +153,12 @@ TEST_F(EventManagerTest, EventManagerTest_queue_dispatch) { event_mgr.subscribe<MouseClickEvent>(mouse_handler2, test_channel); event_mgr.queue_event<MouseClickEvent>( - MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}); + MouseClickEvent {.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE} + ); event_mgr.queue_event<MouseClickEvent>( - MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}, - test_channel); + MouseClickEvent {.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE}, + test_channel + ); event_mgr.dispatch_events(); EXPECT_TRUE(triggered1); EXPECT_TRUE(triggered2); @@ -172,16 +173,16 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { // Define EventHandlers EventHandler<MouseClickEvent> mouse_handler1 = [&](const MouseClickEvent & e) { triggered1 = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.mouse_pos.x, 100); + EXPECT_EQ(e.mouse_pos.y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; // Allows propagation }; EventHandler<MouseClickEvent> mouse_handler2 = [&](const MouseClickEvent & e) { triggered2 = true; - EXPECT_EQ(e.mouse_x, 100); - EXPECT_EQ(e.mouse_y, 200); + EXPECT_EQ(e.mouse_pos.x, 100); + EXPECT_EQ(e.mouse_pos.y, 200); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; // Allows propagation }; @@ -191,7 +192,8 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { // Queue events event_mgr.queue_event<MouseClickEvent>( - MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}); + MouseClickEvent {.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE} + ); // Dispatch events - both handlers should be triggered event_mgr.dispatch_events(); @@ -207,7 +209,8 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { // Queue the same event again event_mgr.queue_event<MouseClickEvent>( - MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}); + MouseClickEvent {.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE} + ); // Dispatch events - only handler 2 should be triggered, handler 1 should NOT event_mgr.dispatch_events(); @@ -222,7 +225,8 @@ TEST_F(EventManagerTest, EventManagerTest_unsubscribe) { // Queue the event again event_mgr.queue_event<MouseClickEvent>( - MouseClickEvent{.mouse_x = 100, .mouse_y = 200, .button = MouseButton::LEFT_MOUSE}); + MouseClickEvent {.mouse_pos = {100, 200}, .button = MouseButton::LEFT_MOUSE} + ); // Dispatch events - no handler should be triggered event_mgr.dispatch_events(); diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index 8b40cea..9a541a0 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -1,7 +1,10 @@ -#include "system/RenderSystem.h" #include <gtest/gtest.h> + +#include <crepe/manager/ResourceManager.h> +#include <crepe/system/RenderSystem.h> #define protected public #define private public + #include "api/KeyCodes.h" #include "manager/ComponentManager.h" #include "manager/EventManager.h" @@ -15,6 +18,7 @@ #include <crepe/api/Metadata.h> #include <crepe/api/Transform.h> #include <crepe/api/Vector2.h> +#include <crepe/facade/SDLContext.h> #include <gmock/gmock.h> using namespace std; @@ -24,26 +28,29 @@ using namespace crepe; class InputTest : public ::testing::Test { public: Mediator mediator; - ComponentManager mgr{mediator}; - SDLContext sdl_context{mediator}; + ComponentManager mgr {mediator}; + SDLContext sdl_context {mediator}; - InputSystem input_system{mediator}; - RenderSystem render{mediator}; - EventManager event_manager{mediator}; + InputSystem input_system {mediator}; + ResourceManager resman {mediator}; + RenderSystem render {mediator}; + EventManager event_manager {mediator}; //GameObject camera; + vec2 offset = {100, 200}; protected: void SetUp() override { - GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); - auto & camera - = obj.add_component<Camera>(ivec2{500, 500}, vec2{500, 500}, - Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); - render.update(); + GameObject obj = mgr.new_object("camera", "camera", offset, 0, 1); + auto & camera = obj.add_component<Camera>( + ivec2 {500, 500}, vec2 {500, 500}, + Camera::Data {.bg_color = Color::WHITE, .zoom = 1.0f} + ); + render.frame_update(); //mediator.event_manager = event_manager; //mediator.component_manager = mgr; //event_manager.clear(); } - + void TearDown() override {} void simulate_mouse_click(int mouse_x, int mouse_y, Uint8 mouse_button) { SDL_Event event; @@ -70,8 +77,7 @@ TEST_F(InputTest, MouseDown) { EventHandler<MousePressEvent> on_mouse_down = [&](const MousePressEvent & event) { mouse_triggered = true; //middle of the screen = 0,0 - EXPECT_EQ(event.mouse_x, 0); - EXPECT_EQ(event.mouse_y, 0); + EXPECT_EQ(event.mouse_pos, offset); EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); return false; }; @@ -86,7 +92,7 @@ TEST_F(InputTest, MouseDown) { event.button.button = SDL_BUTTON_LEFT; SDL_PushEvent(&event); - input_system.update(); + input_system.fixed_update(); event_manager.dispatch_events(); EXPECT_TRUE(mouse_triggered); } @@ -95,8 +101,7 @@ TEST_F(InputTest, MouseUp) { bool function_triggered = false; EventHandler<MouseReleaseEvent> on_mouse_release = [&](const MouseReleaseEvent & e) { function_triggered = true; - EXPECT_EQ(e.mouse_x, 0); - EXPECT_EQ(e.mouse_y, 0); + EXPECT_EQ(e.mouse_pos, offset); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; }; @@ -110,7 +115,7 @@ TEST_F(InputTest, MouseUp) { event.button.button = SDL_BUTTON_LEFT; SDL_PushEvent(&event); - input_system.update(); + input_system.fixed_update(); event_manager.dispatch_events(); EXPECT_TRUE(function_triggered); } @@ -119,10 +124,9 @@ TEST_F(InputTest, MouseMove) { bool function_triggered = false; EventHandler<MouseMoveEvent> on_mouse_move = [&](const MouseMoveEvent & e) { function_triggered = true; - EXPECT_EQ(e.mouse_x, 0); - EXPECT_EQ(e.mouse_y, 0); - EXPECT_EQ(e.delta_x, 10); - EXPECT_EQ(e.delta_y, 10); + EXPECT_EQ(e.mouse_pos, offset); + EXPECT_EQ(e.mouse_delta.x, 10); + EXPECT_EQ(e.mouse_delta.y, 10); return false; }; event_manager.subscribe<MouseMoveEvent>(on_mouse_move); @@ -136,7 +140,7 @@ TEST_F(InputTest, MouseMove) { event.motion.yrel = 10; SDL_PushEvent(&event); - input_system.update(); + input_system.fixed_update(); event_manager.dispatch_events(); EXPECT_TRUE(function_triggered); } @@ -162,7 +166,7 @@ TEST_F(InputTest, KeyDown) { test_event.key.repeat = 1; // Set repeat flag SDL_PushEvent(&test_event); - input_system.update(); // Process the event + input_system.fixed_update(); // Process the event event_manager.dispatch_events(); // Dispatch events to handlers EXPECT_TRUE(function_triggered); // Check if the handler was triggered @@ -183,7 +187,7 @@ TEST_F(InputTest, KeyUp) { event.key.keysym.scancode = SDL_SCANCODE_B; SDL_PushEvent(&event); - input_system.update(); + input_system.fixed_update(); event_manager.dispatch_events(); EXPECT_TRUE(function_triggered); } @@ -193,65 +197,99 @@ TEST_F(InputTest, MouseClick) { EventHandler<MouseClickEvent> on_mouse_click = [&](const MouseClickEvent & event) { on_click_triggered = true; EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); - EXPECT_EQ(event.mouse_x, 0); - EXPECT_EQ(event.mouse_y, 0); + EXPECT_EQ(event.mouse_pos, offset); return false; }; event_manager.subscribe<MouseClickEvent>(on_mouse_click); this->simulate_mouse_click(250, 250, SDL_BUTTON_LEFT); - input_system.update(); + input_system.fixed_update(); event_manager.dispatch_events(); EXPECT_TRUE(on_click_triggered); } TEST_F(InputTest, testButtonClick) { - GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); + GameObject button_obj = mgr.new_object("body", "person", vec2 {0, 0}, 0, 1); bool button_clicked = false; - std::function<void()> on_click = [&]() { button_clicked = true; }; - auto & button - = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false); + event_manager.subscribe<ButtonPressEvent>([&](const ButtonPressEvent & event) { + button_clicked = true; + EXPECT_EQ(event.metadata.game_object_id, button_obj.id); + return false; + }); + auto & button = button_obj.add_component<Button>(vec2 {100, 100}, vec2 {0, 0}); bool hover = false; button.active = true; - - button.is_pressed = false; - button.is_toggle = false; this->simulate_mouse_click(999, 999, SDL_BUTTON_LEFT); - input_system.update(); + input_system.fixed_update(); event_manager.dispatch_events(); EXPECT_FALSE(button_clicked); this->simulate_mouse_click(250, 250, SDL_BUTTON_LEFT); - input_system.update(); + input_system.fixed_update(); event_manager.dispatch_events(); EXPECT_TRUE(button_clicked); } - -TEST_F(InputTest, testButtonHover) { - GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); +TEST_F(InputTest, buttonPositionCamera) { + GameObject button_obj = mgr.new_object("body", "person", vec2 {50, 50}, 0, 1); bool button_clicked = false; - std::function<void()> on_click = [&]() { button_clicked = true; }; - auto & button - = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false); - button.active = true; - button.is_pressed = false; - button.is_toggle = false; + event_manager.subscribe<ButtonPressEvent>([&](const ButtonPressEvent & event) { + button_clicked = true; + EXPECT_EQ(event.metadata.game_object_id, button_obj.id); + return false; + }); + auto & button = button_obj.add_component<Button>(vec2 {10, 10}, vec2 {0, 0}); - // Mouse not on button - SDL_Event event; - SDL_zero(event); - event.type = SDL_MOUSEMOTION; - event.motion.x = 700; - event.motion.y = 700; - event.motion.xrel = 10; - event.motion.yrel = 10; - SDL_PushEvent(&event); + button.world_space = false; + bool hover = false; + button.active = true; + this->simulate_mouse_click(999, 999, SDL_BUTTON_LEFT); + input_system.fixed_update(); + event_manager.dispatch_events(); + EXPECT_FALSE(button_clicked); - input_system.update(); + this->simulate_mouse_click(300, 300, SDL_BUTTON_LEFT); + input_system.fixed_update(); event_manager.dispatch_events(); - EXPECT_FALSE(button.hover); + EXPECT_TRUE(button_clicked); +} +TEST_F(InputTest, buttonPositionWorld) { + GameObject button_obj = mgr.new_object("body", "person", vec2 {50, 50}, 0, 1); + bool button_clicked = false; + event_manager.subscribe<ButtonPressEvent>([&](const ButtonPressEvent & event) { + button_clicked = true; + EXPECT_EQ(event.metadata.game_object_id, button_obj.id); + return false; + }); + auto & button = button_obj.add_component<Button>(vec2 {10, 10}, vec2 {0, 0}); + button.world_space = true; + bool hover = false; + button.active = true; + this->simulate_mouse_click(999, 999, SDL_BUTTON_LEFT); + input_system.fixed_update(); + event_manager.dispatch_events(); + EXPECT_FALSE(button_clicked); + this->simulate_mouse_click(300, 300, SDL_BUTTON_LEFT); + input_system.fixed_update(); + event_manager.dispatch_events(); + EXPECT_FALSE(button_clicked); +} +TEST_F(InputTest, testButtonHover) { + GameObject button_obj = mgr.new_object("body", "person", vec2 {0, 0}, 0, 1); + bool button_hover = false; + event_manager.subscribe<ButtonEnterEvent>([&](const ButtonEnterEvent & event) { + button_hover = true; + EXPECT_EQ(event.metadata.game_object_id, button_obj.id); + return false; + }); + event_manager.subscribe<ButtonExitEvent>([&](const ButtonExitEvent & event) { + button_hover = false; + EXPECT_EQ(event.metadata.game_object_id, button_obj.id); + return false; + }); + auto & button = button_obj.add_component<Button>(vec2 {100, 100}, vec2 {0, 0}); + button.active = true; // Mouse on button SDL_Event hover_event; SDL_zero(hover_event); @@ -262,7 +300,22 @@ TEST_F(InputTest, testButtonHover) { hover_event.motion.yrel = 10; SDL_PushEvent(&hover_event); - input_system.update(); + input_system.fixed_update(); event_manager.dispatch_events(); EXPECT_TRUE(button.hover); + EXPECT_TRUE(button_hover); + // Mouse not on button + SDL_Event event; + SDL_zero(event); + event.type = SDL_MOUSEMOTION; + event.motion.x = 500; + event.motion.y = 500; + event.motion.xrel = 10; + event.motion.yrel = 10; + SDL_PushEvent(&event); + + input_system.fixed_update(); + event_manager.dispatch_events(); + EXPECT_FALSE(button.hover); + EXPECT_FALSE(button_hover); } diff --git a/src/test/LoopManagerTest.cpp b/src/test/LoopManagerTest.cpp index df132ae..302d96c 100644 --- a/src/test/LoopManagerTest.cpp +++ b/src/test/LoopManagerTest.cpp @@ -4,7 +4,7 @@ #include <thread> #define private public #define protected public -#include <crepe/api/LoopManager.h> +#include <crepe/api/Engine.h> #include <crepe/manager/EventManager.h> #include <crepe/manager/LoopTimerManager.h> using namespace std::chrono; @@ -12,7 +12,7 @@ using namespace crepe; class DISABLED_LoopManagerTest : public ::testing::Test { protected: - class TestGameLoop : public crepe::LoopManager { + class TestGameLoop : public crepe::Engine { public: MOCK_METHOD(void, fixed_update, (), (override)); MOCK_METHOD(void, frame_update, (), (override)); @@ -72,7 +72,7 @@ TEST_F(DISABLED_LoopManagerTest, ShutDown) { // Start the loop in a separate thread std::thread loop_thread([&]() { test_loop.start(); }); std::this_thread::sleep_for(std::chrono::milliseconds(1)); - test_loop.event_manager.trigger_event<ShutDownEvent>(ShutDownEvent{}); + test_loop.event_manager.trigger_event<ShutDownEvent>(ShutDownEvent {}); // Wait for the loop thread to finish loop_thread.join(); } diff --git a/src/test/LoopTimerTest.cpp b/src/test/LoopTimerTest.cpp index 7bd6305..52e412e 100644 --- a/src/test/LoopTimerTest.cpp +++ b/src/test/LoopTimerTest.cpp @@ -14,7 +14,7 @@ using namespace crepe; class LoopTimerTest : public ::testing::Test { protected: Mediator mediator; - LoopTimerManager loop_timer{mediator}; + LoopTimerManager loop_timer {mediator}; void SetUp() override { loop_timer.start(); } }; diff --git a/src/test/ParticleTest.cpp b/src/test/ParticleTest.cpp index 9112a3f..eee7a73 100644 --- a/src/test/ParticleTest.cpp +++ b/src/test/ParticleTest.cpp @@ -1,15 +1,18 @@ #include "api/Asset.h" -#include <crepe/Particle.h> #include <crepe/api/Config.h> #include <crepe/api/GameObject.h> -#include <crepe/api/ParticleEmitter.h> #include <crepe/api/Rigidbody.h> #include <crepe/api/Sprite.h> #include <crepe/api/Transform.h> #include <crepe/manager/ComponentManager.h> -#include <crepe/system/ParticleSystem.h> +#include <crepe/manager/LoopTimerManager.h> #include <gtest/gtest.h> #include <math.h> +#define protected public +#define private public +#include <crepe/Particle.h> +#include <crepe/api/ParticleEmitter.h> +#include <crepe/system/ParticleSystem.h> using namespace std; using namespace std::chrono_literals; @@ -19,44 +22,49 @@ class ParticlesTest : public ::testing::Test { Mediator m; public: - ComponentManager component_manager{m}; - ParticleSystem particle_system{m}; + ComponentManager component_manager {m}; + ParticleSystem particle_system {m}; + LoopTimerManager loop_timer {m}; void SetUp() override { ComponentManager & mgr = this->component_manager; std::vector<std::reference_wrapper<Transform>> transforms = mgr.get_components_by_id<Transform>(0); if (transforms.empty()) { - GameObject game_object = mgr.new_object("", "", vec2{0, 0}, 0, 0); + GameObject game_object = mgr.new_object("", "", vec2 {0, 0}, 0, 0); Color color(0, 0, 0, 0); auto s1 = Asset("asset/texture/img.png"); Sprite & test_sprite = game_object.add_component<Sprite>( - s1, Sprite::Data{ - .color = color, - .flip = Sprite::FlipSettings{true, true}, - .size = {10, 10}, - }); + s1, + Sprite::Data { + .color = color, + .flip = Sprite::FlipSettings {true, true}, + .size = {10, 10}, + } + ); - game_object.add_component<ParticleEmitter>(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 = vec2{0, 0}, - .boundary{ - .width = 0, - .height = 0, - .offset = vec2{0, 0}, - .reset_on_exit = false, - }, - .sprite = test_sprite, - }); + game_object.add_component<ParticleEmitter>( + test_sprite, + ParticleEmitter::Data { + .offset = {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 = vec2 {0, 0}, + .boundary { + .width = 0, + .height = 0, + .offset = vec2 {0, 0}, + .reset_on_exit = false, + }, + } + ); } transforms = mgr.get_components_by_id<Transform>(0); Transform & transform = transforms.front().get(); @@ -66,7 +74,7 @@ public: std::vector<std::reference_wrapper<ParticleEmitter>> rigidbodies = mgr.get_components_by_id<ParticleEmitter>(0); ParticleEmitter & emitter = rigidbodies.front().get(); - emitter.data.position = {0, 0}; + emitter.data.offset = {0, 0}; emitter.data.emission_rate = 0; emitter.data.min_speed = 0; emitter.data.max_speed = 0; @@ -74,9 +82,9 @@ public: emitter.data.max_angle = 0; emitter.data.begin_lifespan = 0; emitter.data.end_lifespan = 0; - emitter.data.force_over_time = vec2{0, 0}; - emitter.data.boundary = {0, 0, vec2{0, 0}, false}; - for (auto & particle : emitter.data.particles) { + emitter.data.force_over_time = vec2 {0, 0}; + emitter.data.boundary = {0, 0, vec2 {0, 0}, false}; + for (auto & particle : emitter.particles) { particle.active = false; } } @@ -93,21 +101,21 @@ TEST_F(ParticlesTest, spawnParticle) { emitter.data.max_angle = 0.1; emitter.data.max_speed = 10; emitter.data.max_angle = 10; - particle_system.update(); + particle_system.fixed_update(); //check if nothing happend - EXPECT_EQ(emitter.data.particles[0].active, false); - emitter.data.emission_rate = 1; + EXPECT_EQ(emitter.particles[0].active, false); + emitter.data.emission_rate = 50; //check particle spawnes - particle_system.update(); - EXPECT_EQ(emitter.data.particles[0].active, true); - particle_system.update(); - EXPECT_EQ(emitter.data.particles[1].active, true); - particle_system.update(); - EXPECT_EQ(emitter.data.particles[2].active, true); - particle_system.update(); - EXPECT_EQ(emitter.data.particles[3].active, true); + particle_system.fixed_update(); + EXPECT_EQ(emitter.particles[0].active, true); + particle_system.fixed_update(); + EXPECT_EQ(emitter.particles[1].active, true); + particle_system.fixed_update(); + EXPECT_EQ(emitter.particles[2].active, true); + particle_system.fixed_update(); + EXPECT_EQ(emitter.particles[3].active, true); - for (auto & particle : emitter.data.particles) { + for (auto & particle : emitter.particles) { // Check velocity range EXPECT_GE(particle.velocity.x, emitter.data.min_speed); // Speed should be greater than or equal to min_speed @@ -133,13 +141,13 @@ TEST_F(ParticlesTest, moveParticleHorizontal) { emitter.data.end_lifespan = 100; emitter.data.boundary.height = 100; emitter.data.boundary.width = 100; - emitter.data.min_speed = 1; - emitter.data.max_speed = 1; + emitter.data.min_speed = 50; + emitter.data.max_speed = 50; emitter.data.max_angle = 0; - emitter.data.emission_rate = 1; + emitter.data.emission_rate = 50; for (int a = 1; a < emitter.data.boundary.width / 2; a++) { - particle_system.update(); - EXPECT_EQ(emitter.data.particles[0].position.x, a); + particle_system.fixed_update(); + EXPECT_EQ(emitter.particles[0].position.x, a); } } @@ -150,14 +158,14 @@ TEST_F(ParticlesTest, moveParticleVertical) { emitter.data.end_lifespan = 100; emitter.data.boundary.height = 100; emitter.data.boundary.width = 100; - emitter.data.min_speed = 1; - emitter.data.max_speed = 1; + emitter.data.min_speed = 50; + emitter.data.max_speed = 50; emitter.data.min_angle = 90; emitter.data.max_angle = 90; - emitter.data.emission_rate = 1; + emitter.data.emission_rate = 50; for (int a = 1; a < emitter.data.boundary.width / 2; a++) { - particle_system.update(); - EXPECT_EQ(emitter.data.particles[0].position.y, a); + particle_system.fixed_update(); + EXPECT_EQ(emitter.particles[0].position.y, a); } } @@ -175,9 +183,9 @@ TEST_F(ParticlesTest, boundaryParticleReset) { emitter.data.max_angle = 90; emitter.data.emission_rate = 1; for (int a = 0; a < emitter.data.boundary.width / 2 + 1; a++) { - particle_system.update(); + particle_system.fixed_update(); } - EXPECT_EQ(emitter.data.particles[0].active, false); + EXPECT_EQ(emitter.particles[0].active, false); } TEST_F(ParticlesTest, boundaryParticleStop) { @@ -194,15 +202,19 @@ TEST_F(ParticlesTest, boundaryParticleStop) { emitter.data.max_angle = 90; emitter.data.emission_rate = 1; for (int a = 0; a < emitter.data.boundary.width / 2 + 1; a++) { - particle_system.update(); + particle_system.fixed_update(); } const double TOLERANCE = 0.01; - EXPECT_NEAR(emitter.data.particles[0].velocity.x, 0, TOLERANCE); - EXPECT_NEAR(emitter.data.particles[0].velocity.y, 0, TOLERANCE); - if (emitter.data.particles[0].velocity.x != 0) - EXPECT_NEAR(std::abs(emitter.data.particles[0].position.x), - emitter.data.boundary.height / 2, TOLERANCE); - if (emitter.data.particles[0].velocity.y != 0) - EXPECT_NEAR(std::abs(emitter.data.particles[0].position.y), - emitter.data.boundary.width / 2, TOLERANCE); + EXPECT_NEAR(emitter.particles[0].velocity.x, 0, TOLERANCE); + EXPECT_NEAR(emitter.particles[0].velocity.y, 0, TOLERANCE); + if (emitter.particles[0].velocity.x != 0) + EXPECT_NEAR( + std::abs(emitter.particles[0].position.x), emitter.data.boundary.height / 2, + TOLERANCE + ); + if (emitter.particles[0].velocity.y != 0) + EXPECT_NEAR( + std::abs(emitter.particles[0].position.y), emitter.data.boundary.width / 2, + TOLERANCE + ); } diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp index 3afb3c7..85eb6d5 100644 --- a/src/test/PhysicsTest.cpp +++ b/src/test/PhysicsTest.cpp @@ -16,17 +16,17 @@ class PhysicsTest : public ::testing::Test { Mediator m; public: - ComponentManager component_manager{m}; - PhysicsSystem system{m}; - LoopTimerManager loop_timer{m}; + ComponentManager component_manager {m}; + PhysicsSystem system {m}; + LoopTimerManager loop_timer {m}; void SetUp() override { ComponentManager & mgr = this->component_manager; vector<reference_wrapper<Transform>> transforms = mgr.get_components_by_id<Transform>(0); if (transforms.empty()) { - auto entity = mgr.new_object("", "", vec2{0, 0}, 0, 0); - entity.add_component<Rigidbody>(Rigidbody::Data{ + auto entity = mgr.new_object("", "", vec2 {0, 0}, 0, 0); + entity.add_component<Rigidbody>(Rigidbody::Data { .mass = 1, .gravity_scale = 1, .body_type = Rigidbody::BodyType::DYNAMIC, @@ -57,10 +57,10 @@ TEST_F(PhysicsTest, gravity) { ASSERT_FALSE(transforms.empty()); EXPECT_EQ(transform.position.y, 0); - system.update(); + system.fixed_update(); EXPECT_NEAR(transform.position.y, 0.0004, 0.0001); - system.update(); + system.fixed_update(); EXPECT_NEAR(transform.position.y, 0.002, 0.001); } @@ -74,14 +74,14 @@ TEST_F(PhysicsTest, max_velocity) { rigidbody.add_force_linear({100, 100}); rigidbody.add_force_angular(100); - system.update(); + system.fixed_update(); EXPECT_NEAR(rigidbody.data.linear_velocity.y, 7.07, 0.01); EXPECT_NEAR(rigidbody.data.linear_velocity.x, 7.07, 0.01); EXPECT_EQ(rigidbody.data.angular_velocity, 10); rigidbody.add_force_linear({-100, -100}); rigidbody.add_force_angular(-100); - system.update(); + system.fixed_update(); EXPECT_NEAR(rigidbody.data.linear_velocity.y, -7.07, 0.01); EXPECT_NEAR(rigidbody.data.linear_velocity.x, -7.07, 0.01); EXPECT_EQ(rigidbody.data.angular_velocity, -10); @@ -99,7 +99,7 @@ TEST_F(PhysicsTest, movement) { rigidbody.add_force_linear({1, 1}); rigidbody.add_force_angular(1); - system.update(); + system.fixed_update(); EXPECT_NEAR(transform.position.x, 0.02, 0.001); EXPECT_NEAR(transform.position.y, 0.02, 0.001); EXPECT_NEAR(transform.rotation, 0.02, 0.001); @@ -112,7 +112,7 @@ TEST_F(PhysicsTest, movement) { rigidbody.data.linear_velocity_coefficient.x = 0.5; rigidbody.data.linear_velocity_coefficient.y = 0.5; rigidbody.data.angular_velocity_coefficient = 0.5; - system.update(); + system.fixed_update(); EXPECT_NEAR(rigidbody.data.linear_velocity.x, 0.98, 0.01); EXPECT_NEAR(rigidbody.data.linear_velocity.y, 0.98, 0.01); EXPECT_NEAR(rigidbody.data.angular_velocity, 0.98, 0.01); @@ -121,12 +121,12 @@ TEST_F(PhysicsTest, movement) { rigidbody.data.angular_velocity_coefficient = 0; rigidbody.data.max_angular_velocity = 1000; rigidbody.data.angular_velocity = 360; - system.update(); + system.fixed_update(); EXPECT_NEAR(transform.rotation, 7.24, 0.01); rigidbody.data.angular_velocity = -360; - system.update(); + system.fixed_update(); EXPECT_NEAR(transform.rotation, 0.04, 0.001); - system.update(); + system.fixed_update(); EXPECT_NEAR(transform.rotation, 352.84, 0.01); } diff --git a/src/test/Profiling.cpp b/src/test/Profiling.cpp index 35f52dc..d8bd09d 100644 --- a/src/test/Profiling.cpp +++ b/src/test/Profiling.cpp @@ -32,14 +32,15 @@ using namespace testing; class TestScript : public Script { bool oncollision(const CollisionEvent & test) { - Log::logf("Box {} script on_collision()", test.info.this_collider.game_object_id); + Log::logf("Box {} script on_collision()", test.info.self.transform.game_object_id); return true; } void init() { - subscribe<CollisionEvent>( - [this](const CollisionEvent & ev) -> bool { return this->oncollision(ev); }); + subscribe<CollisionEvent>([this](const CollisionEvent & ev) -> bool { + return this->oncollision(ev); + }); } - void update() { + void fixed_update() { // Retrieve component from the same GameObject this script is on } }; @@ -50,22 +51,24 @@ public: // Minimum amount to let test pass const int min_gameobject_count = 100; // Maximum amount to stop test - const int max_gameobject_count = 150; + const int max_gameobject_count = 3000; // Amount of times a test runs to calculate average const int average = 5; // Maximum duration to stop test const std::chrono::microseconds duration = 16000us; Mediator m; - SDLContext sdl_context{m}; - ResourceManager resman{m}; - ComponentManager mgr{m}; + SDLContext sdl_context {m}; + ResourceManager resman {m}; + ComponentManager mgr {m}; // Add system used for profling tests - CollisionSystem collision_sys{m}; - PhysicsSystem physics_sys{m}; - ParticleSystem particle_sys{m}; - RenderSystem render_sys{m}; - ScriptSystem script_sys{m}; + EventManager evmgr {m}; + LoopTimerManager loopmgr {m}; + CollisionSystem collision_sys {m}; + PhysicsSystem physics_sys {m}; + ParticleSystem particle_sys {m}; + RenderSystem render_sys {m}; + ScriptSystem script_sys {m}; // Test data std::map<std::string, std::chrono::microseconds> timings; @@ -75,16 +78,18 @@ public: void SetUp() override { GameObject do_not_use = mgr.new_object("DO_NOT_USE", "", {0, 0}); - do_not_use.add_component<Camera>(ivec2{1080, 720}, vec2{2000, 2000}, - Camera::Data{ - .bg_color = Color::WHITE, - .zoom = 1.0f, - }); + do_not_use.add_component<Camera>( + ivec2 {1080, 720}, vec2 {2000, 2000}, + Camera::Data { + .bg_color = Color::WHITE, + .zoom = 1.0f, + } + ); // initialize systems here: //calls init - script_sys.update(); + script_sys.fixed_update(); //creates window - render_sys.update(); + render_sys.frame_update(); } // Helper function to time an update call and store its duration @@ -102,12 +107,14 @@ public: // Run and profile all systems, return the total time in milliseconds std::chrono::microseconds run_all_systems() { std::chrono::microseconds total_microseconds = 0us; - total_microseconds += time_function("PhysicsSystem", [&]() { physics_sys.update(); }); total_microseconds - += time_function("CollisionSystem", [&]() { collision_sys.update(); }); + += time_function("PhysicsSystem", [&]() { physics_sys.fixed_update(); }); total_microseconds - += time_function("ParticleSystem", [&]() { particle_sys.update(); }); - total_microseconds += time_function("RenderSystem", [&]() { render_sys.update(); }); + += time_function("CollisionSystem", [&]() { collision_sys.fixed_update(); }); + total_microseconds + += time_function("ParticleSystem", [&]() { particle_sys.fixed_update(); }); + total_microseconds + += time_function("RenderSystem", [&]() { render_sys.frame_update(); }); return total_microseconds; } @@ -164,23 +171,25 @@ TEST_F(DISABLED_ProfilingTest, Profiling_2) { { //define gameobject used for testing GameObject gameobject = mgr.new_object( - "gameobject", "", {static_cast<float>(game_object_count * 2), 0}); - gameobject.add_component<Rigidbody>(Rigidbody::Data{ + "gameobject", "", {static_cast<float>(game_object_count * 2), 0} + ); + gameobject.add_component<Rigidbody>(Rigidbody::Data { .gravity_scale = 0.0, .body_type = Rigidbody::BodyType::STATIC, }); - gameobject.add_component<BoxCollider>(vec2{0, 0}, vec2{1, 1}); + gameobject.add_component<BoxCollider>(vec2 {0, 0}, vec2 {1, 1}); gameobject.add_component<BehaviorScript>().set_script<TestScript>(); Sprite & test_sprite = gameobject.add_component<Sprite>( - Asset{"asset/texture/square.png"}, - Sprite::Data{ + Asset {"asset/texture/square.png"}, + Sprite::Data { .color = {0, 0, 0, 0}, .flip = {.flip_x = false, .flip_y = false}, .sorting_in_layer = 1, .order_in_layer = 1, .size = {.y = 500}, - }); + } + ); } this->game_object_count++; @@ -203,36 +212,41 @@ TEST_F(DISABLED_ProfilingTest, Profiling_3) { { //define gameobject used for testing GameObject gameobject = mgr.new_object( - "gameobject", "", {static_cast<float>(game_object_count * 2), 0}); - gameobject.add_component<Rigidbody>(Rigidbody::Data{ + "gameobject", "", {static_cast<float>(game_object_count * 2), 0} + ); + gameobject.add_component<Rigidbody>(Rigidbody::Data { .gravity_scale = 0, .body_type = Rigidbody::BodyType::STATIC, }); - gameobject.add_component<BoxCollider>(vec2{0, 0}, vec2{1, 1}); + gameobject.add_component<BoxCollider>(vec2 {0, 0}, vec2 {1, 1}); gameobject.add_component<BehaviorScript>().set_script<TestScript>(); Sprite & test_sprite = gameobject.add_component<Sprite>( - Asset{"asset/texture/square.png"}, - Sprite::Data{ + Asset {"asset/texture/square.png"}, + Sprite::Data { .color = {0, 0, 0, 0}, .flip = {.flip_x = false, .flip_y = false}, .sorting_in_layer = 1, .order_in_layer = 1, .size = {.y = 500}, - }); - auto & test = gameobject.add_component<ParticleEmitter>(ParticleEmitter::Data{ - .max_particles = 10, - .emission_rate = 100, - .end_lifespan = 100000, - .boundary{ - .width = 1000, - .height = 1000, - .offset = vec2{0, 0}, - .reset_on_exit = false, - }, - .sprite = test_sprite, - }); + } + ); + auto & test = gameobject.add_component<ParticleEmitter>( + test_sprite, + ParticleEmitter::Data { + .max_particles = 10, + .emission_rate = 100, + .end_lifespan = 100000, + .boundary { + .width = 1000, + .height = 1000, + .offset = vec2 {0, 0}, + .reset_on_exit = false, + }, + + } + ); } - render_sys.update(); + render_sys.frame_update(); this->game_object_count++; this->total_time = 0us; diff --git a/src/test/RenderSystemTest.cpp b/src/test/RenderSystemTest.cpp index b4519cb..bdd87ee 100644 --- a/src/test/RenderSystemTest.cpp +++ b/src/test/RenderSystemTest.cpp @@ -26,10 +26,10 @@ class RenderSystemTest : public Test { Mediator m; public: - ComponentManager mgr{m}; - SDLContext ctx{m}; - ResourceManager resource_manager{m}; - RenderSystem sys{m}; + ComponentManager mgr {m}; + SDLContext ctx {m}; + ResourceManager resource_manager {m}; + RenderSystem sys {m}; GameObject entity1 = this->mgr.new_object("name"); GameObject entity2 = this->mgr.new_object("name"); GameObject entity3 = this->mgr.new_object("name"); @@ -40,44 +40,52 @@ public: auto s2 = Asset("asset/texture/img.png"); auto s3 = Asset("asset/texture/img.png"); auto s4 = Asset("asset/texture/img.png"); - auto & sprite1 - = entity1.add_component<Sprite>(s1, Sprite::Data{ - .color = Color(0, 0, 0, 0), - .flip = Sprite::FlipSettings{false, false}, - .sorting_in_layer = 5, - .order_in_layer = 5, - .size = {10, 10}, - }); + auto & sprite1 = entity1.add_component<Sprite>( + s1, + Sprite::Data { + .color = Color(0, 0, 0, 0), + .flip = Sprite::FlipSettings {false, false}, + .sorting_in_layer = 5, + .order_in_layer = 5, + .size = {10, 10}, + } + ); EXPECT_EQ(sprite1.data.order_in_layer, 5); EXPECT_EQ(sprite1.data.sorting_in_layer, 5); - auto & sprite2 - = entity2.add_component<Sprite>(s2, Sprite::Data{ - .color = Color(0, 0, 0, 0), - .flip = Sprite::FlipSettings{false, false}, - .sorting_in_layer = 2, - .order_in_layer = 1, - }); + auto & sprite2 = entity2.add_component<Sprite>( + s2, + Sprite::Data { + .color = Color(0, 0, 0, 0), + .flip = Sprite::FlipSettings {false, false}, + .sorting_in_layer = 2, + .order_in_layer = 1, + } + ); EXPECT_EQ(sprite2.data.sorting_in_layer, 2); EXPECT_EQ(sprite2.data.order_in_layer, 1); - auto & sprite3 - = entity3.add_component<Sprite>(s3, Sprite::Data{ - .color = Color(0, 0, 0, 0), - .flip = Sprite::FlipSettings{false, false}, - .sorting_in_layer = 1, - .order_in_layer = 2, - }); + auto & sprite3 = entity3.add_component<Sprite>( + s3, + Sprite::Data { + .color = Color(0, 0, 0, 0), + .flip = Sprite::FlipSettings {false, false}, + .sorting_in_layer = 1, + .order_in_layer = 2, + } + ); EXPECT_EQ(sprite3.data.sorting_in_layer, 1); EXPECT_EQ(sprite3.data.order_in_layer, 2); - auto & sprite4 - = entity4.add_component<Sprite>(s4, Sprite::Data{ - .color = Color(0, 0, 0, 0), - .flip = Sprite::FlipSettings{false, false}, - .sorting_in_layer = 1, - .order_in_layer = 1, - }); + auto & sprite4 = entity4.add_component<Sprite>( + s4, + Sprite::Data { + .color = Color(0, 0, 0, 0), + .flip = Sprite::FlipSettings {false, false}, + .sorting_in_layer = 1, + .order_in_layer = 1, + } + ); EXPECT_EQ(sprite4.data.sorting_in_layer, 1); EXPECT_EQ(sprite4.data.order_in_layer, 1); } @@ -85,7 +93,7 @@ public: TEST_F(RenderSystemTest, NoCamera) { // No camera - EXPECT_ANY_THROW({ this->sys.update(); }); + EXPECT_ANY_THROW({ this->sys.frame_update(); }); } TEST_F(RenderSystemTest, make_sprites) {} @@ -128,8 +136,10 @@ TEST_F(RenderSystemTest, sorting_sprites) { } TEST_F(RenderSystemTest, Update) { - entity1.add_component<Camera>(ivec2{100, 100}, vec2{100, 100}, - Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); + entity1.add_component<Camera>( + ivec2 {100, 100}, vec2 {100, 100}, + Camera::Data {.bg_color = Color::WHITE, .zoom = 1.0f} + ); { vector<reference_wrapper<Sprite>> sprites = this->mgr.get_components_by_type<Sprite>(); ASSERT_EQ(sprites.size(), 4); @@ -139,7 +149,7 @@ TEST_F(RenderSystemTest, Update) { EXPECT_EQ(sprites[2].get().game_object_id, 2); EXPECT_EQ(sprites[3].get().game_object_id, 3); } - this->sys.update(); + this->sys.frame_update(); { vector<reference_wrapper<Sprite>> sprites = this->mgr.get_components_by_type<Sprite>(); ASSERT_EQ(sprites.size(), 4); @@ -157,8 +167,10 @@ TEST_F(RenderSystemTest, Camera) { EXPECT_NE(cameras.size(), 1); } { - entity1.add_component<Camera>(ivec2{100, 100}, vec2{100, 100}, - Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); + entity1.add_component<Camera>( + ivec2 {100, 100}, vec2 {100, 100}, + Camera::Data {.bg_color = Color::WHITE, .zoom = 1.0f} + ); auto cameras = this->mgr.get_components_by_type<Camera>(); EXPECT_EQ(cameras.size(), 1); @@ -167,8 +179,10 @@ TEST_F(RenderSystemTest, Camera) { //TODO improve with newer version } TEST_F(RenderSystemTest, Color) { - entity1.add_component<Camera>(ivec2{100, 100}, vec2{100, 100}, - Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); + entity1.add_component<Camera>( + ivec2 {100, 100}, vec2 {100, 100}, + Camera::Data {.bg_color = Color::WHITE, .zoom = 1.0f} + ); auto & sprite = this->mgr.get_components_by_id<Sprite>(entity1.id).front().get(); //ASSERT_NE(sprite.texture.texture.get(), nullptr); @@ -178,7 +192,7 @@ TEST_F(RenderSystemTest, Color) { EXPECT_EQ(sprite.data.color.g, Color::GREEN.g); EXPECT_EQ(sprite.data.color.b, Color::GREEN.b); EXPECT_EQ(sprite.data.color.a, Color::GREEN.a); - this->sys.update(); + this->sys.frame_update(); EXPECT_EQ(sprite.data.color.r, Color::GREEN.r); EXPECT_EQ(sprite.data.color.g, Color::GREEN.g); EXPECT_EQ(sprite.data.color.b, Color::GREEN.b); diff --git a/src/test/ReplayManagerTest.cpp b/src/test/ReplayManagerTest.cpp new file mode 100644 index 0000000..b2619eb --- /dev/null +++ b/src/test/ReplayManagerTest.cpp @@ -0,0 +1,38 @@ +#include <gtest/gtest.h> + +#include <crepe/api/BehaviorScript.h> +#include <crepe/api/Scene.h> +#include <crepe/api/Script.h> +#include <crepe/manager/ReplayManager.h> +#include <crepe/system/ReplaySystem.h> + +using namespace std; +using namespace crepe; +using namespace testing; + +class ReplayManagerTest : public Test { + Mediator mediator; + +public: + ComponentManager component_manager {mediator}; + ReplayManager replay_manager {mediator}; + ReplaySystem replay_system {mediator}; + + GameObject entity = component_manager.new_object("foo"); + Transform & entity_transform + = component_manager.get_components_by_id<Transform>(entity.id).back(); + Metadata & entity_metadata + = component_manager.get_components_by_id<Metadata>(entity.id).back(); +}; + +TEST_F(ReplayManagerTest, Default) { + // replay_manager.record_start(); + + // replay_system.fixed_update(); + // entity_transform.position += {1, 1}; + // replay_system.fixed_update(); + // entity_transform.position += {1, 1}; + // replay_system.fixed_update(); + + // recording_t recording = replay_manager.record_end(); +} diff --git a/src/test/ResourceManagerTest.cpp b/src/test/ResourceManagerTest.cpp index 965eeab..e5a7fad 100644 --- a/src/test/ResourceManagerTest.cpp +++ b/src/test/ResourceManagerTest.cpp @@ -16,14 +16,14 @@ class ResourceManagerTest : public Test { Mediator mediator; public: - ResourceManager resource_manager{mediator}; + ResourceManager resource_manager {mediator}; class Unrelated : public Resource { using Resource::Resource; }; - Asset asset_a{"asset/texture/img.png"}; - Asset asset_b{"asset/texture/ERROR.png"}; + Asset asset_a {"asset/texture/img.png"}; + Asset asset_b {"asset/texture/ERROR.png"}; class TestResource : public Resource { public: diff --git a/src/test/SaveManagerTest.cpp b/src/test/SaveManagerTest.cpp index 7609e69..fd53200 100644 --- a/src/test/SaveManagerTest.cpp +++ b/src/test/SaveManagerTest.cpp @@ -14,12 +14,12 @@ class SaveManagerTest : public Test { using SaveManager::SaveManager; // in-memory database for testing - DB db{}; + DB db {}; virtual DB & get_db() override { return this->db; } }; public: - TestSaveManager mgr{m}; + TestSaveManager mgr {m}; }; TEST_F(SaveManagerTest, ReadWrite) { diff --git a/src/test/SceneManagerTest.cpp b/src/test/SceneManagerTest.cpp index 480e07a..e58ce36 100644 --- a/src/test/SceneManagerTest.cpp +++ b/src/test/SceneManagerTest.cpp @@ -15,9 +15,9 @@ using namespace crepe; class ConcreteScene1 : public Scene { public: void load_scene() { - GameObject object1 = new_object("scene_1", "tag_scene_1", vec2{0, 0}, 0, 1); - GameObject object2 = new_object("scene_1", "tag_scene_1", vec2{1, 0}, 0, 1); - GameObject object3 = new_object("scene_1", "tag_scene_1", vec2{2, 0}, 0, 1); + GameObject object1 = new_object("scene_1", "tag_scene_1", vec2 {0, 0}, 0, 1); + GameObject object2 = new_object("scene_1", "tag_scene_1", vec2 {1, 0}, 0, 1); + GameObject object3 = new_object("scene_1", "tag_scene_1", vec2 {2, 0}, 0, 1); } string get_name() const { return "scene1"; } @@ -26,10 +26,10 @@ public: class ConcreteScene2 : public Scene { public: void load_scene() { - GameObject object1 = new_object("scene_2", "tag_scene_2", vec2{0, 0}, 0, 1); - GameObject object2 = new_object("scene_2", "tag_scene_2", vec2{0, 1}, 0, 1); - GameObject object3 = new_object("scene_2", "tag_scene_2", vec2{0, 2}, 0, 1); - GameObject object4 = new_object("scene_2", "tag_scene_2", vec2{0, 3}, 0, 1); + GameObject object1 = new_object("scene_2", "tag_scene_2", vec2 {0, 0}, 0, 1); + GameObject object2 = new_object("scene_2", "tag_scene_2", vec2 {0, 1}, 0, 1); + GameObject object3 = new_object("scene_2", "tag_scene_2", vec2 {0, 2}, 0, 1); + GameObject object4 = new_object("scene_2", "tag_scene_2", vec2 {0, 3}, 0, 1); } string get_name() const { return "scene2"; } @@ -40,7 +40,7 @@ public: ConcreteScene3(const string & name) : name(name) {} void load_scene() { - GameObject object1 = new_object("scene_3", "tag_scene_3", vec2{0, 0}, 0, 1); + GameObject object1 = new_object("scene_3", "tag_scene_3", vec2 {0, 0}, 0, 1); } string get_name() const { return name; } @@ -53,8 +53,8 @@ class SceneManagerTest : public ::testing::Test { Mediator m; public: - ComponentManager component_mgr{m}; - SceneManager scene_mgr{m}; + ComponentManager component_mgr {m}; + SceneManager scene_mgr {m}; }; TEST_F(SceneManagerTest, loadScene) { diff --git a/src/test/ScriptEventTest.cpp b/src/test/ScriptEventTest.cpp index c1b4028..8b4a72d 100644 --- a/src/test/ScriptEventTest.cpp +++ b/src/test/ScriptEventTest.cpp @@ -23,7 +23,7 @@ class ScriptEventTest : public ScriptTest { public: EventManager & event_manager = mediator.event_manager; - class MyEvent : public Event {}; + struct MyEvent : public Event {}; }; TEST_F(ScriptEventTest, Default) { @@ -37,7 +37,7 @@ TEST_F(ScriptEventTest, Default) { return true; }); - system.update(); + system.fixed_update(); behaviorscript.active = false; EXPECT_EQ(0, event_count); diff --git a/src/test/ScriptSaveManagerTest.cpp b/src/test/ScriptSaveManagerTest.cpp index 64403c4..e2debae 100644 --- a/src/test/ScriptSaveManagerTest.cpp +++ b/src/test/ScriptSaveManagerTest.cpp @@ -19,11 +19,11 @@ public: using SaveManager::SaveManager; // in-memory database for testing - DB db{}; + DB db {}; virtual DB & get_db() override { return this->db; } }; - TestSaveManager save_mgr{mediator}; + TestSaveManager save_mgr {mediator}; }; TEST_F(ScriptSaveManagerTest, GetSaveManager) { diff --git a/src/test/ScriptSceneTest.cpp b/src/test/ScriptSceneTest.cpp index 2568049..7d01f14 100644 --- a/src/test/ScriptSceneTest.cpp +++ b/src/test/ScriptSceneTest.cpp @@ -13,7 +13,7 @@ using namespace testing; class ScriptSceneTest : public ScriptTest { public: - SceneManager scene_manager{mediator}; + SceneManager scene_manager {mediator}; class MyScene : public Scene {}; }; diff --git a/src/test/ScriptTest.cpp b/src/test/ScriptTest.cpp index acdae70..40aa25c 100644 --- a/src/test/ScriptTest.cpp +++ b/src/test/ScriptTest.cpp @@ -28,7 +28,8 @@ void ScriptTest::SetUp() { TEST_F(ScriptTest, Default) { MyScript & script = this->script; EXPECT_CALL(script, init()).Times(0); - EXPECT_CALL(script, update()).Times(0); + EXPECT_CALL(script, fixed_update(_)).Times(0); + EXPECT_CALL(script, frame_update(_)).Times(0); } TEST_F(ScriptTest, UpdateOnce) { @@ -38,16 +39,23 @@ TEST_F(ScriptTest, UpdateOnce) { InSequence seq; EXPECT_CALL(script, init()).Times(1); - EXPECT_CALL(script, update()).Times(1); - system.update(); + EXPECT_CALL(script, fixed_update(_)).Times(1); + system.fixed_update(); } { InSequence seq; EXPECT_CALL(script, init()).Times(0); - EXPECT_CALL(script, update()).Times(1); - system.update(); + EXPECT_CALL(script, fixed_update(_)).Times(1); + system.fixed_update(); + } + + { + InSequence seq; + + EXPECT_CALL(script, frame_update(_)).Times(1); + system.frame_update(); } } @@ -59,17 +67,29 @@ TEST_F(ScriptTest, UpdateInactive) { InSequence seq; EXPECT_CALL(script, init()).Times(0); - EXPECT_CALL(script, update()).Times(0); + EXPECT_CALL(script, fixed_update(_)).Times(0); behaviorscript.active = false; - system.update(); + system.fixed_update(); } { InSequence seq; EXPECT_CALL(script, init()).Times(1); - EXPECT_CALL(script, update()).Times(1); + EXPECT_CALL(script, fixed_update(_)).Times(1); behaviorscript.active = true; - system.update(); + system.fixed_update(); } } + +TEST_F(ScriptTest, SaveManager) { + MyScript & script = this->script; + + EXPECT_EQ(&script.get_save_manager(), &this->save_manager); +} + +TEST_F(ScriptTest, LoopTimerManager) { + MyScript & script = this->script; + + EXPECT_EQ(&script.get_loop_timer(), &this->loop_timer); +} diff --git a/src/test/ScriptTest.h b/src/test/ScriptTest.h index 31fa7c9..f953aab 100644 --- a/src/test/ScriptTest.h +++ b/src/test/ScriptTest.h @@ -7,16 +7,21 @@ #include <crepe/api/Script.h> #include <crepe/manager/ComponentManager.h> #include <crepe/manager/EventManager.h> +#include <crepe/manager/LoopTimerManager.h> +#include <crepe/manager/SaveManager.h> #include <crepe/system/ScriptSystem.h> + class ScriptTest : public testing::Test { protected: crepe::Mediator mediator; static constexpr const char * OBJ_NAME = "foo"; public: - crepe::ComponentManager component_manager{mediator}; - crepe::ScriptSystem system{mediator}; - crepe::EventManager event_mgr{mediator}; + crepe::ComponentManager component_manager {mediator}; + crepe::ScriptSystem system {mediator}; + crepe::EventManager event_mgr {mediator}; + crepe::LoopTimerManager loop_timer {mediator}; + crepe::SaveManager save_manager {mediator}; crepe::GameObject entity = component_manager.new_object(OBJ_NAME); class MyScript : public crepe::Script { @@ -24,7 +29,8 @@ public: public: MOCK_METHOD(void, init, (), (override)); - MOCK_METHOD(void, update, (), (override)); + MOCK_METHOD(void, fixed_update, (crepe::duration_t), (override)); + MOCK_METHOD(void, frame_update, (crepe::duration_t), (override)); }; crepe::OptionalRef<crepe::BehaviorScript> behaviorscript; diff --git a/src/test/ValueBrokerTest.cpp b/src/test/ValueBrokerTest.cpp index e6bb058..5928c37 100644 --- a/src/test/ValueBrokerTest.cpp +++ b/src/test/ValueBrokerTest.cpp @@ -13,7 +13,7 @@ public: int write_count = 0; int value = 0; - ValueBroker<int> broker{ + ValueBroker<int> broker { [this](const int & target) -> void { this->write_count++; this->value = target; @@ -23,7 +23,7 @@ public: return this->value; }, }; - Proxy<int> proxy{broker}; + Proxy<int> proxy {broker}; void SetUp() override { ASSERT_EQ(read_count, 0); diff --git a/src/test/Vector2Test.cpp b/src/test/Vector2Test.cpp index 1e21af9..b17f95a 100644 --- a/src/test/Vector2Test.cpp +++ b/src/test/Vector2Test.cpp @@ -1,6 +1,7 @@ #include <gtest/gtest.h> #include <crepe/api/Vector2.h> +#include <crepe/types.h> using namespace crepe; @@ -530,3 +531,12 @@ TEST_F(Vector2Test, Perpendicular) { EXPECT_FLOAT_EQ(result4.x, -4.0f); EXPECT_FLOAT_EQ(result4.y, 3.0f); } + +TEST_F(Vector2Test, Rotate) { + vec2 foo {0, 1}; + + foo = foo.rotate(90); + const float GOOD_ENOUGH = 0.001; + EXPECT_NEAR(foo.x, 1, GOOD_ENOUGH); + EXPECT_NEAR(foo.y, 0, GOOD_ENOUGH); +} |