]> Dogcows Code - chaz/yoink/blob - src/Tilemap.cc
renamed mippleton to library
[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 <Moof/Library.hh>
30 #include <Moof/OpenGL.hh>
31 #include <Moof/Script.hh>
32
33 #include "Tilemap.hh"
34
35
36 struct Tilemap::Impl : public Mf::Library<Impl>
37 {
38 Impl(const std::string& name) :
39 Mf::Library<Impl>(name),
40 mMagFilter(GL_NEAREST),
41 mMinFilter(GL_NEAREST),
42 mTilesS(1),
43 mTilesT(1),
44 mWrapS(GL_CLAMP),
45 mWrapT(GL_CLAMP)
46 {
47 loadFromFile();
48 }
49
50
51 static void bindScriptConstants(Mf::Script& script)
52 {
53 script.push(GL_CLAMP); script.set("CLAMP");
54 script.push(GL_REPEAT); script.set("REPEAT");
55 script.push(GL_LINEAR); script.set("LINEAR");
56 script.push(GL_NEAREST); script.set("NEAREST");
57 script.push(GL_LINEAR_MIPMAP_LINEAR); script.set("LINEAR_MIPMAP_LINEAR");
58 script.push(GL_LINEAR_MIPMAP_NEAREST); script.set("LINEAR_MIPMAP_NEAREST");
59 script.push(GL_NEAREST_MIPMAP_LINEAR); script.set("NEAREST_MIPMAP_LINEAR");
60 script.push(GL_NEAREST_MIPMAP_NEAREST); script.set("NEAREST_MIPMAP_NEAREST");
61 }
62
63 void loadFromFile()
64 {
65 Mf::Script script;
66 std::string filePath = Tilemap::getPath(getName());
67
68 script.importStandardLibraries();
69 importLogScript(script);
70 bindScriptConstants(script);
71
72 if (script.doFile(filePath) != Mf::Script::SUCCESS)
73 {
74 std::string str;
75 script[-1].get(str);
76 Mf::logScript("%s", str.c_str());
77 return; // TODO needs a better exit strategy
78 }
79
80 Mf::logInfo("loading tiles from tilemap %s", filePath.c_str());
81
82 Mf::Script::Value globals = script.getGlobalTable();
83 Mf::Script::Value top = script[-1];
84
85 globals.pushField("tiles_s");
86 top.get(mTilesS);
87
88 globals.pushField("tiles_t");
89 top.get(mTilesT);
90
91 globals.pushField("min_filter");
92 top.get(mMinFilter);
93
94 globals.pushField("mag_filter");
95 top.get(mMagFilter);
96
97 globals.pushField("wrap_s");
98 top.get(mWrapS);
99
100 globals.pushField("wrap_t");
101 top.get(mWrapT);
102 }
103
104
105 bool getTileCoords(Tilemap::Index index, Mf::Scalar coords[8]) const
106 {
107 // make sure the index represents a real tile
108 if (index >= mTilesS * mTilesT) return false;
109
110 Mf::Scalar w = 1.0 / Mf::Scalar(mTilesS);
111 Mf::Scalar h = 1.0 / Mf::Scalar(mTilesT);
112
113 coords[0] = Mf::Scalar(index % mTilesS) * w;
114 coords[1] = (Mf::Scalar(mTilesT - 1) -
115 Mf::Scalar(index / mTilesS)) * h;
116 coords[2] = coords[0] + w;
117 coords[3] = coords[1];
118 coords[4] = coords[2];
119 coords[5] = coords[1] + h;
120 coords[6] = coords[0];
121 coords[7] = coords[5];
122
123 return true;
124 }
125
126
127 GLuint mMagFilter;
128 GLuint mMinFilter;
129 unsigned mTilesS;
130 unsigned mTilesT;
131 GLuint mWrapS;
132 GLuint mWrapT;
133 };
134
135
136 Tilemap::Tilemap(const std::string& name) :
137 Texture(name),
138 mImpl(Tilemap::Impl::getInstance(name))
139 {
140 setMinFilter(mImpl->mMinFilter);
141 setMagFilter(mImpl->mMagFilter);
142 setWrapS(mImpl->mWrapS);
143 setWrapT(mImpl->mWrapT);
144 }
145
146
147 bool Tilemap::getTileCoords(Index index, Mf::Scalar coords[8]) const
148 {
149 // pass through
150 return mImpl->getTileCoords(index, coords);
151 }
152
153 bool Tilemap::getTileCoords(Index index, Mf::Scalar coords[8],
154 Orientation orientation) const
155 {
156 if (getTileCoords(index, coords))
157 {
158 if (orientation & FLIP)
159 {
160 // this looks kinda weird, but it's just swapping in a way that
161 // doesn't require an intermediate variable
162 coords[1] = coords[5];
163 coords[5] = coords[3];
164 coords[3] = coords[7];
165 coords[7] = coords[5];
166 }
167 if (orientation & REVERSE)
168 {
169 coords[0] = coords[2];
170 coords[2] = coords[6];
171 coords[4] = coords[6];
172 coords[6] = coords[0];
173 }
174
175 return true;
176 }
177
178 return false;
179 }
180
181
182 std::string Tilemap::getPath(const std::string& name)
183 {
184 return Resource::getPath("tilemaps/" + name + ".lua");
185 }
186
187
188 /** vim: set ts=4 sw=4 tw=80: *************************************************/
189
This page took 0.035945 seconds and 4 git commands to generate.