Преглед изворни кода

Merge pull request #79 from blackberry-gaming/next-cculy

Image class refactoring
Sean Paul Taylor пре 14 година
родитељ
комит
b643cd807d

+ 3 - 0
gameplay/gameplay.vcxproj

@@ -36,6 +36,7 @@
     <ClCompile Include="src\Game.cpp" />
     <ClCompile Include="src\gameplay-main-qnx.cpp" />
     <ClCompile Include="src\gameplay-main-win32.cpp" />
+    <ClCompile Include="src\Image.cpp" />
     <ClCompile Include="src\Input.cpp" />
     <ClCompile Include="src\Joint.cpp" />
     <ClCompile Include="src\Light.cpp" />
@@ -103,6 +104,7 @@
     <ClInclude Include="src\Frustum.h" />
     <ClInclude Include="src\Game.h" />
     <ClInclude Include="src\gameplay.h" />
+    <ClInclude Include="src\Image.h" />
     <ClInclude Include="src\Input.h" />
     <ClInclude Include="src\Joint.h" />
     <ClInclude Include="src\Light.h" />
@@ -173,6 +175,7 @@
     <None Include="src\BoundingSphere.inl" />
     <None Include="src\Game.inl" />
     <None Include="src\gameplay-main-macos.mm" />
+    <None Include="src\Image.inl" />
     <None Include="src\Matrix.inl" />
     <None Include="src\Plane.inl" />
     <None Include="src\PlatformMacOS.mm" />

+ 9 - 0
gameplay/gameplay.vcxproj.filters

@@ -210,6 +210,9 @@
     <ClCompile Include="src\SceneLoader.cpp">
       <Filter>src</Filter>
     </ClCompile>
+    <ClCompile Include="src\Image.cpp">
+      <Filter>src</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="src\Animation.h">
@@ -404,6 +407,9 @@
     <ClInclude Include="src\SceneLoader.h">
       <Filter>src</Filter>
     </ClInclude>
+    <ClInclude Include="src\Image.h">
+      <Filter>src</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <None Include="res\shaders\bumped-specular.vsh">
@@ -478,6 +484,9 @@
     <None Include="src\Game.inl">
       <Filter>src</Filter>
     </None>
+    <None Include="src\Image.inl">
+      <Filter>src</Filter>
+    </None>
   </ItemGroup>
   <ItemGroup>
     <None Include="src\PhysicsFixedConstraint.inl">

+ 111 - 0
gameplay/src/Image.cpp

