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"
39 class Tilemap::Impl
: public Mippleton
<Impl
>
41 static GLint
filterFromString(const std::string
& filter
)
43 if (filter
== "linear")
47 else if (filter
== "nearest_mipmap_nearest")
49 return GL_NEAREST_MIPMAP_NEAREST
;
51 else if (filter
== "nearest_mipmap_linear")
53 return GL_NEAREST_MIPMAP_LINEAR
;
55 else if (filter
== "linear_mipmap_nearest")
57 return GL_LINEAR_MIPMAP_NEAREST
;
59 else if (filter
== "linear_mipmap_linear")
61 return GL_LINEAR_MIPMAP_LINEAR
;
67 static GLint
wrapFromString(const std::string
& wrap
)
78 Impl(const std::string
& name
) :
79 Mippleton
<Impl
>(name
),
80 magFilter_(GL_NEAREST
),
81 minFilter_(GL_NEAREST
),
92 Deserializer
deserializer(Tilemap::getPath(getName()));
94 SerializableP root
= deserializer
.deserialize();
98 Serializable::Map rootObj
;
100 if (root
->get(rootObj
))
102 Serializable::Map::iterator it
;
104 if ((it
= rootObj
.find("tiles_s")) != rootObj
.end())
107 if ((*it
).second
->get(value
))
109 nTilesS_
= unsigned(value
);
112 if ((it
= rootObj
.find("tiles_t")) != rootObj
.end())
115 if ((*it
).second
->get(value
))
117 nTilesT_
= unsigned(value
);
120 if ((it
= rootObj
.find("min_filter")) != rootObj
.end())
123 if ((*it
).second
->get(value
))
125 minFilter_
= filterFromString(value
);
128 if ((it
= rootObj
.find("mag_filter")) != rootObj
.end())
131 if ((*it
).second
->get(value
))
133 magFilter_
= filterFromString(value
);
136 if ((it
= rootObj
.find("wrap_s")) != rootObj
.end())
139 if ((*it
).second
->get(value
))
141 wrapS_
= wrapFromString(value
);
144 if ((it
= rootObj
.find("wrap_t")) != rootObj
.end())
147 if ((*it
).second
->get(value
))
149 wrapT_
= wrapFromString(value
);
165 Tilemap::Tilemap(const std::string
& name
) :
167 impl_(Tilemap::Impl::getInstance(name
))
169 setMinFilter(impl_
->minFilter_
);
170 setMagFilter(impl_
->magFilter_
);
171 setWrapS(impl_
->wrapS_
);
172 setWrapT(impl_
->wrapT_
);
176 bool Tilemap::getTileCoords(unsigned index
, Scalar coords
[8]) const
178 // make sure the index represents a real tile
179 if (index
>= impl_
->nTilesS_
* impl_
->nTilesT_
) return false;
181 Scalar w
= 1.0 / Scalar(impl_
->nTilesS_
);
182 Scalar h
= 1.0 / Scalar(impl_
->nTilesT_
);
184 coords
[0] = Scalar(index
% impl_
->nTilesS_
) * w
;
185 coords
[1] = (Scalar(impl_
->nTilesT_
- 1) -
186 Scalar(index
/ impl_
->nTilesS_
)) * h
;
187 coords
[2] = coords
[0] + w
;
188 coords
[3] = coords
[1];
189 coords
[4] = coords
[2];
190 coords
[5] = coords
[1] + h
;
191 coords
[6] = coords
[0];
192 coords
[7] = coords
[5];
197 bool Tilemap::getTileCoords(unsigned index
, Scalar coords
[8],
198 Orientation orientation
) const
200 if (getTileCoords(index
, coords
))
202 if (orientation
& FLIP
)
204 // this looks kinda weird, but it's just swapping in a way that
205 // doesn't require an intermediate variable
206 coords
[1] = coords
[5];
207 coords
[5] = coords
[3];
208 coords
[3] = coords
[7];
209 coords
[7] = coords
[5];
211 if (orientation
& REVERSE
)
213 coords
[0] = coords
[2];
214 coords
[2] = coords
[6];
215 coords
[4] = coords
[6];
216 coords
[6] = coords
[0];
226 std::string
Tilemap::getPath(const std::string
& name
)
228 return Resource::getPath("tilemaps/" + name
+ ".json");
234 /** vim: set ts=4 sw=4 tw=80: *************************************************/