goat3d

view src/goat3d_scene.cc @ 15:f1b4c27382ce

blah
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Sep 2013 14:06:14 +0300
parents 188c697b3b49
children
line source
1 #include <stdarg.h>
2 #include "goat3d.h"
3 #include "goat3d_impl.h"
4 #include "chunk.h"
6 Scene::Scene()
7 : name("unnamed"), ambient(0.05, 0.05, 0.05)
8 {
9 }
11 Scene::~Scene()
12 {
13 clear();
14 }
16 void Scene::clear()
17 {
18 for(size_t i=0; i<materials.size(); i++) {
19 delete materials[i];
20 }
21 materials.clear();
23 for(size_t i=0; i<meshes.size(); i++) {
24 delete meshes[i];
25 }
26 meshes.clear();
28 for(size_t i=0; i<lights.size(); i++) {
29 delete lights[i];
30 }
31 lights.clear();
33 for(size_t i=0; i<cameras.size(); i++) {
34 delete cameras[i];
35 }
36 cameras.clear();
38 for(size_t i=0; i<nodes.size(); i++) {
39 delete_node_tree(nodes[i]);
40 }
41 nodes.clear();
43 name = "unnamed";
44 }
46 void Scene::set_name(const char *name)
47 {
48 this->name = name;
49 }
51 const char *Scene::get_name() const
52 {
53 return name.c_str();
54 }
56 void Scene::set_ambient(const Vector3 &amb)
57 {
58 ambient = amb;
59 }
61 const Vector3 &Scene::get_ambient() const
62 {
63 return ambient;
64 }
66 void Scene::add_material(Material *mat)
67 {
68 materials.push_back(mat);
69 }
71 Material *Scene::get_material(int idx) const
72 {
73 return idx >=0 && idx < (int)materials.size() ? materials[idx] : 0;
74 }
76 Material *Scene::get_material(const char *name) const
77 {
78 for(size_t i=0; i<materials.size(); i++) {
79 if(materials[i]->name == std::string(name)) {
80 return materials[i];
81 }
82 }
83 return 0;
84 }
86 int Scene::get_material_count() const
87 {
88 return (int)materials.size();
89 }
92 void Scene::add_mesh(Mesh *mesh)
93 {
94 meshes.push_back(mesh);
95 }
97 Mesh *Scene::get_mesh(int idx) const
98 {
99 return idx >= 0 && idx < (int)meshes.size() ? meshes[idx] : 0;
100 }
102 Mesh *Scene::get_mesh(const char *name) const
103 {
104 for(size_t i=0; i<meshes.size(); i++) {
105 if(meshes[i]->name == std::string(name)) {
106 return meshes[i];
107 }
108 }
109 return 0;
110 }
112 int Scene::get_mesh_count() const
113 {
114 return (int)meshes.size();
115 }
118 void Scene::add_light(Light *light)
119 {
120 lights.push_back(light);
121 }
123 Light *Scene::get_light(int idx) const
124 {
125 return idx >= 0 && idx < (int)lights.size() ? lights[idx] : 0;
126 }
128 Light *Scene::get_light(const char *name) const
129 {
130 for(size_t i=0; i<lights.size(); i++) {
131 if(lights[i]->name == std::string(name)) {
132 return lights[i];
133 }
134 }
135 return 0;
136 }
138 int Scene::get_light_count() const
139 {
140 return (int)lights.size();
141 }
144 void Scene::add_camera(Camera *cam)
145 {
146 cameras.push_back(cam);
147 }
149 Camera *Scene::get_camera(int idx) const
150 {
151 return idx >= 0 && idx < (int)cameras.size() ? cameras[idx] : 0;
152 }
154 Camera *Scene::get_camera(const char *name) const
155 {
156 for(size_t i=0; i<cameras.size(); i++) {
157 if(cameras[i]->name == std::string(name)) {
158 return cameras[i];
159 }
160 }
161 return 0;
162 }
164 int Scene::get_camera_count() const
165 {
166 return (int)cameras.size();
167 }
170 void Scene::add_node(Node *node)
171 {
172 nodes.push_back(node);
173 }
175 Node *Scene::get_node(int idx) const
176 {
177 return idx >= 0 && idx < (int)nodes.size() ? nodes[idx] : 0;
178 }
180 Node *Scene::get_node(const char *name) const
181 {
182 for(size_t i=0; i<nodes.size(); i++) {
183 if(strcmp(nodes[i]->get_name(), name) == 0) {
184 return nodes[i];
185 }
186 }
187 return 0;
188 }
190 int Scene::get_node_count() const
191 {
192 return (int)nodes.size();
193 }
196 bool Scene::load(goat3d_io *io)
197 {
198 return false;
199 }
201 bool Scene::loadxml(goat3d_io *io)
202 {
203 return false;
204 }
206 // Scene::save is defined in goat3d_write.cc
209 void io_fprintf(goat3d_io *io, const char *fmt, ...)
210 {
211 va_list ap;
213 va_start(ap, fmt);
214 io_vfprintf(io, fmt, ap);
215 va_end(ap);
216 }
219 void io_vfprintf(goat3d_io *io, const char *fmt, va_list ap)
220 {
221 char smallbuf[256];
222 char *buf = smallbuf;
223 int sz = sizeof smallbuf;
225 int retsz = vsnprintf(buf, sz - 1, fmt, ap);
227 if(retsz >= sz) {
228 /* C99 mandates that snprintf with a short count should return the
229 * number of characters that *would* be printed.
230 */
231 buf = new char[retsz + 1];
233 vsnprintf(buf, retsz, fmt, ap);
235 } else if(retsz <= 0) {
236 /* SUSv2 and microsoft specify that snprintf with a short count
237 * returns an arbitrary value <= 0. So let's try allocating
238 * bigger and bigger arrays until we find the correct size.
239 */
240 sz = sizeof smallbuf;
241 do {
242 sz *= 2;
243 if(buf != smallbuf) {
244 delete [] buf;
245 }
246 buf = new char[sz + 1];
248 retsz = vsnprintf(buf, sz, fmt, ap);
249 } while(retsz <= 0);
250 }
252 io->write(buf, retsz, io->cls);
254 if(buf != smallbuf) {
255 delete [] buf;
256 }
258 }