]> Dogcows Code - chaz/yoink/blob - src/tilemap.cc
4a55c65eef9f7fa7871507b9dad8808a05e362d6
[chaz/yoink] / src / 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
34 #include "tilemap.hh"
35
36
37 namespace dc {
38
39
40 class tilemap::tilemap_impl : public mippleton<tilemap_impl>
41 {
42 public:
43 tilemap_impl(const std::string& name) :
44 mippleton<tilemap_impl>(name),
45 tilesU_(1),
46 tilesV_(1),
47 minFilter_(GL_NEAREST),
48 maxFilter_(GL_NEAREST),
49 wrapU_(GL_CLAMP),
50 wrapV_(GL_CLAMP)
51 {
52 loadFromFile();
53 }
54
55 void loadFromFile()
56 {
57 deserializer in(tilemap::getPathToResource(getName()));
58
59 serializable_ptr root = in.deserialize();
60
61 if (root)
62 {
63 std::map<std::string,serializable_ptr> rootMap;
64
65 if (root->get(rootMap))
66 {
67 std::map<std::string,serializable_ptr>::iterator it;
68
69 if ((it = rootMap.find("TilesU")) != rootMap.end())
70 {
71 long value;
72 if ((*it).second->get(value))
73 {
74 tilesU_ = unsigned(value);
75 }
76 }
77 if ((it = rootMap.find("TilesV")) != rootMap.end())
78 {
79 long value;
80 if ((*it).second->get(value))
81 {
82 tilesV_ = unsigned(value);
83 }
84 }
85 if ((it = rootMap.find("MinFilter")) != rootMap.end())
86 {
87 std::string value;
88 if ((*it).second->get(value))
89 {
90 if (value == "Linear")
91 {
92 minFilter_ = GL_LINEAR;
93 }
94 }
95 }
96 if ((it = rootMap.find("MaxFilter")) != rootMap.end())
97 {
98 std::string value;
99 if ((*it).second->get(value))
100 {
101 if (value == "Linear")
102 {
103 maxFilter_ = GL_LINEAR;
104 }
105 }
106 }
107 if ((it = rootMap.find("WrapU")) != rootMap.end())
108 {
109 std::string value;
110 if ((*it).second->get(value))
111 {
112 if (value == "Repeat")
113 {
114 wrapU_ = GL_REPEAT;
115 }
116 }
117 }
118 if ((it = rootMap.find("WrapV")) != rootMap.end())
119 {
120 std::string value;
121 if ((*it).second->get(value))
122 {
123 if (value == "Repeat")
124 {
125 wrapV_ = GL_REPEAT;
126 }
127 }
128 }
129 }
130 }
131 }
132
133 unsigned tilesU_;
134 unsigned tilesV_;
135 GLuint minFilter_;
136 GLuint maxFilter_;
137 GLuint wrapU_;
138 GLuint wrapV_;
139 };
140
141
142 tilemap::tilemap(const std::string& name) :
143 texture(name),
144 impl(tilemap::tilemap_impl::retain(name), &tilemap::tilemap_impl::release)
145 {
146 setMinFilter(impl->minFilter_);
147 setMaxFilter(impl->maxFilter_);
148 setWrapU(impl->wrapU_);
149 setWrapV(impl->wrapV_);
150 applyChanges();
151 }
152
153
154 bool tilemap::getTileCoords(unsigned index, scalar coords[8])
155 {
156 // make sure the index represents a real tile
157 if (index >= impl->tilesU_ * impl->tilesV_) return false;
158
159 scalar w = 1.0 / scalar(impl->tilesU_);
160 scalar h = 1.0 / scalar(impl->tilesV_);
161
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];
170
171 return true;
172 }
173
174 bool tilemap::getTileCoords(unsigned index, scalar coords[8], orientation what)
175 {
176 if (getTileCoords(index, coords))
177 {
178 if (what & flip)
179 {
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];
186 }
187 if (what & reverse)
188 {
189 coords[0] = coords[2];
190 coords[2] = coords[6];
191 coords[4] = coords[6];
192 coords[6] = coords[0];
193 }
194
195 return true;
196 }
197
198 return false;
199 }
200
201
202 std::string tilemap::getPathToResource(const std::string& name)
203 {
204 return resource::getPathToResource("tilemaps/" + name + ".json");
205 }
206
207
208 } // namespace dc
209
210 /** vim: set ts=4 sw=4 tw=80: *************************************************/
211
This page took 0.03519 seconds and 3 git commands to generate.