From 0649af210afa6e7da051d655c3b28a7cd3b9ebc6 Mon Sep 17 00:00:00 2001
From: Clyne Sullivan <tullivan99@gmail.com>
Date: Sat, 22 Jul 2017 09:07:05 -0400
Subject: variable background layers

---
 include/texture.hpp |  3 +++
 include/world.hpp   |  5 -----
 src/world.cpp       | 54 ++++++++++++++++-------------------------------------
 xml/!town.xml       | 11 ++++++++++-
 4 files changed, 29 insertions(+), 44 deletions(-)

diff --git a/include/texture.hpp b/include/texture.hpp
index 0684cb7..9531d9d 100644
--- a/include/texture.hpp
+++ b/include/texture.hpp
@@ -159,6 +159,9 @@ public:
 	 */
 	inline const vec2& getTextureDim(void)
 	{ return position->getDim(); }
+
+	inline unsigned int size(void)
+	{ return textures.size(); }
 };
 
 /**
diff --git a/include/world.hpp b/include/world.hpp
index bfd0464..a7096a8 100644
--- a/include/world.hpp
+++ b/include/world.hpp
@@ -116,11 +116,6 @@ private:
 	static Mix_Music *bgmObj;
 	static std::string bgmCurrent;
 
-	/**
-	 * Paths of files to get stylized textures from.
-	 */
-	static std::vector<std::string> bgFiles;
-
 	/**
 	 * Allows for iteration between background textures, for rendering.
 	 */
diff --git a/src/world.cpp b/src/world.cpp
index fb2e5f5..ecd8ae4 100644
--- a/src/world.cpp
+++ b/src/world.cpp
@@ -31,7 +31,6 @@ using namespace tinyxml2;
 WorldData2               WorldSystem::world;
 Mix_Music*               WorldSystem::bgmObj;
 std::string              WorldSystem::bgmCurrent;
-std::vector<std::string> WorldSystem::bgFiles;
 TextureIterator          WorldSystem::bgTex;
 XMLDocument              WorldSystem::xmlDoc;
 std::string              WorldSystem::currentXMLFile;
@@ -57,34 +56,10 @@ const unsigned int GRASS_HEIGHT = HLINES(4);
 // the path of the currently loaded XML file, externally referenced in places
 static std::string currentXML;
 
-// pathnames of images for world themes
-//using StyleList = std::string[8];
-constexpr const char* bgPaths[1][8] = {
-	{ // Forest
-		"bg.png", 				// sky/background
-		"bgFarMountain.png",	// layer 1 (furthest)
-		"forestTileFar.png",	// layer 2
-		"forestTileBack.png",	// layer 3
-		"forestTileMid.png",	// layer 4
-		"forestTileFront.png",	// layer 5 (closest)
-		"dirt.png",				// ground texture
-		"grass.png"				// grass (ground-top) texture
-	}
-};
-
 /* ----------------------------------------------------------------------------
 ** Functions section
 ** --------------------------------------------------------------------------*/
 
-/*int generateString(const std::string& s)
-{
-	int mag = 0;
-	std::stringstream ss;
-	ss.str(s);
-
-	ss >> mag >> ':';
-}*/
-
 int WorldSystem::getLineIndex(float x)
 {
 	return std::clamp(static_cast<int>((x - world.startX) / game::HLINE),
@@ -295,16 +270,18 @@ void WorldSystem::loader(void)
 			world.style = static_cast<WorldBGType>(styleNo);
 			world.bgm = wxml->StrAttribute("bgm");
 
-			bgFiles.clear();
-
-			for (const auto& f : bgPaths[styleNo])
-				bgFiles.push_back(world.styleFolder + "bg/" + f);
+			std::vector<std::string> bgFiles;
+			auto layerTag = wxml->FirstChildElement("layer");
+			while (layerTag != nullptr) {
+				bgFiles.emplace_back(world.styleFolder + layerTag->StrAttribute("path"));
+				layerTag = layerTag->NextSiblingElement("layer");
+			}
 
 			bgTex = TextureIterator(bgFiles);
 		}
 
-        // world generation
-        else if (tagName == "generation") {
+		// world generation
+		 else if (tagName == "generation") {
 			generate(wxml->IntAttribute("width"));
 		}
 
@@ -678,22 +655,23 @@ void WorldSystem::render(void)
 	glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0);
 	glUniform4f(Render::worldShader.uniform[WU_ambient], ambient.red, ambient.green, ambient.blue, 1.0);
 
-	constexpr float parallax[5] = {
-		0.85f, 0.60f, 0.40f, 0.25f, 0.10f
-	};
+	int layerCount = bgTex.size() - 3;
+	float parallax = 0.85f;
+	float parallaxChange = 0.75f / layerCount;
+
 	float z = 8.0f;
-	for (int i = 0; i < 5; i++, z -= 0.1f) {
+	for (int i = 0; i < layerCount; i++, z -= 0.1f, parallax -= parallaxChange) {
 		bgTex++;
 		auto mountainDim = bgTex.getTextureDim() * game::HLINE;
-	    auto xcoord = width / 2 * -1 + offset.x * parallax[i];
+	    auto xcoord = width / 2 * -1 + offset.x * parallax;
 
 		int count = width / mountainDim.x + 1;
 		GLfloat* bgItems = new GLfloat[count * 30];
 		GLfloat* bgItemsFront = bgItems;
 
-		for (int i = 0; i < count; i++) {
+		for (int j = 0; j < count; j++) {
 			GLfloat five[5] = {
-				0, 0, mountainDim.x * i + xcoord, GROUND_HEIGHT_MINIMUM, z
+				0, 0, mountainDim.x * j + xcoord, GROUND_HEIGHT_MINIMUM, z
 			};
 
 			push5(bgItemsFront, five);
diff --git a/xml/!town.xml b/xml/!town.xml
index 219656f..1b8054d 100644
--- a/xml/!town.xml
+++ b/xml/!town.xml
@@ -2,7 +2,16 @@
 <include file="entities.xml"/>
 
 <World>
-    <style background="0" bgm="assets/music/town.ogg" folder="assets/style/classic/"/>
+	<style background="0" bgm="assets/music/town.ogg" folder="assets/style/classic/">
+		<layer path="bg/bg.png"/>
+		<layer path="bg/bgFarMountain.png"/>
+		<layer path="bg/forestTileFar.png"/>
+		<layer path="bg/forestTileBack.png"/>
+		<layer path="bg/forestTileMid.png"/>
+		<layer path="bg/forestTileFront.png"/>
+		<layer path="bg/dirt.png"/>
+		<layer path="bg/grass.png"/>
+	</style>
     <generation width="320"/>
 	<weather>Sunny</weather>
     <link right="!town2.xml"/>
-- 
cgit v1.2.3