From ab7773c0e9d03cb98d134ac7a7c0bb729140be3a Mon Sep 17 00:00:00 2001
From: jwendt <wendt@vr.rwth-aachen.de>
Date: Tue, 24 Jul 2018 11:19:13 +0200
Subject: [PATCH] use resourcecomponent instead of resourcepointer directly as
 component

---
 library/phx/core/component.hpp                |  4 +-
 .../phx/input/openvr_controller_system.cpp    |  5 +-
 .../components/resource_component.cpp         | 30 +++++++++
 .../components/resource_component.hpp         | 64 +++++++++++++++++++
 library/phx/rendering/rendering_system.cpp    |  9 ++-
 .../phx/resources/loaders/scene_loader.cpp    |  5 +-
 library/phx/resources/resource_pointer.hpp    |  3 +-
 .../src/integration_test_model_rendering.cpp  |  5 +-
 ...ation_test_opengl_buffer_data_download.cpp |  5 +-
 tests/src/integration_test_rendering.cpp      |  5 +-
 tests/src/test_openvr_controller_system.cpp   |  9 ++-
 tests/src/test_resource_pointer.cpp           |  8 ++-
 tests/src/test_scene_loader.cpp               |  3 +-
 13 files changed, 129 insertions(+), 26 deletions(-)
 create mode 100644 library/phx/rendering/components/resource_component.cpp
 create mode 100644 library/phx/rendering/components/resource_component.hpp

