X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2FMoof%2FScene.cc;h=b3313124d22100bb4e59871ca8d4e3070d290987;hb=892da43bf5796e7c5f593a6d0f53bd797a36bd3e;hp=418c0512e3c690e4f29406181cac8346c67ada80;hpb=25aefe01ef7dbdb603c51411e04b0d6a6107684f;p=chaz%2Fyoink diff --git a/src/Moof/Scene.cc b/src/Moof/Scene.cc index 418c051..b331312 100644 --- a/src/Moof/Scene.cc +++ b/src/Moof/Scene.cc @@ -31,395 +31,380 @@ #include "Aabb.hh" #include "Camera.hh" -#include "Deserializer.hh" #include "Entity.hh" #include "Log.hh" #include "Math.hh" #include "Scene.hh" -#include "Serializable.hh" +#include "Script.hh" +#include "Settings.hh" #include "Tilemap.hh" namespace Mf { -static void loadBox(Aabb& theBox, SerializableP obj) +static std::string getPath(const std::string& name) { - Serializable::Array numbers; - - if (obj->get(numbers) && numbers.size() == 6) - { - Serializable::Float num; - - if (numbers[0]->getNumber(num)) theBox.min[0] = Scalar(num); - if (numbers[1]->getNumber(num)) theBox.min[1] = Scalar(num); - if (numbers[2]->getNumber(num)) theBox.min[2] = Scalar(num); - if (numbers[3]->getNumber(num)) theBox.max[0] = Scalar(num); - if (numbers[4]->getNumber(num)) theBox.max[1] = Scalar(num); - if (numbers[5]->getNumber(num)) theBox.max[2] = Scalar(num); - } + return Resource::getPath("scenes/" + name + ".lua"); } -static void loadTilemap(SerializableP root, const Matrix4& transform, - const std::string& texture, OctreeP octree) +struct Meh { - Serializable::Map rootObj; - Serializable::Map::iterator it; - - if (!root->get(rootObj)) - { - logError("invalid tilemap instruction"); - return; - } + Matrix4 transform; + std::string texture; - long width = 1; - long height = 1; - std::vector< std::vector > indices; + OctreeP octree; - if ((it = rootObj.find("width")) != rootObj.end()) + enum AXIS { - (*it).second->get(width); - } - else + X = 0, + Y = 1, + Z = 2 + }; + + Meh() { - logError("missing required field width for tilemap instruction"); - return; + octree = Octree::alloc(Aabb()); } - Serializable::Array tiles; - - if ((it = rootObj.find("tiles")) != rootObj.end() && - (*it).second->get(tiles) && - tiles.size() % width == 0) + static int loadBox(Script& script, Aabb& aabb) { - Serializable::Array::iterator jt; - int w, h; - - height = tiles.size() / width; - indices.resize(height); - - // the indices are stored upside-down in the scene file so that they - // are easier to edit as text, so we'll need to load them last row - // first + Script::Value table[] = {script[1], script[2]}; - for (h = height - 1, jt = tiles.begin(); jt != tiles.end(); --h) + if (!table[0].isTable() || !table[1].isTable()) { - std::vector row; + logWarning("wrong arguments to setPlayfieldBounds; ignoring..."); + return 0; + } - for (w = 0; w < width && jt != tiles.end(); ++w, ++jt) + for (int i = 0; i <= 1; ++i) + { + for (int j = 1; j <= 3; ++j) { - Serializable::Integer index; - - if ((*jt)->get(index)) - { - row.push_back(Tilemap::Index(index)); - } + script.push((long)j); + table[i].pushField(); } - - indices[h] = row; } + + script[3].get(aabb.min[0]); + script[4].get(aabb.min[1]); + script[5].get(aabb.min[2]); + script[6].get(aabb.max[0]); + script[7].get(aabb.max[1]); + script[8].get(aabb.max[2]); + + return 0; } - else + + int setPlayfieldBounds(Script& script) { - logError("invalid tiles in tilemap instruction"); - return; + Aabb bounds; + return loadBox(script, bounds); } - Vector4 vertices[height+1][width+1]; - - Matrix4 transposedTransform = transform; - transposedTransform.transpose(); - - for (int h = 0; h <= height; ++h) + int setMaximumBounds(Script& script) { - for (int w = 0; w <= width; ++w) - { - vertices[h][w] = Vector4(Scalar(w), Scalar(h), 0.0, 1.0) * - transposedTransform; - } + Aabb bounds; + int ret = loadBox(script, bounds); + octree = Octree::alloc(bounds); + return ret; } - for (int h = 0; h < height; ++h) + int resetTransform(Script& script) { - for (int w = 0; w < width; ++w) - { - if (indices[h][w] == Tilemap::NO_TILE) continue; + transform.identity(); + return 0; + } - Vector3 quadVertices[4]; + int translate(Script& script) + { + Script::Value x = script[1].requireNumber(); + Script::Value y = script[2].requireNumber(); + Script::Value z = script[3].requireNumber(); - demoteVector(quadVertices[0], vertices[h][w]); - demoteVector(quadVertices[1], vertices[h][w+1]); - demoteVector(quadVertices[2], vertices[h+1][w+1]); - demoteVector(quadVertices[3], vertices[h+1][w]); + Vector3 vec; + x.get(vec[0]); + y.get(vec[1]); + z.get(vec[2]); - Quad* quad = new Quad(quadVertices, texture, indices[h][w]); - boost::shared_ptr quadPtr(quad); + Matrix4 translation; + cml::matrix_translation(translation, vec); + transform = translation * transform; - octree->insert(quadPtr); - } + return 0; } -} -static void loadBillboard(SerializableP root, const Matrix4& transform, - const std::string& texture, OctreeP octree) -{ - Serializable::Map rootObj; - Serializable::Map::iterator it; - - Tilemap::Index index = 0; - long width = 1; - bool blending = false; - bool fog = false; - - if (root->get(rootObj)) + int scale(Script& script) { - if ((it = rootObj.find("tile")) != rootObj.end()) + if (script.getSize() == 3) { - Serializable::Integer value; - if ((*it).second->get(value)) - { - index = Tilemap::Index(value); - } + Vector3 vec; + script[1].requireNumber().get(vec[0]); + script[2].requireNumber().get(vec[1]); + script[3].requireNumber().get(vec[2]); + + Matrix4 scaling; + cml::matrix_scale(scaling, vec); + transform = scaling * transform; } - - if ((it = rootObj.find("u_scale")) != rootObj.end()) + else if (script.getSize() == 1) { - (*it).second->get(width); - } + Scalar value = 1.0; + script[1].requireNumber().get(value); - if ((it = rootObj.find("blend")) != rootObj.end()) - { - (*it).second->get(blending); + Matrix4 scaling; + cml::matrix_uniform_scale(scaling, value); + transform = scaling * transform; } - - if ((it = rootObj.find("fog")) != rootObj.end()) + else { - (*it).second->get(fog); + script.getTop().throwError("wrong number of arguments"); } + + return 0; } + int rotate(Script& script) + { + Script::Value axis = script[1].requireString(); + Script::Value angle = script[2].requireNumber(); + + size_t index = 0; + axis.get(index); - Vector4 vertices[2][width+1]; + Scalar value; + angle.get(value); - Matrix4 transposedTransform = transform; - transposedTransform.transpose(); + cml::matrix_rotate_about_world_axis(transform, index, cml::rad(value)); - Scalar xf; - Scalar increment = 1.0 / Scalar(width); + return 0; + } - for (int h = 0; h <= 1; ++h) + int setTexture(Script& script) { - xf = 0.0; - for (int w = 0; w <= width; ++w, xf += increment) - { - vertices[h][w] = Vector4(xf, Scalar(h), 0.0, 1.0) * - transposedTransform; - } + Script::Value name = script[1].requireString(); + + name.get(texture); + + return 0; } - for (int w = 0; w < width; ++w) + int makeTilemap(Script& script) { - Vector3 quadVertices[4]; + Script::Value table = script[1].requireTable(); + Script::Value top = script[-1]; - demoteVector(quadVertices[0], vertices[0][w]); - demoteVector(quadVertices[1], vertices[0][w+1]); - demoteVector(quadVertices[2], vertices[1][w+1]); - demoteVector(quadVertices[3], vertices[1][w]); + long width = 1; + long height = 1; - Quad* quad = new Quad(quadVertices, texture, index); - quad->setBlending(blending); - quad->setFog(fog); + table.pushField("width"); + top.get(width); - boost::shared_ptr quadPtr(quad); + long nTiles = 0; - octree->insert(quadPtr); - } -} + table.pushField("tiles"); + Script::Value tiles = script.getTop(); + nTiles = tiles.getLength(); + if (nTiles % width != 0) table.throwError("invalid number of tiles"); -static void loadInstructions(SerializableP root, OctreeP octree) -{ - Serializable::Array rootObj; - Serializable::Array::iterator it; + std::vector< std::vector > indices; - if (!root->get(rootObj)) - { - logError("scene instructions must be an array"); - return; - } + int i, w, h; - Matrix4 transform; - std::string texture; + height = nTiles / width; + indices.resize(height); - for (it = rootObj.begin(); it != rootObj.end(); ++it) - { - std::string instruction; + // the indices are stored upside-down in the scene file so that they + // are easier to edit as text, so we'll need to load them last row + // first - if ((*it)->get(instruction)) + i = 1; + for (h = height - 1; h >= 0; --h) { - if (instruction == "reset_transform") - { - transform.identity(); - } - else if (instruction == "translate") - { - Serializable::Array values; - - ++it; - if ((*it)->get(values)) - { - Vector3 vec; - - for (size_t i = 0; i < values.size(); ++i) - { - Serializable::Float value; - - if (values[i]->getNumber(value)) - { - vec[i] = value; - } - } - - Matrix4 translation; - cml::matrix_translation(translation, vec); - transform = translation * transform; - } - } - else if (instruction == "scale") - { - Serializable::Array values; - - ++it; - if ((*it)->get(values)) - { - if (values.size() == 1) - { - Serializable::Float value = 1.0; - - values[0]->getNumber(value); - - Matrix4 scaling; - cml::matrix_uniform_scale(scaling, - Scalar(value)); - transform = scaling * transform; - } - else if (values.size() == 3) - { - Vector3 vec; - - for (size_t i = 0; i < values.size(); ++i) - { - Serializable::Float value; - - if (values[i]->getNumber(value)) - { - vec[i] = value; - } - } - - Matrix4 scaling; - cml::matrix_scale(scaling, vec); - transform = scaling * transform; - } - } - } - else if (instruction == "rotate") + std::vector row; + + for (w = 0; w < width; ++w, ++i) { - Serializable::Array values; - - ++it; - if ((*it)->get(values)) - { - if (values.size() == 2) - { - std::string axis; - size_t index = 0; - Serializable::Float value = 0.0; - - if (values[0]->get(axis)) - { - if (axis == "x") index = 0; - else if (axis == "y") index = 1; - else if (axis == "z") index = 2; - - values[1]->getNumber(value); - } - - cml::matrix_rotate_about_world_axis(transform, - index, cml::rad(Scalar(value))); - } - } + script.checkStack(2); + script.push(long(i)); + tiles.pushField(); + + long index; + top.get(index); + + row.push_back(Tilemap::Index(index)); } - else if (instruction == "texture") + + indices[h] = row; + } + + Vector4 vertices[height+1][width+1]; + + Matrix4 transposedTransform = transform; + transposedTransform.transpose(); + + for (int h = 0; h <= height; ++h) + { + for (int w = 0; w <= width; ++w) { - ++it; - (*it)->get(texture); + vertices[h][w] = Vector4(Scalar(w), Scalar(h), 0.0, 1.0) * + transposedTransform; } - else if (instruction == "tilemap") + } + + for (int h = 0; h < height; ++h) + { + for (int w = 0; w < width; ++w) { - ++it; - loadTilemap(*it, transform, texture, octree); + if (indices[h][w] == Tilemap::NO_TILE) continue; + + Vector3 quadVertices[4]; + + demoteVector(quadVertices[0], vertices[h][w]); + demoteVector(quadVertices[1], vertices[h][w+1]); + demoteVector(quadVertices[2], vertices[h+1][w+1]); + demoteVector(quadVertices[3], vertices[h+1][w]); + + Quad* quad = new Quad(quadVertices, texture, indices[h][w]); + boost::shared_ptr quadPtr(quad); + + octree->insert(quadPtr); } - else if (instruction == "billboard") + } + + return 0; + } + + int makeBillboard(Script& script) + { + Script::Value table = script[1]; + Script::Value top = script[-1]; + + long index = 0; + long width = 1; + bool blending = false; + bool fog = false; + + if (table.isTable()) + { + table.pushField("tile"); + top.get(index); + + table.pushField("u_scale"); + top.get(width); + + table.pushField("blend"); + top.get(blending); + + table.pushField("fog"); + top.get(fog); + } + + Vector4 vertices[2][width+1]; + + Matrix4 transposedTransform = transform; + transposedTransform.transpose(); + + Scalar xf; + Scalar increment = 1.0 / Scalar(width); + + for (int h = 0; h <= 1; ++h) + { + xf = 0.0; + for (int w = 0; w <= width; ++w, xf += increment) { - ++it; - loadBillboard(*it, transform, texture, octree); + vertices[h][w] = Vector4(xf, Scalar(h), 0.0, 1.0) * + transposedTransform; } } + + for (int w = 0; w < width; ++w) + { + Vector3 quadVertices[4]; + + demoteVector(quadVertices[0], vertices[0][w]); + demoteVector(quadVertices[1], vertices[0][w+1]); + demoteVector(quadVertices[2], vertices[1][w+1]); + demoteVector(quadVertices[3], vertices[1][w]); + + Quad* quad = new Quad(quadVertices, texture, Tilemap::Index(index)); + quad->setBlending(blending); + quad->setFog(fog); + + boost::shared_ptr quadPtr(quad); + + octree->insert(quadPtr); + } + + return 0; } -} +}; -static std::string getPath(const std::string& name) +static void importSceneBindings(Script& script, Meh& scene) { - return Resource::getPath("scenes/" + name + ".json"); + script.importFunction("SetPlayfieldBounds", + boost::bind(&Meh::setPlayfieldBounds, &scene, _1)); + script.importFunction("SetMaximumBounds", + boost::bind(&Meh::setMaximumBounds, &scene, _1)); + script.importFunction("ResetTransform", + boost::bind(&Meh::resetTransform, &scene, _1)); + script.importFunction("Translate", + boost::bind(&Meh::translate, &scene, _1)); + script.importFunction("Scale", + boost::bind(&Meh::scale, &scene, _1)); + script.importFunction("Rotate", + boost::bind(&Meh::rotate, &scene, _1)); + script.importFunction("SetTexture", + boost::bind(&Meh::setTexture, &scene, _1)); + script.importFunction("MakeTilemap", + boost::bind(&Meh::makeTilemap, &scene, _1)); + script.importFunction("MakeBillboard", + boost::bind(&Meh::makeBillboard, &scene, _1)); + + long detail = 3; + Settings::getInstance().get("detail", detail); + script.push(detail); + script.set("detail"); + + script.push(Quad::LEFT); + script.set("LEFT"); + script.push(Quad::RIGHT); + script.set("RIGHT"); + script.push(Quad::TOP); + script.set("TOP"); + + script.push(Meh::X); + script.set("X"); + script.push(Meh::Y); + script.set("Y"); + script.push(Meh::Z); + script.set("Z"); } + OctreeP loadScene(const std::string& name) { std::string filePath = getPath(name); - Deserializer deserializer(filePath, true); - SerializableP root = deserializer.deserialize(); + Meh cool; - Serializable::Map rootObj; - Serializable::Map::iterator it; + Script script; + script.importStandardLibraries(); + importLogScript(script); + importSceneBindings(script, cool); - if (!root || !root->get(rootObj)) + if (script.doFile(filePath) != Script::SUCCESS) { - logError("no root map in scene file"); - return OctreeP(); + std::string str; + script[-1].get(str); + logScript("%s", str.c_str()); } - Aabb playfieldBounds; - Aabb maximumBounds; - - if ((it = rootObj.find("playfield_bounds")) != rootObj.end()) - { - loadBox(playfieldBounds, (*it).second); - } - if ((it = rootObj.find("maximum_bounds")) != rootObj.end()) - { - loadBox(maximumBounds, (*it).second); - } - else - { - logError("missing required maximum bounds"); - return OctreeP(); - } - - // create the tree to store the quads - OctreeP octree = Octree::alloc(maximumBounds); - - if ((it = rootObj.find("instructions")) != rootObj.end()) - { - loadInstructions((*it).second, octree); - } - - octree->sort(); - - return octree; + cool.octree->sort(); + return cool.octree; }