]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Texture.cc
dispatch class not a singleton, engine is static
[chaz/yoink] / src / Moof / Texture.cc
index b778c1ccde0299b1e306d33724457728c563d65a..6769d8887644a1924d2fdc67974b1afa4df4083d 100644 (file)
 
 *******************************************************************************/
 
-#include <cstring>             // memcpy
+#include <cstdio>              // FILE
+#include <cstring>             // strncmp
 
 #include <boost/bind.hpp>
 
-#include <SDL/SDL.h>
-#include <SDL/SDL_image.h>
-
-#include "Dispatcher.hh"
+#include "Dispatch.hh"
+#include "Engine.hh"
+#include "Error.hh"
+#include "Image.hh"
+#include "Library.hh"
 #include "Log.hh"
-#include "Mippleton.hh"
 #include "OpenGL.hh"
 #include "Texture.hh"
 
@@ -48,11 +49,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
- * mippleton so that multiple texture objects can share the same internal
- * objects and avoid having duplicate textures loaded to GL.
+ * library so that multiple texture objects can share the same internal objects
+ * and avoid having duplicate textures loaded to GL.
  */
 
-class Texture::Impl : public Mippleton<Impl>
+class Texture::Impl : public Library<Impl>
 {
 
        /**
@@ -61,15 +62,15 @@ class Texture::Impl : public Mippleton<Impl>
 
        void unloadFromGL()
        {
-               if (object_)
+               if (mObject)
                {
-                       if (object_ == globalObject_)
+                       if (mObject == gObject)
                        {
-                               globalObject_ = 0;
+                               gObject = 0;
                        }
 
-                       glDeleteTextures(1, &object_);
-                       object_ = 0;
+                       glDeleteTextures(1, &mObject);
+                       mObject = 0;
                }
        }
 
@@ -79,9 +80,9 @@ class Texture::Impl : public Mippleton<Impl>
         * to cache it if the client has plenty of RAM.
         */
 
-       void contextRecreated(const Notification* note)
+       void contextRecreated()
        {
-               object_ = globalObject_ = 0;
+               mObject = gObject = 0;
                uploadToGL();
        }
 
@@ -101,29 +102,6 @@ class Texture::Impl : public Mippleton<Impl>
                return value;
        }
 
-
-       static void flipSurface(SDL_Surface* image)
-       {
-               unsigned char*  pixels = (Uint8*)(image->pixels);
-
-               unsigned                pitch = image->pitch;
-               unsigned char   line[pitch];
-
-               int             yBegin = 0;
-               int             yEnd = image->h - 1;
-
-               if (SDL_MUSTLOCK(image)) SDL_LockSurface(image);
-               while (yBegin < yEnd)
-               {
-                       memcpy(line,                    pixels + pitch * yBegin, pitch);
-                       memcpy(pixels + pitch * yBegin, pixels + pitch * yEnd,   pitch);
-                       memcpy(pixels + pitch * yEnd,   line,                    pitch);
-                       yBegin++;
-                       yEnd--;
-               }
-               if (SDL_MUSTLOCK(image)) SDL_UnlockSurface(image);
-       }
-
 public:
 
        /**
@@ -131,34 +109,32 @@ public:
         */
 
        explicit Impl(const std::string& name) :
-               Mippleton<Impl>(name),
-               surface_(0),
-               width_(0),
-               height_(0),
-               mode_(0),
-               minFilter_(GL_NEAREST),
-               magFilter_(GL_NEAREST),
-               wrapS_(GL_CLAMP),
-               wrapT_(GL_CLAMP),
-               object_(0)
+               Library<Impl>(name),
+               //mContext(0),
+               mImage(Texture::getPath(getName())),
+               mWidth(0),
+               mHeight(0),
+               mMode(0),
+               mMinFilter(GL_NEAREST),
+               mMagFilter(GL_NEAREST),
+               mWrapS(GL_CLAMP),
+               mWrapT(GL_CLAMP),
+               mObject(0)
        {
-               loadFromFile();
+               // make sure we have a video
+               VideoP video = engine.getVideo();
+               ASSERT(video && "cannot load textures without a current video context");
 
                // we want to know when the GL context is recreated
-               Dispatcher::getInstance().addHandler("video.context_recreated",
-                               boost::bind(&Impl::contextRecreated, this, _1), this);
+               mDispatchHandler = engine.addHandler("video.newcontext",
+                               boost::bind(&Impl::contextRecreated, this));
+
+               loadFromFile();
        }
 
        ~Impl()
        {
-               if (surface_)
-               {
-                       SDL_FreeSurface(surface_);
-               }
-
                unloadFromGL();
-
-               Dispatcher::getInstance().removeHandler(this);
        }
 
 
