From 334fec286f01571bfdc0c7bf7347c4093aa23330 Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Mon, 17 Oct 2022 16:26:56 +0100
Subject: [PATCH] clean up some linting errors

---
 demo/data/rollball.lua                  |  4 +++-
 fggl/gfx/window.cpp                     | 27 +++++++++++-----------
 fggl/math/shapes.cpp                    | 16 +++----------
 include/fggl/debug/impl/logging_fmt.hpp |  6 ++---
 include/fggl/entity/gridworld/zone.hpp  | 28 +++++++++++------------
 include/fggl/math/triangulation.hpp     |  2 +-
 include/fggl/math/types.hpp             | 18 ++++++++++++++-
 include/fggl/util/guid.hpp              |  3 +--
 integrations/lua/src/engine.cpp         | 30 +++++++++++++++++++++++++
 9 files changed, 84 insertions(+), 50 deletions(-)

diff --git a/demo/data/rollball.lua b/demo/data/rollball.lua
index 7c16c4b..0d6eb7f 100644
--- a/demo/data/rollball.lua
+++ b/demo/data/rollball.lua
@@ -1,2 +1,4 @@
 print("File has been loaded!")
-print(state);
\ No newline at end of file
+
+-- when the scene loads, switch to topdown to show state integration
+--switch_scene(state, "topdown");
\ No newline at end of file
diff --git a/fggl/gfx/window.cpp b/fggl/gfx/window.cpp
index a71a273..43ddacd 100644
--- a/fggl/gfx/window.cpp
+++ b/fggl/gfx/window.cpp
@@ -21,7 +21,6 @@
 #include <string>
 #include <stdexcept>
 #include <GLFW/glfw3.h>
