X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2Fmoof%2Fmesh_bindings.cc;fp=src%2Fmoof%2Fmesh_bindings.cc;h=02138add2249c6c3ecae12a085ea9bf96b97f5e4;hp=0000000000000000000000000000000000000000;hb=6f1b787a10d8ab1a3117a4b8c004dd2d90599608;hpb=c143f7e806766a73cd69dc6e084e977641019ce6 diff --git a/src/moof/mesh_bindings.cc b/src/moof/mesh_bindings.cc new file mode 100644 index 0000000..02138ad --- /dev/null +++ b/src/moof/mesh_bindings.cc @@ -0,0 +1,136 @@ + +/*] Copyright (c) 2009-2010, Charles McGarvey [************************** +**] All rights reserved. +* +* vi:ts=4 sw=4 tw=75 +* +* Distributable under the terms and conditions of the 2-clause BSD license; +* see the file COPYING for a complete text of the license. +* +**************************************************************************/ + +#include "log.hh" +#include "mesh.hh" +#include "script.hh" + + +namespace moof { + + +static int mesh_new(script& script) +{ + script::slot name = script[2].require_string("mesh name"); + + std::string str; + name.get(str); + + mesh_handle mesh = resource::load(str, "ac"); + if (!mesh) name.raise("unable to load mesh from file"); + + script.push(mesh); + return 1; +} + +static int mesh_draw(script& script) +{ + mesh_handle* mesh; + script[1].require_object("mesh").get(mesh); + + scalar alpha; + if (script[2].get(alpha)) (*mesh)->draw(alpha); + else (*mesh)->draw(); + + return 0; +} + +static int mesh_index(script& script) +{ + mesh_handle* mesh; + script[1].require_object("mesh").get(mesh); + + unsigned index; + script[2].require_number("index").get(index); + --index; + + script.push((**mesh)[index]); + script::slot e = script[-1].push_environment(); + e.set_field("mesh", *mesh); + e.pop(); + return 1; +} + + +static int object_new(script& script) +{ + return script.raise("cannot construct a mesh object without a mesh"); +} + +static int object_draw(script& script) +{ + mesh::object_ptr* object; + script[1].require_object("mesh object").get(object); + + scalar alpha; + bool recurse; + if (script[2].get(alpha)) + { + if (script[3].get(recurse)) (*object)->draw(alpha, recurse); + else (*object)->draw(alpha); + } + else + { + if (script[2].get(recurse)) (*object)->draw(SCALAR(0.0), recurse); + else (*object)->draw(); + } + + return 0; +} + +static int object_index(script& script) +{ + mesh::object_ptr* object; + script[1].require_object("mesh object").get(object); + + if (script[2].is_number()) + { + unsigned index; + script[2].get(index); + --index; + script.push((*object)->kids[index]); + } + else if (script[2].is_string()) + { + std::string name; + script[2].get(name); + script.push((*object)->kids_byname[name]); + } + else + { + script[2].raise_type_error("index or name"); + } + return 1; +} + + +void mesh::import(script& script, const std::string& nspace) +{ + script.check_stack(8); + + script::slot parent = script.push_table(nspace); + script::slot meta = script.push_class(mesh_new); + + meta.set_field("draw", mesh_draw); + meta.set_field("object", mesh_index); + + script::slot object_meta = script.push_class(object_new); + object_meta.set_field("draw", object_draw); + object_meta.set_field("kid", object_index); + meta.set_field("__object"); + + parent.set_field("mesh"); + parent.pop(); +} + + +} // namespace moof +