]> git.localhorst.tv Git - blank.git/blobdiff - src/app/app.cpp
load models from assets
[blank.git] / src / app / app.cpp
index eb3dac1b8b4aa0eedcbd3456981a8554c408ebfa..7f5f02f0d5e1c557675c56dbdad3b7a1ac970189 100644 (file)
@@ -14,6 +14,8 @@
 #include "../graphics/Texture.hpp"
 #include "../io/TokenStreamReader.hpp"
 #include "../model/bounds.hpp"
+#include "../model/Model.hpp"
+#include "../model/ModelRegistry.hpp"
 #include "../model/Shape.hpp"
 #include "../model/ShapeRegistry.hpp"
 #include "../world/BlockType.hpp"
@@ -313,7 +315,12 @@ CuboidBounds slab_shape({{ -0.5f, -0.5f, -0.5f }, { 0.5f, 0.0f, 0.5f }});
 
 }
 
-void AssetLoader::LoadBlockTypes(const string &set_name, BlockTypeRegistry &reg, TextureIndex &tex_index) const {
+void AssetLoader::LoadBlockTypes(
+       const string &set_name,
+       BlockTypeRegistry &reg,
+       TextureIndex &tex_index,
+       const ShapeRegistry &shapes
+) const {
        string full = data + set_name + ".types";
        std::ifstream file(full);
        if (!file) {
@@ -395,20 +402,12 @@ void AssetLoader::LoadBlockTypes(const string &set_name, BlockTypeRegistry &reg,
                                type.commonness = in.GetFloat();
                        } else if (name == "shape") {
                                in.ReadIdentifier(shape_name);
-                               if (shape_name == "block") {
-                                       type.shape = &block_shape;
-                                       type.fill = {  true,  true,  true,  true,  true,  true };
-                               } else if (shape_name == "slab") {
-                                       type.shape = &slab_shape;
-                                       type.fill = { false,  true, false, false, false, false };
-                               } else if (shape_name == "stair") {
-                                       type.shape = &stair_shape;
-                                       type.fill = { false,  true, false, false, false,  true };
-                               } else {
-                                       throw runtime_error("unknown block shape: " + shape_name);
-                               }
+                               type.shape = &shapes.Get(shape_name);
                        } else {
-                               throw runtime_error("unknown block property: " + name);
+                               std::cerr << "warning: unknown block type property " << name << std::endl;
+                               while (in.Peek().type != Token::SEMICOLON) {
+                                       in.Next();
+                               }
                        }
                        in.Skip(Token::SEMICOLON);
                }
@@ -497,6 +496,43 @@ Font AssetLoader::LoadFont(const string &name, int size) const {
        return Font(full.c_str(), size);
 }
 
+void AssetLoader::LoadModels(
+       const string &set_name,
+       ModelRegistry &models,
+       TextureIndex &tex_index,
+       const ShapeRegistry &shapes
+) const {
+       string full = data + set_name + ".models";
+       std::ifstream file(full);
+       if (!file) {
+               throw std::runtime_error("failed to open model file " + full);
+       }
+       TokenStreamReader in(file);
+       string model_name;
+       string prop_name;
+       while (in.HasMore()) {
+               in.ReadIdentifier(model_name);
+               in.Skip(Token::EQUALS);
+               in.Skip(Token::ANGLE_BRACKET_OPEN);
+               Model &model = models.Add(model_name);
+               while (in.HasMore() && in.Peek().type != Token::ANGLE_BRACKET_CLOSE) {
+                       in.ReadIdentifier(prop_name);
+                       in.Skip(Token::EQUALS);
+                       if (prop_name == "root") {
+                               model.RootPart().Read(in, tex_index, shapes);
+                       } else {
+                               while (in.HasMore() && in.Peek().type != Token::SEMICOLON) {
+                                       in.Next();
+                               }
+                       }
+                       in.Skip(Token::SEMICOLON);
+               }
+               model.Enumerate();
+               in.Skip(Token::ANGLE_BRACKET_CLOSE);
+               in.Skip(Token::SEMICOLON);
+       }
+}
+
 void AssetLoader::LoadShapes(const string &set_name, ShapeRegistry &shapes) const {
        string full = data + set_name + ".shapes";
        std::ifstream file(full);