-#include <spdlog/spdlog.h>
 
 namespace fggl::display::glfw {
 
@@ -34,37 +33,37 @@ namespace fggl::display::glfw {
 		fgglWindow->framesize(width, height);
 	}
 
-	static void fggl_input_cursor(GLFWwindow *window, double x, double y) {
+	static void fggl_input_cursor(GLFWwindow *window, double xPos, double yPos) {
 		auto &input = GlfwInputManager::instance();
 		auto *fgglWin = static_cast<Window *>(glfwGetWindowUserPointer(window));
 
 		#ifndef FGGL_INPUT_SCREEN_COORDS
 		// convert to nice ranges...
-		x = (x / fgglWin->width() * 2) - 1.0; // [-1, 1]
-		y = (y / fgglWin->height() * 2) - 1.0; // [-1, 1]
+		xPos = (xPos / fgglWin->width() * 2) - 1.0; // [-1, 1]
+		yPos = (yPos / fgglWin->height() * 2) - 1.0; // [-1, 1]
 		#endif
 
 		// inform the input system
-		input.onMouseMove(x, y);
+		input.onMouseMove(xPos, yPos);
 	}
 
-	static void fggl_input_scroll(GLFWwindow *window, double x, double y) {
+	static void fggl_input_scroll(GLFWwindow */*window*/, double xPos, double yPos) {
 		auto &input = GlfwInputManager::instance();
-		input.onMouseScroll(x, y);
+		input.onMouseScroll(xPos, yPos);
 	}
 
-	static void fggl_input_mouse_btn(GLFWwindow *window, int btn, int action, int mods) {
+	static void fggl_input_mouse_btn(GLFWwindow */*window*/, int btn, int action, int /*mods*/) {
 		auto &input = GlfwInputManager::instance();
 		input.onMouseButton(btn, action == GLFW_PRESS);
 	}
 
-	static void fggl_input_keyboard(GLFWwindow *window, int key, int scancode, int action, int mods) {
+	static void fggl_input_keyboard(GLFWwindow */*window*/, int /*key*/, int scancode, int action, int /*mods*/) {
 		auto &input = GlfwInputManager::instance();
 		input.onKeyEvent(scancode, action == GLFW_PRESS || action == GLFW_REPEAT);
 	}
 
 	static void fggl_update_joystick(fggl::input::GamepadInput &input, int jid) {
-		bool isGamepad = glfwJoystickIsGamepad(jid);
+		bool isGamepad = (glfwJoystickIsGamepad(jid) == GLFW_TRUE);
 
 		if (isGamepad) {
 			if (!input.present(jid)) {
@@ -144,7 +143,7 @@ namespace fggl::display::glfw {
 
 	GlfwContext::~GlfwContext() {
 		glfwTerminate();
-		spdlog::debug("[glfw] context terminated");
+		debug::trace("[glfw] context terminated");
 	}
 
 	void GlfwContext::pollEvents() {
@@ -157,7 +156,9 @@ namespace fggl::display::glfw {
 
 	Window::Window(std::shared_ptr<GlfwContext> context, gfx::WindowGraphics *graphics)
 		: m_context(std::move(context)), m_window(nullptr), m_framesize() {
-		spdlog::debug("[glfw] creating window");
+
+		// don't iconify when focus is lost.
+		glfwWindowHint( GLFW_AUTO_ICONIFY, GLFW_FALSE );
 
 		// FIXME - this ties the graphics API before window creation
 		auto graphicsConfig = graphics->config();
@@ -185,8 +186,6 @@ namespace fggl::display::glfw {
 		// bind the graphics API
 		glfwMakeContextCurrent(m_window);
 		m_graphics = graphics->createMain(*this);
-
-		spdlog::debug("[glfw] window creation complete");
 	}
 
 	Window::~Window() {
diff --git a/fggl/math/shapes.cpp b/fggl/math/shapes.cpp
index 5188927..4d093db 100644
--- a/fggl/math/shapes.cpp
+++ b/fggl/math/shapes.cpp
@@ -22,19 +22,9 @@ namespace fggl::math::phs3d {
 		max = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
 	}
 
-	void AABB::add(const math::vec3 &p) {
-		if (p.x < min.x)
-			min.x = p.x;
-		if (p.x > max.x)
-			max.x = p.x;
-		if (p.y < min.y)
-			min.y = p.y;
-		if (p.y > min.y)
-			max.y = p.y;
-		if (p.z < min.z)
-			min.z = p.z;
-		if (p.z > max.z)
-			max.z = p.z;
+	void AABB::add(const math::vec3 &point) {
+		min = minElm(min, point);
+		max = maxElm(max, point);
 	}
 
 	AABB AABB::fromPoints(const std::vector<math::vec3> &points) {
diff --git a/include/fggl/debug/impl/logging_fmt.hpp b/include/fggl/debug/impl/logging_fmt.hpp
index 71374b4..68ef08e 100644
--- a/include/fggl/debug/impl/logging_fmt.hpp
+++ b/include/fggl/debug/impl/logging_fmt.hpp
@@ -103,10 +103,8 @@ namespace fggl::debug {
 
 	template<typename ...T>
 	inline void trace(FmtType fmt, T &&...args) {
-		#ifdef FGGL_LOG_TRACE
-			auto fmtStr = fmt::format(fmt::runtime(fmt), args...);
-			fmt::print(CERR_FMT, level_to_string(Level::trace), fmtStr);
-		#endif
+		auto fmtStr = fmt::format(fmt::runtime(fmt), args...);
+		fmt::print(CERR_FMT, level_to_string(Level::trace), fmtStr);
 	}
 
 }
diff --git a/include/fggl/entity/gridworld/zone.hpp b/include/fggl/entity/gridworld/zone.hpp
index 94ea20f..28d0c05 100644
--- a/include/fggl/entity/gridworld/zone.hpp
+++ b/include/fggl/entity/gridworld/zone.hpp
@@ -58,7 +58,7 @@ namespace fggl::entity::grid {
 			}
 
 		private:
-			constexpr static math::vec2i size = math::vec2i(width, height);
+			constexpr static math::vec2i size = math::vec2ui(width, height);
 			std::array<T, size.x * size.y> m_cells;
 
 			inline uint32_t getCellIndex(GridPos pos) const {
@@ -111,32 +111,32 @@ namespace fggl::entity::grid {
 			void clear() {
 				WallState noWall;
 
-				for (auto x = 0U; x<width; ++x) {
-					for (auto y=0U; y<height; ++y) {
-						m_floors.set({x, y}, 0);
-						m_walls.set({x, y}, noWall);
+				for (auto xPos = 0U; xPos<width; ++xPos) {
+					for (auto yPos=0U; yPos<height; ++yPos) {
+						m_floors.set({xPos, yPos}, 0);
+						m_walls.set({xPos, yPos}, noWall);
 					}
 				}
 			}
 
-			inline FloorTile& floorAt(uint32_t x, uint32_t y) {
-				return m_tiles.m_floors.at( m_floors.get({x, y}) );
+			inline FloorTile& floorAt(uint32_t xPos, uint32_t yPos) {
+				return m_tiles.m_floors.at( m_floors.get({xPos, yPos}) );
 			}
 
-			inline void setFloorAt(uint32_t x, uint32_t y, uint32_t floor) {
-				m_floors.set({x, y}, floor);
+			inline void setFloorAt(uint32_t xPos, uint32_t yPos, uint32_t floor) {
+				m_floors.set({xPos, yPos}, floor);
 			}
 
-			inline WallTile& wallAt(uint32_t x, uint32_t y, bool north) {
+			inline WallTile& wallAt(uint32_t xPos, uint32_t yPos, bool north) {
 				if (north) {
-					return m_tiles.m_walls.at(m_walls.get({x, y}).north);
+					return m_tiles.m_walls.at(m_walls.get({xPos, yPos}).north);
 				} else {
-					return m_tiles.m_walls.at(m_walls.get({x, y}).west);
+					return m_tiles.m_walls.at(m_walls.get({xPos, yPos}).west);
 				}
 			}
 
-			inline void setWallAt(uint32_t x, uint32_t y, bool north, uint32_t wall) {
-				auto& state = m_walls.get({x, y});
+			inline void setWallAt(uint32_t xPos, uint32_t yPos, bool north, uint32_t wall) {
+				auto& state = m_walls.get({xPos, yPos});
 				if (north) {
 					state.north = wall;
 				} else {
diff --git a/include/fggl/math/triangulation.hpp b/include/fggl/math/triangulation.hpp
index 06f6daa..628349e 100644
--- a/include/fggl/math/triangulation.hpp
+++ b/include/fggl/math/triangulation.hpp
@@ -127,7 +127,7 @@ namespace fggl::math {
 	}
 
 	static data::Vertex2D pointToVertex(const math::vec2 &point) {
-		return data::Vertex2D{point, {1.0f, 1.0f, 1.0f}};
+		return data::Vertex2D{point, {1.0F, 1.0F, 1.0F}, {0.0F, 0.0F}};
 	}
 
 	/**
diff --git a/include/fggl/math/types.hpp b/include/fggl/math/types.hpp
index 07ad9a0..b6c790d 100644
--- a/include/fggl/math/types.hpp
+++ b/include/fggl/math/types.hpp
@@ -91,7 +91,7 @@ namespace fggl::math {
 	/**
 	 * a 2D unsigned integer vector
 	 */
-	using vec2ui = glm::ivec2;
+	using vec2ui = glm::uvec2;
 
 	/**
 	 * A 2x2 floating-point matrix.
@@ -120,6 +120,22 @@ namespace fggl::math {
 	constexpr static const math::vec3 AXIS_Y{0.0F, 1.0F, 0.0F};
 	constexpr static const math::vec3 AXIS_Z{0.0F, 0.0F, 1.0F};
 
+	constexpr auto minElm(vec3 a, vec3 b) -> vec3{
+		return {
+			a.x < b.x ? a.x : b.x,
+			a.y < b.y ? a.y : b.y,
+			a.z < b.z ? a.z : b.z
+		};
+	}
+
+	constexpr auto maxElm(vec3 a, vec3 b) -> vec3 {
+		return {
+			a.x > b.x ? a.x : b.x,
+			a.y > b.y ? a.y : b.y,
+			a.z > b.z ? a.z : b.z
+		};
+	}
+
 	// fastFloor from OpenSimplex2
 	inline int fastFloor(double x) {
 		int xi = (int) x;
diff --git a/include/fggl/util/guid.hpp b/include/fggl/util/guid.hpp
index 03b52b5..7b188c7 100644
--- a/include/fggl/util/guid.hpp
+++ b/include/fggl/util/guid.hpp
@@ -94,8 +94,7 @@ namespace fggl::util {
 // formatter
 template<> struct fmt::formatter<fggl::util::GUID> {
 	constexpr auto parse(format_parse_context& ctx) -> decltype(ctx.begin()) {
-		auto it = ctx.begin(), end = ctx.end();
-		return it;
+		return ctx.begin();
 	}
 
 	template <typename FormatContext>
diff --git a/integrations/lua/src/engine.cpp b/integrations/lua/src/engine.cpp
index e171ef8..cd5d98d 100644
--- a/integrations/lua/src/engine.cpp
+++ b/integrations/lua/src/engine.cpp
@@ -19,11 +19,40 @@
 #include "fggl/script/lua/engine.hpp"
 #include "fggl/debug/logging.hpp"
 
+#include "fggl/scenes/game.hpp"
+#include "fggl/entity/loader/loader.hpp"
+
 #include <cassert>
 
+extern "C" {
+	int lua_switch_scene(lua_State *L) {
+		auto *scene = lua_tostring(L, -1);
+		auto *statePtr = (fggl::AppState *) (lua_topointer(L, -2));
+		statePtr->owner().change_state(scene);
+		return 0;
+	}
+
+	int lua_create_entity(lua_State *L) {
+		/*auto *prototype = lua_tostring(L, -1);
+		auto *statePtr = (fggl::scenes::Game*) (lua_topointer(L, -2));
+
+		auto *factory = statePtr->owner().service<fggl::entity::EntityFactory>();
+		//factory->create( statePtr->world(), , [](){} );
+		*/
+		return 0;
+	}
+
+	static void open_lua_fggl(lua_State *L) {
+		// switch scene
+		lua_pushcfunction(L, lua_switch_scene);
+		lua_setglobal(L, "switch_scene");
+	}
+}
+
 namespace fggl::script::lua {
 
 	LuaScriptProvider::LuaScriptProvider(data::Storage *storage) : m_storage(storage) {
+
 	}
 
 	LuaScriptEngine *LuaScriptProvider::create() {
@@ -32,6 +61,7 @@ namespace fggl::script::lua {
 
 	LuaScriptEngine::LuaScriptEngine(data::Storage* storage) : m_state(luaL_newstate()), m_storage(storage) {
 		luaL_openlibs(m_state);
+		open_lua_fggl(m_state);
 	}
 
 	LuaScriptEngine::~LuaScriptEngine() {
-- 
GitLab