]> Dogcows Code - chaz/yoink/blob - src/moof/mesh_bindings.cc
cleaned up script wrapper and bindings
[chaz/yoink] / src / moof / mesh_bindings.cc
1
2 /*] Copyright (c) 2009-2011, Charles McGarvey [*****************************
3 **] All rights reserved.
4 *
5 * Distributable under the terms and conditions of the 2-clause BSD license;
6 * see the file COPYING for a complete text of the license.
7 *
8 *****************************************************************************/
9
10 #include "log.hh"
11 #include "mesh.hh"
12 #include "script.hh"
13
14
15 namespace moof {
16
17
18 static int mesh_new(script& script)
19 {
20 script::slot name = script[2].require_string("mesh name");
21
22 std::string str;
23 name.get(str);
24
25 mesh_handle mesh = resource::load(str, "ac");
26 if (!mesh) name.raise("unable to load mesh from file");
27
28 script.push(mesh);
29 return 1;
30 }
31
32 static int mesh_draw(script& script)
33 {
34 mesh_handle mesh;
35 script[1].require_object<mesh_handle>("mesh").get(mesh);
36
37 scalar alpha;
38 if (script[2].get(alpha))
39 mesh->draw(alpha);
40 else
41 mesh->draw();
42
43 return 0;
44 }
45
46 static int mesh_index(script& script)
47 {
48 mesh_handle* mesh;
49 script[1].require_object<mesh_handle>("mesh").get(mesh);
50
51 unsigned index;
52 script[2].require_number("index").get(index);
53 --index;
54
55 script.push((**mesh)[index]);
56 script::slot e = script[-1].push_environment();
57 e.set_field("mesh", *mesh);
58 e.pop();
59 return 1;
60 }
61
62
63 static int object_new(script& script)
64 {
65 return script.raise("cannot construct a mesh object without a mesh");
66 }
67
68 static int object_draw(script& script)
69 {
70 mesh::object_ptr* object;
71 script[1].require_object<mesh::object_ptr>("mesh object").get(object);
72
73 scalar alpha;
74 bool recurse;
75 if (script[2].get(alpha)) {
76 if (script[3].get(recurse))
77 (*object)->draw(alpha, recurse);
78 else
79 (*object)->draw(alpha);
80 }
81 else {
82 if (script[2].get(recurse))
83 (*object)->draw(SCALAR(0.0), recurse);
84 else
85 (*object)->draw();
86 }
87
88 return 0;
89 }
90
91 static int object_index(script& script)
92 {
93 mesh::object_ptr* object;
94 script[1].require_object<mesh::object_ptr>("mesh object").get(object);
95
96 if (script[2].is_number()) {
97 unsigned index;
98 script[2].get(index);
99 --index;
100 script.push((*object)->kids[index]);
101 }
102 else if (script[2].is_string()) {
103 std::string name;
104 script[2].get(name);
105 script.push((*object)->kids_byname[name]);
106 }
107 else {
108 script[2].raise_type_error("index or name");
109 }
110 return 1;
111 }
112
113
114 void mesh::import(script& script, const std::string& nspace)
115 {
116 script.check_stack(8);
117
118 script::slot parent = script.push_table(nspace);
119 script::slot meta = script.push_class<mesh_handle>(mesh_new);
120
121 meta.set_field("draw", mesh_draw);
122 meta.set_field("object", mesh_index);
123
124 script::slot object_meta = script.push_class<mesh::object_ptr>(object_new);
125 object_meta.set_field("draw", object_draw);
126 object_meta.set_field("kid", object_index);
127 meta.set_field("__object");
128
129 parent.set_field("mesh");
130 parent.pop();
131 }
132
133
134 } // namespace moof
135
This page took 0.034823 seconds and 4 git commands to generate.