]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
variable background layers
authorClyne Sullivan <tullivan99@gmail.com>
Sat, 22 Jul 2017 13:07:05 +0000 (09:07 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Sat, 22 Jul 2017 13:07:05 +0000 (09:07 -0400)
include/texture.hpp
include/world.hpp
src/world.cpp
xml/!town.xml

index 0684cb792ad4b799ee7b53efc596d8df6c3b39c9..9531d9d3a195115d836ec97c9e998fda283afff8 100644 (file)
@@ -159,6 +159,9 @@ public:
         */
        inline const vec2& getTextureDim(void)
        { return position->getDim(); }
+
+       inline unsigned int size(void)
+       { return textures.size(); }
 };
 
 /**
index bfd0464506963d182948016e6cce98e99fa72f58..a7096a8660f398cd63f845908edc321b3d7266f9 100644 (file)
@@ -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.
         */
index fb2e5f508afff106e7c57de0d17b087c98a00d47..ecd8ae4ad1bb36473a15a695b1e900f405bfb264 100644 (file)
@@ -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);
index 219656fff808042e9893cb9bcf76792d652541a6..1b8054d8b6b433fe575178ad98737c66ade35d3e 100644 (file)
@@ -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"/>