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