From 46e624122cf4cee0b3d91f8b58351ee3d5372ebb Mon Sep 17 00:00:00 2001
From: Joseph Walton-Rivers <joseph@walton-rivers.uk>
Date: Mon, 6 Sep 2021 22:22:10 +0100
Subject: [PATCH] factor out shader loading code out of main

---
 demo/main.cpp | 34 ++++++++++++----------------------
 1 file changed, 12 insertions(+), 22 deletions(-)

diff --git a/demo/main.cpp b/demo/main.cpp
index f885428..263624a 100644
--- a/demo/main.cpp
+++ b/demo/main.cpp
@@ -190,6 +190,14 @@ void process_freecam(fggl::gfx::Window& window, fggl::ecs::ECS& ecs, fggl::gfx::
 	camComp->target = pivot;
 }
 
+void loadShader(fggl::gfx::ShaderCache& cache, const char* name, bool hasGeom) {
+	fggl::gfx::ShaderConfig config;
+	config.name = name;
+	config.vertex = config.name + "_vert.glsl";
+	config.fragment = config.name + "_frag.glsl";
+	cache.load(config);
+}
+
 int main(int argc, char* argv[]) {
 	fggl::gfx::Context ctx;
 
@@ -214,27 +222,9 @@ int main(int argc, char* argv[]) {
 	CameraHacks hacks{win, fggl::gfx::Input::instance()};
 
 	fggl::gfx::ShaderCache cache(storage);
-
-	fggl::gfx::ShaderConfig config;
-	config.name = "unlit";
-	config.vertex = "unlit_vert.glsl";
-	config.fragment = "unlit_frag.glsl";
-	auto shader = cache.load(config);
-
-	fggl::gfx::ShaderConfig configPhong;
-	configPhong.name = "phong";
-	configPhong.vertex = configPhong.name + "_vert.glsl";
-	configPhong.fragment = configPhong.name + "_frag.glsl";
-	//	configPhong.fragment = configPhong.name + "_normals_frag.glsl";
-	auto shaderPhong = cache.load(configPhong);
-
-	fggl::gfx::ShaderConfig configNormals;
-	configNormals.name = "normals";
-	configNormals.hasGeom = true;
-	configNormals.vertex = configNormals.name + "_vert.glsl";
-	configNormals.geometry = configNormals.name + "_geom.glsl";
-	configNormals.fragment = configNormals.name + "_frag.glsl";
-	auto shaderNormals = cache.load( configNormals );
+	loadShader(cache, "unlit", false);
+	loadShader(cache, "phong", false);
+	loadShader(cache, "normals", true);
 
 	// create ECS
 	fggl::ecs2::World world;
@@ -288,7 +278,7 @@ int main(int argc, char* argv[]) {
 
 		// upload mesh to GPU and get a rendering token
 		auto token = meshRenderer.upload( mesh );
-		token.pipeline = shaderPhong;
+		token.pipeline = cache.get("phong");
 
 		// add the rendering token to the prototype
 		bunkerPrototype.set<fggl::components::GfxToken>( token );
-- 
GitLab