@@ -169,6 +145,7 @@ public:
         * method makes them ready.
         */
 
+       /*
        static SDL_Surface* prepareImageForGL(SDL_Surface* surface)
        {
                int w = powerOfTwo(surface->w);
@@ -229,6 +206,7 @@ public:
 
                return image;
        }
+       */
 
        /**
         * Use SDL_image to load images from file.  A surface with the image data is
@@ -238,42 +216,17 @@ public:
 
        void loadFromFile()
        {
-               SDL_Surface* surface;
-
-               surface = IMG_Load(Texture::getPath(getName()).c_str());
-
-               if (!surface)
+               if (!mImage.isValid())
                {
-                       logWarning("texture not found: %s", getName().c_str());
-                       throw Exception(Exception::FILE_NOT_FOUND);
+                       logWarning << "texture not found: " << getName() << std::endl;
+                       throw Error(Error::RESOURCE_NOT_FOUND, getName());
                }
 
-               SDL_Surface* temp = prepareImageForGL(surface);
-               SDL_FreeSurface(surface);
-
-               if (!temp)
-               {
-                       throw Exception(Exception::OPENGL_ERROR);
-               }
+               mImage.flip();
 
-               if (temp->format->BytesPerPixel == 3)
-               {
-                       mode_ = GL_RGB;
-               }
-               else if (temp->format->BytesPerPixel == 4)
-               {
-                       mode_ = GL_RGBA;
-               }
-               else
-               {
-                       SDL_FreeSurface(temp);
-                       throw Exception(Exception::BAD_IMAGE_FORMAT);
-               }
-
-               width_ = temp->w;
-               height_ = temp->h;
-
-               surface_ = temp;
+               mWidth = mImage.getWidth();
+               mHeight = mImage.getHeight();
+               mMode = mImage.getColorMode();
        }
 
 
@@ -284,34 +237,36 @@ public:
 
        void uploadToGL()
        {
-               if (object_)
+               if (mObject)
                {
                        // already loaded
                        return;
                }
 
-               if (!surface_) loadFromFile();
+               //if (!mContext) loadFromFile();
 
-               glGenTextures(1, &object_);
-               glBindTexture(GL_TEXTURE_2D, object_);
+               glGenTextures(1, &mObject);
+               glBindTexture(GL_TEXTURE_2D, mObject);
 
                glTexImage2D
+               //gluBuild2DMipmaps
                (
                        GL_TEXTURE_2D,
                        0,
-                       mode_,
-                       surface_->w,
-                       surface_->h,
+                       mMode,
+                       //3,
+                       mWidth,
+                       mHeight,
                        0,
-                       mode_,
+                       mMode,
                        GL_UNSIGNED_BYTE,
-                       surface_->pixels
+                       mImage.getPixels()
                );
 
                setProperties();
 
-               SDL_FreeSurface(surface_);
-               surface_ = 0;
+               //SDL_FreeSurface(mContext);
+               //mContext = 0;
        }
 
 
@@ -322,75 +277,77 @@ public:
 
        void setProperties()
        {
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter_);
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter_);
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS_);
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT_);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mMinFilter);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mMagFilter);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mWrapS);
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mWrapT);
        }
 
        inline void setMinFilter(GLuint filter)
        {
                bind();
-               minFilter_ = filter;
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter_);
+               mMinFilter = filter;
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, mMinFilter);
        }
 
        inline void setMagFilter(GLuint filter)
        {
                bind();
-               magFilter_ = filter;
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter_);
+               mMagFilter = filter;
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, mMagFilter);
        }
 
        inline void setWrapS(GLuint wrap)
        {
                bind();
-               wrapS_ = wrap;
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapS_);
+               mWrapS = wrap;
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, mWrapS);
        }
 
        inline void setWrapT(GLuint wrap)
        {
                bind();
-               wrapT_ = wrap;
-               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapT_);
+               mWrapT = wrap;
+               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, mWrapT);
        }
 
 
        inline void bind()
        {
-               if (object_ == 0)
+               if (mObject == 0)
                {
                        uploadToGL();
                }
-               if (object_ != globalObject_)
+               if (mObject != gObject)
                {
-                       glBindTexture(GL_TEXTURE_2D, object_);
-                       globalObject_ = object_;
+                       glBindTexture(GL_TEXTURE_2D, mObject);
+                       gObject = mObject;
                }
        }
 
 
-       SDL_Surface*    surface_;
-       unsigned                width_;                 ///< Horizontal dimension of the image.
-       unsigned                height_;                ///< Vertical dimension.
+       Image                           mImage;
+       unsigned                        mWidth;                 ///< Horizontal dimension of the image.
+       unsigned                        mHeight;                ///< Vertical dimension.
+
+       GLuint                          mMode;                  ///< GL_RGB or GL_RGBA.
+       GLuint                          mMinFilter;             ///< Minifcation filter.
+       GLuint                          mMagFilter;             ///< Magnification filter.
+       GLuint                          mWrapS;                 ///< Wrapping behavior horizontally.
+       GLuint                          mWrapT;                 ///< Wrapping behavior vertically.
 
-       GLuint                  mode_;                  ///< Depth of the image, GL_RGB or GL_RGBA.
-       GLuint                  minFilter_;             ///< Minifcation filter.
-       GLuint                  magFilter_;             ///< Magnification filter.
-       GLuint                  wrapS_;                 ///< Wrapping behavior horizontally.
-       GLuint                  wrapT_;                 ///< Wrapping behavior vertically.
+       GLuint                          mObject;                ///< GL texture handle.
+       static GLuint           gObject;                ///< Global GL texture handle.
 
-       GLuint                  object_;                ///< GL texture handle.
-       static GLuint   globalObject_;  ///< Global GL texture handle.
+       Dispatch::Handler       mDispatchHandler;
 };
 
-GLuint Texture::Impl::globalObject_ = 0;
+GLuint Texture::Impl::gObject = 0;
 
 
 Texture::Texture(const std::string& name) :
        // pass through
-       impl_(Texture::Impl::getInstance(name)) {}
+       mImpl(Texture::Impl::getInstance(name)) {}
 
 
 /**
@@ -400,7 +357,7 @@ Texture::Texture(const std::string& name) :
 void Texture::bind() const
 {
        // pass through
-       impl_->bind();
+       mImpl->bind();
 }
 
 
@@ -411,52 +368,52 @@ void Texture::bind() const
 GLuint Texture::getObject() const
 {
        // pass through
-       return impl_->object_;
+       return mImpl->mObject;
 }
 
 
 void Texture::resetBind()
 {
        glBindTexture(GL_TEXTURE_2D, 0);
-       Impl::globalObject_ = 0;
+       Impl::gObject = 0;
 }
 
 
 unsigned Texture::getWidth() const
 {
        // pass through
-       return impl_->width_;
+       return mImpl->mWidth;
 }
 
 unsigned Texture::getHeight() const
 {
        // pass through
-       return impl_->height_;
+       return mImpl->mHeight;
 }
 
 
 void Texture::setMinFilter(GLuint filter)
 {
        // pass through
-       impl_->setMinFilter(filter);
+       mImpl->setMinFilter(filter);
 }
 
 void Texture::setMagFilter(GLuint filter)
 {
        // pass through
-       impl_->setMagFilter(filter);
+       mImpl->setMagFilter(filter);
 }
 
 void Texture::setWrapS(GLuint wrap)
 {
        // pass through
-       impl_->setWrapS(wrap);
+       mImpl->setWrapS(wrap);
 }
 
 void Texture::setWrapT(GLuint wrap)
 {
        // pass through
-       impl_->setWrapT(wrap);
+       mImpl->setWrapT(wrap);
 }
 
 
This page took 0.029135 seconds and 4 git commands to generate.