aboutsummaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authormax-001 <maxsmits21@kpnmail.nl>2024-12-16 17:54:06 +0100
committermax-001 <maxsmits21@kpnmail.nl>2024-12-16 17:54:06 +0100
commitf0cda55f4336672e655420a22aaab3ccb7331e36 (patch)
treed41b8d97d9eb89d1d13c05877b5ce002d28ce659 /src/test
parentccc7dd1f167daf869fb91118bd3fca8ea796fc8d (diff)
parent876896e50711509e80ef551b4e8ad440e8039b97 (diff)
Merge remote-tracking branch 'origin/master' into max/game
Diffstat (limited to 'src/test')
-rw-r--r--src/test/CollisionTest.cpp12
-rw-r--r--src/test/LoopTimerTest.cpp18
-rw-r--r--src/test/PhysicsTest.cpp46
-rw-r--r--src/test/SceneManagerTest.cpp22
4 files changed, 57 insertions, 41 deletions
diff --git a/src/test/CollisionTest.cpp b/src/test/CollisionTest.cpp
index 5dbc670..2ad65fa 100644
--- a/src/test/CollisionTest.cpp
+++ b/src/test/CollisionTest.cpp
@@ -232,7 +232,7 @@ TEST_F(CollisionTest, collision_box_box_dynamic_x_direction) {
collision_happend = true;
EXPECT_EQ(ev.info.this_collider.game_object_id, 1);
EXPECT_EQ(ev.info.resolution.x, -5);
- EXPECT_EQ(ev.info.resolution.y, -5);
+ EXPECT_EQ(ev.info.resolution.y, 5);
EXPECT_EQ(ev.info.resolution_direction,
crepe::CollisionSystem::Direction::X_DIRECTION);
};
@@ -240,7 +240,7 @@ TEST_F(CollisionTest, collision_box_box_dynamic_x_direction) {
collision_happend = true;
EXPECT_EQ(ev.info.this_collider.game_object_id, 2);
EXPECT_EQ(ev.info.resolution.x, 5);
- EXPECT_EQ(ev.info.resolution.y, 5);
+ EXPECT_EQ(ev.info.resolution.y, -5);
EXPECT_EQ(ev.info.resolution_direction,
crepe::CollisionSystem::Direction::X_DIRECTION);
};
@@ -260,7 +260,7 @@ TEST_F(CollisionTest, collision_box_box_dynamic_y_direction) {
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.resolution.x, -5);
+ 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);
@@ -268,7 +268,7 @@ TEST_F(CollisionTest, collision_box_box_dynamic_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.resolution.x, 5);
+ 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);
@@ -312,7 +312,7 @@ TEST_F(CollisionTest, collision_box_box_static_x_direction) {
collision_happend = true;
EXPECT_EQ(ev.info.this_collider.game_object_id, 1);
EXPECT_EQ(ev.info.resolution.x, -5);
- EXPECT_EQ(ev.info.resolution.y, -5);
+ EXPECT_EQ(ev.info.resolution.y, 5);
EXPECT_EQ(ev.info.resolution_direction,
crepe::CollisionSystem::Direction::X_DIRECTION);
};
@@ -336,7 +336,7 @@ TEST_F(CollisionTest, collision_box_box_static_y_direction) {
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.resolution.x, -5);
+ 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);
diff --git a/src/test/LoopTimerTest.cpp b/src/test/LoopTimerTest.cpp
index d76bf45..7bd6305 100644
--- a/src/test/LoopTimerTest.cpp
+++ b/src/test/LoopTimerTest.cpp
@@ -30,7 +30,7 @@ TEST_F(LoopTimerTest, EnforcesTargetFrameRate) {
auto elapsed_ms = duration_cast<milliseconds>(elapsed_time).count();
// For 60 FPS, the target frame time is around 16.67ms
- ASSERT_NEAR(elapsed_ms, 16.7, 1);
+ ASSERT_NEAR(elapsed_ms, 16.7, 5);
}
TEST_F(LoopTimerTest, SetTargetFps) {
@@ -79,3 +79,19 @@ TEST_F(LoopTimerTest, DISABLED_getCurrentTime) {
ASSERT_NEAR(loop_timer.get_elapsed_time().count(), elapsed_time, 5);
}
+TEST_F(LoopTimerTest, getFPS) {
+ // Set the target FPS to 60 (which gives a target time per frame of ~16.67 ms)
+ loop_timer.set_target_framerate(60);
+
+ auto start_time = steady_clock::now();
+ loop_timer.enforce_frame_rate();
+
+ auto elapsed_time = steady_clock::now() - start_time;
+ loop_timer.update();
+ unsigned int fps = loop_timer.get_fps();
+ auto elapsed_ms = duration_cast<milliseconds>(elapsed_time).count();
+
+ // For 60 FPS, the target frame time is around 16.67ms
+ ASSERT_NEAR(elapsed_ms, 16.7, 1);
+ ASSERT_NEAR(fps, 60, 2);
+}
diff --git a/src/test/PhysicsTest.cpp b/src/test/PhysicsTest.cpp
index 43d2931..3afb3c7 100644
--- a/src/test/PhysicsTest.cpp
+++ b/src/test/PhysicsTest.cpp
@@ -3,6 +3,8 @@
#include <crepe/api/Rigidbody.h>
#include <crepe/api/Transform.h>
#include <crepe/manager/ComponentManager.h>
+#include <crepe/manager/LoopTimerManager.h>
+#include <crepe/manager/Mediator.h>
#include <crepe/system/PhysicsSystem.h>
#include <gtest/gtest.h>
@@ -16,6 +18,7 @@ class PhysicsTest : public ::testing::Test {
public:
ComponentManager component_manager{m};
PhysicsSystem system{m};
+ LoopTimerManager loop_timer{m};
void SetUp() override {
ComponentManager & mgr = this->component_manager;
@@ -27,7 +30,7 @@ public:
.mass = 1,
.gravity_scale = 1,
.body_type = Rigidbody::BodyType::DYNAMIC,
- .max_linear_velocity = vec2{10, 10},
+ .max_linear_velocity = 10,
.max_angular_velocity = 10,
.constraints = {0, 0},
});
@@ -55,39 +58,40 @@ TEST_F(PhysicsTest, gravity) {
EXPECT_EQ(transform.position.y, 0);
system.update();
- EXPECT_EQ(transform.position.y, 1);
+ EXPECT_NEAR(transform.position.y, 0.0004, 0.0001);
system.update();
- EXPECT_EQ(transform.position.y, 3);
+ EXPECT_NEAR(transform.position.y, 0.002, 0.001);
}
TEST_F(PhysicsTest, max_velocity) {
ComponentManager & mgr = this->component_manager;
vector<reference_wrapper<Rigidbody>> rigidbodies = mgr.get_components_by_id<Rigidbody>(0);
Rigidbody & rigidbody = rigidbodies.front().get();
+ rigidbody.data.gravity_scale = 0;
ASSERT_FALSE(rigidbodies.empty());
EXPECT_EQ(rigidbody.data.linear_velocity.y, 0);
rigidbody.add_force_linear({100, 100});
rigidbody.add_force_angular(100);
system.update();
- EXPECT_EQ(rigidbody.data.linear_velocity.y, 10);
- EXPECT_EQ(rigidbody.data.linear_velocity.x, 10);
+ 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();
- EXPECT_EQ(rigidbody.data.linear_velocity.y, -10);
- EXPECT_EQ(rigidbody.data.linear_velocity.x, -10);
+ 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);
}
TEST_F(PhysicsTest, movement) {
- Config::get_instance().physics.gravity = 0;
ComponentManager & mgr = this->component_manager;
vector<reference_wrapper<Rigidbody>> rigidbodies = mgr.get_components_by_id<Rigidbody>(0);
Rigidbody & rigidbody = rigidbodies.front().get();
+ rigidbody.data.gravity_scale = 0;
vector<reference_wrapper<Transform>> transforms = mgr.get_components_by_id<Transform>(0);
const Transform & transform = transforms.front().get();
ASSERT_FALSE(rigidbodies.empty());
@@ -96,31 +100,33 @@ TEST_F(PhysicsTest, movement) {
rigidbody.add_force_linear({1, 1});
rigidbody.add_force_angular(1);
system.update();
- EXPECT_EQ(transform.position.x, 1);
- EXPECT_EQ(transform.position.y, 1);
- EXPECT_EQ(transform.rotation, 1);
+ 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);
rigidbody.data.constraints = {1, 1, 1};
- EXPECT_EQ(transform.position.x, 1);
- EXPECT_EQ(transform.position.y, 1);
- EXPECT_EQ(transform.rotation, 1);
-
+ 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);
+ rigidbody.data.constraints = {0, 0, 0};
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();
- 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);
+ 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);
rigidbody.data.constraints = {1, 1, 0};
rigidbody.data.angular_velocity_coefficient = 0;
rigidbody.data.max_angular_velocity = 1000;
rigidbody.data.angular_velocity = 360;
system.update();
- EXPECT_EQ(transform.rotation, 1);
+ EXPECT_NEAR(transform.rotation, 7.24, 0.01);
rigidbody.data.angular_velocity = -360;
system.update();
- EXPECT_EQ(transform.rotation, 1);
+ EXPECT_NEAR(transform.rotation, 0.04, 0.001);
+ system.update();
+ EXPECT_NEAR(transform.rotation, 352.84, 0.01);
}
diff --git a/src/test/SceneManagerTest.cpp b/src/test/SceneManagerTest.cpp
index 9bb260c..480e07a 100644
--- a/src/test/SceneManagerTest.cpp
+++ b/src/test/SceneManagerTest.cpp
@@ -15,11 +15,9 @@ using namespace crepe;
class ConcreteScene1 : public Scene {
public:
void load_scene() {
- Mediator & mediator = this->mediator;
- ComponentManager & mgr = mediator.component_manager;
- GameObject object1 = mgr.new_object("scene_1", "tag_scene_1", vec2{0, 0}, 0, 1);
- GameObject object2 = mgr.new_object("scene_1", "tag_scene_1", vec2{1, 0}, 0, 1);
- GameObject object3 = mgr.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"; }
@@ -28,12 +26,10 @@ public:
class ConcreteScene2 : public Scene {
public:
void load_scene() {
- Mediator & mediator = this->mediator;
- ComponentManager & mgr = mediator.component_manager;
- GameObject object1 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 0}, 0, 1);
- GameObject object2 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 1}, 0, 1);
- GameObject object3 = mgr.new_object("scene_2", "tag_scene_2", vec2{0, 2}, 0, 1);
- GameObject object4 = mgr.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"; }
@@ -44,9 +40,7 @@ public:
ConcreteScene3(const string & name) : name(name) {}
void load_scene() {
- Mediator & mediator = this->mediator;
- ComponentManager & mgr = mediator.component_manager;
- GameObject object1 = mgr.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; }