]> git.localhorst.tv Git - blobs.git/blobdiff - src/app/app.cpp
load resource colors
[blobs.git] / src / app / app.cpp
index cfdbf8070355b1d2ec55364445baa61913ef1829..f2d0e5904d209ccba1113692877974e36e884ac8 100644 (file)
@@ -4,10 +4,15 @@
 
 #include "init.hpp"
 #include "../graphics/Viewport.hpp"
+#include "../io/Token.hpp"
+#include "../io/TokenStreamReader.hpp"
 
+#include <fstream>
 #include <SDL.h>
 #include <SDL_image.h>
 
+using std::string;
+
 
 namespace blobs {
 namespace app {
@@ -167,56 +172,28 @@ void State::OnQuit() {
 
 Assets::Assets()
 : path("assets/")
+, data_path(path + "data/")
+, font_path(path + "fonts/")
+, skin_path(path + "skins/")
 , tile_path(path + "tiles/")
-, skin_path(path + "skins/") {
-       data.resources.Add({ "air", "Air", 0 });
-       data.resources.Add({ "biomass", "Biomass", 0 });
-       data.resources.Add({ "dirt", "Dirt", 0 });
-       data.resources.Add({ "ice", "Ice", 0 });
-       data.resources.Add({ "rock", "Rock", 0 });
-       data.resources.Add({ "sand", "Sand", 0 });
-       data.resources.Add({ "water", "Water", 0 });
-       data.resources.Add({ "wood", "Wood", 0 });
-
-       data.tiles.Add({ "algae",    "Algae",    0,  0 });
-       data.tiles.Add({ "desert",   "Desert",   0,  1 });
-       data.tiles.Add({ "forest",   "Forest",   0,  2 });
-       data.tiles.Add({ "grass",    "Grass",    0,  3 });
-       data.tiles.Add({ "ice",      "Ice",      0,  4 });
-       data.tiles.Add({ "jungle",   "Jungle",   0,  5 });
-       data.tiles.Add({ "mountain", "Mountain", 0,  6 });
-       data.tiles.Add({ "ocean",    "Ocean",    0,  7 });
-       data.tiles.Add({ "rock",     "Rock",     0,  8 });
-       data.tiles.Add({ "sand",     "Sand",     0,  9 });
-       data.tiles.Add({ "taiga",    "Taiga",    0, 10 });
-       data.tiles.Add({ "tundra",   "Tundra",   0, 11 });
-       data.tiles.Add({ "water",    "Water",    0, 12 });
-       data.tiles.Add({ "wheat",    "Wheat",    0, 13 });
-
-       data.tiles["algae"]   .resources.push_back({ data.resources["water"].id,   1.0  });
-       data.tiles["algae"]   .resources.push_back({ data.resources["biomass"].id, 0.5  });
-       data.tiles["desert"]  .resources.push_back({ data.resources["sand"].id,    1.0  });
-       data.tiles["forest"]  .resources.push_back({ data.resources["wood"].id,    1.0  });
-       data.tiles["forest"]  .resources.push_back({ data.resources["dirt"].id,    0.5  });
-       data.tiles["grass"]   .resources.push_back({ data.resources["dirt"].id,    0.5  });
-       data.tiles["grass"]   .resources.push_back({ data.resources["biomass"].id, 0.25 });
-       data.tiles["grass"]   .resources.push_back({ data.resources["water"].id,   0.25 });
-       data.tiles["ice"]     .resources.push_back({ data.resources["ice"].id,     1.0  });
-       data.tiles["ice"]     .resources.push_back({ data.resources["water"].id,   0.25 });
-       data.tiles["jungle"]  .resources.push_back({ data.resources["wood"].id,    0.5  });
-       data.tiles["jungle"]  .resources.push_back({ data.resources["biomass"].id, 0.5  });
-       data.tiles["mountain"].resources.push_back({ data.resources["rock"].id,    1.0  });
-       data.tiles["ocean"]   .resources.push_back({ data.resources["water"].id,   1.0  });
-       data.tiles["rock"]    .resources.push_back({ data.resources["rock"].id,    1.0  });
-       data.tiles["sand"]    .resources.push_back({ data.resources["sand"].id,    1.0  });
-       data.tiles["taiga"]   .resources.push_back({ data.resources["wood"].id,    1.0  });
-       data.tiles["taiga"]   .resources.push_back({ data.resources["water"].id,   0.5  });
-       data.tiles["tundra"]  .resources.push_back({ data.resources["rock"].id,    1.0  });
-       data.tiles["tundra"]  .resources.push_back({ data.resources["ice"].id,     0.5  });
-       data.tiles["water"]   .resources.push_back({ data.resources["water"].id,   1.0  });
-       data.tiles["water"]   .resources.push_back({ data.resources["biomass"].id, 0.25 });
-       data.tiles["wheat"]   .resources.push_back({ data.resources["biomass"].id, 1.0  });
-       data.tiles["wheat"]   .resources.push_back({ data.resources["water"].id,   0.25 });
+, random(0x6283B64CEFE47925)
+, fonts{
+       graphics::Font(font_path + "DejaVuSans.ttf", 32),
+       graphics::Font(font_path + "DejaVuSans.ttf", 24),
+       graphics::Font(font_path + "DejaVuSans.ttf", 16)
+} {
+       {
+               std::ifstream resource_file(data_path + "resources");
+               io::TokenStreamReader resource_reader(resource_file);
+               ReadResources(resource_reader);
+       }
+
+       {
+               std::ifstream tile_file(data_path + "tile_types");
+               io::TokenStreamReader tile_reader(tile_file);
+               ReadTileTypes(tile_reader);
+       }
+
 
        graphics::Format format;
        textures.tiles.Bind();
@@ -235,6 +212,7 @@ Assets::Assets()
        LoadTileTexture("tundra",   textures.tiles, 11);
        LoadTileTexture("water",    textures.tiles, 12);
        LoadTileTexture("wheat",    textures.tiles, 13);
+       textures.tiles.FilterTrilinear();
 
        textures.skins.Bind();
        textures.skins.Reserve(256, 256, 9, format);
@@ -247,13 +225,120 @@ Assets::Assets()
        LoadSkinTexture("7", textures.skins, 6);
        LoadSkinTexture("8", textures.skins, 7);
        LoadSkinTexture("9", textures.skins, 8);
+       textures.skins.FilterTrilinear();
 }
 
 Assets::~Assets() {
 }
 
-void Assets::LoadTileTexture(const std::string &name, graphics::ArrayTexture &tex, int layer) const {
-       std::string path = tile_path + name + ".png";
+void Assets::ReadResources(io::TokenStreamReader &in) {
+       while (in.HasMore()) {
+               string name;
+               in.ReadIdentifier(name);
+               in.Skip(io::Token::EQUALS);
+
+               int id = 0;
+               if (data.resources.Has(name)) {
+                       id = data.resources[name].id;
+               } else {
+                       world::Resource res;
+                       res.name = name;
+                       id = data.resources.Add(res);
+               }
+
+               in.Skip(io::Token::ANGLE_BRACKET_OPEN);
+               while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
+                       in.ReadIdentifier(name);
+                       in.Skip(io::Token::EQUALS);
+                       if (name == "label") {
+                               in.ReadString(data.resources[id].label);
+                       } else if (name == "density") {
+                               data.resources[id].density = in.GetDouble();
+                       } else if (name == "state") {
+                               in.ReadIdentifier(name);
+                               if (name == "solid") {
+                                       data.resources[id].state = world::Resource::SOLID;
+                               } else if (name == "liquid") {
+                                       data.resources[id].state = world::Resource::LIQUID;
+                               } else if (name == "gas") {
+                                       data.resources[id].state = world::Resource::GAS;
+                               } else if (name == "plasma") {
+                                       data.resources[id].state = world::Resource::PLASMA;
+                               } else {
+                                       throw std::runtime_error("unknown resource state '" + name + "'");
+                               }
+                       } else if (name == "base_color") {
+                               in.ReadVec(data.resources[id].base_color);
+                       } else {
+                               throw std::runtime_error("unknown resource property '" + name + "'");
+                       }
+                       in.Skip(io::Token::SEMICOLON);
+               }
+               in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
+               in.Skip(io::Token::SEMICOLON);
+       }
+}
+
+void Assets::ReadTileTypes(io::TokenStreamReader &in) {
+       while (in.HasMore()) {
+               string name;
+               in.ReadIdentifier(name);
+               in.Skip(io::Token::EQUALS);
+
+               int id = 0;
+               if (data.tile_types.Has(name)) {
+                       id = data.tile_types[name].id;
+               } else {
+                       world::TileType type;
+                       type.name = name;
+                       id = data.tile_types.Add(type);
+               }
+
+               in.Skip(io::Token::ANGLE_BRACKET_OPEN);
+               while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
+                       in.ReadIdentifier(name);
+                       in.Skip(io::Token::EQUALS);
+                       if (name == "label") {
+                               in.ReadString(data.tile_types[id].label);
+                       } else if (name == "texture") {
+                               data.tile_types[id].texture = in.GetInt();
+                       } else if (name == "yield") {
+                               in.Skip(io::Token::BRACKET_OPEN);
+                               while (in.Peek().type != io::Token::BRACKET_CLOSE) {
+                                       world::TileType::Yield yield;
+                                       in.Skip(io::Token::ANGLE_BRACKET_OPEN);
+                                       while (in.Peek().type != io::Token::ANGLE_BRACKET_CLOSE) {
+                                               in.ReadIdentifier(name);
+                                               in.Skip(io::Token::EQUALS);
+                                               if (name == "resource") {
+                                                       in.ReadIdentifier(name);
+                                                       yield.resource = data.resources[name].id;
+                                               } else if (name == "ubiquity") {
+                                                       yield.ubiquity = in.GetDouble();
+                                               } else {
+                                                       throw std::runtime_error("unknown tile type yield property '" + name + "'");
+                                               }
+                                               in.Skip(io::Token::SEMICOLON);
+                                       }
+                                       in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
+                                       data.tile_types[id].resources.push_back(yield);
+                                       if (in.Peek().type == io::Token::COMMA) {
+                                               in.Skip(io::Token::COMMA);
+                                       }
+                               }
+                               in.Skip(io::Token::BRACKET_CLOSE);
+                       } else {
+                               throw std::runtime_error("unknown tile type property '" + name + "'");
+                       }
+                       in.Skip(io::Token::SEMICOLON);
+               }
+               in.Skip(io::Token::ANGLE_BRACKET_CLOSE);
+               in.Skip(io::Token::SEMICOLON);
+       }
+}
+
+void Assets::LoadTileTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
+       string path = tile_path + name + ".png";
        SDL_Surface *srf = IMG_Load(path.c_str());
        if (!srf) {
                throw SDLError("IMG_Load");
@@ -267,8 +352,8 @@ void Assets::LoadTileTexture(const std::string &name, graphics::ArrayTexture &te
        SDL_FreeSurface(srf);
 }
 
-void Assets::LoadSkinTexture(const std::string &name, graphics::ArrayTexture &tex, int layer) const {
-       std::string path = skin_path + name + ".png";
+void Assets::LoadSkinTexture(const string &name, graphics::ArrayTexture &tex, int layer) const {
+       string path = skin_path + name + ".png";
        SDL_Surface *srf = IMG_Load(path.c_str());
        if (!srf) {
                throw SDLError("IMG_Load");