From: Charles McGarvey Date: Sun, 24 Jan 2010 01:14:26 +0000 (-0700) Subject: library class revamped as manager, goodbye tilemap X-Git-Url: https://git.brokenzipper.com/gitweb?p=chaz%2Fyoink;a=commitdiff_plain;h=e973a129b5b83b628ba3f09e8c95682fc74080cd library class revamped as manager, goodbye tilemap --- diff --git a/data/scenes/Classic.lua b/data/scenes/Classic.lua index 2ec9e02..10ba6a2 100644 --- a/data/scenes/Classic.lua +++ b/data/scenes/Classic.lua @@ -1,10 +1,9 @@ --- Scene: Classic Yoink --- created by Neil Carter --- converted to Lua by Charles McGarvey - -LogInfo("-----", "Scene: Classic", "Created by Neil Carter", - "Converted to Lua by Charles McGarvey", "-----") +LogInfo("-----", + "Scene: Classic", + "Created by Neil Carter", + "Converted to Lua by Charles McGarvey", + "-----") -- Scene API: -- diff --git a/data/textures/AlienWarrior.png b/data/textures/AlienWarrior.png index ece94d0..c93ee69 100644 Binary files a/data/textures/AlienWarrior.png and b/data/textures/AlienWarrior.png differ diff --git a/data/textures/BackgroundFar.png b/data/textures/BackgroundFar.png index df10334..486c191 100644 Binary files a/data/textures/BackgroundFar.png and b/data/textures/BackgroundFar.png differ diff --git a/data/textures/BackgroundNear.png b/data/textures/BackgroundNear.png index fd30b7f..94e6747 100644 Binary files a/data/textures/BackgroundNear.png and b/data/textures/BackgroundNear.png differ diff --git a/data/textures/BigExplosion.png b/data/textures/BigExplosion.png index 6bfc837..58bfe0d 100644 Binary files a/data/textures/BigExplosion.png and b/data/textures/BigExplosion.png differ diff --git a/data/textures/Bonuses.png b/data/textures/Bonuses.png index 1e9a21f..2ab194e 100644 Binary files a/data/textures/Bonuses.png and b/data/textures/Bonuses.png differ diff --git a/data/textures/Building.png b/data/textures/Building.png index 43a4586..5cb2a63 100644 Binary files a/data/textures/Building.png and b/data/textures/Building.png differ diff --git a/data/textures/Font.png b/data/textures/Font.png index 30b54aa..dd90aa5 100644 Binary files a/data/textures/Font.png and b/data/textures/Font.png differ diff --git a/data/textures/Heroine.png b/data/textures/Heroine.png index aff3282..b08bf9a 100644 Binary files a/data/textures/Heroine.png and b/data/textures/Heroine.png differ diff --git a/data/textures/Jetbot.png b/data/textures/Jetbot.png index 1f02c6d..c1c6b0a 100644 Binary files a/data/textures/Jetbot.png and b/data/textures/Jetbot.png differ diff --git a/data/textures/Particles.png b/data/textures/Particles.png index 659fae5..0fae414 100644 Binary files a/data/textures/Particles.png and b/data/textures/Particles.png differ diff --git a/data/textures/RobotTrooper.png b/data/textures/RobotTrooper.png index ee76d85..e5cf0ca 100644 Binary files a/data/textures/RobotTrooper.png and b/data/textures/RobotTrooper.png differ diff --git a/data/textures/Scenery.png b/data/textures/Scenery.png index 99ede6e..b2fe47c 100644 Binary files a/data/textures/Scenery.png and b/data/textures/Scenery.png differ diff --git a/data/textures/StatusBars.png b/data/textures/StatusBars.png index 49abbc8..904ea4c 100644 Binary files a/data/textures/StatusBars.png and b/data/textures/StatusBars.png differ diff --git a/data/textures/TowerBlock1.png b/data/textures/TowerBlock1.png index 67ec31b..677aa21 100644 Binary files a/data/textures/TowerBlock1.png and b/data/textures/TowerBlock1.png differ diff --git a/data/textures/Trees.png b/data/textures/Trees.png index f665ff6..fc9fc35 100644 Binary files a/data/textures/Trees.png and b/data/textures/Trees.png differ diff --git a/data/tilemaps/AlienWarrior.lua b/data/tilemaps/AlienWarrior.lua deleted file mode 100644 index a98bb7c..0000000 --- a/data/tilemaps/AlienWarrior.lua +++ /dev/null @@ -1,4 +0,0 @@ -tiles_s = 8 -tiles_t = 4 -min_filter = NEAREST -mag_filter = NEAREST diff --git a/data/tilemaps/BackgroundFar.lua b/data/tilemaps/BackgroundFar.lua deleted file mode 100644 index 1128761..0000000 --- a/data/tilemaps/BackgroundFar.lua +++ /dev/null @@ -1,6 +0,0 @@ -tiles_s = 1 -tiles_t = 1 -min_filter = LINEAR -mag_filter = LINEAR -wrap_s = CLAMP -wrap_t = CLAMP diff --git a/data/tilemaps/BackgroundNear.lua b/data/tilemaps/BackgroundNear.lua deleted file mode 100644 index 26d50f2..0000000 --- a/data/tilemaps/BackgroundNear.lua +++ /dev/null @@ -1,6 +0,0 @@ -tiles_s = 1 -tiles_t = 1 -min_filter = LINEAR -mag_filter = LINEAR -wrap_s = REPEAT -wrap_t = REPEAT diff --git a/data/tilemaps/BigExplosion.lua b/data/tilemaps/BigExplosion.lua deleted file mode 100644 index 2acfc3c..0000000 --- a/data/tilemaps/BigExplosion.lua +++ /dev/null @@ -1,6 +0,0 @@ -tiles_s = 8 -tiles_t = 1 -min_filter = NEAREST -mag_filter = NEAREST -wrap_s = CLAMP -wrap_t = CLAMP diff --git a/data/tilemaps/Bonuses.lua b/data/tilemaps/Bonuses.lua deleted file mode 100644 index e2cf01d..0000000 --- a/data/tilemaps/Bonuses.lua +++ /dev/null @@ -1,6 +0,0 @@ -tiles_s = 8 -tiles_t = 4 -min_filter = NEAREST -mag_filter = NEAREST -wrap_s = CLAMP -wrap_t = CLAMP diff --git a/data/tilemaps/Building.lua b/data/tilemaps/Building.lua deleted file mode 100644 index e2cf01d..0000000 --- a/data/tilemaps/Building.lua +++ /dev/null @@ -1,6 +0,0 @@ -tiles_s = 8 -tiles_t = 4 -min_filter = NEAREST -mag_filter = NEAREST -wrap_s = CLAMP -wrap_t = CLAMP diff --git a/data/tilemaps/Font.lua b/data/tilemaps/Font.lua deleted file mode 100644 index d3670e1..0000000 --- a/data/tilemaps/Font.lua +++ /dev/null @@ -1,4 +0,0 @@ -tiles_s = 8 -tiles_t = 8 -min_filter = NEAREST -mag_filter = NEAREST diff --git a/data/tilemaps/Heroine.lua b/data/tilemaps/Heroine.lua deleted file mode 100644 index a98bb7c..0000000 --- a/data/tilemaps/Heroine.lua +++ /dev/null @@ -1,4 +0,0 @@ -tiles_s = 8 -tiles_t = 4 -min_filter = NEAREST -mag_filter = NEAREST diff --git a/data/tilemaps/Jetbot.lua b/data/tilemaps/Jetbot.lua deleted file mode 100644 index d62e532..0000000 --- a/data/tilemaps/Jetbot.lua +++ /dev/null @@ -1,4 +0,0 @@ -tiles_s = 4 -tiles_t = 2 -min_filter = NEAREST -mag_filter = NEAREST diff --git a/data/tilemaps/Particles.lua b/data/tilemaps/Particles.lua deleted file mode 100644 index e2cf01d..0000000 --- a/data/tilemaps/Particles.lua +++ /dev/null @@ -1,6 +0,0 @@ -tiles_s = 8 -tiles_t = 4 -min_filter = NEAREST -mag_filter = NEAREST -wrap_s = CLAMP -wrap_t = CLAMP diff --git a/data/tilemaps/RobotTrooper.lua b/data/tilemaps/RobotTrooper.lua deleted file mode 100644 index a98bb7c..0000000 --- a/data/tilemaps/RobotTrooper.lua +++ /dev/null @@ -1,4 +0,0 @@ -tiles_s = 8 -tiles_t = 4 -min_filter = NEAREST -mag_filter = NEAREST diff --git a/data/tilemaps/Scenery.lua b/data/tilemaps/Scenery.lua deleted file mode 100644 index d42abc4..0000000 --- a/data/tilemaps/Scenery.lua +++ /dev/null @@ -1,6 +0,0 @@ -tiles_s = 4 -tiles_t = 4 -min_filter = NEAREST -mag_filter = NEAREST -wrap_s = REPEAT -wrap_t = REPEAT diff --git a/data/tilemaps/StatusBars.lua b/data/tilemaps/StatusBars.lua deleted file mode 100644 index 0b6a8fe..0000000 --- a/data/tilemaps/StatusBars.lua +++ /dev/null @@ -1,4 +0,0 @@ -tiles_s = 4 -tiles_t = 1 -min_filter = NEAREST -mag_filter = NEAREST diff --git a/data/tilemaps/TowerBlock1.lua b/data/tilemaps/TowerBlock1.lua deleted file mode 100644 index e31c4cc..0000000 --- a/data/tilemaps/TowerBlock1.lua +++ /dev/null @@ -1,6 +0,0 @@ -tiles_s = 4 -tiles_t = 4 -min_filter = NEAREST -mag_filter = NEAREST -wrap_s = CLAMP -wrap_t = CLAMP diff --git a/data/tilemaps/Trees.lua b/data/tilemaps/Trees.lua deleted file mode 100644 index 4f97474..0000000 --- a/data/tilemaps/Trees.lua +++ /dev/null @@ -1,6 +0,0 @@ -tiles_s = 2 -tiles_t = 1 -min_filter = NEAREST -mag_filter = NEAREST -wrap_s = CLAMP -wrap_t = CLAMP diff --git a/src/Animation.cc b/src/Animation.cc index 598361b..4779970 100644 --- a/src/Animation.cc +++ b/src/Animation.cc @@ -29,7 +29,7 @@ #include #include -#include +#include #include #include @@ -47,7 +47,7 @@ class Animation::Impl { - friend class Animation; +public: /** * Contains "global" animation data for the various animations which get @@ -55,10 +55,9 @@ class Animation::Impl * which wants to use these loaded sequences. */ - class Data : public Mf::Library + class Data : public Mf::Manager { - friend class Impl; - friend class Mf::Library; + public: /** * A frame of an animation sequence. A frame is merely an index which @@ -68,7 +67,7 @@ class Animation::Impl class Frame { - friend class Impl; + public: unsigned mIndex; ///< Frame index. Mf::Scalar mDuration; ///< Frame duration. @@ -98,9 +97,9 @@ class Animation::Impl * that they should be played. */ - struct Sequence + class Sequence { - friend class Impl; + public: std::vector mFrames; ///< List of frames. Mf::Scalar mDelay; ///< Scale frame durations. @@ -161,10 +160,10 @@ class Animation::Impl * individual sequence. */ - void loadFromFile() + void init(const std::string& name) { Mf::Script script; - std::string filePath = Animation::getPath(getName()); + std::string filePath = Animation::getPath(name); script.importBaseLibrary(); importLogFunctions(script); @@ -186,7 +185,7 @@ class Animation::Impl std::string nameStr; name.get(nameStr); - sequences.insert(std::pair(nameStr, + mSequences.insert(std::pair(nameStr, Sequence(script, table))); return 0; @@ -208,18 +207,7 @@ class Animation::Impl } - /** - * Construction is initialization. The animation class data container - * registers itself as a mippleton and then loads the animation data. - */ - - explicit Data(const std::string& name) : - Mf::Library(name) - { - loadFromFile(); - } - - std::map sequences; ///< List of sequences. + std::map mSequences; ///< List of sequences. }; @@ -246,9 +234,9 @@ class Animation::Impl { std::map::iterator it; - it = mData->sequences.find(name); + it = mData->mSequences.find(name); - if (it != mData->sequences.end()) + if (it != mData->mSequences.end()) { mCurrentSequence = &(*it).second; mFrameCounter = 0; diff --git a/src/Character.cc b/src/Character.cc index 9da4344..2290e3c 100644 --- a/src/Character.cc +++ b/src/Character.cc @@ -126,11 +126,10 @@ void Character::draw(Mf::Scalar alpha) const //glColor3f(1.0f, 1.0f, 1.0f); tilemap.bind(); - Tilemap::Index frame = animation.getFrame(); + Mf::Texture::TileIndex frame = animation.getFrame(); + Mf::Texture::Orientation orientation = Mf::Texture::NORMAL; - Tilemap::Orientation orientation = Tilemap::NORMAL; - - if (mState.velocity[0] < 0.0) orientation = Tilemap::REVERSE; + if (mState.velocity[0] < 0.0) orientation = Mf::Texture::REVERSE; Mf::Scalar coords[8]; tilemap.getTileCoords(frame, coords, orientation); diff --git a/src/Character.hh b/src/Character.hh index 6507906..82804ba 100644 --- a/src/Character.hh +++ b/src/Character.hh @@ -36,9 +36,9 @@ #include #include #include +#include #include "Animation.hh" -#include "Tilemap.hh" @@ -67,7 +67,7 @@ public: //virtual int getOctant(const Mf::Aabb<3>& aabb) const; - Tilemap tilemap; + Mf::Texture tilemap; Animation animation; }; diff --git a/src/Hud.cc b/src/Hud.cc index 1fdf39b..f8f40d9 100644 --- a/src/Hud.cc +++ b/src/Hud.cc @@ -33,7 +33,7 @@ #include "Hud.hh" -ProgressBar::ProgressBar(const Tilemap& tilemap, Tilemap::Index index) : +ProgressBar::ProgressBar(const Mf::Texture& tilemap, Mf::Texture::TileIndex index) : mProgress(0.0), mTilemap(tilemap) { @@ -120,8 +120,8 @@ void ProgressBar::draw(Mf::Scalar alpha) const Hud::Hud(GameState& state) : mState(state), - mBar1(Tilemap("StatusBars"), 0), - mBar2(Tilemap("StatusBars"), 2), + mBar1(Mf::Texture("StatusBars"), 0), + mBar2(Mf::Texture("StatusBars"), 2), mFont("Font") { ASSERT(Mf::video && "no current video context from which to get dimensions"); diff --git a/src/Hud.hh b/src/Hud.hh index 75ced92..2262b06 100644 --- a/src/Hud.hh +++ b/src/Hud.hh @@ -38,9 +38,9 @@ #include #include #include +#include #include "GameState.hh" -#include "Tilemap.hh" // TODO this stuff is still just hacked up @@ -49,7 +49,7 @@ class ProgressBar : public Mf::Drawable { public: - ProgressBar(const Tilemap& tilemap, Tilemap::Index index); + ProgressBar(const Mf::Texture& tilemap, Mf::Texture::TileIndex index); void resize(const Mf::Rectangle& rect); @@ -64,7 +64,7 @@ private: Mf::Vector2 mVertices[8]; Mf::Scalar mWidth; - Tilemap mTilemap; + Mf::Texture mTilemap; Mf::Scalar mTexCoords[8]; Mf::Scalar mMidCoords[2]; }; @@ -112,7 +112,7 @@ private: ProgressBar mBar2; unsigned mNumber; - Tilemap mFont; + Mf::Texture mFont; Mf::Matrix4 mProjection; }; diff --git a/src/Makefile.am b/src/Makefile.am index 3c3fc53..58b3111 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -35,10 +35,10 @@ libmoof_a_SOURCES = \ Moof/Image.hh \ Moof/Interpolator.hh \ Moof/Layer.hh \ - Moof/Library.hh \ Moof/Line.hh \ Moof/Log.cc \ Moof/Log.hh \ + Moof/Manager.hh \ Moof/Math.hh \ Moof/ModalDialog.hh \ Moof/Octree.hh \ @@ -100,8 +100,6 @@ yoink_SOURCES = \ Main.hh \ Scene.cc \ Scene.hh \ - Tilemap.cc \ - Tilemap.hh \ TilemapFont.cc \ TilemapFont.hh \ TitleLayer.cc \ diff --git a/src/Moof/Core.cc b/src/Moof/Core.cc index 9002730..190f4c8 100644 --- a/src/Moof/Core.cc +++ b/src/Moof/Core.cc @@ -32,7 +32,6 @@ #include #include -#include #include #include "fastevents.h" @@ -368,9 +367,7 @@ class Backend_ { public: - Backend_() : - mAlDevice(0), - mAlContext(0) + Backend_() { #if defined(_WIN32) || defined(__WIN32__) if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) @@ -397,31 +394,11 @@ public: return; // fatal } - mAlDevice = alcOpenDevice(0); - mAlContext = alcCreateContext(mAlDevice, 0); - if (!mAlDevice || !mAlContext) - { - const char* error = alcGetString(mAlDevice,alcGetError(mAlDevice)); - gError.init(Error::OPENAL_INIT, error); - return; - } - else - { - alcMakeContextCurrent(mAlContext); - logInfo << "opened sound device `" - << alcGetString(mAlDevice, ALC_DEFAULT_DEVICE_SPECIFIER) - << "'" << std::endl; - } - gError.init(Error::NONE); } ~Backend_() { - alcMakeContextCurrent(0); - alcDestroyContext(mAlContext); - alcCloseDevice(mAlDevice); - FE_Quit(); SDL_Quit(); } @@ -443,17 +420,13 @@ public: } } - static bool check(Error& error) + static const Error& getError() { - error = gError; - return error.code() == Error::NONE; + return gError; } private: - ALCdevice* mAlDevice; - ALCcontext* mAlContext; - static Error gError; static int gRetainCount; static BackendP gInstance; @@ -474,9 +447,14 @@ Backend::~Backend() Backend_::release(); } -bool Backend::check(Error& error) +bool Backend::isInitialized() +{ + return getError().code() == Error::NONE; +} + +const Error& Backend::getError() { - return Backend_::check(error); + return Backend_::getError(); } diff --git a/src/Moof/Core.hh b/src/Moof/Core.hh index fcb0340..109999a 100644 --- a/src/Moof/Core.hh +++ b/src/Moof/Core.hh @@ -100,7 +100,8 @@ public: Backend(); ~Backend(); - static bool check(Error& error); + static bool isInitialized(); + static const Error& getError(); }; diff --git a/src/Moof/Error.hh b/src/Moof/Error.hh index 05c6377..c452ae6 100644 --- a/src/Moof/Error.hh +++ b/src/Moof/Error.hh @@ -57,7 +57,7 @@ public: UNKNOWN_IMAGE_FORMAT, // name of resource }; - explicit Error(unsigned code, const std::string& what = "") + explicit Error(unsigned code = NONE, const std::string& what = "") { init(code, what); } @@ -70,7 +70,7 @@ public: mCode = code; } - virtual void raise() + virtual void raise() const { throw *this; } @@ -85,8 +85,9 @@ public: return mWhat; } - bool isError() const throw() + operator bool () const { + // resolves to true if error code is not NONE return mCode != NONE; } diff --git a/src/Moof/Image.cc b/src/Moof/Image.cc index 51e7ad8..ee44dd7 100644 --- a/src/Moof/Image.cc +++ b/src/Moof/Image.cc @@ -29,30 +29,28 @@ #include // FILE #include // strncmp +#include + #include #include #include "Core.hh" #include "Error.hh" #include "Image.hh" -#include "Library.hh" #include "Log.hh" +#include "Manager.hh" namespace Mf { -class Image::Impl : public Library +class Image::Impl : public Manager { public: - explicit Impl(const std::string& name, bool flipped = false) : - Library(name), + explicit Impl() : mContext(0), - mPixels(0) - { - init(getName(), flipped); - } + mPixels(0) {} ~Impl() { @@ -89,23 +87,12 @@ public: } - SDL_Surface* mContext; - char* mPixels; - - unsigned mDepth; - GLuint mColorMode; - - std::string mComment; - - -private: - - Backend mBackend; - - bool init(const std::string& filePath, bool flipped) + bool init(const std::string& name, bool flipped = false) { - logInfo("opening image file..."); - FILE* fp = fopen(filePath.c_str(), "rb"); + std::string path = Image::getPath(name); + + logInfo << "opening image file " << path << std::endl; + FILE* fp = fopen(path.c_str(), "rb"); if (!fp) return false; png_byte signature[8]; @@ -185,7 +172,7 @@ private: logInfo << "num texts: " << numTexts << std::endl; for (int i = 0; i < numTexts; ++i) { - if (strncmp(texts[i].key, "Comment", 7) == 0) + if (strncmp(texts[i].key, "TextureInfo", 11) == 0) { mComment = texts[i].text; break; @@ -248,6 +235,19 @@ private: return mContext; } + + + SDL_Surface* mContext; + char* mPixels; + + unsigned mDepth; + GLuint mColorMode; + + std::string mComment; + +private: + + Backend mBackend; }; @@ -281,7 +281,7 @@ unsigned Image::getPitch() const return mImpl->mContext->pitch; } -GLuint Image::getColorMode() const +GLuint Image::getMode() const { return mImpl->mColorMode; } @@ -315,11 +315,19 @@ void Image::setAsIcon() const } - std::string Image::getPath(const std::string& name) { - std::string path = Resource::getPath("images/" + name + ".png"); - return path; + if (boost::find_last(name, ".png")) + { + return Resource::getPath(name); + } + else + { + std::string path("images/"); + path += name; + path += ".png"; + return Resource::getPath(path); + } } diff --git a/src/Moof/Image.hh b/src/Moof/Image.hh index 5f7c5a3..e958edc 100644 --- a/src/Moof/Image.hh +++ b/src/Moof/Image.hh @@ -38,10 +38,18 @@ namespace Mf { +class Image; +typedef boost::shared_ptr ImageP; + class Image : public Resource { public: + static ImageP alloc(const std::string& name) + { + return ImageP(new Image(name)); + } + explicit Image(const std::string& name); bool isValid() const; @@ -51,7 +59,7 @@ public: unsigned getDepth() const; unsigned getPitch() const; - GLuint getColorMode() const; + GLuint getMode() const; std::string getComment() const; diff --git a/src/Moof/Library.hh b/src/Moof/Library.hh deleted file mode 100644 index c215567..0000000 --- a/src/Moof/Library.hh +++ /dev/null @@ -1,121 +0,0 @@ - -/******************************************************************************* - - Copyright (c) 2009, Charles McGarvey - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*******************************************************************************/ - -#ifndef _MOOF_LIBRARY_HH_ -#define _MOOF_LIBRARY_HH_ - -/** - * @file Library.hh - * A library is a collection of named objects of the same type. Libraries use - * reference counting to automagically delete objects which no longer have any - * interested code. - */ - -#include - -#include - -#include - - -namespace Mf { - - -template -class Library -{ - typedef std::pair PtrValue; - typedef stlplus::hash PtrMap; - - static PtrMap mPtrMap; - std::string mName; - - static T* retain(const std::string& name) - { - typename PtrMap::iterator it = mPtrMap.find(name); - - if (it != mPtrMap.end()) - { - ++((*it).second.first); - return (*it).second.second; - } - else - { - T* newObj(new T(name)); - if (newObj) - { - mPtrMap.insert(std::make_pair(name, std::make_pair(1, newObj))); - } - return newObj; - } - } - - static void release(T* obj) - { - releaseByName(obj->mName); - } - - static void releaseByName(const std::string& name) - { - typename PtrMap::iterator it; - - if ((it = mPtrMap.find(name)) != mPtrMap.end() && - --(*it).second.first == 0) - { - delete (*it).second.second; - mPtrMap.erase((*it).first); - } - } - -public: - - explicit Library(const std::string& name) : - mName(name) {} - - const std::string& getName() const - { - return mName; - } - - static boost::shared_ptr getInstance(const std::string& name) - { - return boost::shared_ptr(retain(name), &release); - } -}; - -template -stlplus::hash< std::string,std::pair,getHash > - Library::mPtrMap; - - -} // namespace Mf - -#endif // _MOOF_LIBRARY_HH_ - -/** vim: set ts=4 sw=4 tw=80: *************************************************/ - diff --git a/src/Moof/Sound.cc b/src/Moof/Sound.cc index ca3ecfe..f8f05ba 100644 --- a/src/Moof/Sound.cc +++ b/src/Moof/Sound.cc @@ -31,13 +31,15 @@ #include #include +#include + #include +#include #include #include -#include "Core.hh" #include "Error.hh" -#include "Library.hh" +#include "Manager.hh" #include "Log.hh" #include "Sound.hh" #include "Timer.hh" @@ -62,16 +64,14 @@ public: class Buffer; typedef boost::shared_ptr BufferP; - class Buffer : public Library + class Buffer : public Manager { public: - explicit Buffer(const std::string& name) : - Library(name), + Buffer() : mBuffer(-1) { mOggStream.datasource = 0; - openFile(); } ~Buffer() @@ -84,7 +84,7 @@ public: } - void openFile() + void init(const std::string& name) { if (mOggStream.datasource) { @@ -92,14 +92,13 @@ public: mOggStream.datasource = 0; } - std::string filePath = Sound::getPath(getName()); - int result = ov_fopen((char*)filePath.c_str(), &mOggStream); + std::string path = Sound::getPath(name); + int result = ov_fopen((char*)path.c_str(), &mOggStream); if (result < 0) { - logWarning << "error while loading sound " - << getName() << std::endl; - throw Error(Error::UNKNOWN_AUDIO_FORMAT, getName()); + logWarning << "couldn't load sound: " << path << std::endl; + throw Error(Error::UNKNOWN_AUDIO_FORMAT, path); } vorbis_info* vorbisInfo = ov_info(&mOggStream, -1); @@ -110,7 +109,7 @@ public: void loadAll(ALuint source) { - if (!mOggStream.datasource) openFile(); + if (!mOggStream.datasource) init(getName()); if (!mOggStream.datasource) return; char data[BUFFER_SIZE]; @@ -180,7 +179,7 @@ public: void rewind() { - if (!mOggStream.datasource) openFile(); + if (!mOggStream.datasource) init(getName()); else ov_raw_seek(&mOggStream, 0); } @@ -207,6 +206,8 @@ public: void init() { + retainBackend(); + mIsLoaded = false; mIsPlaying = false; mIsLooping = false; @@ -233,6 +234,8 @@ public: alDeleteBuffers(1, &mBuffers.back()); mBuffers.pop_back(); } + + releaseBackend(); } @@ -426,6 +429,39 @@ public: // than a timer, probably as a compile-time option } + static void retainBackend() + { + if (gRetainCount++ == 0) + { + gAlDevice = alcOpenDevice(0); + gAlContext = alcCreateContext(gAlDevice, 0); + if (!gAlDevice || !gAlContext) + { + const char* error = alcGetString(gAlDevice, + alcGetError(gAlDevice)); + logError << "audio subsystem initialization failure: " + << error << std::endl; + } + else + { + alcMakeContextCurrent(gAlContext); + logInfo << "opened sound device `" + << alcGetString(gAlDevice, ALC_DEFAULT_DEVICE_SPECIFIER) + << "'" << std::endl; + } + } + } + + static void releaseBackend() + { + if (--gRetainCount == 0) + { + alcMakeContextCurrent(0); + alcDestroyContext(gAlContext); + alcCloseDevice(gAlDevice); + } + } + ALuint mSource; std::list mBuffers; @@ -438,9 +474,15 @@ public: Timer mStreamTimer; - Backend mBackend; + static unsigned gRetainCount; + static ALCdevice* gAlDevice; + static ALCcontext* gAlContext; }; +unsigned Sound::Impl::gRetainCount = 0; +ALCdevice* Sound::Impl::gAlDevice = 0; +ALCcontext* Sound::Impl::gAlContext = 0; + Sound::Sound() : // pass through @@ -550,8 +592,17 @@ void Sound::setListenerOrientation(const Vector3& forward, const Vector3& up) std::string Sound::getPath(const std::string& name) { - std::string path = Resource::getPath("sounds/" + name + ".ogg"); - return path; + if (boost::find_last(name, ".ogg")) + { + return Resource::getPath(name); + } + else + { + std::string path("sounds/"); + path += name; + path += ".ogg"; + return Resource::getPath(path); + } } diff --git a/src/Moof/Texture.cc b/src/Moof/Texture.cc index 5341af3..801acba 100644 --- a/src/Moof/Texture.cc +++ b/src/Moof/Texture.cc @@ -29,15 +29,15 @@ #include // FILE #include // strncmp +#include #include #include "Dispatch.hh" -#include "Core.hh" #include "Error.hh" -#include "Image.hh" -#include "Library.hh" +#include "Manager.hh" #include "Log.hh" #include "OpenGL.hh" +#include "Script.hh" #include "Texture.hh" #include "Video.hh" @@ -50,11 +50,11 @@ namespace Mf { * which is worth having in memory. The image data itself is not worth keeping * in memory if the texture has been loaded to GL, but the name of the resource * is retained so that it can be reloaded if necessary. The implementation is a - * library so that multiple texture objects can share the same internal objects + * manager so that multiple texture objects can share the same internal objects * and avoid having duplicate textures loaded to GL. */ -class Texture::Impl : public Library +class Texture::Impl : public Manager { /** @@ -103,33 +103,40 @@ class Texture::Impl : public Library return value; } + + static void bindScriptConstants(Mf::Script& script) + { + script.push(GL_CLAMP); script.set("CLAMP"); + script.push(GL_REPEAT); script.set("REPEAT"); + script.push(GL_LINEAR); script.set("LINEAR"); + script.push(GL_NEAREST); script.set("NEAREST"); + script.push(GL_LINEAR_MIPMAP_LINEAR); script.set("LINEAR_MIPMAP_LINEAR"); + script.push(GL_LINEAR_MIPMAP_NEAREST); script.set("LINEAR_MIPMAP_NEAREST"); + script.push(GL_NEAREST_MIPMAP_LINEAR); script.set("NEAREST_MIPMAP_LINEAR"); + script.push(GL_NEAREST_MIPMAP_NEAREST); script.set("NEAREST_MIPMAP_NEAREST"); + } + public: /** * Construction is initialization. */ - explicit Impl(const std::string& name) : - Library(name), - //mContext(0), - mImage(Texture::getPath(getName())), - mWidth(0), - mHeight(0), - mMode(0), + Impl() : mMinFilter(GL_NEAREST), mMagFilter(GL_NEAREST), mWrapS(GL_CLAMP), mWrapT(GL_CLAMP), + mTilesS(1), + mTilesT(1), mObject(0) { // make sure we have a video context - //ASSERT(video && "cannot load textures without a current video context"); + ASSERT(video && "cannot load textures without a current video context"); // we want to know when the GL context is recreated mDispatchHandler = core.addHandler("video.newcontext", boost::bind(&Impl::contextRecreated, this)); - - loadFromFile(); } ~Impl() @@ -214,19 +221,55 @@ public: * @return Image data. */ - void loadFromFile() + void init(const std::string& name) { - if (!mImage.isValid()) + std::string path = Texture::getPath(name); + + mImage = Image::alloc(path); + if (!mImage->isValid()) + { + logWarning << "texture not found: " << path << std::endl; + Error(Error::RESOURCE_NOT_FOUND, path).raise(); + } + + mImage->flip(); + + Mf::Script script; + + importLogFunctions(script); + bindScriptConstants(script); + + if (script.doString(mImage->getComment()) != Mf::Script::SUCCESS) { - logWarning << "texture not found: " << getName() << std::endl; - throw Error(Error::RESOURCE_NOT_FOUND, getName()); + std::string str; + script[-1].get(str); + Mf::logWarning(str); } + else + { + Mf::logInfo << "loading tiles from texture " << path << std::endl; + + Mf::Script::Slot globals = script.getGlobalTable(); + Mf::Script::Slot top = script[-1]; - mImage.flip(); + globals.pushField("tiles_s"); + top.get(mTilesS); - mWidth = mImage.getWidth(); - mHeight = mImage.getHeight(); - mMode = mImage.getColorMode(); + globals.pushField("tiles_t"); + top.get(mTilesT); + + globals.pushField("min_filter"); + top.get(mMinFilter); + + globals.pushField("mag_filter"); + top.get(mMagFilter); + + globals.pushField("wrap_s"); + top.get(mWrapS); + + globals.pushField("wrap_t"); + top.get(mWrapT); + } } @@ -243,8 +286,6 @@ public: return; } - //if (!mContext) loadFromFile(); - glGenTextures(1, &mObject); glBindTexture(GL_TEXTURE_2D, mObject); @@ -253,20 +294,17 @@ public: ( GL_TEXTURE_2D, 0, - mMode, + mImage->getMode(), //3, - mWidth, - mHeight, + mImage->getWidth(), + mImage->getHeight(), 0, - mMode, + mImage->getMode(), GL_UNSIGNED_BYTE, - mImage.getPixels() + mImage->getPixels() ); setProperties(); - - //SDL_FreeSurface(mContext); - //mContext = 0; } @@ -326,15 +364,35 @@ public: } - Image mImage; - unsigned mWidth; ///< Horizontal dimension of the image. - unsigned mHeight; ///< Vertical dimension. + bool getTileCoords(Texture::TileIndex index, Scalar coords[8]) const + { + // make sure the index represents a real tile + if (index >= mTilesS * mTilesT) return false; + + Scalar w = 1.0 / Scalar(mTilesS); + Scalar h = 1.0 / Scalar(mTilesT); + + coords[0] = Scalar(index % mTilesS) * w; + coords[1] = (Scalar(mTilesT - 1) - + Scalar(index / mTilesS)) * h; + coords[2] = coords[0] + w; + coords[3] = coords[1]; + coords[4] = coords[2]; + coords[5] = coords[1] + h; + coords[6] = coords[0]; + coords[7] = coords[5]; + + return true; + } + + ImageP mImage; - GLuint mMode; ///< GL_RGB or GL_RGBA. - GLuint mMinFilter; ///< Minifcation filter. + GLuint mMinFilter; ///< Minification filter. GLuint mMagFilter; ///< Magnification filter. GLuint mWrapS; ///< Wrapping behavior horizontally. GLuint mWrapT; ///< Wrapping behavior vertically. + unsigned mTilesS; + unsigned mTilesT; GLuint mObject; ///< GL texture handle. static GLuint gObject; ///< Global GL texture handle. @@ -346,6 +404,7 @@ GLuint Texture::Impl::gObject = 0; Texture::Texture(const std::string& name) : + Image(Texture::getPath(name)), // pass through mImpl(Texture::Impl::getInstance(name)) {} @@ -379,19 +438,6 @@ void Texture::resetBind() } -unsigned Texture::getWidth() const -{ - // pass through - return mImpl->mWidth; -} - -unsigned Texture::getHeight() const -{ - // pass through - return mImpl->mHeight; -} - - void Texture::setMinFilter(GLuint filter) { // pass through @@ -417,10 +463,54 @@ void Texture::setWrapT(GLuint wrap) } +bool Texture::getTileCoords(TileIndex index, Scalar coords[8]) const +{ + // pass through + return mImpl->getTileCoords(index, coords); +} + +bool Texture::getTileCoords(TileIndex index, Scalar coords[8], + Orientation orientation) const +{ + if (getTileCoords(index, coords)) + { + if (orientation & FLIP) + { + // this looks kinda weird, but it's just swapping in a way that + // doesn't require an intermediate variable + coords[1] = coords[5]; + coords[5] = coords[3]; + coords[3] = coords[7]; + coords[7] = coords[5]; + } + if (orientation & REVERSE) + { + coords[0] = coords[2]; + coords[2] = coords[6]; + coords[4] = coords[6]; + coords[6] = coords[0]; + } + + return true; + } + + return false; +} + + std::string Texture::getPath(const std::string& name) { - std::string path = Resource::getPath("textures/" + name + ".png"); - return path; + if (boost::find_last(name, ".png")) + { + return Resource::getPath(name); + } + else + { + std::string path("textures/"); + path += name; + path += ".png"; + return Resource::getPath(path); + } } diff --git a/src/Moof/Texture.hh b/src/Moof/Texture.hh index 4e311ea..41add2b 100644 --- a/src/Moof/Texture.hh +++ b/src/Moof/Texture.hh @@ -36,8 +36,8 @@ #include +#include #include -#include namespace Mf { @@ -47,10 +47,26 @@ class Texture; typedef boost::shared_ptr TextureP; -class Texture : public Resource +class Texture : public Image { public: + /** + * Possible orientations for texture coordinates. + */ + + typedef unsigned TileIndex; + static const TileIndex NO_TILE = -1; + + typedef enum + { + NORMAL = 0, ///< Normal orientation. + FLIP = 1, ///< Flip over a horizontal axis. + REVERSE = 2, ///< Flip over a vertical axis. + FLIP_AND_REVERSE = 3 ///< Flip over both. + } Orientation; + + static TextureP alloc(const std::string& name) { return TextureP(new Texture(name)); @@ -63,14 +79,38 @@ public: static void resetBind(); - unsigned getWidth() const; - unsigned getHeight() const; - void setMinFilter(GLuint filter); void setMagFilter(GLuint filter); void setWrapS(GLuint wrap); void setWrapT(GLuint wrap); + + /** + * Calculate texture coordinates for a tile at a certain index. Tiles are + * indexed start with zero as the to-left tile and moving across, then down. + * @param index The tile index. + * @param coords An array of scalars where the texture coordinates will be + * stored after this call. The first coordinate (u,v) will be in the first + * two places and so on until all four coordinates are stored, therefore + * requiring enough room for an array of eight scalars. The winding of the + * coordinates is always counter-clockwise (the GL default). + * @return True if index is valid, false otherwise. + */ + + bool getTileCoords(TileIndex index, Scalar coords[8]) const; + + + /** + * This version let's you specify an orientation that will be reflected in + * the texture coordinates. This allows you to easily map a texture + * backwards or upside-down. + * @param what The orientation; can be flip, reverse, or flip_and_reverse. + * @return True if index is valid, false otherwise. + */ + + bool getTileCoords(TileIndex index, Scalar coords[8], Orientation what) const; + + static std::string getPath(const std::string& name); private: diff --git a/src/Moof/Video.cc b/src/Moof/Video.cc index 62addb4..e071609 100644 --- a/src/Moof/Video.cc +++ b/src/Moof/Video.cc @@ -39,42 +39,39 @@ namespace Mf { Video::Video() { - init(mAttribs); + init(); } -Video::Video(const Attributes& attribs) +Video::Video(const Attributes& attribs) : + mAttribs(attribs) { - init(attribs); + init(); } Video::Video(const std::string& caption, const std::string& icon) { - if (mAttribs.caption == "Untitled") - { - mAttribs.caption = caption; - } - if (mAttribs.icon == "") - { - mAttribs.icon = icon; - } + mAttribs.caption = caption; + mAttribs.icon = icon; - init(mAttribs); + init(); } -void Video::init(const Attributes& attribs) +void Video::init() { + Error error = Backend::getError(); + if (error) error.raise(); + mContext = 0; mFlags = 0; - mAttribs = attribs; - setFull(attribs.fullscreen); - setResizable(attribs.resizable); + setFull(mAttribs.fullscreen); + setResizable(mAttribs.resizable); setOpenGLAttributes(); - setCaption(attribs.caption); + setCaption(mAttribs.caption); setIcon(); - setCursorVisible(attribs.cursorVisible); - setCursorGrab(attribs.cursorGrab); - setVideoMode(attribs.mode); + setCursorVisible(mAttribs.cursorVisible); + setCursorGrab(mAttribs.cursorGrab); + setVideoMode(mAttribs.mode); video = this; } @@ -104,7 +101,7 @@ void Video::setOpenGLAttributes() SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, mAttribs.multisampleBuffers); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, mAttribs.multisampleSamples); SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, mAttribs.swapControl); - SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, mAttribs.hardwareonly); + SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, mAttribs.hardwareOnly); } @@ -312,7 +309,7 @@ Video::Attributes::Attributes() multisampleBuffers = 0; multisampleSamples = 0; swapControl = false; - hardwareonly = false; + hardwareOnly = false; mode[0] = 640; mode[1] = 480; mode[2] = 0; @@ -344,7 +341,7 @@ Video::Attributes::Attributes() settings.get("multiesamplebuffers", multisampleBuffers); settings.get("multiesamplesamples", multisampleSamples); settings.get("swapcontrol", swapControl); - settings.get("hardwareonly", hardwareonly); + settings.get("hardwareonly", hardwareOnly); if (!settings.get("caption", caption)) { @@ -364,7 +361,7 @@ Video::Attributes::Attributes() mode[0] = dimensions[0]; mode[1] = dimensions[1]; } - else if (fullscreen) + else if (fullscreen && Backend::isInitialized()) { SDL_Rect** modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE); diff --git a/src/Moof/Video.hh b/src/Moof/Video.hh index 0199bc5..0cc1a4e 100644 --- a/src/Moof/Video.hh +++ b/src/Moof/Video.hh @@ -1,30 +1,13 @@ -/******************************************************************************* - - Copyright (c) 2009, Charles McGarvey - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*******************************************************************************/ +/*] Copyright (c) 2009-2010, Charles McGarvey [************************** +**] All rights reserved. +* +* vi:ts=8 sw=4 tw=75 +* +* Distributable under the terms and conditions of the 2-clause BSD license; +* see the file COPYING for a complete text of the license. +* +**************************************************************************/ #ifndef _MOOF_VIDEO_HH_ #define _MOOF_VIDEO_HH_ @@ -62,7 +45,7 @@ public: long multisampleBuffers; long multisampleSamples; bool swapControl; - bool hardwareonly; + bool hardwareOnly; // Window attributes std::string caption; @@ -77,11 +60,13 @@ public: private: - Backend backend; + Backend mBackend; + }; - static VideoP alloc(const std::string& caption, const std::string& icon) + static VideoP alloc(const std::string& caption, + const std::string& icon) { return VideoP(new Video(caption, icon)); } @@ -125,7 +110,7 @@ public: private: - void init(const Attributes& attribs); + void init(); void recreateContext(); void setOpenGLAttributes(); @@ -147,5 +132,3 @@ extern Video* video; #endif // _MOOF_VIDEO_HH_ -/** vim: set ts=4 sw=4 tw=80: *************************************************/ - diff --git a/src/Scene.cc b/src/Scene.cc index aa2249c..45fc3a0 100644 --- a/src/Scene.cc +++ b/src/Scene.cc @@ -31,20 +31,20 @@ #include #include #include -#include +#include #include #include #include //#include #include #include +#include #include "Character.hh" #include "Scene.hh" -#include "Tilemap.hh" -struct Scene::Impl : public Mf::Library +struct Scene::Impl : public Mf::Manager { struct Quad : public Mf::Entity { @@ -57,7 +57,7 @@ struct Scene::Impl : public Mf::Library }; Quad(const Mf::Vector3* vertices[4], const std::string& texture, - Tilemap::Index tileIndex) : + Mf::Texture::TileIndex tileIndex) : mTilemap(texture), mBlending(false), mFog(false), @@ -143,7 +143,7 @@ struct Scene::Impl : public Mf::Library Mf::Vector3 mVertices[4]; Mf::Scalar mTexCoords[8]; - Tilemap mTilemap; + Mf::Texture mTilemap; bool mBlending; bool mFog; @@ -170,8 +170,8 @@ struct Scene::Impl : public Mf::Library }; - explicit Impl(const std::string& name) : - Mf::Library(name) {} + void init(const std::string& name) {} + void importSceneBindings(Mf::Script& script) { @@ -372,7 +372,7 @@ struct Scene::Impl : public Mf::Library table.pushField(i); - Tilemap::Index index; + Mf::Texture::TileIndex index; top.get(index); script.pop(); @@ -380,7 +380,7 @@ struct Scene::Impl : public Mf::Library vertices[h][wPlus1] = Mf::demote(mTransform * Mf::Vector4(wPlus1, h, 0.0, 1.0)); - if (index == Tilemap::NO_TILE) continue; + if (index == Mf::Texture::NO_TILE) continue; const Mf::Vector3* corners[4] = { &vertices[h][w], @@ -425,10 +425,10 @@ struct Scene::Impl : public Mf::Library Mf::Script::Slot param = script[1]; Mf::Script::Slot top = script[-1]; - Tilemap::Index index = 0; - int width = 1; - bool blending = false; - bool fog = false; + Mf::Texture::TileIndex index = 0; + int width = 1; + bool blending = false; + bool fog = false; if (param.isTable()) { diff --git a/src/Tilemap.cc b/src/Tilemap.cc deleted file mode 100644 index dc3785a..0000000 --- a/src/Tilemap.cc +++ /dev/null @@ -1,189 +0,0 @@ - -/******************************************************************************* - - Copyright (c) 2009, Charles McGarvey - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*******************************************************************************/ - -#include -#include -#include - -#include "Tilemap.hh" - - -struct Tilemap::Impl : public Mf::Library -{ - Impl(const std::string& name) : - Mf::Library(name), - mMagFilter(GL_NEAREST), - mMinFilter(GL_NEAREST), - mTilesS(1), - mTilesT(1), - mWrapS(GL_CLAMP), - mWrapT(GL_CLAMP) - { - loadFromFile(); - } - - - static void bindScriptConstants(Mf::Script& script) - { - script.push(GL_CLAMP); script.set("CLAMP"); - script.push(GL_REPEAT); script.set("REPEAT"); - script.push(GL_LINEAR); script.set("LINEAR"); - script.push(GL_NEAREST); script.set("NEAREST"); - script.push(GL_LINEAR_MIPMAP_LINEAR); script.set("LINEAR_MIPMAP_LINEAR"); - script.push(GL_LINEAR_MIPMAP_NEAREST); script.set("LINEAR_MIPMAP_NEAREST"); - script.push(GL_NEAREST_MIPMAP_LINEAR); script.set("NEAREST_MIPMAP_LINEAR"); - script.push(GL_NEAREST_MIPMAP_NEAREST); script.set("NEAREST_MIPMAP_NEAREST"); - } - - void loadFromFile() - { - Mf::Script script; - std::string filePath = Tilemap::getPath(getName()); - - script.importStandardLibraries(); - importLogFunctions(script); - bindScriptConstants(script); - - if (script.doFile(filePath) != Mf::Script::SUCCESS) - { - std::string str; - script[-1].get(str); - Mf::logWarning(str); - return; // TODO needs a better exit strategy - } - - Mf::logInfo << "loading tiles from tilemap " << filePath << std::endl; - - Mf::Script::Slot globals = script.getGlobalTable(); - Mf::Script::Slot top = script[-1]; - - globals.pushField("tiles_s"); - top.get(mTilesS); - - globals.pushField("tiles_t"); - top.get(mTilesT); - - globals.pushField("min_filter"); - top.get(mMinFilter); - - globals.pushField("mag_filter"); - top.get(mMagFilter); - - globals.pushField("wrap_s"); - top.get(mWrapS); - - globals.pushField("wrap_t"); - top.get(mWrapT); - } - - - bool getTileCoords(Tilemap::Index index, Mf::Scalar coords[8]) const - { - // make sure the index represents a real tile - if (index >= mTilesS * mTilesT) return false; - - Mf::Scalar w = 1.0 / Mf::Scalar(mTilesS); - Mf::Scalar h = 1.0 / Mf::Scalar(mTilesT); - - coords[0] = Mf::Scalar(index % mTilesS) * w; - coords[1] = (Mf::Scalar(mTilesT - 1) - - Mf::Scalar(index / mTilesS)) * h; - coords[2] = coords[0] + w; - coords[3] = coords[1]; - coords[4] = coords[2]; - coords[5] = coords[1] + h; - coords[6] = coords[0]; - coords[7] = coords[5]; - - return true; - } - - - GLuint mMagFilter; - GLuint mMinFilter; - unsigned mTilesS; - unsigned mTilesT; - GLuint mWrapS; - GLuint mWrapT; -}; - - -Tilemap::Tilemap(const std::string& name) : - Texture(name), - mImpl(Tilemap::Impl::getInstance(name)) -{ - setMinFilter(mImpl->mMinFilter); - setMagFilter(mImpl->mMagFilter); - setWrapS(mImpl->mWrapS); - setWrapT(mImpl->mWrapT); -} - - -bool Tilemap::getTileCoords(Index index, Mf::Scalar coords[8]) const -{ - // pass through - return mImpl->getTileCoords(index, coords); -} - -bool Tilemap::getTileCoords(Index index, Mf::Scalar coords[8], - Orientation orientation) const -{ - if (getTileCoords(index, coords)) - { - if (orientation & FLIP) - { - // this looks kinda weird, but it's just swapping in a way that - // doesn't require an intermediate variable - coords[1] = coords[5]; - coords[5] = coords[3]; - coords[3] = coords[7]; - coords[7] = coords[5]; - } - if (orientation & REVERSE) - { - coords[0] = coords[2]; - coords[2] = coords[6]; - coords[4] = coords[6]; - coords[6] = coords[0]; - } - - return true; - } - - return false; -} - - -std::string Tilemap::getPath(const std::string& name) -{ - return Resource::getPath("tilemaps/" + name + ".lua"); -} - - -/** vim: set ts=4 sw=4 tw=80: *************************************************/ - diff --git a/src/Tilemap.hh b/src/Tilemap.hh deleted file mode 100644 index 9277387..0000000 --- a/src/Tilemap.hh +++ /dev/null @@ -1,110 +0,0 @@ - -/******************************************************************************* - - Copyright (c) 2009, Charles McGarvey - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - * Redistributions of source code must retain the above copyright notice, - this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" - AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE - FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -*******************************************************************************/ - -#ifndef _TILEMAP_HH_ -#define _TILEMAP_HH_ - -/** - * @file Tilemap.hh - * Small subclass to give some basic tile-mapping functionality to textures. - */ - -#include - -#include -#include - - -/** - * A tilemap is a texture which is meant to be divided into smaller sprites. - * This class provides all the functionality of a texture and adds some methods - * to get texture coordinates to individual tiles within the tilemap. For - * simplicity, this class assumes square tiles. - */ - -class Tilemap : public Mf::Texture -{ -public: - - /** - * Possible orientations for texture coordinates. - */ - - typedef unsigned Index; - static const Index NO_TILE = -1; - - typedef enum - { - NORMAL = 0, ///< Normal orientation. - FLIP = 1, ///< Flip over a horizontal axis. - REVERSE = 2, ///< Flip over a vertical axis. - FLIP_AND_REVERSE = 3 ///< Flip over both. - } Orientation; - - - explicit Tilemap(const std::string& name); - - /** - * Calculate texture coordinates for a tile at a certain index. Tiles are - * indexed start with zero as the to-left tile and moving across, then down. - * @param index The tile index. - * @param coords An array of scalars where the texture coordinates will be - * stored after this call. The first coordinate (u,v) will be in the first - * two places and so on until all four coordinates are stored, therefore - * requiring enough room for an array of eight scalars. The winding of the - * coordinates is always counter-clockwise (the GL default). - * @return True if index is valid, false otherwise. - */ - - bool getTileCoords(Index index, Mf::Scalar coords[8]) const; - - - /** - * This version let's you specify an orientation that will be reflected in - * the texture coordinates. This allows you to easily map a texture - * backwards or upside-down. - * @param what The orientation; can be flip, reverse, or flip_and_reverse. - * @return True if index is valid, false otherwise. - */ - - bool getTileCoords(Index index, Mf::Scalar coords[8], Orientation what) const; - - - static std::string getPath(const std::string& name); - -private: - - class Impl; - boost::shared_ptr mImpl; -}; - - -#endif // _TILEMAP_HH_ - -/** vim: set ts=4 sw=4 tw=80: *************************************************/ - diff --git a/src/TilemapFont.cc b/src/TilemapFont.cc index da0e9a6..48b0fc3 100644 --- a/src/TilemapFont.cc +++ b/src/TilemapFont.cc @@ -32,13 +32,13 @@ TilemapFont::TilemapFont() : - Tilemap("Font") {} + Mf::Texture("Font") {} void TilemapFont::getTileCoords(char symbol, Mf::Scalar coords[8], - Tilemap::Orientation what) + Mf::Texture::Orientation what) { - Tilemap::Index index = 0; + Mf::Texture::TileIndex index = 0; if (symbol >= ' ' && symbol <= '_') { @@ -53,7 +53,7 @@ void TilemapFont::getTileCoords(char symbol, Mf::Scalar coords[8], index = 0; } - Tilemap::getTileCoords(index, coords, what); + Mf::Texture::getTileCoords(index, coords, what); } diff --git a/src/TilemapFont.hh b/src/TilemapFont.hh index 98a3a13..89cb062 100644 --- a/src/TilemapFont.hh +++ b/src/TilemapFont.hh @@ -34,17 +34,17 @@ * Text on the screen. */ -#include "Tilemap.hh" +#include -class TilemapFont : public Tilemap +class TilemapFont : public Mf::Texture { public: TilemapFont(); void getTileCoords(char symbol, Mf::Scalar coords[8], - Tilemap::Orientation what = Tilemap::NORMAL); + Mf::Texture::Orientation what = Mf::Texture::NORMAL); };