From be357a1a1fd3e1bceeca92a2be0d8100c251a2ed Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Sat, 23 Apr 2022 11:44:59 +0100
Subject: [PATCH] clean up heighmap APIs

---
 demo/demo/GameScene.cpp       | 35 +++++++++++++++++------------------
 include/fggl/data/heightmap.h |  8 +++++---
 2 files changed, 22 insertions(+), 21 deletions(-)

diff --git a/demo/demo/GameScene.cpp b/demo/demo/GameScene.cpp
index c601208..9fee528 100644
--- a/demo/demo/GameScene.cpp
+++ b/demo/demo/GameScene.cpp
@@ -65,18 +65,18 @@ static void process_camera(fggl::ecs3::World& ecs, const fggl::input::Input& inp
 	fggl::input::process_edgescroll( ecs, input, cam );
 }
 
-static void setupCamera(fggl::ecs3::World& world, fggl::ecs3::TypeRegistry& types) {
+static void setupCamera(fggl::ecs3::World& world) {
 	auto prototype = world.create(false);
-	world.add(prototype, types.find(fggl::math::Transform::name));
-	world.add(prototype, types.find(fggl::gfx::Camera::name));
-	world.add(prototype, types.find(fggl::input::FreeCamKeys::name));
 
-	auto camTf = world.get<fggl::math::Transform>(prototype);
-	if ( camTf != nullptr) {
-		camTf->origin(glm::vec3(10.0f, 3.0f, 10.0f));
+	// setup camera position/transform
+	auto* transform = world.add<fggl::math::Transform>(prototype);
+	if ( transform != nullptr) {
+		transform->origin(glm::vec3(10.0f, 3.0f, 10.0f));
 	}
 
-	auto cameraKeys = world.get<fggl::input::FreeCamKeys>(prototype);
+	// setup camera components
+	world.add<fggl::gfx::Camera>(prototype);
+	auto* cameraKeys = world.add<fggl::input::FreeCamKeys>(prototype);
 	if ( cameraKeys != nullptr ) {
 		cameraKeys->forward = glfwGetKeyScancode(GLFW_KEY_W);
 		cameraKeys->backward = glfwGetKeyScancode(GLFW_KEY_S);
@@ -87,26 +87,25 @@ static void setupCamera(fggl::ecs3::World& world, fggl::ecs3::TypeRegistry& type
 	}
 }
 
-static fggl::ecs3::entity_t setupTerrain(fggl::ecs3::World& world, fggl::ecs3::TypeRegistry& types) {
+static fggl::ecs3::entity_t setupTerrain(fggl::ecs3::World& world) {
 	fggl::ecs3::entity_t terrain;
 	{
 		terrain = world.create(false);
-		world.add(terrain, types.find(fggl::math::Transform::name));
 
-		auto camTf = world.get<fggl::math::Transform>(terrain);
+		auto* camTf = world.add<fggl::math::Transform>(terrain);
 		camTf->origin( glm::vec3(-128.0f, 0.0f, 128.0f) );
 
 		//auto terrainData = m_world.get<fggl::data::HeightMap>(terrain);
 		fggl::data::HeightMap terrainData{};
 		terrainData.clear();
 
-		const siv::PerlinNoise::seed_type seed = 123456u;
+		const siv::PerlinNoise::seed_type seed = 123456U;
 		const siv::PerlinNoise perlin{ seed };
 
-		for (int y = 0; y < 255; ++y) {
-			for (int x = 0; x < 255; ++x) {
-				const double noise = perlin.octave2D_11( (x * 0.01), (y * 0.01) , 4) * 10.f;
-				terrainData.heightValues[x * 255 +y] = (float)noise;
+		for (int z = 0; z < fggl::data::heightMaxZ; ++z) {
+			for (int x = 0; x < fggl::data::heightMaxX; ++x) {
+				const double noise = perlin.octave2D_11( (x * 0.01), (z * 0.01) , 4) * 10.f;
+				terrainData.setValue(x, z, (float)noise);
 			}
 		}
 		world.set<fggl::data::HeightMap>(terrain, &terrainData);
@@ -116,8 +115,8 @@ static fggl::ecs3::entity_t setupTerrain(fggl::ecs3::World& world, fggl::ecs3::T
 
 static fggl::ecs3::entity_t setupEnvironment(fggl::ecs3::World& world) {
 	auto& types = world.types();
-	setupCamera(world, types);
-	return setupTerrain(world, types);
+	setupCamera(world);
+	return setupTerrain(world);
 }
 
 static fggl::ecs3::entity_t setupBunkerPrototype(fggl::ecs3::World& world) {
diff --git a/include/fggl/data/heightmap.h b/include/fggl/data/heightmap.h
index bcf1a57..1cc2e1a 100644
--- a/include/fggl/data/heightmap.h
+++ b/include/fggl/data/heightmap.h
@@ -6,6 +6,8 @@
 #define FGGL_HEIGHTMAP_H
 
 #include <cstdint>
+#include <array>
+
 #include "fggl/data/model.hpp"
 
 namespace fggl::data {
@@ -16,7 +18,7 @@ namespace fggl::data {
 
 	struct HeightMap {
 		constexpr static const char name[] = "Heightmap";
-		float heightValues[heightMaxX * heightMaxZ];
+		std::array<float, heightMaxX * heightMaxZ> heightValues;
 
 		void clear() {
 			for (float &heightValue : heightValues) {
@@ -26,11 +28,11 @@ namespace fggl::data {
 
 		[[nodiscard]]
 		inline float getValue(std::size_t x, std::size_t z) const {
-			return heightValues[x * heightMaxZ + z];
+			return heightValues.at(x * heightMaxZ + z);
 		}
 
 		inline void setValue(std::size_t x, std::size_t z, float value) {
-			heightValues[x * heightMaxZ + z] = value;
+			heightValues.at(x * heightMaxZ + z) = value;
 		}
 	};
 
-- 
GitLab