From: Daniel Karbach Date: Thu, 13 Aug 2015 12:28:04 +0000 (+0200) Subject: collect and load textures required by block types X-Git-Url: http://git.localhorst.tv/?a=commitdiff_plain;h=65dbb0391afb6867bd9b388c6351b947a022abad;p=blank.git collect and load textures required by block types --- diff --git a/TODO b/TODO index 057b735..fffc6de 100644 --- 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 diff --git a/src/app/Assets.hpp b/src/app/Assets.hpp index e6c209e..65c72fe 100644 --- a/src/app/Assets.hpp +++ b/src/app/Assets.hpp @@ -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 index 0000000..8c6e22f --- /dev/null +++ b/src/app/TextureIndex.hpp @@ -0,0 +1,29 @@ +#ifndef BLANK_APP_TEXTUREINDEX_HPP_ +#define BLANK_APP_TEXTUREINDEX_HPP_ + +#include +#include + + +namespace blank { + +class TextureIndex { + + using MapType = std::map; + +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 diff --git a/src/app/app.cpp b/src/app/app.cpp index 3257f8b..8f3801c 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -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 ®) const { +void Assets::LoadBlockTypes(const std::string &set_name, BlockTypeRegistry ®, 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 ®) 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(); diff --git a/src/world/World.cpp b/src/world/World.cpp index 5827e8f..3d3eb6c 100644 --- a/src/world/World.cpp +++ b/src/world/World.cpp @@ -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 });