]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Texture.cc
foundational changes; tying up some loose ends
[chaz/yoink] / src / Moof / Texture.cc
index fc50cfadeffcc62535faae1cf80cee6e24ec2576..a701424d5dd7588537dce06511cfb9e1f0fa6c1e 100644 (file)
 #include <SDL/SDL.h>
 #include <SDL/SDL_image.h>
 
-#include "Dispatcher.hh"
+#include "Dispatch.hh"
+#include "Engine.hh"
+#include "Exception.hh"
+#include "Library.hh"
 #include "Log.hh"
-#include "Mippleton.hh"
 #include "OpenGL.hh"
 #include "Texture.hh"
 
@@ -52,7 +54,7 @@ namespace Mf {
  * objects and avoid having duplicate textures loaded to GL.
  */
 
-class Texture::Impl : public Mippleton<Impl>
+class Texture::Impl : public Library<Impl>
 {
 
        /**
@@ -63,9 +65,9 @@ class Texture::Impl : public Mippleton<Impl>
        {
                if (mObject)
                {
-                       if (mObject == globalObject_)
+                       if (mObject == gObject)
                        {
-                               globalObject_ = 0;
+                               gObject = 0;
                        }
 
                        glDeleteTextures(1, &mObject);
@@ -79,9 +81,9 @@ class Texture::Impl : public Mippleton<Impl>
         * to cache it if the client has plenty of RAM.
         */
 
-       void contextRecreated(const Notification* note)
+       void contextRecreated()
        {
-               mObject = globalObject_ = 0;
+               mObject = gObject = 0;
                uploadToGL();
        }
 
@@ -131,7 +133,7 @@ public:
         */
 
        explicit Impl(const std::string& name) :
-               Mippleton<Impl>(name),
+               Library<Impl>(name),
                mContext(0),
                mWidth(0),
                mHeight(0),
@@ -142,11 +144,16 @@ public:
                mWrapT(GL_CLAMP),
                mObject(0)
        {
-               loadFromFile();
+               // make sure the engine is initialized
+               Engine& engine = Engine::getInstance();
+               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()
@@ -157,8 +164,6 @@ public:
                }
 
                unloadFromGL();
-
-               Dispatcher::getInstance().removeHandler(this);
        }
 
 
@@ -245,7 +250,7 @@ public:
                if (!surface)
                {
                        logWarning("texture not found: %s", getName().c_str());
-                       throw Exception(Exception::FILE_NOT_FOUND);
+                       throw Exception(ErrorCode::FILE_NOT_FOUND, getName());
                }
 
                SDL_Surface* temp = prepareImageForGL(surface);
@@ -253,7 +258,7 @@ public:
 
                if (!temp)
                {
-                       throw Exception(Exception::OPENGL_ERROR);
+                       throw Exception(ErrorCode::UNKNOWN_IMAGE_FORMAT, getName());
                }
 
                if (temp->format->BytesPerPixel == 3)
@@ -267,7 +272,7 @@ public:
                else
                {
                        SDL_FreeSurface(temp);
-                       throw Exception(Exception::BAD_IMAGE_FORMAT);
+                       throw Exception(ErrorCode::UNKNOWN_IMAGE_FORMAT, getName());
                }
 
                mWidth = temp->w;
@@ -296,10 +301,12 @@ public:
                glBindTexture(GL_TEXTURE_2D, mObject);
 
                glTexImage2D
+               //gluBuild2DMipmaps
                (
                        GL_TEXTURE_2D,
                        0,
                        mMode,
+                       //3,
                        mContext->w,
                        mContext->h,
                        0,
@@ -363,29 +370,31 @@ public:
                {
                        uploadToGL();
                }
-               if (mObject != globalObject_)
+               if (mObject != gObject)
                {
                        glBindTexture(GL_TEXTURE_2D, mObject);
-                       globalObject_ = mObject;
+                       gObject = mObject;
                }
        }
 
 
-       SDL_Surface*    mContext;
-       unsigned                mWidth;                 ///< Horizontal dimension of the image.
-       unsigned                mHeight;                ///< Vertical dimension.
+       SDL_Surface*            mContext;
+       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                  mMode;                  ///< Depth of the image, GL_RGB or GL_RGBA.
-       GLuint                  mMinFilter;             ///< Minifcation filter.
-       GLuint                  mMagFilter;             ///< Magnification filter.
-       GLuint                  mWrapS;                 ///< Wrapping behavior horizontally.
-       GLuint                  mWrapT;                 ///< Wrapping behavior vertically.
+       GLuint                          mObject;                ///< GL texture handle.
+       static GLuint           gObject;                ///< Global GL texture handle.
 
-       GLuint                  mObject;                ///< 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) :
@@ -418,7 +427,7 @@ GLuint Texture::getObject() const
 void Texture::resetBind()
 {
        glBindTexture(GL_TEXTURE_2D, 0);
-       Impl::globalObject_ = 0;
+       Impl::gObject = 0;
 }
 
 
This page took 0.026694 seconds and 4 git commands to generate.