From b241f3b931948359a9c49d92e22c21da1709a9a2 Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Sat, 20 Aug 2022 11:58:47 +0100
Subject: [PATCH] add fallback (null) physics implementation

---
 demo/demo/main.cpp                |  4 ++
 fggl/CMakeLists.txt               |  1 +
 fggl/phys/CMakeLists.txt          |  4 ++
 fggl/phys/null.cpp                | 30 +++++++++++++
 include/fggl/audio/null_audio.hpp |  4 +-
 include/fggl/phys/null.hpp        | 75 +++++++++++++++++++++++++++++++
 include/fggl/phys/service.hpp     |  2 +-
 7 files changed, 117 insertions(+), 3 deletions(-)
 create mode 100644 fggl/phys/CMakeLists.txt
 create mode 100644 fggl/phys/null.cpp
 create mode 100644 include/fggl/phys/null.hpp

diff --git a/demo/demo/main.cpp b/demo/demo/main.cpp
index ff9e602..3a0dc62 100644
--- a/demo/demo/main.cpp
+++ b/demo/demo/main.cpp
@@ -23,6 +23,8 @@
 
 #if __has_include("fggl/phys/bullet/bullet.hpp")
 	#include "fggl/phys/bullet/bullet.hpp"
+#else
+	#include "fggl/phys/null.hpp"
 #endif
 
 #include "fggl/fggl.hpp"
@@ -87,6 +89,8 @@ int main(int argc, const char* argv[]) {
 	moduleManager.use<fggl::entity::ECS>();
 	#ifdef FGGL_MODULE_BULLET
 		moduleManager.use<fggl::phys::Bullet3>();
+	#else
+		moduleManager.use<fggl::phys::NullPhysics>();
 	#endif
 	moduleManager.resolve();
 
diff --git a/fggl/CMakeLists.txt b/fggl/CMakeLists.txt
index 22cbcbe..a5bf05f 100644
--- a/fggl/CMakeLists.txt
+++ b/fggl/CMakeLists.txt
@@ -36,6 +36,7 @@ add_subdirectory(math)
 add_subdirectory(util)
 
 add_subdirectory(assets)
+add_subdirectory(phys)
 
 target_sources(${PROJECT_NAME}
     PRIVATE
diff --git a/fggl/phys/CMakeLists.txt b/fggl/phys/CMakeLists.txt
new file mode 100644
index 0000000..8589e54
--- /dev/null
+++ b/fggl/phys/CMakeLists.txt
@@ -0,0 +1,4 @@
+target_sources(fggl
+        PRIVATE
+            null.cpp
+)
\ No newline at end of file
diff --git a/fggl/phys/null.cpp b/fggl/phys/null.cpp
new file mode 100644
index 0000000..dc46b38
--- /dev/null
+++ b/fggl/phys/null.cpp
@@ -0,0 +1,30 @@
+/*
+ * This file is part of FGGL.
+ *
+ * FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
+ * later version.
+ *
+ * FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License along with FGGL.
+ * If not, see <https://www.gnu.org/licenses/>.
+ */
+
+//
+// Created by webpigeon on 20/08/22.
+//
+
+#include "fggl/phys/null.hpp"
+
+namespace fggl::phys {
+
+	bool NullPhysics::factory(modules::ModuleService serviceName, modules::Services &serviceManager) {
+		if (serviceName == phys::PhysicsProvider::service) {
+			serviceManager.bind<phys::PhysicsProvider, NullPhysicsProvider>();
+			return true;
+		}
+		return false;
+	}
+}
\ No newline at end of file
diff --git a/include/fggl/audio/null_audio.hpp b/include/fggl/audio/null_audio.hpp
index e89f99a..b5d293f 100644
--- a/include/fggl/audio/null_audio.hpp
+++ b/include/fggl/audio/null_audio.hpp
@@ -28,8 +28,8 @@ namespace fggl::audio {
 			NullAudioService() = default;
 			virtual ~NullAudioService() = default;
 
-			void play(const std::string& filename, bool looping = false) override {}
-			void play(AudioClip& clip, bool looping = false) override {}
+			void play(const std::string& /*filename*/, bool /*looping = false*/) override {}
+			void play(AudioClip& /*clip*/, bool /*looping = false*/) override {}
 	};
 
 	struct NullAudio {
diff --git a/include/fggl/phys/null.hpp b/include/fggl/phys/null.hpp
new file mode 100644
index 0000000..5fe7923
--- /dev/null
+++ b/include/fggl/phys/null.hpp
@@ -0,0 +1,75 @@
+/*
+ * This file is part of FGGL.
+ *
+ * FGGL is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any
+ * later version.
+ *
+ * FGGL is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
+ * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License along with FGGL.
+ * If not, see <https://www.gnu.org/licenses/>.
+ */
+
+//
+// Created by webpigeon on 20/08/22.
+//
+
+#ifndef FGGL_PHYS_NULL_HPP
+#define FGGL_PHYS_NULL_HPP
+
+#include "fggl/phys/types.hpp"
+#include "fggl/phys/service.hpp"
+
+namespace fggl::phys {
+
+	inline void build_noop(const entity::ComponentSpec& /*config*/,
+							  entity::EntityManager& /*manager*/,
+							  const entity::EntityID& /*entity*/) {}
+
+	class NullPhysicsEngine : public PhysicsEngine {
+		public:
+			inline void step() override {}
+			void setDebugDraw(bool /* enable */) override {}
+
+			inline std::vector<ContactPoint> scanCollisions(entity::EntityID /*entity*/) override {
+				return {};
+			}
+
+			inline entity::EntityID raycast(math::vec3 /*from*/, math::vec3 /*to*/) override {
+				return entity::INVALID;
+			}
+
+			inline std::vector<entity::EntityID> raycastAll(math::vec3 /*from*/, math::vec3 /*to*/) override {
+				return {};
+			}
+
+			inline std::vector<entity::EntityID> sweep(PhyShape& /*shape*/, math::Transform& /*from*/, math::Transform& /*to*/) override {
+				return {};
+			}
+	};
+
+	class NullPhysicsProvider : public PhysicsProvider {
+		public:
+			virtual ~NullPhysicsProvider() = default;
+			inline PhysicsEngine* create(entity::EntityManager* /*entityManager*/, entity::EntityFactory* factory) override {
+				factory->bind(util::make_guid("phys::Body"), build_noop );
+				return new NullPhysicsEngine();
+			}
+	};
+
+	struct NullPhysics {
+		constexpr static const char* name = "fggl::phys::null";
+		constexpr static const std::array<modules::ModuleService, 1> provides = {
+			phys::PhysicsProvider::service
+		};
+		constexpr static const std::array<modules::ModuleService, 1> depends = {
+			entity::EntityFactory::service
+		};
+		static bool factory(modules::ModuleService serviceName, modules::Services& serviceManager);
+	};
+
+} // namespace fggl::phys
+
+#endif //FGGL_PHYS_NULL_HPP
diff --git a/include/fggl/phys/service.hpp b/include/fggl/phys/service.hpp
index 54b84f9..b497465 100644
--- a/include/fggl/phys/service.hpp
+++ b/include/fggl/phys/service.hpp
@@ -28,7 +28,7 @@ namespace fggl::phys {
 	class PhysicsProvider {
 		public:
 			constexpr static const modules::ModuleService service = modules::make_service("fggl::phys::service");
-
+			virtual ~PhysicsProvider() = default;
 			virtual PhysicsEngine* create(entity::EntityManager* entityManager, entity::EntityFactory* factory) = 0;
 	};
 
-- 
GitLab