]>
Dogcows Code - chaz/yoink/blob - src/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"
41 #include "serializable.hh"
50 class scene::scene_impl
: public mippleton
<scene_impl
>
52 class scenery
: public drawable
, public cullable
55 scenery(const matrix4
& transform
, const std::string
& textureName
) :
56 transformation(transform
),
60 matrix4 transformation
;
67 class tiles
: public scenery
70 tiles(const matrix4
& transform
, const std::string
& textureName
,
71 serializable_ptr root
) :
72 scenery(transform
, textureName
),
76 std::map
<std::string
,serializable_ptr
> rootObj
;
78 if (root
->get(rootObj
))
80 std::map
<std::string
,serializable_ptr
>::iterator it
;
82 if ((it
= rootObj
.find("width")) != rootObj
.end())
84 (*it
).second
->get(width
);
86 if ((it
= rootObj
.find("tiles")) != rootObj
.end())
88 std::vector
<serializable_ptr
> theTiles
;
90 if ((*it
).second
->get(theTiles
))
92 std::vector
<serializable_ptr
>::iterator jt
;
94 height
= theTiles
.size() / width
;
97 indices
.resize(height
);
99 for (h
= height
- 1, jt
= theTiles
.begin();
100 jt
!= theTiles
.end(); h
--)
102 std::vector
<unsigned> row
;
104 for (w
= 0; w
< width
&& jt
!= theTiles
.end();
109 if ((*jt
)->get(index
))
111 row
.push_back(unsigned(index
));
122 void draw(scalar alpha
)
125 //std::cout << "transforming..." << std::endl;
126 //std::cout << transformation << std::endl;
127 glMultMatrixf(transformation
.data());
129 glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
135 for (y
= 0, yf
= 0.0; y
< height
; y
++, yf
+= 1.0)
137 for (x
= 0, xf
= 0.0; x
< width
; x
++, xf
+= 1.0)
141 unsigned index
= indices
[y
][x
];
143 if (image
.getTileCoords(index
, texCoords
))
145 glBegin(GL_TRIANGLE_FAN
);
146 glTexCoord2f(texCoords
[0], texCoords
[1]);
147 glVertex3f(xf
, yf
, 0.0f
);
148 glTexCoord2f(texCoords
[2], texCoords
[3]);
149 glVertex3f(xf
+1.0, yf
, 0.0f
);
150 glTexCoord2f(texCoords
[4], texCoords
[5]);
151 glVertex3f(xf
+1.0, yf
+1.0, 0.0f
);
152 glTexCoord2f(texCoords
[6], texCoords
[7]);
153 glVertex3f(xf
, yf
+1.0, 0.0f
);
162 bool isVisible(const camera
& cam
)
169 std::vector
<std::vector
<unsigned> > indices
;
172 class billboard
: public scenery
175 billboard(const matrix4
& transform
, const std::string
& textureName
,
176 serializable_ptr root
) :
177 scenery(transform
, textureName
),
180 std::map
<std::string
,serializable_ptr
> rootObj
;
182 if (root
->get(rootObj
))
184 std::map
<std::string
,serializable_ptr
>::iterator it
;
186 if ((it
= rootObj
.find("tile")) != rootObj
.end())
189 if ((*it
).second
->get(value
))
191 index
= unsigned(value
);
196 image
.getTileCoords(index
, texCoords
);
199 void draw(scalar alpha
)
202 glMultMatrixf(transformation
.data());
204 glColor4f(1.0f
, 1.0f
, 1.0f
, 1.0f
);
207 glBegin(GL_TRIANGLE_FAN
);
208 glTexCoord2f(texCoords
[0], texCoords
[1]);
209 glVertex2f(0.0f
, 0.0f
);
210 glTexCoord2f(texCoords
[2], texCoords
[3]);
211 glVertex2f(1.0f
, 0.0f
);
212 glTexCoord2f(texCoords
[4], texCoords
[5]);
213 glVertex2f(1.0f
, 1.0f
);
214 glTexCoord2f(texCoords
[6], texCoords
[7]);
215 glVertex2f(0.0f
, 1.0f
);
221 bool isVisible(const camera
& cam
)
232 static bool loadBox(aabb
& theBox
, serializable_ptr obj
)
234 std::vector
<serializable_ptr
> numbers
;
236 if (obj
->get(numbers
))
238 if (numbers
.size() == 6)
242 if (numbers
[0]->getNumber(num
))
253 scene_impl(const std::string
& name
) :
254 mippleton
<scene_impl
>(name
)
260 void loadInstructions(serializable_ptr root
)
262 std::vector
<serializable_ptr
> rootObj
;
264 if (root
->get(rootObj
))
266 std::vector
<serializable_ptr
>::iterator it
;
271 for (it
= rootObj
.begin(); it
!= rootObj
.end(); it
++)
273 std::string instruction
;
275 if ((*it
)->get(instruction
))
277 if (instruction
== "reset_transform")
279 transform
.identity();
280 //std::cout << "===================RESET=====================" << std::endl;
282 else if (instruction
== "translate")
284 std::vector
<serializable_ptr
> values
;
287 if ((*it
)->get(values
))
291 for (unsigned i
= 0; i
< values
.size(); i
++)
295 if (values
[i
]->getNumber(value
))
302 cml::matrix_translation(translation
, vec
);
303 transform
= translation
* transform
;
304 //std::cout << "TRANSLATE\t" << vec << std::endl
305 //<< transform << std::endl;
308 else if (instruction
== "scale")
310 std::vector
<serializable_ptr
> values
;
313 if ((*it
)->get(values
))
315 if (values
.size() == 1)
319 values
[0]->getNumber(value
);
322 cml::matrix_uniform_scale(scaling
, scalar(value
));
323 transform
= scaling
* transform
;
324 //std::cout << "SCALE\t\t" << value << std::endl
325 //<< transform << std::endl;
327 else if (values
.size() == 3)
331 for (unsigned i
= 0; i
< values
.size(); i
++)
335 if (values
[i
]->getNumber(value
))
342 cml::matrix_scale(scaling
, vec
);
343 transform
= scaling
* transform
;
344 //std::cout << "SCALE\t\t" << vec << std::endl
345 //<< transform << std::endl;
349 else if (instruction
== "rotate")
351 std::vector
<serializable_ptr
> values
;
354 if ((*it
)->get(values
))
356 if (values
.size() == 2)
359 size_t axisIndex
= 0;
362 if (values
[0]->get(axis
))
368 else if (axis
== "y")
372 else if (axis
== "z")
376 values
[1]->getNumber(value
);
379 cml::matrix_rotate_about_local_axis(transform
,
380 axisIndex
, scalar(value
* cml::constantsd::rad_per_deg()));
381 //std::cout << "ROTATE\t" << axis << " " << value << std::endl
382 //<< transform << std::endl;
386 else if (instruction
== "texture")
391 else if (instruction
== "tilemap")
393 //std::cout << "TILEMAP\t" << texture<< std::endl;
394 //std::cout << transform << std::endl;
397 tiles
* newTiles
= new tiles(transform
, texture
, *it
);
398 boost::shared_ptr
<scenery
> sceneItem(newTiles
);
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
* newBB
= new billboard(transform
, texture
,
409 boost::shared_ptr
<scenery
> sceneItem(newBB
);
410 objects
.push_back(sceneItem
);
420 std::string filePath
= scene::getPathToResource(getName());
422 deserializer
in(filePath
, true);
424 serializable_ptr root
= in
.deserialize();
428 std::map
<std::string
,serializable_ptr
> rootObj
;
430 if (root
->get(rootObj
))
432 std::map
<std::string
,serializable_ptr
>::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 scenery_list::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
> > scenery_list
;
467 scenery_list objects
;
471 scene::scene(const std::string
& name
) :
473 impl(scene::scene_impl::retain(name
), &scene::scene_impl::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.060817 seconds and 4 git commands to generate.