diff --git a/library/phx/core/component.hpp b/library/phx/core/component.hpp
index 67261e88..e80fdc57 100644
--- a/library/phx/core/component.hpp
+++ b/library/phx/core/component.hpp
@@ -45,10 +45,10 @@ class PHOENIX_EXPORT Component : public Loggable {
 
  protected:
   Component() = default;
-  Component(const Component&) = default;
+  Component(const Component&) = delete;
   Component(Component&&) = default;
 
-  Component& operator=(const Component&) = default;
+  Component& operator=(const Component&) = delete;
   Component& operator=(Component&&) = default;
 
  private:
diff --git a/library/phx/input/openvr_controller_system.cpp b/library/phx/input/openvr_controller_system.cpp
index e30451dc..aa92e579 100644
--- a/library/phx/input/openvr_controller_system.cpp
+++ b/library/phx/input/openvr_controller_system.cpp
@@ -29,6 +29,7 @@
 #include "phx/core/logger.hpp"
 #include "phx/display/display_system_openvr.hpp"
 #include "phx/input/openvr_controller_behavior.hpp"
+#include "phx/rendering/components/resource_component.hpp"
 #include "phx/rendering/components/transform.hpp"
 #include "phx/resources/loaders/openvr_resource_loader.hpp"
 #include "phx/resources/resource_manager.hpp"
@@ -84,9 +85,9 @@ Entity* OpenVRControllerSystem::AddControllerEntity(
     phx::Scene* scene, ResourcePointer<Mesh> mesh,
     ResourcePointer<Material> material, OpenVRControllerBehavior::Side side) {
   Entity* controller = scene->CreateEntity();
-  controller->AddComponent<ResourcePointer<Mesh>>(mesh);
+  controller->AddComponent<ResourceComponent<Mesh>>(mesh);
   controller->AddComponent<Transform>();
-  controller->AddComponent<ResourcePointer<Material>>(material);
+  controller->AddComponent<ResourceComponent<Material>>(material);
   controller->AddComponent<OpenVRControllerBehavior>()->SetSide(side);
 
   return controller;
diff --git a/library/phx/rendering/components/resource_component.cpp b/library/phx/rendering/components/resource_component.cpp
new file mode 100644
index 00000000..507c1974
--- /dev/null
+++ b/library/phx/rendering/components/resource_component.cpp
@@ -0,0 +1,30 @@
+//------------------------------------------------------------------------------
+// Project Phoenix
+//
+// Copyright (c) 2017-2018 RWTH Aachen University, Germany,
+// Virtual Reality & Immersive Visualization Group.
+//------------------------------------------------------------------------------
+//                                 License
+//
+// Licensed under the 3-Clause BSD License (the "License");
+// you may not use this file except in compliance with the License.
+// See the file LICENSE for the full text.
+// You may obtain a copy of the License at
+//
+//     https://opensource.org/licenses/BSD-3-Clause
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//------------------------------------------------------------------------------
+
+#include "phx/rendering/components/resource_component.hpp"
+
+#include <string>
+
+namespace phx {
+
+
+}  // namespace phx
diff --git a/library/phx/rendering/components/resource_component.hpp b/library/phx/rendering/components/resource_component.hpp
new file mode 100644
index 00000000..30671dc1
--- /dev/null
+++ b/library/phx/rendering/components/resource_component.hpp
@@ -0,0 +1,64 @@
+//------------------------------------------------------------------------------
+// Project Phoenix
+//
+// Copyright (c) 2017-2018 RWTH Aachen University, Germany,
+// Virtual Reality & Immersive Visualization Group.
+//------------------------------------------------------------------------------
+//                                 License
+//
+// Licensed under the 3-Clause BSD License (the "License");
+// you may not use this file except in compliance with the License.
+// See the file LICENSE for the full text.
+// You may obtain a copy of the License at
+//
+//     https://opensource.org/licenses/BSD-3-Clause
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//------------------------------------------------------------------------------
+
+#ifndef LIBRARY_PHX_RENDERING_COMPONENTS_RESOURCE_COMPONENT_HPP_
+#define LIBRARY_PHX_RENDERING_COMPONENTS_RESOURCE_COMPONENT_HPP_
+
+#include <string>
+
+#include "phx/core/component.hpp"
+#include "phx/export.hpp"
+#include "phx/resources/resource_pointer.hpp"
+
+namespace phx {
+
+template <class ResourceType>
+class PHOENIX_EXPORT ResourceComponent final : public Component {
+ public:
+  std::string ToString() const override { return "Resource Pointer Component"; }
+
+  ResourcePointer<ResourceType> GetResourcePointer() {
+    return resource_pointer_;
+  }
+
+  void SetResourcePointer(ResourcePointer<ResourceType> pointer) {
+    resource_pointer_ = pointer;
+  }
+
+ protected:
+  friend Entity;
+  ResourceComponent() = default;
+  explicit ResourceComponent(ResourcePointer<ResourceType> pointer)
+      : resource_pointer_(pointer) {}
+  ResourceComponent(const ResourceComponent&) = delete;
+  ResourceComponent(ResourceComponent&&) = default;
+
+  ResourceComponent& operator=(const ResourceComponent&) = delete;
+  ResourceComponent& operator=(ResourceComponent&&) = default;
+
+ private:
+  ResourcePointer<ResourceType> resource_pointer_;
+};
+
+}  // namespace phx
+
+#endif  // LIBRARY_PHX_RENDERING_COMPONENTS_RESOURCE_COMPONENT_HPP_
diff --git a/library/phx/rendering/rendering_system.cpp b/library/phx/rendering/rendering_system.cpp
index fd4bb1cb..c10e4201 100644
--- a/library/phx/rendering/rendering_system.cpp
+++ b/library/phx/rendering/rendering_system.cpp
@@ -32,6 +32,7 @@
 #include "phx/core/scene.hpp"
 #include "phx/core/system.hpp"
 #include "phx/rendering/components/light.hpp"
+#include "phx/rendering/components/resource_component.hpp"
 #include "phx/rendering/components/transform.hpp"
 #include "phx/resources/resource_pointer.hpp"
 #include "phx/resources/types/material.hpp"
@@ -74,12 +75,14 @@ void RenderingSystem::Update(const FrameTimer::TimeInfo&) {
     auto light = entity->GetFirstComponent<Light>();
     auto transform = entity->GetFirstComponent<Transform>();
     const auto material =
-        entity->GetFirstComponent<ResourcePointer<Material>>();
-    const auto mesh = entity->GetFirstComponent<ResourcePointer<Mesh>>();
+        entity->GetFirstComponent<ResourceComponent<Material>>();
+    const auto mesh = entity->GetFirstComponent<ResourceComponent<Mesh>>();
     if (transform != nullptr) {
       if (mesh != nullptr) {
         rendering_instances.push_back(
-            {mesh->Get(), (material != nullptr ? material->Get() : nullptr),
+            {mesh->GetResourcePointer().Get(),
+             (material != nullptr ? material->GetResourcePointer().Get()
+                                  : nullptr),
              transform});
       } else if (light != nullptr) {
         light_transform_pairs.push_back(
diff --git a/library/phx/resources/loaders/scene_loader.cpp b/library/phx/resources/loaders/scene_loader.cpp
index d8757f07..0e353229 100644
--- a/library/phx/resources/loaders/scene_loader.cpp
+++ b/library/phx/resources/loaders/scene_loader.cpp
@@ -24,6 +24,7 @@
 
 #include <string>
 
+#include "phx/rendering/components/resource_component.hpp"
 #include "phx/rendering/components/transform.hpp"
 #include "phx/resources/resource_pointer.hpp"
 #include "phx/resources/resource_utils.hpp"
@@ -47,10 +48,10 @@ bool SceneLoader::InsertModelIntoScene(const std::string& file_name,
   for (auto mesh : model->GetMeshes()) {
     auto entity = scene->CreateEntity();
     entity->AddComponent<Transform>()->SetParent(root_transform);
-    entity->AddComponent<ResourcePointer<Mesh>>(mesh);
+    entity->AddComponent<ResourceComponent<Mesh>>(mesh);
     ResourcePointer<Material> material = model->GetMaterialForMesh(mesh);
     if (material != nullptr) {
-      entity->AddComponent<ResourcePointer<Material>>(material);
+      entity->AddComponent<ResourceComponent<Material>>(material);
     }
   }
 
diff --git a/library/phx/resources/resource_pointer.hpp b/library/phx/resources/resource_pointer.hpp
index 02c59504..123f9a56 100644
--- a/library/phx/resources/resource_pointer.hpp
+++ b/library/phx/resources/resource_pointer.hpp
@@ -25,14 +25,13 @@
 
 #include <cstddef>
 
-#include "phx/core/component.hpp"
 #include "phx/export.hpp"
 #include "phx/resources/resource_proxy.hpp"
 
 namespace phx {
 
 template <class ResourceType>
-class PHOENIX_EXPORT ResourcePointer : public Component {
+class PHOENIX_EXPORT ResourcePointer {
  public:
   ResourcePointer() : proxy_(nullptr) {}
   explicit ResourcePointer(std::nullptr_t) : proxy_(nullptr) {}
diff --git a/tests/src/integration_test_model_rendering.cpp b/tests/src/integration_test_model_rendering.cpp
index f8ebd5fb..28450005 100644
--- a/tests/src/integration_test_model_rendering.cpp
+++ b/tests/src/integration_test_model_rendering.cpp
@@ -32,6 +32,7 @@
 #include "phx/display/display_system_window.hpp"
 #include "phx/display/window.hpp"
 #include "phx/rendering/backend/opengl_image_buffer_data.hpp"
+#include "phx/rendering/components/resource_component.hpp"
 #include "phx/rendering/rendering_system.hpp"
 #include "phx/resources/loaders/assimp_model_loader.hpp"
 #include "phx/resources/resource_manager.hpp"
@@ -57,11 +58,11 @@ phx::Entity* LoadBunny(glm::vec3 pos, float angleDeg,
 
   auto bunny_mesh = phx::ResourceUtils::LoadResourceFromFile<phx::Mesh>(
       "models/bunny.obj", {{"mesh_index", 0}});
-  bunny->AddComponent<phx::ResourcePointer<phx::Mesh>>(bunny_mesh);
+  bunny->AddComponent<phx::ResourceComponent<phx::Mesh>>(bunny_mesh);
 
   auto bunny_material = phx::ResourceUtils::LoadResourceFromFile<phx::Material>(
       "models/bunny.obj", {{"material_name", material_name}});
-  bunny->AddComponent<phx::ResourcePointer<phx::Material>>(bunny_material);
+  bunny->AddComponent<phx::ResourceComponent<phx::Material>>(bunny_material);
 
   phx::Transform* bunny_transform = bunny->AddComponent<phx::Transform>();
   bunny_transform->SetLocalTranslation(pos);
diff --git a/tests/src/integration_test_opengl_buffer_data_download.cpp b/tests/src/integration_test_opengl_buffer_data_download.cpp
index 9e809577..dee66eac 100644
--- a/tests/src/integration_test_opengl_buffer_data_download.cpp
+++ b/tests/src/integration_test_opengl_buffer_data_download.cpp
@@ -34,6 +34,7 @@
 #include "phx/core/scene.hpp"
 #include "phx/display/window.hpp"
 #include "phx/rendering/backend/opengl_image_buffer_data.hpp"
+#include "phx/rendering/components/resource_component.hpp"
 #include "phx/rendering/rendering_system.hpp"
 #include "phx/resources/resource_manager.hpp"
 #include "phx/resources/resource_pointer.hpp"
@@ -98,7 +99,7 @@ class SceneSetupSimple {
             {{"TYPE", "custommaterial"}});
     material.Load();
 
-    triangle->AddComponent<phx::ResourcePointer<phx::Material>>(material);
+    triangle->AddComponent<phx::ResourceComponent<phx::Material>>(material);
 
     std::vector<glm::vec3> vertices = {
         {-4.0f, -3.0f, -0.05f}, {4.0f, -3.0f, -0.05f}, {4.0f, 3.0f, -0.05f}};
@@ -114,7 +115,7 @@ class SceneSetupSimple {
         {{"TYPE", "staticmesh"}});
     mesh.Load();
 
-    triangle->AddComponent<phx::ResourcePointer<phx::Mesh>>(mesh);
+    triangle->AddComponent<phx::ResourceComponent<phx::Mesh>>(mesh);
 
     phx::Entity* main_light = scene->CreateEntity();
     main_light->AddComponent<phx::Transform>();
diff --git a/tests/src/integration_test_rendering.cpp b/tests/src/integration_test_rendering.cpp
index 1783f13e..dcb7a9b0 100644
--- a/tests/src/integration_test_rendering.cpp
+++ b/tests/src/integration_test_rendering.cpp
@@ -33,6 +33,7 @@
 #include "phx/display/display_system_window.hpp"
 #include "phx/display/window.hpp"
 #include "phx/rendering/backend/opengl_image_buffer_data.hpp"
+#include "phx/rendering/components/resource_component.hpp"
 #include "phx/rendering/rendering_system.hpp"
 #include "phx/resources/resource_manager.hpp"
 #include "phx/resources/types/mesh.hpp"
@@ -88,7 +89,7 @@ void CreateTestTriangleComponent(phx::Entity* triangle) {
       {{"TYPE", "staticmesh"}});
   mesh.Load();
 
-  triangle->AddComponent<phx::ResourcePointer<phx::Mesh>>(mesh);
+  triangle->AddComponent<phx::ResourceComponent<phx::Mesh>>(mesh);
 }
 
 SUPPRESS_WARNINGS_END
@@ -116,7 +117,7 @@ SCENARIO("We can render a simple triangle", "[phx][phx::Rendering]") {
     material.Load();
 
     triangle->AddComponent<phx::Transform>();
-    triangle->AddComponent<phx::ResourcePointer<phx::Material>>(material);
+    triangle->AddComponent<phx::ResourceComponent<phx::Material>>(material);
 
     WHEN("We render the scene") {
       rendering_system->Update(phx::FrameTimer::TimeInfo{});
diff --git a/tests/src/test_openvr_controller_system.cpp b/tests/src/test_openvr_controller_system.cpp
index 0e2a039c..5a8d237a 100644
--- a/tests/src/test_openvr_controller_system.cpp
+++ b/tests/src/test_openvr_controller_system.cpp
@@ -37,6 +37,7 @@ SUPPRESS_WARNINGS_END
 #include "phx/display/display_system_openvr.hpp"
 #include "phx/input/openvr_controller_behavior.hpp"
 #include "phx/input/openvr_controller_system.hpp"
+#include "phx/rendering/components/resource_component.hpp"
 #include "phx/rendering/rendering_system.hpp"
 
 #include "mocks/opengl_mock.hpp"
@@ -102,12 +103,10 @@ SCENARIO(
 
         for (auto entity : controller_entities) {
           REQUIRE(
-              entity->GetFirstComponent<phx::ResourcePointer<phx::Mesh>>() !=
-              nullptr);
-          REQUIRE(
-              entity
-                  ->GetFirstComponent<phx::ResourcePointer<phx::Material>>() !=
+              entity->GetFirstComponent<phx::ResourceComponent<phx::Mesh>>() !=
               nullptr);
+          REQUIRE(entity->GetFirstComponent<
+                      phx::ResourceComponent<phx::Material>>() != nullptr);
         }
 
         auto side0 = controller_entities[0]
diff --git a/tests/src/test_resource_pointer.cpp b/tests/src/test_resource_pointer.cpp
index f5b8ae60..fd924b1f 100644
--- a/tests/src/test_resource_pointer.cpp
+++ b/tests/src/test_resource_pointer.cpp
@@ -23,6 +23,7 @@
 #include "catch/catch.hpp"
 
 #include "phx/core/entity.hpp"
+#include "phx/rendering/components/resource_component.hpp"
 #include "phx/resources/resource_manager.hpp"
 #include "phx/resources/resource_pointer.hpp"
 #include "phx/resources/resource_utils.hpp"
@@ -98,14 +99,15 @@ SCENARIO("ResourcePointers can be used as components directly") {
     auto mesh_resource_pointer = resource_pointer->GetMeshes()[0];
     phx::Entity entity;
     WHEN("We add the mesh_resource_pointer as resource component") {
-      entity.AddComponent<phx::ResourcePointer<phx::Mesh>>(
+      entity.AddComponent<phx::ResourceComponent<phx::Mesh>>(
           mesh_resource_pointer);
       THEN(
           "the resouce pointer itself is the resource and can be obtained as "
           "usual") {
         auto resource_component =
-            entity.GetFirstComponent<phx::ResourcePointer<phx::Mesh>>();
-        REQUIRE(resource_component->Get() == mesh_resource_pointer.Get());
+            entity.GetFirstComponent<phx::ResourceComponent<phx::Mesh>>();
+        REQUIRE(resource_component->GetResourcePointer().Get() ==
+                mesh_resource_pointer.Get());
       }
     }
   }
diff --git a/tests/src/test_scene_loader.cpp b/tests/src/test_scene_loader.cpp
index ee851ad9..807a22be 100644
--- a/tests/src/test_scene_loader.cpp
+++ b/tests/src/test_scene_loader.cpp
@@ -26,6 +26,7 @@
 
 #include "phx/core/entity.hpp"
 #include "phx/core/scene.hpp"
+#include "phx/rendering/components/resource_component.hpp"
 #include "phx/rendering/components/transform.hpp"
 #include "phx/resources/loaders/scene_loader.hpp"
 #include "phx/resources/resource_pointer.hpp"
@@ -57,7 +58,7 @@ SCENARIO("The scene loader can load a model", "[phx][phx::SceneLoader]") {
             nameCounter++;
           }
           if (currentEntity
-                  ->GetFirstComponent<phx::ResourcePointer<phx::Mesh>>() !=
+                  ->GetFirstComponent<phx::ResourceComponent<phx::Mesh>>() !=
               nullptr) {
             auto parentEntity =
                 currentEntity->GetFirstComponent<phx::Transform>()
-- 
GitLab