]>
Dogcows Code - chaz/yoink/blob - src/Moof/Scene.cc
2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
10 * Redistributions of source code must retain the above copyright notice,
11 this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above copyright notice,
13 this list of conditions and the following disclaimer in the documentation
14 and/or other materials provided with the distribution.
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *******************************************************************************/
35 #include "Cullable.hh"
36 #include "Deserializer.hh"
37 #include "Drawable.hh"
39 #include "Mippleton.hh"
42 #include "Serializable.hh"
49 class Scene::SceneImpl
: public Mippleton
<SceneImpl
>
51 class Scenery
: public Drawable
, public Cullable
54 Scenery(const Matrix4
& transform
, const std::string
& textureName
) :
55 transformation(transform
),
59 Matrix4 transformation
;
66 class TilePanel
: public Scenery
69 TilePanel(const Matrix4
& transform
, const std::string
& textureName
,
70 SerializablePtr root
) :
71 Scenery(transform
, textureName
),
75 std::map
<std::string
,SerializablePtr
> rootObj
;
77 if (root
->get(rootObj
))
79 std::map
<std::string
,SerializablePtr
>::iterator it
;
81 if ((it
= rootObj
.find("width")) != rootObj
.end())
83 (*it
).second
->get(width
);
85 if ((it
= rootObj
.find("tiles")) != rootObj
.end())
87 std::vector
<SerializablePtr
> theTiles
;
89 if ((*it
).second
->get(theTiles
))
91 std::vector
<SerializablePtr
>::iterator jt
;
93 height
= theTiles
.size() / width
;
96 indices
.resize(height
);
98 for (h
= height
- 1, jt
= theTiles
.begin();
99 jt
!= theTiles
.end(); h
--)
101 std::vector
<Tilemap::Index
> row
;
103 for (w
= 0; w
< width
&& jt
!= theTiles
.end();
108 if ((*jt
)->get(index
))
110 row
.push_back(Tilemap::Index(index
));
121 void draw(Scalar alpha
)
124 //std::cout << "transforming..." << std::endl;
125 //std::cout << transformation << std::endl;
126 glMultMatrix(transformation
.data());
128 glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
134 for (y
= 0, yf
= 0.0; y
< height
; y
++, yf
+= 1.0)
136 for (x
= 0, xf
= 0.0; x
< width
; x
++, xf
+= 1.0)
140 Tilemap::Index index
= indices
[y
][x
];
142 if (image
.getTileCoords(index
, texCoords
))
144 glBegin(GL_TRIANGLE_FAN
);
145 glTexCoord2f(texCoords
[0], texCoords
[1]);
147 glTexCoord2f(texCoords
[2], texCoords
[3]);
148 glVertex2f(xf
+1.0, yf
);
149 glTexCoord2f(texCoords
[4], texCoords
[5]);
150 glVertex2f(xf
+1.0, yf
+1.0);
151 glTexCoord2f(texCoords
[6], texCoords
[7]);
152 glVertex2f(xf
, yf
+1.0);
161 bool isVisible(const Camera
& cam
)
168 std::vector
<std::vector
<Tilemap::Index
> > indices
;
171 class Billboard
: public Scenery
174 Billboard(const Matrix4
& transform
, const std::string
& textureName
,
175 SerializablePtr root
) :
176 Scenery(transform
, textureName
),
180 std::map
<std::string
,SerializablePtr
> rootObj
;
182 if (root
->get(rootObj
))
184 std::map
<std::string
,SerializablePtr
>::iterator it
;
186 if ((it
= rootObj
.find("tile")) != rootObj
.end())
189 if ((*it
).second
->get(value
))
191 index
= Tilemap::Index(value
);
194 if ((it
= rootObj
.find("u_scale")) != rootObj
.end())
196 (*it
).second
->get(uScale
);
200 image
.getTileCoords(index
, texCoords
);
203 void draw(Scalar alpha
)
206 glMultMatrix(transformation
.data());
208 glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
211 float increment
= 1.0f
/ float(uScale
);
215 for (x
= 0, xf
= 0.0f
; x
< uScale
; x
++, xf
+= increment
)
217 glBegin(GL_TRIANGLE_FAN
);
218 glTexCoord2f(texCoords
[0], texCoords
[1]);
219 glVertex2f(xf
, 0.0f
);
220 glTexCoord2f(texCoords
[2], texCoords
[3]);
221 glVertex2f(xf
+increment
, 0.0f
);
222 glTexCoord2f(texCoords
[4], texCoords
[5]);
223 glVertex2f(xf
+increment
, 1.0f
);
224 glTexCoord2f(texCoords
[6], texCoords
[7]);
225 glVertex2f(xf
, 1.0f
);
232 bool isVisible(const Camera
& cam
)
238 Tilemap::Index index
;
244 static bool loadBox(Aabb
& theBox
, SerializablePtr obj
)
246 std::vector
<SerializablePtr
> numbers
;
248 if (obj
->get(numbers
))
250 if (numbers
.size() == 6)
254 if (numbers
[0]->getNumber(num
))
265 SceneImpl(const std::string
& name
) :
266 Mippleton
<SceneImpl
>(name
)
272 void loadInstructions(SerializablePtr root
)
274 std::vector
<SerializablePtr
> rootObj
;
276 if (root
->get(rootObj
))
278 std::vector
<SerializablePtr
>::iterator it
;
283 for (it
= rootObj
.begin(); it
!= rootObj
.end(); it
++)
285 std::string instruction
;
287 if ((*it
)->get(instruction
))
289 if (instruction
== "reset_transform")
291 transform
.identity();
292 //std::cout << "===================RESET=====================" << std::endl;
294 else if (instruction
== "translate")
296 std::vector
<SerializablePtr
> values
;
299 if ((*it
)->get(values
))
303 for (size_t i
= 0; i
< values
.size(); i
++)
307 if (values
[i
]->getNumber(value
))
314 cml::matrix_translation(translation
, vec
);
315 transform
= translation
* transform
;
316 //std::cout << "TRANSLATE\t" << vec << std::endl
317 //<< transform << std::endl;
320 else if (instruction
== "scale")
322 std::vector
<SerializablePtr
> values
;
325 if ((*it
)->get(values
))
327 if (values
.size() == 1)
331 values
[0]->getNumber(value
);
334 cml::matrix_uniform_scale(scaling
, Scalar(value
));
335 transform
= scaling
* transform
;
336 //std::cout << "SCALE\t\t" << value << std::endl
337 //<< transform << std::endl;
339 else if (values
.size() == 3)
343 for (size_t i
= 0; i
< values
.size(); i
++)
347 if (values
[i
]->getNumber(value
))
354 cml::matrix_scale(scaling
, vec
);
355 transform
= scaling
* transform
;
356 //std::cout << "SCALE\t\t" << vec << std::endl
357 //<< transform << std::endl;
361 else if (instruction
== "rotate")
363 std::vector
<SerializablePtr
> values
;
366 if ((*it
)->get(values
))
368 if (values
.size() == 2)
371 size_t axisIndex
= 0;
374 if (values
[0]->get(axis
))
380 else if (axis
== "y")
384 else if (axis
== "z")
388 values
[1]->getNumber(value
);
391 cml::matrix_rotate_about_local_axis(transform
,
392 axisIndex
, Scalar(value
* cml::constantsd::rad_per_deg()));
393 //std::cout << "ROTATE\t" << axis << " " << value << std::endl
394 //<< transform << std::endl;
398 else if (instruction
== "texture")
403 else if (instruction
== "tilemap")
405 //std::cout << "TILEMAP\t" << texture<< std::endl;
406 //std::cout << transform << std::endl;
409 TilePanel
* tilePanel
= new TilePanel(transform
, texture
,
411 boost::shared_ptr
<Scenery
> sceneItem(tilePanel
);
412 objects
.push_back(sceneItem
);
414 else if (instruction
== "billboard")
416 //std::cout << "BILLBOARD\t" << texture << std::endl;
417 //std::cout << transform << std::endl;
420 Billboard
* billboard
= new Billboard(transform
, texture
,
422 boost::shared_ptr
<Scenery
> sceneItem(billboard
);
423 objects
.push_back(sceneItem
);
433 std::string filePath
= Scene::getPathToResource(getName());
435 Deserializer
deserializer(filePath
, true);
437 SerializablePtr root
= deserializer
.deserialize();
441 std::map
<std::string
,SerializablePtr
> rootObj
;
443 if (root
->get(rootObj
))
445 std::map
<std::string
,SerializablePtr
>::iterator it
;
447 if ((it
= rootObj
.find("playfield_bounds")) != rootObj
.end())
449 loadBox(playfieldBounds
, (*it
).second
);
451 if ((it
= rootObj
.find("maximum_bounds")) != rootObj
.end())
453 loadBox(maximumBounds
, (*it
).second
);
455 if ((it
= rootObj
.find("instructions")) != rootObj
.end())
457 loadInstructions((*it
).second
);
464 void draw(Scalar alpha
)
466 SceneryVector::iterator it
;
468 for (it
= objects
.begin(); it
!= objects
.end(); it
++)
470 //std::cout << "draw object";
476 Aabb playfieldBounds
;
479 typedef std::vector
<boost::shared_ptr
<Scenery
> > SceneryVector
;
480 SceneryVector objects
;
484 Scene::Scene(const std::string
& name
) :
486 impl_(Scene::SceneImpl::retain(name
), &Scene::SceneImpl::release
) {}
489 void Scene::draw(Scalar alpha
)
497 * Specialized search location for scene files. They can be found in the
498 * "scenes" subdirectory of any of the searched directories.
501 std::string
Scene::getPathToResource(const std::string
& name
)
503 return Resource::getPathToResource("scenes/" + name
+ ".json");
509 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.051796 seconds and 4 git commands to generate.