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