2 /*******************************************************************************
4 Copyright (c) 2009, Charles McGarvey
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
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.
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.
27 *******************************************************************************/
29 #include <Moof/Library.hh>
30 #include <Moof/OpenGL.hh>
31 #include <Moof/Script.hh>
36 struct Tilemap::Impl
: public Mf::Library
<Impl
>
38 Impl(const std::string
& name
) :
39 Mf::Library
<Impl
>(name
),
40 mMagFilter(GL_NEAREST
),
41 mMinFilter(GL_NEAREST
),
51 static void bindScriptConstants(Mf::Script
& script
)
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");
66 std::string filePath
= Tilemap::getPath(getName());
68 script
.importStandardLibraries();
69 importLogPrintFunction(script
);
70 bindScriptConstants(script
);
72 if (script
.doFile(filePath
) != Mf::Script::SUCCESS
)
76 Mf::logScript("%s", str
.c_str());
77 return; // TODO needs a better exit strategy
80 Mf::logInfo("loading tiles from tilemap %s", filePath
.c_str());
82 Mf::Script::Value globals
= script
.getGlobalTable();
83 Mf::Script::Value top
= script
[-1];
85 globals
.pushField("tiles_s");
88 globals
.pushField("tiles_t");
91 globals
.pushField("min_filter");
94 globals
.pushField("mag_filter");
97 globals
.pushField("wrap_s");
100 globals
.pushField("wrap_t");
105 bool getTileCoords(Tilemap::Index index
, Mf::Scalar coords
[8]) const
107 // make sure the index represents a real tile
108 if (index
>= mTilesS
* mTilesT
) return false;
110 Mf::Scalar w
= 1.0 / Mf::Scalar(mTilesS
);
111 Mf::Scalar h
= 1.0 / Mf::Scalar(mTilesT
);
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];
136 Tilemap::Tilemap(const std::string
& name
) :
138 mImpl(Tilemap::Impl::getInstance(name
))
140 setMinFilter(mImpl
->mMinFilter
);
141 setMagFilter(mImpl
->mMagFilter
);
142 setWrapS(mImpl
->mWrapS
);
143 setWrapT(mImpl
->mWrapT
);
147 bool Tilemap::getTileCoords(Index index
, Mf::Scalar coords
[8]) const
150 return mImpl
->getTileCoords(index
, coords
);
153 bool Tilemap::getTileCoords(Index index
, Mf::Scalar coords
[8],
154 Orientation orientation
) const
156 if (getTileCoords(index
, coords
))
158 if (orientation
& FLIP
)
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];
167 if (orientation
& REVERSE
)
169 coords
[0] = coords
[2];
170 coords
[2] = coords
[6];
171 coords
[4] = coords
[6];
172 coords
[6] = coords
[0];
182 std::string
Tilemap::getPath(const std::string
& name
)
184 return Resource::getPath("tilemaps/" + name
+ ".lua");
188 /** vim: set ts=4 sw=4 tw=80: *************************************************/