@@ -0,0 +1,111 @@
+#include "Base.h"
+#include "FileSystem.h"
+#include "Image.h"
+
+namespace gameplay
+{
+
+Image* Image::create(const char* path)
+{
+    // Open the file.
+    FILE* fp = FileSystem::openFile(path, "rb");
+    if (fp == NULL)
+    {
+        return NULL;
+    }
+
+    // Verify PNG signature.
+    unsigned char sig[8];
+    if (fread(sig, 1, 8, fp) != 8 || png_sig_cmp(sig, 0, 8) != 0)
+    {
+        LOG_ERROR_VARG("Texture is not a valid PNG: %s", path);
+        fclose(fp);
+        return NULL;
+    }
+
+    // Initialize png read struct (last three parameters use stderr+longjump if NULL).
+    png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+    if (png == NULL)
+    {
+        fclose(fp);
+        return NULL;
+    }
+
+    // Initialize info struct.
+    png_infop info = png_create_info_struct(png);
+    if (info == NULL)
+    {
+        fclose(fp);
+        png_destroy_read_struct(&png, NULL, NULL);
+        return NULL;
+    }
+
+    // Set up error handling (required without using custom error handlers above).
+    if (setjmp(png_jmpbuf(png)))
+    {
+        fclose(fp);
+        png_destroy_read_struct(&png, &info, NULL);
+        return NULL;
+    }
+
+    // Initialize file io.
+    png_init_io(png, fp);
+
+    // Indicate that we already read the first 8 bytes (signature).
+    png_set_sig_bytes(png, 8);
+
+    // Read the entire image into memory.
+    png_read_png(png, info, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, NULL);
+
+    Image* image = new Image();
+    image->_width = png_get_image_width(png, info);
+    image->_height = png_get_image_height(png, info);
+
+    png_byte colorType = png_get_color_type(png, info);
+    switch (colorType)
+    {
+    case PNG_COLOR_TYPE_RGBA:
+        image->_format = RGBA8888;
+        break;
+
+    case PNG_COLOR_TYPE_RGB:
+        image->_format = RGB888;
+        break;
+
+    default:
+        LOG_ERROR_VARG("Unsupported PNG color type (%d) for texture: %s", (int)colorType, path);
+        fclose(fp);
+        png_destroy_read_struct(&png, &info, NULL);
+        return NULL;
+    }
+
+    unsigned int stride = png_get_rowbytes(png, info);
+
+    // Allocate image data.
+    image->_data = new unsigned char[stride * image->_height];
+
+    // Read rows into image data.
+    png_bytepp rows = png_get_rows(png, info);
+    for (unsigned int i = 0; i < image->_height; ++i)
+    {
+        memcpy(image->_data+(stride * (image->_height-1-i)), rows[i], stride);
+    }
+
+    // Clean up.
+    png_destroy_read_struct(&png, &info, NULL);
+    fclose(fp);
+
+    return image;
+}
+
+Image::Image()
+{
+    // Unused
+}
+
+Image::~Image()
+{
+    SAFE_DELETE_ARRAY(_data);
+}
+
+}

+ 84 - 0
gameplay/src/Image.h

@@ -0,0 +1,84 @@
+#ifndef IMAGE_H__
+#define IMAGE_H__
+
+#include "Ref.h"
+#include "Texture.h"
+
+namespace gameplay
+{
+
+/**
+ * Represents an image (currently only supports PNG files).
+ */
+class Image : public Ref
+{
+public:
+
+    /**
+     * Defines the set of supported image formats.
+     */
+    enum Format
+    {
+        RGB888 = Texture::RGB888,
+        RGBA8888 = Texture::RGBA8888
+    };
+
+    /**
+     * Creates an image from the image file at the given path.
+     * 
+     * @param path The path to the image file.
+     * @return The newly created image.
+     */
+    static Image* create(const char* path);
+
+    /**
+     * Gets the image's raw pixel data.
+     * 
+     * @return The image's pixel data.
+     */
+    inline unsigned char* getData() const;
+
+    /**
+     * Gets the image's format.
+     * 
+     * @return The image's format.
+     */
+    inline Format getFormat() const;
+
+    /**
+     * Gets the height of the image.
+     * 
+     * @return The height of the image.
+     */
+    inline unsigned int getHeight() const;
+        
+    /**
+     * Gets the width of the image.
+     * 
+     * @return The width of the image.
+     */
+    inline unsigned int getWidth() const;
+
+private:
+
+    /**
+     * Constructor.
+     */
+    Image();
+        
+    /**
+     * Destructor.
+     */
+    ~Image();
+
+    unsigned char* _data;
+    Format _format;
+    unsigned int _height;
+    unsigned int _width;
+};
+
+}
+
+#include "Image.inl"
+
+#endif

+ 26 - 0
gameplay/src/Image.inl

@@ -0,0 +1,26 @@
+#include "Image.h"
+
+namespace gameplay
+{
+
+inline unsigned char* Image::getData() const
+{
+    return _data;
+}
+
+inline Image::Format Image::getFormat() const
+{
+    return _format;
+}
+
+inline unsigned int Image::getHeight() const
+{
+    return _height;
+}
+        
+inline unsigned int Image::getWidth() const
+{
+    return _width;
+}
+
+}

+ 5 - 4
gameplay/src/PhysicsRigidBody.cpp

