]> Dogcows Code - chaz/yoink/blob - src/moof/mesh_bindings.cc
02138add2249c6c3ecae12a085ea9bf96b97f5e4
[chaz/yoink] / src / moof / mesh_bindings.cc
1
2 /*] Copyright (c) 2009-2010, Charles McGarvey [**************************
3 **] All rights reserved.
4 *
5 * vi:ts=4 sw=4 tw=75
6 *
7 * Distributable under the terms and conditions of the 2-clause BSD license;
8 * see the file COPYING for a complete text of the license.
9 *
10 **************************************************************************/
11
12 #include "log.hh"
13 #include "mesh.hh"
14 #include "script.hh"
15
16
17 namespace moof {
18
19
20 static int mesh_new(script& script)
21 {
22 script::slot name = script[2].require_string("mesh name");
23
24 std::string str;
25 name.get(str);
26
27 mesh_handle mesh = resource::load(str, "ac");
28 if (!mesh) name.raise("unable to load mesh from file");
29
30 script.push(mesh);
31 return 1;
32 }
33
34 static int mesh_draw(script& script)
35 {
36 mesh_handle* mesh;
37 script[1].require_object<mesh_handle>("mesh").get(mesh);
38
39 scalar alpha;
40 if (script[2].get(alpha)) (*mesh)->draw(alpha);
41 else (*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 {
77 if (script[3].get(recurse)) (*object)->draw(alpha, recurse);
78 else (*object)->draw(alpha);
79 }
80 else
81 {
82 if (script[2].get(recurse)) (*object)->draw(SCALAR(0.0), recurse);
83 else (*object)->draw();
84 }
85
86 return 0;
87 }
88
89 static int object_index(script& script)
90 {
91 mesh::object_ptr* object;
92 script[1].require_object<mesh::object_ptr>("mesh object").get(object);
93
94 if (script[2].is_number())
95 {
96 unsigned index;
97 script[2].get(index);
98 --index;
99 script.push((*object)->kids[index]);
100 }
101 else if (script[2].is_string())
102 {
103 std::string name;
104 script[2].get(name);
105 script.push((*object)->kids_byname[name]);
106 }
107 else
108 {
109 script[2].raise_type_error("index or name");
110 }
111 return 1;
112 }
113
114
115 void mesh::import(script& script, const std::string& nspace)
116 {
117 script.check_stack(8);
118
119 script::slot parent = script.push_table(nspace);
120 script::slot meta = script.push_class<mesh_handle>(mesh_new);
121
122 meta.set_field("draw", mesh_draw);
123 meta.set_field("object", mesh_index);
124
125 script::slot object_meta = script.push_class<mesh::object_ptr>(object_new);
126 object_meta.set_field("draw", object_draw);
127 object_meta.set_field("kid", object_index);
128 meta.set_field("__object");
129
130 parent.set_field("mesh");
131 parent.pop();
132 }
133
134
135 } // namespace moof
136
This page took 0.039267 seconds and 3 git commands to generate.