From 6b7c48c2fdc122e27908f9c1e158f0c6b12e051c Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Sat, 27 Aug 2022 14:26:37 +0100
Subject: [PATCH] prevent segfault when closing or clicking

---
 demo/demo/topdown.cpp           | 6 +++++-
 include/fggl/gfx/ogl/shader.hpp | 2 +-
 include/fggl/scenes/game.hpp    | 5 +++++
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/demo/demo/topdown.cpp b/demo/demo/topdown.cpp
index fb313b6..dfc9609 100644
--- a/demo/demo/topdown.cpp
+++ b/demo/demo/topdown.cpp
@@ -137,6 +137,10 @@ void TopDown::update() {
 }
 
 void TopDown::pick_object() {
+		if (!hasPhys()) {
+			return;
+		}
+
 	auto cameras = world().find<fggl::gfx::Camera>();
 	if ( cameras.empty() ) {
 		return;
@@ -152,7 +156,7 @@ void TopDown::pick_object() {
 	auto ray = fggl::gfx::get_camera_ray(world(), cam, position);
 	auto hit = phys().raycast(ray);
 	if ( hit != fggl::entity::INVALID) {
-		//fggl::debug::log("hit: {}", hit);
+		fggl::debug::info("hit: {}", (int)hit);
 	} else {
 		fggl::debug::info("no hit");
 	}
diff --git a/include/fggl/gfx/ogl/shader.hpp b/include/fggl/gfx/ogl/shader.hpp
index 5ac1877..a5cba96 100644
--- a/include/fggl/gfx/ogl/shader.hpp
+++ b/include/fggl/gfx/ogl/shader.hpp
@@ -82,7 +82,7 @@ namespace fggl::gfx {
 			void initFallbackPipelines();
 
 		private:
-			std::shared_ptr<fggl::data::Storage> m_storage;
+			fggl::data::Storage* m_storage;
 			std::unordered_map<std::string, GLuint> m_shaders;
 
 			// extensions
diff --git a/include/fggl/scenes/game.hpp b/include/fggl/scenes/game.hpp
index 07be305..c5a074d 100644
--- a/include/fggl/scenes/game.hpp
+++ b/include/fggl/scenes/game.hpp
@@ -41,7 +41,12 @@ namespace fggl::scenes {
 				return *m_world;
 			}
 
+			bool hasPhys() const {
+				return m_phys != nullptr;
+			}
+
 			inline auto phys() -> phys::PhysicsEngine & {
+				assert(m_phys != nullptr);
 				return *m_phys;
 			}
 
-- 
GitLab