Procházet zdrojové kódy

fix broken check in file system, change raw pixel pointer to std::vector to simply code and help track down memory courrpting issue

Paul před 11 roky
rodič
revize
9f5edc6c80

+ 1 - 1
QGearsMain/src/common/FileSystem.cpp

@@ -39,7 +39,7 @@ FileSystem::ReadFile(const Ogre::String &path, void* buffer, const unsigned int
     fseek(file, start, SEEK_SET);
     const auto ret = fread(buffer, sizeof(char), length, file);
     fclose(file);
-    if (ret != sizeof(char))
+    if (ret != sizeof(char) * length)
     {
         LOG_ERROR("Failed to read all data\n");
         return false;

+ 2 - 2
utilities/common/FontFile.cpp

@@ -88,7 +88,7 @@ FontFile::GetSurface(void)
                 color.g = (((data >> i) & 0x01) == 1) ? 0 : 255;
                 color.b = (((data >> i) & 0x01) == 1) ? 0 : 255;
                 color.a = (((data >> i) & 0x01) == 1) ? 255 : 255;
-                memcpy(glyth->pixels + 64 * y + j, &color, sizeof(ClutColor));
+                memcpy(glyth->pixels.data() + 64 * y + j, &color, sizeof(ClutColor));
                 j += 4;
             }
 
@@ -100,7 +100,7 @@ FontFile::GetSurface(void)
                 color.g = ((data >> i) & 0x01 == 1) ? 0 : 255;
                 color.b = ((data >> i) & 0x01 == 1) ? 0 : 255;
                 color.a = ((data >> i) & 0x01 == 1) ? 255 : 255;
-                memcpy(glyth->pixels + 64 * y + j, &color, sizeof(ClutColor));
+                memcpy(glyth->pixels.data() + 64 * y + j, &color, sizeof(ClutColor));
                 j += 4;
             }
         }

+ 7 - 42
utilities/common/Surface.cpp

@@ -23,8 +23,7 @@ Surface::Surface( const Surface &copy ):
 {
     if( width && height )
     {
-        pixels = new unsigned char[ width * height * 4 ];
-        memcpy( pixels, copy.pixels, width * height * 4 );
+        pixels = copy.pixels;
     }
 }
 
@@ -33,18 +32,12 @@ Surface::Surface( const Surface &copy ):
 Surface&
 Surface::operator =( const Surface &copy )
 {
-    if( width && height )
-    {
-        delete[] pixels;
-    }
-
     if( copy.width && copy.height )
     {
         width  = copy.width;
         height = copy.height;
 
-        pixels = new unsigned char[ width * height * 4 ];
-        memcpy( pixels, copy.pixels, width * height * 4 );
+        pixels = copy.pixels;
     }
 
     return *this;
@@ -54,10 +47,7 @@ Surface::operator =( const Surface &copy )
 
 Surface::~Surface()
 {
-    if( width && height )
-    {
-        delete[] pixels;
-    }
+
 }
 
 
@@ -69,7 +59,7 @@ CreateSurface( const int width, const int height )
 
     image->width   = width;
     image->height  = height;
-    image->pixels  = new unsigned char[ width * height * 4 ];
+    image->pixels.resize( width * height * 4 );
 
     return image;
 }
@@ -86,7 +76,7 @@ CopyToSurface( Surface* dest, const int x_d, const int y_d, Surface* src )
 
     for( int y_from = y_d, y_to = y_d + src->height; y_from < y_to; ++y_from )
     {
-        memcpy( dest->pixels + y_from * dest->width * 4 + x_d * 4, src->pixels + ( y_from - y_d ) * src->width * 4, src->width * 4 );
+        memcpy( dest->pixels.data() + y_from * dest->width * 4 + x_d * 4, src->pixels.data() + ( y_from - y_d ) * src->width * 4, src->width * 4 );
     }
 }
 
@@ -101,7 +91,7 @@ CreateSubSurface( const int x, const int y, const int width, const int height, S
     {
         for( int y_from = y, y_to = y + image->height; y_from < y_to; ++y_from )
         {
-            memcpy( image->pixels + ( y_from - y ) * image->width * 4, surface->pixels + ( y_from * surface->width + x ) * 4, image->width * 4) ;
+            memcpy( image->pixels.data() + ( y_from - y ) * image->width * 4, surface->pixels.data() + ( y_from * surface->width + x ) * 4, image->width * 4) ;
         }
     }
 
@@ -114,34 +104,9 @@ Surface*
 CreateSurfaceFrom( const int width, const int height, unsigned char* pixels )
 {
     Surface* image = CreateSurface( width, height );
-
     if( pixels != NULL )
     {
-        memcpy( image->pixels, pixels, width * height * 4 );
+        memcpy( image->pixels.data(), pixels, width * height * 4 );
     }
-
     return image;
 }
-
-
-
-void
-SetSurfaceSize( Surface* &surface, const int &width, const int &height )
-{
-    unsigned char* pixels = new unsigned char[ width * height * 4 ];
-    memset( pixels, 0x00, width * height * 4 );
-
-    for( int y = 0; y < height; y++ )
-    {
-        if( y < surface->height )
-        {
-            int size_to_copy = ( surface->width < width ) ? surface->width * 4 : width * 4;
-            memcpy( pixels + y * width * 4, surface->pixels + y * surface->width * 4, size_to_copy );
-        }
-    }
-
-    delete surface;
-    surface = CreateSurfaceFrom( width, height, pixels );
-
-    delete[] pixels;
-}

+ 1 - 4
utilities/common/Surface.h

@@ -13,7 +13,7 @@ struct Surface
     ~Surface();
 
 
-    unsigned char *pixels;
+    std::vector<unsigned char> pixels;
     int            width;
     int            height;
 };
@@ -25,8 +25,5 @@ void     CopyToSurface( Surface* dest, const int x_d, const int y_d, Surface* sr
 Surface* CreateSubSurface( const int x, const int y, const int width, const int height, Surface* surface );
 Surface* CreateSurfaceFrom( const int width, const int height, unsigned char* pixels );
 
-void     SetSurfaceSize( Surface* &surface, const int &width, const int &height );
-
-
 
 #endif

+ 3 - 3
utilities/ffvii_field_dat_dumper/src/MimFile.cpp

@@ -96,7 +96,7 @@ MimFile::GetSurface( const u16 page_x, const u16 page_y, const u16 clut_x, const
                     color.a = 255;
                 }
 
-                memcpy( ret->pixels + x * 8 + ret->width * 4 * y + 0x00, &color, sizeof( ClutColor ) );
+                memcpy( ret->pixels.data() + x * 8 + ret->width * 4 * y + 0x00, &color, sizeof( ClutColor ) );
 
 
 
@@ -129,7 +129,7 @@ MimFile::GetSurface( const u16 page_x, const u16 page_y, const u16 clut_x, const
                     color.a = 255;
                 }
 
-                memcpy( ret->pixels + x * 8 + ret->width * 4 * y + 0x04, &color, sizeof( ClutColor ) );
+                memcpy( ret->pixels.data() + x * 8 + ret->width * 4 * y + 0x04, &color, sizeof( ClutColor ) );
             }
         }
     }
@@ -175,7 +175,7 @@ MimFile::GetSurface( const u16 page_x, const u16 page_y, const u16 clut_x, const
                     color.a = 255;
                 }
 
-                memcpy( ret->pixels + x * 4 + ret->width * 4 * y, &color, sizeof( ClutColor ) );
+                memcpy( ret->pixels.data() + x * 4 + ret->width * 4 * y, &color, sizeof( ClutColor ) );
             }
         }
     }