]> git.localhorst.tv Git - blank.git/commitdiff
collect and load textures required by block types
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 13 Aug 2015 12:28:04 +0000 (14:28 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Thu, 13 Aug 2015 12:28:04 +0000 (14:28 +0200)
TODO
src/app/Assets.hpp
src/app/TextureIndex.hpp [new file with mode: 0644]
src/app/app.cpp
src/world/World.cpp

diff --git a/TODO b/TODO
index 057b7359385a55917da083adbb8ae2df18fdfa1b..fffc6dedce77407f8db9a4f82884d05ec82bc8a4 100644 (file)
--- a/TODO
+++ b/TODO
@@ -39,12 +39,7 @@ persistence
 
 block asset loading
 
-       block type loader can determine which textures are needed in the array
-       it should compose a map while loading, assigning an ID to each
-       individual texture and after all types are loaded, load the appropriate
-       textures into the array
-
-       also, parameterization of chunk generator should be less static/dangerous
+       parameterization of chunk generator should be less static/dangerous
 
 networking
 
index e6c209effdd7745b6c0d9ad6e798b47a131f1e9f..65c72fe04d73ad30a1fcf9d215244ad3dfe4ec66 100644 (file)
@@ -12,17 +12,19 @@ class ArrayTexture;
 class BlockTypeRegistry;
 class Sound;
 class Texture;
+class TextureIndex;
 
 class Assets {
 
 public:
        explicit Assets(const std::string &base);
 
-       void LoadBlockTypes(const std::string &set_name, BlockTypeRegistry &) const;
+       void LoadBlockTypes(const std::string &set_name, BlockTypeRegistry &, TextureIndex &) const;
        Font LoadFont(const std::string &name, int size) const;
        Sound LoadSound(const std::string &name) const;
        Texture LoadTexture(const std::string &name) const;
        void LoadTexture(const std::string &name, ArrayTexture &, int layer) const;
+       void LoadTextures(const TextureIndex &, ArrayTexture &) const;
 
 private:
        std::string fonts;
diff --git a/src/app/TextureIndex.hpp b/src/app/TextureIndex.hpp
new file mode 100644 (file)
index 0000000..8c6e22f
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef BLANK_APP_TEXTUREINDEX_HPP_
+#define BLANK_APP_TEXTUREINDEX_HPP_
+
+#include <map>
+#include <string>
+
+
+namespace blank {
+
+class TextureIndex {
+
+       using MapType = std::map<std::string, int>;
+
+public:
+       TextureIndex();
+
+       int GetID(const std::string &);
+
+       std::size_t Size() const noexcept { return id_map.size(); }
+       const MapType &Entries() const noexcept { return id_map; }
+
+private:
+       MapType id_map;
+
+};
+
+};
+
+#endif
index 3257f8b890867d4ae1e001cbd7816a58f1cab0d6..8f3801c3893709a0bc0c92ee652158e25c40c6fd 100644 (file)
@@ -4,6 +4,7 @@
 #include "FrameCounter.hpp"
 #include "State.hpp"
 #include "StateControl.hpp"
+#include "TextureIndex.hpp"
 
 #include "init.hpp"
 #include "../audio/Sound.hpp"
@@ -239,7 +240,7 @@ CuboidShape slab_shape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }});
 
 }
 
-void Assets::LoadBlockTypes(const std::string &set_name, BlockTypeRegistry &reg) const {
+void Assets::LoadBlockTypes(const std::string &set_name, BlockTypeRegistry &reg, TextureIndex &tex_index) const {
        string full = data + set_name + ".types";
        std::ifstream file(full);
        if (!file) {
@@ -263,19 +264,8 @@ void Assets::LoadBlockTypes(const std::string &set_name, BlockTypeRegistry &reg)
                        if (name == "visible") {
                                type.visible = in.GetBool();
                        } else if (name == "texture") {
-                               // TODO: load textures as requested
                                in.ReadString(tex_name);
-                               if (tex_name == "rock-1") {
-                                       type.texture = 1;
-                               } else if (tex_name == "rock-2") {
-                                       type.texture = 2;
-                               } else if (tex_name == "rock-3") {
-                                       type.texture = 3;
-                               } else if (tex_name == "debug") {
-                                       type.texture = 0;
-                               } else {
-                                       throw runtime_error("unknown texture: " + tex_name);
-                               }
+                               type.texture = tex_index.GetID(tex_name);
                        } else if (name == "color") {
                                in.ReadVec(type.color);
                        } else if (name == "label") {
@@ -353,6 +343,30 @@ void Assets::LoadTexture(const string &name, ArrayTexture &tex, int layer) const
        SDL_FreeSurface(srf);
 }
 
+void Assets::LoadTextures(const TextureIndex &index, ArrayTexture &tex) const {
+       // TODO: where the hell should that size come from?
+       tex.Reserve(16, 16, index.Size(), Format());
+       for (const auto &entry : index.Entries()) {
+               LoadTexture(entry.first, tex, entry.second);
+       }
+}
+
+
+TextureIndex::TextureIndex()
+: id_map() {
+
+}
+
+int TextureIndex::GetID(const string &name) {
+       auto entry = id_map.find(name);
+       if (entry == id_map.end()) {
+               auto result = id_map.emplace(name, Size());
+               return result.first->second;
+       } else {
+               return entry->second;
+       }
+}
+
 
 void FrameCounter::EnterFrame() noexcept {
        last_enter = SDL_GetTicks();
index 5827e8f872f2112e6c057d5c98958ab6d4bbd5bf..3d3eb6c3d9ba11a6aebc685e01480dca6b6e773f 100644 (file)
@@ -2,6 +2,7 @@
 
 #include "WorldCollision.hpp"
 #include "../app/Assets.hpp"
+#include "../app/TextureIndex.hpp"
 #include "../graphics/Format.hpp"
 #include "../graphics/Viewport.hpp"
 
@@ -21,16 +22,13 @@ World::World(const Assets &assets, const Config &config, const WorldSave &save)
 , entities()
 , light_direction(config.light_direction)
 , fog_density(config.fog_density) {
+       TextureIndex tex_index;
+       assets.LoadBlockTypes("default", block_type, tex_index);
+
        block_tex.Bind();
-       block_tex.Reserve(16, 16, 4, Format());
-       assets.LoadTexture("debug", block_tex, 0);
-       assets.LoadTexture("rock-1", block_tex, 1);
-       assets.LoadTexture("rock-2", block_tex, 2);
-       assets.LoadTexture("rock-3", block_tex, 3);
+       assets.LoadTextures(tex_index, block_tex);
        block_tex.FilterNearest();
 
-       assets.LoadBlockTypes("default", block_type);
-
        generate.Space(0);
        generate.Light(13);
        generate.Solids({ 1, 4, 7, 10 });