]> Dogcows Code - chaz/yoink/blobdiff - src/Moof/Texture.cc
dispatch class not a singleton, engine is static
[chaz/yoink] / src / Moof / Texture.cc
index a701424d5dd7588537dce06511cfb9e1f0fa6c1e..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 "Dispatch.hh"
 #include "Engine.hh"
-#include "Exception.hh"
+#include "Error.hh"
+#include "Image.hh"
 #include "Library.hh"
 #include "Log.hh"
 #include "OpenGL.hh"
@@ -50,8 +49,8 @@ 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 Library<Impl>
@@ -103,29 +102,6 @@ class Texture::Impl : public Library<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:
 
        /**
@@ -134,7 +110,8 @@ public:
 
        explicit Impl(const std::string& name) :
                Library<Impl>(name),
-               mContext(0),
+               //mContext(0),
+               mImage(Texture::getPath(getName())),
                mWidth(0),
                mHeight(0),
                mMode(0),
@@ -144,8 +121,7 @@ public:
                mWrapT(GL_CLAMP),
                mObject(0)
        {
-               // make sure the engine is initialized
-               Engine& engine = Engine::getInstance();
+               // make sure we have a video
                VideoP video = engine.getVideo();
                ASSERT(video && "cannot load textures without a current video context");
 
@@ -158,11 +134,6 @@ public:
 
        ~Impl()
        {
-               if (mContext)
-               {
-                       SDL_FreeSurface(mContext);
-               }
-
                unloadFromGL();
        }
 
@@ -174,6 +145,7 @@ public:
         * method makes them ready.
         */
 
+       /*
        static SDL_Surface* prepareImageForGL(SDL_Surface* surface)
        {
                int w = powerOfTwo(surface->w);
@@ -234,6 +206,7 @@ public:
 
                return image;
        }
+       */
 
        /**
         * Use SDL_image to load images from file.  A surface with the image data is
@@ -243,42 +216,17 @@ public:
 
        void loadFromFile()
        {
-               SDL_Surface* surface;
-
-               surface = IMG_Load(Texture::getPath(getName()).c_str());
-
-               if (!surface)
-               {
-                       logWarning("texture not found: %s", getName().c_str());
-                       throw Exception(ErrorCode::FILE_NOT_FOUND, getName());
-               }
-
-               SDL_Surface* temp = prepareImageForGL(surface);
-               SDL_FreeSurface(surface);
-
-               if (!temp)
-               {
-                       throw Exception(ErrorCode::UNKNOWN_IMAGE_FORMAT, getName());
-               }
-
-               if (temp->format->BytesPerPixel == 3)
-               {
-                       mMode = GL_RGB;
-               }
-               else if (temp->format->BytesPerPixel == 4)
-               {
-                       mMode = GL_RGBA;
-               }
-               else
+               if (!mImage.isValid())
                {
-                       SDL_FreeSurface(temp);
-                       throw Exception(ErrorCode::UNKNOWN_IMAGE_FORMAT, getName());
+                       logWarning << "texture not found: " << getName() << std::endl;
+                       throw Error(Error::RESOURCE_NOT_FOUND, getName());
                }
 
-               mWidth = temp->w;
-               mHeight = temp->h;
+               mImage.flip();
 
-               mContext = temp;
+               mWidth = mImage.getWidth();
+               mHeight = mImage.getHeight();
+               mMode = mImage.getColorMode();
        }
 
 
@@ -295,7 +243,7 @@ public:
                        return;
                }
 
-               if (!mContext) loadFromFile();
+               //if (!mContext) loadFromFile();
 
                glGenTextures(1, &mObject);
                glBindTexture(GL_TEXTURE_2D, mObject);
@@ -307,18 +255,18 @@ public:
                        0,
                        mMode,
                        //3,
-                       mContext->w,
-                       mContext->h,
+                       mWidth,
+                       mHeight,
                        0,
                        mMode,
                        GL_UNSIGNED_BYTE,
-                       mContext->pixels
+                       mImage.getPixels()
                );
 
                setProperties();
 
-               SDL_FreeSurface(mContext);
-               mContext = 0;
+               //SDL_FreeSurface(mContext);
+               //mContext = 0;
        }
 
 
@@ -378,7 +326,7 @@ public:
        }
 
 
-       SDL_Surface*            mContext;
+       Image                           mImage;
        unsigned                        mWidth;                 ///< Horizontal dimension of the image.
        unsigned                        mHeight;                ///< Vertical dimension.
 
This page took 0.022152 seconds and 4 git commands to generate.