]>
Dogcows Code - chaz/yoink/blob - src/tilemap.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 *******************************************************************************/
29 #include "deserializer.hh"
30 #include "mippleton.hh"
32 #include "serializable.hh"
40 class tilemap::tilemap_impl
: public mippleton
<tilemap_impl
>
43 tilemap_impl(const std::string
& name
) :
44 mippleton
<tilemap_impl
>(name
),
47 minFilter_(GL_NEAREST
),
48 maxFilter_(GL_NEAREST
),
57 deserializer
in(tilemap::getPathToResource(getName()));
59 serializable_ptr root
= in
.deserialize();
63 std::map
<std::string
,serializable_ptr
> rootMap
;
65 if (root
->get(rootMap
))
67 std::map
<std::string
,serializable_ptr
>::iterator it
;
69 if ((it
= rootMap
.find("TilesU")) != rootMap
.end())
72 if ((*it
).second
->get(value
))
74 tilesU_
= unsigned(value
);
77 if ((it
= rootMap
.find("TilesV")) != rootMap
.end())
80 if ((*it
).second
->get(value
))
82 tilesV_
= unsigned(value
);
85 if ((it
= rootMap
.find("MinFilter")) != rootMap
.end())
88 if ((*it
).second
->get(value
))
90 if (value
== "Linear")
92 minFilter_
= GL_LINEAR
;
96 if ((it
= rootMap
.find("MaxFilter")) != rootMap
.end())
99 if ((*it
).second
->get(value
))
101 if (value
== "Linear")
103 maxFilter_
= GL_LINEAR
;
107 if ((it
= rootMap
.find("WrapU")) != rootMap
.end())
110 if ((*it
).second
->get(value
))
112 if (value
== "Repeat")
118 if ((it
= rootMap
.find("WrapV")) != rootMap
.end())
121 if ((*it
).second
->get(value
))
123 if (value
== "Repeat")
142 tilemap::tilemap(const std::string
& name
) :
144 impl(tilemap::tilemap_impl::retain(name
), &tilemap::tilemap_impl::release
)
146 setMinFilter(impl
->minFilter_
);
147 setMaxFilter(impl
->maxFilter_
);
148 setWrapU(impl
->wrapU_
);
149 setWrapV(impl
->wrapV_
);
154 bool tilemap::getTileCoords(unsigned index
, scalar coords
[8])
156 // make sure the index represents a real tile
157 if (index
>= impl
->tilesU_
* impl
->tilesV_
) return false;
159 scalar w
= 1.0 / scalar(impl
->tilesU_
);
160 scalar h
= 1.0 / scalar(impl
->tilesV_
);
162 coords
[0] = scalar(index
% impl
->tilesU_
) * w
;
163 coords
[1] = (scalar(impl
->tilesV_
- 1) - scalar(index
/ impl
->tilesU_
)) * h
;
164 coords
[2] = coords
[0] + w
;
165 coords
[3] = coords
[1];
166 coords
[4] = coords
[2];
167 coords
[5] = coords
[1] + h
;
168 coords
[6] = coords
[0];
169 coords
[7] = coords
[5];
174 bool tilemap::getTileCoords(unsigned index
, scalar coords
[8], orientation what
)
176 if (getTileCoords(index
, coords
))
180 // this looks kinda weird, but it's just swapping in a way that
181 // doesn't require an intermediate variable
182 coords
[1] = coords
[5];
183 coords
[5] = coords
[3];
184 coords
[3] = coords
[7];
185 coords
[7] = coords
[5];
189 coords
[0] = coords
[2];
190 coords
[2] = coords
[6];
191 coords
[4] = coords
[6];
192 coords
[6] = coords
[0];
202 std::string
tilemap::getPathToResource(const std::string
& name
)
204 return resource::getPathToResource("tilemaps/" + name
+ ".json");
210 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.041263 seconds and 4 git commands to generate.