]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Texture.cc
foundational changes; tying up some loose ends
[chaz/yoink] / src / Moof / Texture.cc
index 341b5598f13f9b8c7d42f93643f26ca3f4727568..a701424d5dd7588537dce06511cfb9e1f0fa6c1e 100644 (file)
@@ -33,7 +33,8 @@
 #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"
@@ -64,9 +65,9 @@ class Texture::Impl : public Library<Impl>
        {
                if (mObject)
                {
-                       if (mObject == globalObject_)
+                       if (mObject == gObject)
                        {
-                               globalObject_ = 0;
+                               gObject = 0;
                        }
 
                        glDeleteTextures(1, &mObject);
@@ -80,9 +81,9 @@ class Texture::Impl : public Library<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();
        }
 
@@ -143,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()
@@ -158,8 +164,6 @@ public:
                }
 
                unloadFromGL();
-
-               Dispatcher::getInstance().removeHandler(this);
        }
 
 
@@ -246,7 +250,7 @@ public:
                if (!surface)
                {
                        logWarning("texture not found: %s", getName().c_str());
-                       throw Exception(ErrorCode::FILE_NOT_FOUND, getName().c_str());
+                       throw Exception(ErrorCode::FILE_NOT_FOUND, getName());
                }
 
                SDL_Surface* temp = prepareImageForGL(surface);
@@ -254,7 +258,7 @@ public:
 
                if (!temp)
                {
-                       throw Exception(ErrorCode::UNKNOWN_IMAGE_FORMAT);
+                       throw Exception(ErrorCode::UNKNOWN_IMAGE_FORMAT, getName());
                }
 
                if (temp->format->BytesPerPixel == 3)
@@ -268,7 +272,7 @@ public:
                else
                {
                        SDL_FreeSurface(temp);
-                       throw Exception(ErrorCode::UNKNOWN_IMAGE_FORMAT);
+                       throw Exception(ErrorCode::UNKNOWN_IMAGE_FORMAT, getName());
                }
 
                mWidth = temp->w;
@@ -366,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) :
@@ -421,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.024561 seconds and 4 git commands to generate.