X-Git-Url: https://git.dogcows.com/gitweb?p=chaz%2Fyoink;a=blobdiff_plain;f=src%2FMoof%2FTexture.cc;h=7f9f83c335b98eb7f8a39d4e02023ef0b6f68708;hp=801acbae227f89d7a58970a1e97ed836a008391a;hb=299af4f2047e767e5d79501c26444473bda64c64;hpb=e973a129b5b83b628ba3f09e8c95682fc74080cd diff --git a/src/Moof/Texture.cc b/src/Moof/Texture.cc index 801acba..7f9f83c 100644 --- a/src/Moof/Texture.cc +++ b/src/Moof/Texture.cc @@ -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=4 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. +* +**************************************************************************/ #include // FILE #include // strncmp @@ -46,12 +29,13 @@ namespace Mf { /** - * The texture implementation just contains all the information about the image - * 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 - * manager so that multiple texture objects can share the same internal objects - * and avoid having duplicate textures loaded to GL. + * The texture implementation just contains all the information about the + * image 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 manager so that multiple texture + * objects can share the same internal objects and avoid having duplicate + * textures loaded to GL. */ class Texture::Impl : public Manager @@ -76,9 +60,9 @@ class Texture::Impl : public Manager } /** - * If the GL context was recreated, we need to reload the texture. This may - * involve reading it from disk again, but hopefully the OS was smart enough - * to cache it if the client has plenty of RAM. + * If the GL context was recreated, we need to reload the texture. + * This may involve reading it from disk again, but hopefully the OS + * was smart enough to cache it if the client has plenty of RAM. */ void contextRecreated() @@ -88,8 +72,9 @@ class Texture::Impl : public Manager } /** - * This is a helper method used by some of the texture loading code. It - * returns the first power of two which is greater than the input value. + * This is a helper method used by some of the texture loading code. + * It returns the first power of two which is greater than the input + * value. */ static int powerOfTwo(int input) @@ -104,16 +89,18 @@ class Texture::Impl : public Manager } - static void bindScriptConstants(Mf::Script& script) + static void bindScriptConstants(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"); + Script::Slot g = script.globals(); + + g.setField("CLAMP", GL_CLAMP); + g.setField("REPEAT", GL_REPEAT); + g.setField("LINEAR", GL_LINEAR); + g.setField("NEAREST", GL_NEAREST); + g.setField("LINEAR_MIPMAP_LINEAR", GL_LINEAR_MIPMAP_LINEAR); + g.setField("LINEAR_MIPMAP_NEAREST", GL_LINEAR_MIPMAP_NEAREST); + g.setField("NEAREST_MIPMAP_LINEAR", GL_NEAREST_MIPMAP_LINEAR); + g.setField("NEAREST_MIPMAP_NEAREST", GL_NEAREST_MIPMAP_NEAREST); } public: @@ -132,11 +119,13 @@ public: mObject(0) { // make sure we have a video context - ASSERT(video && "cannot load textures without a current video context"); + Video* video = Video::current(); + ASSERT(video && "should have a video context set"); // we want to know when the GL context is recreated - mDispatchHandler = core.addHandler("video.newcontext", - boost::bind(&Impl::contextRecreated, this)); + Dispatch& dispatch = Dispatch::global(); + mNewContextDispatch = dispatch.addTarget("video.newcontext", + boost::bind(&Impl::contextRecreated, this)); } ~Impl() @@ -146,10 +135,10 @@ public: /** - * Adapted from some public domain code. This stuff is common enough that - * it really should be included in SDL_image... We need this because images - * loaded with SDL_image aren't exactly GL-ready right out of the box. This - * method makes them ready. + * Adapted from some public domain code. This stuff is common enough + * that it really should be included in SDL_image... We need this + * because images loaded with SDL_image aren't exactly GL-ready right + * out of the box. This method makes them ready. */ /* @@ -158,16 +147,17 @@ public: int w = powerOfTwo(surface->w); int h = powerOfTwo(surface->h); - // 2. OpenGL textures make more sense within the coordinate system when - // they are "upside down," so let's flip it. + // 2. OpenGL textures make more sense within the coordinate system + // when they are "upside down," so let's flip it. flipSurface(surface); - // 1. OpenGL images must (generally) have dimensions of a power-of-two. - // If this one doesn't, we can at least be more friendly by expanding - // the dimensions so that they are, though there will be some empty - // space within the range of normal texture coordinates. It's better if - // textures are the right size to begin with. + // 1. OpenGL images must (generally) have dimensions of a + // power-of-two. If this one doesn't, we can at least be more + // friendly by expanding the dimensions so that they are, though + // there will be some empty space within the range of normal + // texture coordinates. It's better if textures are the right size + // to begin with. SDL_Surface* image = SDL_CreateRGBSurface ( @@ -216,14 +206,16 @@ public: */ /** - * Use SDL_image to load images from file. A surface with the image data is - * returned. + * Use SDL_image to load images from file. A surface with the image + * data is returned. * @return Image data. */ void init(const std::string& name) { - std::string path = Texture::getPath(name); + std::string path(name); + + Texture::getPath(path); mImage = Image::alloc(path); if (!mImage->isValid()) @@ -234,41 +226,29 @@ public: mImage->flip(); - Mf::Script script; + Script script; importLogFunctions(script); bindScriptConstants(script); - if (script.doString(mImage->getComment()) != Mf::Script::SUCCESS) + if (script.doString(mImage->getComment()) != Script::SUCCESS) { std::string str; script[-1].get(str); - Mf::logWarning(str); + logWarning(str); } else { - Mf::logInfo << "loading tiles from texture " << path << 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); + logInfo << "loading tiles from texture " << path + << std::endl; + + Script::Slot globals = script.globals(); + globals.get(mTilesS, "tiles_s"); + globals.get(mTilesT, "tiles_t"); + globals.get(mMinFilter, "min_filter"); + globals.get(mMagFilter, "mag_filter"); + globals.get(mWrapS, "wrap_s"); + globals.get(mWrapT, "wrap_t"); } } @@ -309,8 +289,8 @@ public: /** - * Sets some texture properties such as the filters and external coordinate - * behavior. + * Sets some texture properties such as the filters and external + * coordinate behavior. */ void setProperties() @@ -373,8 +353,7 @@ public: Scalar h = 1.0 / Scalar(mTilesT); coords[0] = Scalar(index % mTilesS) * w; - coords[1] = (Scalar(mTilesT - 1) - - Scalar(index / mTilesS)) * h; + coords[1] = (Scalar(mTilesT - 1) - Scalar(index / mTilesS)) * h; coords[2] = coords[0] + w; coords[3] = coords[1]; coords[4] = coords[2]; @@ -387,24 +366,24 @@ public: ImageP mImage; - GLuint mMinFilter; ///< Minification filter. - GLuint mMagFilter; ///< Magnification filter. - GLuint mWrapS; ///< Wrapping behavior horizontally. - GLuint mWrapT; ///< Wrapping behavior vertically. + 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. + GLuint mObject; ///< GL texture handle. + static GLuint gObject; ///< Global GL texture handle. - Dispatch::Handler mDispatchHandler; + Dispatch::Handle mNewContextDispatch; }; GLuint Texture::Impl::gObject = 0; -Texture::Texture(const std::string& name) : - Image(Texture::getPath(name)), +Texture::Texture(const std::string& name) : // TODO hmm.. + Image(name), // pass through mImpl(Texture::Impl::getInstance(name)) {} @@ -498,23 +477,11 @@ bool Texture::getTileCoords(TileIndex index, Scalar coords[8], } -std::string Texture::getPath(const std::string& name) +bool Texture::getPath(std::string& name) { - if (boost::find_last(name, ".png")) - { - return Resource::getPath(name); - } - else - { - std::string path("textures/"); - path += name; - path += ".png"; - return Resource::getPath(path); - } + return Resource::getPath(name, "textures/", "png"); } } // namespace Mf -/** vim: set ts=4 sw=4 tw=80: *************************************************/ -