]>
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 glMultMatrixf(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]);
146 glVertex3f(xf
, yf
, 0.0f
);
147 glTexCoord2f(texCoords
[2], texCoords
[3]);
148 glVertex3f(xf
+1.0, yf
, 0.0f
);
149 glTexCoord2f(texCoords
[4], texCoords
[5]);
150 glVertex3f(xf
+1.0, yf
+1.0, 0.0f
);
151 glTexCoord2f(texCoords
[6], texCoords
[7]);
152 glVertex3f(xf
, yf
+1.0, 0.0f
);
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
),
179 std::map
<std::string
,SerializablePtr
> rootObj
;
181 if (root
->get(rootObj
))
183 std::map
<std::string
,SerializablePtr
>::iterator it
;
185 if ((it
= rootObj
.find("tile")) != rootObj
.end())
188 if ((*it
).second
->get(value
))
190 index
= Tilemap::Index(value
);
195 image
.getTileCoords(index
, texCoords
);
198 void draw(Scalar alpha
)
201 glMultMatrixf(transformation
.data());
203 glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
206 glBegin(GL_TRIANGLE_FAN
);
207 glTexCoord2f(texCoords
[0], texCoords
[1]);
208 glVertex2f(0.0f
, 0.0f
);
209 glTexCoord2f(texCoords
[2], texCoords
[3]);
210 glVertex2f(1.0f
, 0.0f
);
211 glTexCoord2f(texCoords
[4], texCoords
[5]);
212 glVertex2f(1.0f
, 1.0f
);
213 glTexCoord2f(texCoords
[6], texCoords
[7]);
214 glVertex2f(0.0f
, 1.0f
);
220 bool isVisible(const Camera
& cam
)
226 Tilemap::Index index
;
231 static bool loadBox(Aabb
& theBox
, SerializablePtr obj
)
233 std::vector
<SerializablePtr
> numbers
;
235 if (obj
->get(numbers
))
237 if (numbers
.size() == 6)
241 if (numbers
[0]->getNumber(num
))
252 SceneImpl(const std::string
& name
) :
253 Mippleton
<SceneImpl
>(name
)
259 void loadInstructions(SerializablePtr root
)
261 std::vector
<SerializablePtr
> rootObj
;
263 if (root
->get(rootObj
))
265 std::vector
<SerializablePtr
>::iterator it
;
270 for (it
= rootObj
.begin(); it
!= rootObj
.end(); it
++)
272 std::string instruction
;
274 if ((*it
)->get(instruction
))
276 if (instruction
== "reset_transform")
278 transform
.identity();
279 //std::cout << "===================RESET=====================" << std::endl;
281 else if (instruction
== "translate")
283 std::vector
<SerializablePtr
> values
;
286 if ((*it
)->get(values
))
290 for (size_t i
= 0; i
< values
.size(); i
++)
294 if (values
[i
]->getNumber(value
))
301 cml::matrix_translation(translation
, vec
);
302 transform
= translation
* transform
;
303 //std::cout << "TRANSLATE\t" << vec << std::endl
304 //<< transform << std::endl;
307 else if (instruction
== "scale")
309 std::vector
<SerializablePtr
> values
;
312 if ((*it
)->get(values
))
314 if (values
.size() == 1)
318 values
[0]->getNumber(value
);
321 cml::matrix_uniform_scale(scaling
, Scalar(value
));
322 transform
= scaling
* transform
;
323 //std::cout << "SCALE\t\t" << value << std::endl
324 //<< transform << std::endl;
326 else if (values
.size() == 3)
330 for (size_t i
= 0; i
< values
.size(); i
++)
334 if (values
[i
]->getNumber(value
))
341 cml::matrix_scale(scaling
, vec
);
342 transform
= scaling
* transform
;
343 //std::cout << "SCALE\t\t" << vec << std::endl
344 //<< transform << std::endl;
348 else if (instruction
== "rotate")
350 std::vector
<SerializablePtr
> values
;
353 if ((*it
)->get(values
))
355 if (values
.size() == 2)
358 size_t axisIndex
= 0;
361 if (values
[0]->get(axis
))
367 else if (axis
== "y")
371 else if (axis
== "z")
375 values
[1]->getNumber(value
);
378 cml::matrix_rotate_about_local_axis(transform
,
379 axisIndex
, Scalar(value
* cml::constantsd::rad_per_deg()));
380 //std::cout << "ROTATE\t" << axis << " " << value << std::endl
381 //<< transform << std::endl;
385 else if (instruction
== "texture")
390 else if (instruction
== "tilemap")
392 //std::cout << "TILEMAP\t" << texture<< std::endl;
393 //std::cout << transform << std::endl;
396 TilePanel
* tilePanel
= new TilePanel(transform
, texture
,
398 boost::shared_ptr
<Scenery
> sceneItem(tilePanel
);
399 objects
.push_back(sceneItem
);
401 else if (instruction
== "billboard")
403 //std::cout << "BILLBOARD\t" << texture << std::endl;
404 //std::cout << transform << std::endl;
407 Billboard
* billboard
= new Billboard(transform
, texture
,
409 boost::shared_ptr
<Scenery
> sceneItem(billboard
);
410 objects
.push_back(sceneItem
);
420 std::string filePath
= Scene::getPathToResource(getName());
422 Deserializer
deserializer(filePath
, true);
424 SerializablePtr root
= deserializer
.deserialize();
428 std::map
<std::string
,SerializablePtr
> rootObj
;
430 if (root
->get(rootObj
))
432 std::map
<std::string
,SerializablePtr
>::iterator it
;
434 if ((it
= rootObj
.find("playfield_bounds")) != rootObj
.end())
436 loadBox(playfieldBounds
, (*it
).second
);
438 if ((it
= rootObj
.find("maximum_bounds")) != rootObj
.end())
440 loadBox(maximumBounds
, (*it
).second
);
442 if ((it
= rootObj
.find("instructions")) != rootObj
.end())
444 loadInstructions((*it
).second
);
451 void draw(Scalar alpha
)
453 SceneryVector::iterator it
;
455 for (it
= objects
.begin(); it
!= objects
.end(); it
++)
457 //std::cout << "draw object";
463 Aabb playfieldBounds
;
466 typedef std::vector
<boost::shared_ptr
<Scenery
> > SceneryVector
;
467 SceneryVector objects
;
471 Scene::Scene(const std::string
& name
) :
473 impl_(Scene::SceneImpl::retain(name
), &Scene::SceneImpl::release
) {}
476 void Scene::draw(Scalar alpha
)
484 * Specialized search location for scene files. They can be found in the
485 * "scenes" subdirectory of any of the searched directories.
488 std::string
Scene::getPathToResource(const std::string
& name
)
490 return Resource::getPathToResource("scenes/" + name
+ ".json");
496 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.0635 seconds and 5 git commands to generate.