]> Dogcows Code - chaz/yoink/blob - src/Moof/Tilemap.cc
9f24b1bce91547a200bf7de7090f9ce6a8110ca8
[chaz/yoink] / src / Moof / Tilemap.cc
1
2 /*******************************************************************************
3
4 Copyright (c) 2009, Charles McGarvey
5 All rights reserved.
6
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9
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.
15
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.
26
27 *******************************************************************************/
28
29 #include "Deserializer.hh"
30 #include "Mippleton.hh"
31 #include "OpenGL.hh"
32 #include "Serializable.hh"
33 #include "Tilemap.hh"
34
35
36 namespace Mf {
37
38
39 class Tilemap::TilemapImpl : public Mippleton<TilemapImpl>
40 {
41 static GLint filterFromString(const std::string& filter)
42 {
43 if (filter == "linear")
44 {
45 return GL_LINEAR;
46 }
47 else if (filter == "nearest_mipmap_nearest")
48 {
49 return GL_NEAREST_MIPMAP_NEAREST;
50 }
51 else if (filter == "nearest_mipmap_linear")
52 {
53 return GL_NEAREST_MIPMAP_LINEAR;
54 }
55 else if (filter == "linear_mipmap_nearest")
56 {
57 return GL_LINEAR_MIPMAP_NEAREST;
58 }
59 else if (filter == "linear_mipmap_linear")
60 {
61 return GL_LINEAR_MIPMAP_LINEAR;
62 }
63
64 return GL_NEAREST;
65 }
66
67 static GLint wrapFromString(const std::string& wrap)
68 {
69 if (wrap == "repeat")
70 {
71 return GL_REPEAT;
72 }
73
74 return GL_CLAMP;
75 }
76
77 public:
78 TilemapImpl(const std::string& name) :
79 Mippleton<TilemapImpl>(name),
80 magFilter_(GL_NEAREST),
81 minFilter_(GL_NEAREST),
82 nTilesS_(1),
83 nTilesT_(1),
84 wrapS_(GL_CLAMP),
85 wrapT_(GL_CLAMP)
86 {
87 loadFromFile();
88 }
89
90 void loadFromFile()
91 {
92 Deserializer deserializer(Tilemap::getPathToResource(getName()));
93
94 SerializablePtr root = deserializer.deserialize();
95
96 if (root)
97 {
98 std::map<std::string,SerializablePtr> rootObj;
99
100 if (root->get(rootObj))
101 {
102 std::map<std::string,SerializablePtr>::iterator it;
103
104 if ((it = rootObj.find("tiles_s")) != rootObj.end())
105 {
106 long value;
107 if ((*it).second->get(value))
108 {
109 nTilesS_ = unsigned(value);
110 }
111 }
112 if ((it = rootObj.find("tiles_t")) != rootObj.end())
113 {
114 long value;
115 if ((*it).second->get(value))
116 {
117 nTilesT_ = unsigned(value);
118 }
119 }
120 if ((it = rootObj.find("min_filter")) != rootObj.end())
121 {
122 std::string value;
123 if ((*it).second->get(value))
124 {
125 minFilter_ = filterFromString(value);
126 }
127 }
128 if ((it = rootObj.find("mag_filter")) != rootObj.end())
129 {
130 std::string value;
131 if ((*it).second->get(value))
132 {
133 magFilter_ = filterFromString(value);
134 }
135 }
136 if ((it = rootObj.find("wrap_s")) != rootObj.end())
137 {
138 std::string value;
139 if ((*it).second->get(value))
140 {
141 wrapS_ = wrapFromString(value);
142 }
143 }
144 if ((it = rootObj.find("wrap_t")) != rootObj.end())
145 {
146 std::string value;
147 if ((*it).second->get(value))
148 {
149 wrapT_ = wrapFromString(value);
150 }
151 }
152 }
153 }
154 }
155
156 GLuint magFilter_;
157 GLuint minFilter_;
158 unsigned nTilesS_;
159 unsigned nTilesT_;
160 GLuint wrapS_;
161 GLuint wrapT_;
162 };
163
164
165 Tilemap::Tilemap(const std::string& name) :
166 Texture(name),
167 impl_(Tilemap::TilemapImpl::retain(name), &Tilemap::TilemapImpl::release)
168 {
169 bind();
170
171 setMinFilter(impl_->minFilter_);
172 setMagFilter(impl_->magFilter_);
173 setWrapS(impl_->wrapS_);
174 setWrapT(impl_->wrapT_);
175 }
176
177
178 bool Tilemap::getTileCoords(unsigned index, Scalar coords[8]) const
179 {
180 // make sure the index represents a real tile
181 if (index >= impl_->nTilesS_ * impl_->nTilesT_) return false;
182
183 Scalar w = 1.0 / Scalar(impl_->nTilesS_);
184 Scalar h = 1.0 / Scalar(impl_->nTilesT_);
185
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];
195
196 return true;
197 }
198
199 bool Tilemap::getTileCoords(unsigned index, Scalar coords[8],
200 Orientation orientation) const
201 {
202 if (getTileCoords(index, coords))
203 {
204 if (orientation & FLIP)
205 {
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];
212 }
213 if (orientation & REVERSE)
214 {
215 coords[0] = coords[2];
216 coords[2] = coords[6];
217 coords[4] = coords[6];
218 coords[6] = coords[0];
219 }
220
221 return true;
222 }
223
224 return false;
225 }
226
227
228 std::string Tilemap::getPathToResource(const std::string& name)
229 {
230 return Resource::getPathToResource("tilemaps/" + name + ".json");
231 }
232
233
234 } // namespace Mf
235
236 /** vim: set ts=4 sw=4 tw=80: *************************************************/
237
This page took 0.038665 seconds and 4 git commands to generate.