]>
Dogcows Code - chaz/yoink/blob - src/Moof/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"
39 class Tilemap::TilemapImpl
: public Mippleton
<TilemapImpl
>
42 TilemapImpl(const std::string
& name
) :
43 Mippleton
<TilemapImpl
>(name
),
46 minFilter_(GL_NEAREST
),
47 maxFilter_(GL_NEAREST
),
56 Deserializer
deserializer(Tilemap::getPathToResource(getName()));
58 SerializablePtr root
= deserializer
.deserialize();
62 std::map
<std::string
,SerializablePtr
> rootObj
;
64 if (root
->get(rootObj
))
66 std::map
<std::string
,SerializablePtr
>::iterator it
;
68 if ((it
= rootObj
.find("TilesU")) != rootObj
.end())
71 if ((*it
).second
->get(value
))
73 tilesU_
= unsigned(value
);
76 if ((it
= rootObj
.find("TilesV")) != rootObj
.end())
79 if ((*it
).second
->get(value
))
81 tilesV_
= unsigned(value
);
84 if ((it
= rootObj
.find("MinFilter")) != rootObj
.end())
87 if ((*it
).second
->get(value
))
89 if (value
== "Linear")
91 minFilter_
= GL_LINEAR
;
95 if ((it
= rootObj
.find("MaxFilter")) != rootObj
.end())
98 if ((*it
).second
->get(value
))
100 if (value
== "Linear")
102 maxFilter_
= GL_LINEAR
;
106 if ((it
= rootObj
.find("WrapU")) != rootObj
.end())
109 if ((*it
).second
->get(value
))
111 if (value
== "Repeat")
117 if ((it
= rootObj
.find("WrapV")) != rootObj
.end())
120 if ((*it
).second
->get(value
))
122 if (value
== "Repeat")
141 Tilemap::Tilemap(const std::string
& name
) :
143 impl_(Tilemap::TilemapImpl::retain(name
), &Tilemap::TilemapImpl::release
)
145 setMinFilter(impl_
->minFilter_
);
146 setMaxFilter(impl_
->maxFilter_
);
147 setWrapU(impl_
->wrapU_
);
148 setWrapV(impl_
->wrapV_
);
153 bool Tilemap::getTileCoords(unsigned index
, Scalar coords
[8])
155 // make sure the index represents a real tile
156 if (index
>= impl_
->tilesU_
* impl_
->tilesV_
) return false;
158 Scalar w
= 1.0 / Scalar(impl_
->tilesU_
);
159 Scalar h
= 1.0 / Scalar(impl_
->tilesV_
);
161 coords
[0] = Scalar(index
% impl_
->tilesU_
) * w
;
162 coords
[1] = (Scalar(impl_
->tilesV_
- 1) -
163 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],
175 Orientation orientation
)
177 if (getTileCoords(index
, coords
))
179 if (orientation
& FLIP
)
181 // this looks kinda weird, but it's just swapping in a way that
182 // doesn't require an intermediate variable
183 coords
[1] = coords
[5];
184 coords
[5] = coords
[3];
185 coords
[3] = coords
[7];
186 coords
[7] = coords
[5];
188 if (orientation
& REVERSE
)
190 coords
[0] = coords
[2];
191 coords
[2] = coords
[6];
192 coords
[4] = coords
[6];
193 coords
[6] = coords
[0];
203 std::string
Tilemap::getPathToResource(const std::string
& name
)
205 return Resource::getPathToResource("tilemaps/" + name
+ ".json");
211 /** vim: set ts=4 sw=4 tw=80: *************************************************/
This page took 0.04333 seconds and 4 git commands to generate.