From 02d5571eef7630c38968af264a441aa43e802d0f Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 14 Nov 2017 20:54:48 +0100 Subject: [PATCH] separate tile type and texture --- src/blobs.cpp | 20 +++++++++++--- src/world/Planet.hpp | 8 +++--- src/world/TileSet.hpp | 43 ++++++++++++++++++++++++++++++ src/world/TileType.hpp | 23 ++++++++++++++++ src/world/world.cpp | 60 ++++++++++++++++++++++++++++++++++-------- 5 files changed, 135 insertions(+), 19 deletions(-) create mode 100644 src/world/TileSet.hpp create mode 100644 src/world/TileType.hpp diff --git a/src/blobs.cpp b/src/blobs.cpp index f5e0918..b1fb631 100644 --- a/src/blobs.cpp +++ b/src/blobs.cpp @@ -6,6 +6,8 @@ #include "world/Planet.hpp" #include "world/Simulation.hpp" #include "world/Sun.hpp" +#include "world/TileSet.hpp" +#include "world/TileType.hpp" #include #include @@ -17,6 +19,17 @@ int main(int argc, char *argv[]) { app::Init init(true, 8); app::Assets assets; + world::TileSet tiles; + tiles.Add({ "ice", "Ice", 0, 0 }); + tiles.Add({ "black", "Black", 0, 1 }); + tiles.Add({ "red", "Red", 0, 2 }); + tiles.Add({ "grass", "Grass", 0, 3 }); + tiles.Add({ "water", "Water", 0, 4 }); + tiles.Add({ "sand", "Sand", 0, 5 }); + tiles.Add({ "tundra", "Tundra", 0, 6 }); + tiles.Add({ "magenta", "Magenta", 0, 7 }); + tiles.Add({ "rock", "Rock", 0, 8 }); + world::Sun sun; sun.Mass(1.0e12); sun.Radius(10.0); @@ -52,9 +65,9 @@ int main(int argc, char *argv[]) { sim.AddPlanet(second_planet); sim.AddPlanet(moon); - world::GenerateEarthlike(planet); - world::GenerateTest(moon); - world::GenerateTest(second_planet); + world::GenerateEarthlike(tiles, planet); + world::GenerateTest(tiles, moon); + world::GenerateTest(tiles, second_planet); std::cout << "length of year: " << planet.OrbitalPeriod() << "s" << std::endl; std::cout << "length of moon cycle: " << moon.OrbitalPeriod() << "s" << std::endl; @@ -80,7 +93,6 @@ int main(int argc, char *argv[]) { // .Reference(sun) // .Orbital(glm::vec3(-500.0f, 500.0f, 500.0f)) //; - planet.BuildVAOs(); app::Application app(init.window, init.viewport); app.PushState(&state); diff --git a/src/world/Planet.hpp b/src/world/Planet.hpp index efc804d..a95238a 100644 --- a/src/world/Planet.hpp +++ b/src/world/Planet.hpp @@ -15,7 +15,7 @@ namespace blobs { namespace world { -struct Tile; +class TileSet; /// A planet has six surfaces, numbered 0 to 5, each filled with /// sidelength² tiles. @@ -63,7 +63,7 @@ public: glm::dvec3 TileCenter(int surface, int x, int y) const noexcept; - void BuildVAOs(); + void BuildVAOs(const TileSet &); void Draw(app::Assets &, graphics::Viewport &) override; private: @@ -78,8 +78,8 @@ private: }; -void GenerateEarthlike(Planet &) noexcept; -void GenerateTest(Planet &) noexcept; +void GenerateEarthlike(const TileSet &, Planet &) noexcept; +void GenerateTest(const TileSet &, Planet &) noexcept; } } diff --git a/src/world/TileSet.hpp b/src/world/TileSet.hpp new file mode 100644 index 0000000..683109e --- /dev/null +++ b/src/world/TileSet.hpp @@ -0,0 +1,43 @@ +#ifndef BLOBS_WORLD_TILESET_HPP_ +#define BLOBS_WORLD_TILESET_HPP_ + +#include +#include +#include + +namespace blobs { +namespace world { + +class TileType; + +class TileSet { + +public: + TileSet(); + ~TileSet(); + + TileSet(const TileSet &) = delete; + TileSet &operator =(const TileSet &) = delete; + + TileSet(TileSet &&) = delete; + TileSet &operator =(TileSet &&) = delete; + +public: + int Add(const TileType &); + + TileType &operator [](int id) noexcept { return types[id]; } + const TileType &operator [](int id) const noexcept { return types[id]; } + + TileType &operator [](const std::string &name); + const TileType &operator [](const std::string &name) const; + +private: + std::vector types; + std::map names; + +}; + +} +} + +#endif diff --git a/src/world/TileType.hpp b/src/world/TileType.hpp new file mode 100644 index 0000000..58294df --- /dev/null +++ b/src/world/TileType.hpp @@ -0,0 +1,23 @@ +#ifndef BLOBS_WORLD_TILETYPE_HPP_ +#define BLOBS_WORLD_TILETYPE_HPP_ + +#include + + +namespace blobs { +namespace world { + +struct TileType { + + std::string name; + std::string label; + + int id; + int texture; + +}; + +} +} + +#endif diff --git a/src/world/world.cpp b/src/world/world.cpp index 57b0804..a939b3e 100644 --- a/src/world/world.cpp +++ b/src/world/world.cpp @@ -4,6 +4,8 @@ #include "Simulation.hpp" #include "Sun.hpp" #include "Tile.hpp" +#include "TileSet.hpp" +#include "TileType.hpp" #include "../const.hpp" #include "../app/Assets.hpp" @@ -270,7 +272,7 @@ glm::dvec3 Planet::TileCenter(int surface, int x, int y) const noexcept { return center; } -void Planet::BuildVAOs() { +void Planet::BuildVAOs(const TileSet &ts) { vao.Bind(); vao.BindAttributes(); vao.EnableAttribute(0); @@ -288,7 +290,7 @@ void Planet::BuildVAOs() { for (int index = 0, surface = 0; surface < 6; ++surface) { for (int y = 0; y < sidelength; ++y) { for (int x = 0; x < sidelength; ++x, ++index) { - float tex = TileAt(surface, x, y).type; + float tex = ts[TileAt(surface, x, y).type].texture; const float tex_u_begin = surface < 3 ? 1.0f : 0.0f; const float tex_u_end = surface < 3 ? 0.0f : 1.0f; attrib[4 * index + 0].position[(surface + 0) % 3] = x + 0 - offset; @@ -373,14 +375,14 @@ void Planet::Draw(app::Assets &assets, graphics::Viewport &viewport) { } -void GenerateEarthlike(Planet &p) noexcept { +void GenerateEarthlike(const TileSet &tiles, Planet &p) noexcept { rand::SimplexNoise elevation_gen(0); - constexpr int ice = 0; - constexpr int grass = 3; - constexpr int water = 4; - constexpr int sand = 5; - constexpr int rock = 8; + const int ice = tiles["ice"].id; + const int grass = tiles["grass"].id; + const int water = tiles["water"].id; + const int sand = tiles["sand"].id; + const int rock = tiles["rock"].id; constexpr double water_thresh = 0.0; constexpr double beach_thresh = 0.1; @@ -419,10 +421,10 @@ void GenerateEarthlike(Planet &p) noexcept { } } } - p.BuildVAOs(); + p.BuildVAOs(tiles); } -void GenerateTest(Planet &p) noexcept { +void GenerateTest(const TileSet &tiles, Planet &p) noexcept { for (int surface = 0; surface <= 5; ++surface) { for (int y = 0; y < p.SideLength(); ++y) { for (int x = 0; x < p.SideLength(); ++x) { @@ -434,7 +436,7 @@ void GenerateTest(Planet &p) noexcept { } } } - p.BuildVAOs(); + p.BuildVAOs(tiles); } @@ -445,5 +447,41 @@ Sun::Sun() Sun::~Sun() { } +TileSet::TileSet() +: types() +, names() { +} + +TileSet::~TileSet() { +} + +int TileSet::Add(const TileType &t) { + int id = types.size(); + if (!names.emplace(t.name, id).second) { + throw std::runtime_error("duplicate tile type name " + t.name); + } + types.emplace_back(t); + types.back().id = id; + return id; +} + +TileType &TileSet::operator [](const std::string &name) { + auto entry = names.find(name); + if (entry != names.end()) { + return types[entry->second]; + } else { + throw std::runtime_error("unknown tile type " + name); + } +} + +const TileType &TileSet::operator [](const std::string &name) const { + auto entry = names.find(name); + if (entry != names.end()) { + return types[entry->second]; + } else { + throw std::runtime_error("unknown tile type " + name); + } +} + } } -- 2.39.2