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::TilemapImpl
: public Mippleton
<TilemapImpl
>
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 TilemapImpl(const std::string
& name
) :
79 Mippleton
<TilemapImpl
>(name
),
80 magFilter_(GL_NEAREST
),
81 minFilter_(GL_NEAREST
),
92 Deserializer
deserializer(Tilemap::getPathToResource(getName()));
94 SerializablePtr root
= deserializer
.deserialize();
98 std::map
<std::string
,SerializablePtr
> rootObj
;
100 if (root
->get(rootObj
))
102 std::map
<std::string
,SerializablePtr
>::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::TilemapImpl::retain(name
), &Tilemap::TilemapImpl::release
)
171 setMinFilter(impl_
->minFilter_
);
172 setMagFilter(impl_
->magFilter_
);
173 setWrapS(impl_
->wrapS_
);
174 setWrapT(impl_
->wrapT_
);
178 bool Tilemap::getTileCoords(unsigned index
, Scalar coords
[8]) const
180 // make sure the index represents a real tile
181 if (index
>= impl_
->nTilesS_
* impl_
->nTilesT_
) return false;
183 Scalar w
= 1.0 / Scalar(impl_
->nTilesS_
);
184 Scalar h
= 1.0 / Scalar(impl_
->nTilesT_
);
186 coords
[0] = Scalar(index
% impl_
->nTilesS_
) * w
;
187 coords
[1] = (Scalar(impl_
->nTilesT_
- 1) -
188 Scalar(index
/ impl_
->nTilesS_
)) * h
;
189 coords
[2] = coords
[0] + w
;
190 coords
[3] = coords
[1];
191 coords
[4] = coords
[2];
192 coords
[5] = coords
[1] + h
;
193 coords
[6] = coords
[0];
194 coords
[7] = coords
[5];
199 bool Tilemap::getTileCoords(unsigned index
, Scalar coords
[8],
200 Orientation orientation
) const
202 if (getTileCoords(index
, coords
))
204 if (orientation
& FLIP
)
206 // this looks kinda weird, but it's just swapping in a way that
207 // doesn't require an intermediate variable
208 coords
[1] = coords
[5];
209 coords
[5] = coords
[3];
210 coords
[3] = coords
[7];
211 coords
[7] = coords
[5];
213 if (orientation
& REVERSE
)
215 coords
[0] = coords
[2];
216 coords
[2] = coords
[6];
217 coords
[4] = coords
[6];
218 coords
[6] = coords
[0];
228 std::string
Tilemap::getPathToResource(const std::string
& name
)
230 return Resource::getPathToResource("tilemaps/" + name
+ ".json");
236 /** vim: set ts=4 sw=4 tw=80: *************************************************/