@@ -1,5 +1,6 @@
 #include "Base.h"
 #include "Game.h"
+#include "Image.h"
 #include "PhysicsController.h"
 #include "PhysicsMotionState.h"
 #include "PhysicsRigidBody.h"
@@ -61,7 +62,7 @@ PhysicsRigidBody::PhysicsRigidBody(Node* node, PhysicsRigidBody::Type type, floa
     Game::getInstance()->getPhysicsController()->addRigidBody(this);
 }
 
-PhysicsRigidBody::PhysicsRigidBody(Node* node, Texture::Image* image, float mass,
+PhysicsRigidBody::PhysicsRigidBody(Node* node, Image* image, float mass,
     float friction, float restitution, float linearDamping, float angularDamping)
         : _shape(NULL), _body(NULL), _node(node), _listeners(NULL), _angularVelocity(NULL),
         _anisotropicFriction(NULL), _gravity(NULL), _linearVelocity(NULL), _vertexData(NULL),
@@ -79,10 +80,10 @@ PhysicsRigidBody::PhysicsRigidBody(Node* node, Texture::Image* image, float mass
     unsigned int pixelSize = 0;
     switch (image->getFormat())
     {
-        case Texture::RGB888:
+        case Image::RGB888:
             pixelSize = 3;
             break;
-        case Texture::RGBA8888:
+        case Image::RGBA8888:
             pixelSize = 4;
             break;
     }
@@ -386,7 +387,7 @@ PhysicsRigidBody* PhysicsRigidBody::create(Node* node, Properties* properties)
     else
     {
         // Load the image data from the given file path.
-        Texture::Image* image = Texture::Image::create(imagePath);
+        Image* image = Image::create(imagePath);
         if (!image)
             return NULL;
 

+ 1 - 1
gameplay/src/PhysicsRigidBody.h

@@ -322,7 +322,7 @@ private:
      * @param linearDamping The percentage of linear velocity lost per second (between 0.0 and 1.0).
      * @param angularDamping The percentage of angular velocity lost per second (between 0.0 and 1.0).
      */
-    PhysicsRigidBody(Node* node, Texture::Image* image, float mass, float friction = 0.5,
+    PhysicsRigidBody(Node* node, Image* image, float mass, float friction = 0.5,
         float restitution = 0.0, float linearDamping = 0.0, float angularDamping = 0.0);
 
     /**

+ 2 - 105
gameplay/src/Texture.cpp

@@ -1,6 +1,6 @@
 #include "Base.h"
+#include "Image.h"
 #include "Texture.h"
-#include "FileSystem.h"
 
 namespace gameplay
 {
@@ -92,7 +92,7 @@ Texture* Texture::create(const char* path, bool generateMipmaps)
 
 Texture* Texture::create(Image* image, bool generateMipmaps)
 {
-    return create(image->_format, image->_width, image->_height, image->_data, generateMipmaps);
+    return create((Texture::Format)image->getFormat(), image->getWidth(), image->getHeight(), image->getData(), generateMipmaps);
 }
 
 Texture* Texture::create(Format format, unsigned int width, unsigned int height, unsigned char* data, bool generateMipmaps)
@@ -233,107 +233,4 @@ void Texture::Sampler::bind()
     GL_ASSERT( glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, (GLenum)_magFilter) );
 }
 
-Texture::Image* Texture::Image::create(const char* path)
-{
-    // Open the file.
-    FILE* fp = FileSystem::openFile(path, "rb");
-    if (fp == NULL)
-    {
-        return NULL;
-    }
-
-    // Verify PNG signature.
-    unsigned char sig[8];
-    if (fread(sig, 1, 8, fp) != 8 || png_sig_cmp(sig, 0, 8) != 0)
-    {
-        LOG_ERROR_VARG("Texture is not a valid PNG: %s", path);
-        fclose(fp);
-        return NULL;
-    }
-
-    // Initialize png read struct (last three parameters use stderr+longjump if NULL).
-    png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
-    if (png == NULL)
-    {
-        fclose(fp);
-        return NULL;
-    }
-
-    // Initialize info struct.
-    png_infop info = png_create_info_struct(png);
-    if (info == NULL)
-    {
-        fclose(fp);
-        png_destroy_read_struct(&png, NULL, NULL);
-        return NULL;
-    }
-
-    // Set up error handling (required without using custom error handlers above).
-    if (setjmp(png_jmpbuf(png)))
-    {
-        fclose(fp);
-        png_destroy_read_struct(&png, &info, NULL);
-        return NULL;
-    }
-
-    // Initialize file io.
-    png_init_io(png, fp);
-
-    // Indicate that we already read the first 8 bytes (signature).
-    png_set_sig_bytes(png, 8);
-
-    // Read the entire image into memory.
-    png_read_png(png, info, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, NULL);
-
-    Image* image = new Image();
-    image->_width = png_get_image_width(png, info);
-    image->_height = png_get_image_height(png, info);
-
-    png_byte colorType = png_get_color_type(png, info);
-    switch (colorType)
-    {
-    case PNG_COLOR_TYPE_RGBA:
-        image->_format = RGBA8888;
-        break;
-
-    case PNG_COLOR_TYPE_RGB:
-        image->_format = RGB888;
-        break;
-
-    default:
-        LOG_ERROR_VARG("Unsupported PNG color type (%d) for texture: %s", (int)colorType, path);
-        fclose(fp);
-        png_destroy_read_struct(&png, &info, NULL);
-        return NULL;
-    }
-
-    unsigned int stride = png_get_rowbytes(png, info);
-
-    // Allocate image data.
-    image->_data = new unsigned char[stride * image->_height];
-
-    // Read rows into image data.
-    png_bytepp rows = png_get_rows(png, info);
-    for (unsigned int i = 0; i < image->_height; ++i)
-    {
-        memcpy(image->_data+(stride * (image->_height-1-i)), rows[i], stride);
-    }
-
-    // Clean up.
-    png_destroy_read_struct(&png, &info, NULL);
-    fclose(fp);
-
-    return image;
-}
-
-Texture::Image::Image()
-{
-    // Unused
-}
-
-Texture::Image::~Image()
-{
-    SAFE_DELETE_ARRAY(_data);
-}
-
 }

+ 2 - 61
gameplay/src/Texture.h

@@ -6,6 +6,8 @@
 namespace gameplay
 {
 
+class Image;
+
 /**
  * Represents a texture.
  */
@@ -119,67 +121,6 @@ public:
         Filter _magFilter;
     };
 
-    /**
-     * Loads image data (currently only supports PNG files).
-     */
-    class Image : public Ref
-    {
-        friend class Texture;
-
-    public:
-        /**
-         * Creates an image from the image file at the given path.
-         * 
-         * @param path The path to the image file.
-         * @return The newly created image.
-         */
-        static Image* create(const char* path);
-
-        /**
-         * Gets the image's raw pixel data.
-         * 
-         * @return The image's pixel data.
-         */
-        inline unsigned char* getData() { return _data; }
-
-        /**
-         * Gets the image's format.
-         * 
-         * @return The image's format.
-         */
-        inline Format getFormat() { return _format; }
-
-        /**
-         * Gets the height of the image.
-         * 
-         * @return The height of the image.
-         */
-        inline unsigned int getHeight() { return _height; }
-        
-        /**
-         * Gets the width of the image.
-         * 
-         * @return The width of the image.
-         */
-        inline unsigned int getWidth() { return _width; }
-
-    private:
-        /**
-         * Constructor.
-         */
-        Image();
-        
-        /**
-         * Destructor.
-         */
-        ~Image();
-
-        unsigned char* _data;
-        Format _format;
-        unsigned int _height;
-        unsigned int _width;
-    };
-
     /**
      * Creates a texture from the given image resource.
      *