Explorar o código

Test scene::in_bounds.

Sam Jaffe %!s(int64=6) %!d(string=hai) anos
pai
achega
0060cbfe42
Modificáronse 3 ficheiros con 42 adicións e 2 borrados
  1. 1 1
      engine/include/game/engine/scene.hpp
  2. 1 1
      engine/src/scene.cpp
  3. 40 0
      engine/test/scene_test.cxx

+ 1 - 1
engine/include/game/engine/scene.hpp

@@ -36,7 +36,7 @@ namespace engine {
   protected:
     graphics::manager const & graphics_manager() const;
     void change_scene(std::string const & scene_id);
-    bool in_bounds(math::dim2::point c) const;
+    bool in_bounds(math::dim2::point const & c) const;
     bool in_bounds(collidable * c) const;
     void check_collisions();
     static void check_collisions(std::vector<collidable *> const & from,

+ 1 - 1
engine/src/scene.cpp

@@ -24,7 +24,7 @@ scene::scene(std::string const & str, math::vec2 const & bounds,
 
 scene::~scene() {}
 
-bool scene::in_bounds(math::dim2::point p) const {
+bool scene::in_bounds(math::dim2::point const & p) const {
   return math::between(p[0], 0.f, local_bounds_[0]) &&
          math::between(p[1], 0.f, local_bounds_[1]);
 }

+ 40 - 0
engine/test/scene_test.cxx

@@ -25,6 +25,8 @@ struct test_scene : engine::scene {
   void render() override {}
   void handle_key_event(engine::event::key_event) override {}
   void handle_mouse_event(engine::event::mouse_event) override {}
+  bool in_bounds(math::dim2::point c) const { return engine::scene::in_bounds(c); }
+  bool in_bounds(engine::collidable * c) const { return engine::scene::in_bounds(c); }
 
   void add_with(engine::collision_t t, engine::collidable & c) {
     colliders[t].push_back(&c);
@@ -67,6 +69,7 @@ void SceneTest::TearDown() {
   renderer_.reset();
 }
 
+using testing::Eq;
 using testing::Ref;
 
 TEST_F(SceneTest, WillCollideOverlappingObjects) {
@@ -152,3 +155,40 @@ TEST_F(SceneTest, DoubleCollisionCanOccur) {
   EXPECT_CALL(one, collide(Ref(two))).Times(2);
   scene().update(0.f);
 }
+
+struct BoundsTest : SceneTest, testing::WithParamInterface<math::vec2> {};
+
+TEST_F(BoundsTest, BoundsCheckIsPointInScreenZone) {
+  // Confirm that the size is as expected...
+  EXPECT_THAT(scene().size(), Eq(make_vector(1600.f, 900.f)));
+  
+  // Lower-Left Corner
+  EXPECT_FALSE(scene().in_bounds(make_vector(  -1.f,   0.f)));
+  EXPECT_FALSE(scene().in_bounds(make_vector(   0.f,  -1.f)));
+  EXPECT_TRUE(scene().in_bounds(make_vector(0.f, 0.f)));
+  // Lower-Right Corner
+  EXPECT_FALSE(scene().in_bounds(make_vector(1600.f,  -1.f)));
+  EXPECT_FALSE(scene().in_bounds(make_vector(1601.f,   0.f)));
+  EXPECT_TRUE(scene().in_bounds(make_vector(1600.f,   0.f)));
+  // Upper-Right Corner
+  EXPECT_FALSE(scene().in_bounds(make_vector(1601.f, 900.f)));
+  EXPECT_FALSE(scene().in_bounds(make_vector(1600.f, 901.f)));
+  EXPECT_TRUE(scene().in_bounds(make_vector(1600.f, 900.f)));
+  // Upper-Left Corner
+  EXPECT_FALSE(scene().in_bounds(make_vector(   0.f, 901.f)));
+  EXPECT_FALSE(scene().in_bounds(make_vector(  -1.f, 900.f)));
+  EXPECT_TRUE(scene().in_bounds(make_vector(   0.f, 900.f)));
+}
+
+TEST_P(BoundsTest, BoundsCheckOnCollidableIsAnyPointInBounds) {
+  math::dim2::square bounds = {GetParam(), 2.f};
+  engine::collidable collide{{{}, bounds, cast<graphics::material>(1), {}}};
+  
+  EXPECT_TRUE(scene().in_bounds(&collide));
+}
+
+INSTANTIATE_TEST_CASE_P(Collidable, BoundsTest,
+                        ::testing::Values(make_vector(  -1.f,  -1.f),
+                                          make_vector(1599.f,  -1.f),
+                                          make_vector(1599.f, 899.f),
+                                          make_vector(  -1.f, 899.f)));