Просмотр исходного кода

Merge pull request #93 from myst6re/master

Implementing 32x32 tiles in PC backgrounds
paulsapps 11 лет назад
Родитель
Сommit
11e39e6f67

+ 2 - 2
QGearsMain/include/data/QGearsBackgroundFile.h

@@ -68,8 +68,8 @@ namespace QGears
             uint16 unknown_04[2]; // Unused
             Pixel src;
             Pixel src2; // used for special effects pages, when data_page2 != 0, it must be used instead of src
-            uint16 width; // Do not use
-            uint16 height; // Do not use
+            uint16 width;
+            uint16 height;
 
             uint16 palette_page;
             uint16 depth; // <=> Z

+ 1 - 0
QGearsMain/include/data/QGearsBackgroundFileSerializer.h

@@ -99,6 +99,7 @@ namespace QGears
 
     private:
         Header  m_header;
+        size_t m_layer_index;
     };
 }
 

+ 62 - 12
QGearsMain/src/data/QGearsBackgroundFile.cpp

@@ -160,7 +160,10 @@ namespace QGears
         Ogre::LogManager::getSingleton().stream()
             << "Image Size: " << width << " x " << height
             << " sprite_count " << sprite_count;
-        size_t dst_x( 0 ), dst_y( 0 );
+        size_t dst_x(0), dst_y(0),
+                dst_x_16(0), dst_y_16(0),
+                dst_x_32(0), dst_y_32(0);
+        size_t dst_n_16(0);
         for( SpriteList::const_iterator it( sprites.begin() )
             ;it != sprites.end()
             ;++it)
@@ -172,11 +175,65 @@ namespace QGears
                 Ogre::LogManager::getSingleton().stream()
                     << "Error: referencing an disabled data page";
             }
+
+            // Position sprites in 32x32 blocks
+            if (it->width == 32)
+            {
+                dst_x = dst_x_32;
+                dst_y = dst_y_32;
+
+                dst_x_32 += 32;
+                if (dst_x_32 == width)
+                {
+                    dst_y_32 += 32;
+                    dst_x_32 = 0;
+                }
+            }
+            else if (it->width == 16)
+            {
+                // if we start new 16x16*4 block
+                if (dst_n_16 == 0)
+                {
+                    dst_x_16 = dst_x_32;
+                    dst_y_16 = dst_y_32;
+
+                    dst_x_32 += 32;
+                    if (dst_x_32 == width)
+                    {
+                        dst_y_32 += 32;
+                        dst_x_32 = 0;
+                    }
+                }
+
+                dst_x = dst_x_16;
+                dst_y = dst_y_16;
+
+                ++dst_n_16;
+                if (dst_n_16 == 1 || dst_n_16 == 3)
+                {
+                    dst_x_16 += 16;
+                }
+                else if (dst_n_16 == 2)
+                {
+                    dst_x_16 -= 16;
+                    dst_y_16 += 16;
+                }
+                else if (dst_n_16 == 4)
+                {
+                    dst_n_16 = 0;
+                }
+            }
+            else
+            {
+                Ogre::LogManager::getSingleton().stream()
+                    << "Error: sprite data with invalid size";
+            }
+
             if (data_page.value_size == 2)
             {
-                for (uint16 y(SPRITE_HEIGHT); y--;)
+                for (uint16 y(it->height); y--;)
                 {
-                    for (uint16 x(SPRITE_WIDTH); x--;)
+                    for (uint16 x(it->width); x--;)
                     {
                         size_t data_index((src.y + y) * PAGE_DATA_WIDTH + src.x + x);
                         if (data_index >= data_page.colors.size())
@@ -210,9 +267,9 @@ namespace QGears
                 {
                     firstColorHidden = m_palette[it->palette_page] > 0;
                 }
-                for (uint16 y(SPRITE_HEIGHT); y--;)
+                for (uint16 y(it->height); y--;)
                 {
-                    for (uint16 x(SPRITE_WIDTH); x--;)
+                    for (uint16 x(it->width); x--;)
                     {
                         size_t data_index((src.y + y) * PAGE_DATA_WIDTH + src.x + x);
                         if (data_index >= data_page.data.size())
@@ -252,13 +309,6 @@ namespace QGears
                     }
                 }
             }
-
-            dst_x += SPRITE_WIDTH;
-            if (dst_x >= width)
-            {
-                dst_x = 0;
-                dst_y += SPRITE_HEIGHT;
-            }
         }
 
         Ogre::DataStreamPtr stream( buffer );

+ 15 - 1
QGearsMain/src/data/QGearsBackgroundFileSerializer.cpp

@@ -41,7 +41,7 @@ namespace QGears
 
     //---------------------------------------------------------------------
     BackgroundFileSerializer::BackgroundFileSerializer() :
-        Serializer()
+        Serializer(), m_layer_index(BackgroundFile::LAYER_COUNT)
     {
     }
 
@@ -150,6 +150,7 @@ namespace QGears
             readShorts( stream, pDest->unknown_0E, 4 );
         }
         stream->skip( 2 * 2 ); // 2 * uint16 unused;
+        m_layer_index = layer_index;
         readVector( stream, pDest->sprites, sprite_count );
     }
 
@@ -164,6 +165,19 @@ namespace QGears
         readShort( stream, pDest.width );
         readShort( stream, pDest.height );
 
+        uint16 size;
+        // width and height are sometimes incorrect in the file
+        if ( m_layer_index < 2 ) {
+            size = 16;
+        } else if ( m_layer_index < BackgroundFile::LAYER_COUNT) {
+            size = 32;
+        } else {
+            OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS
+                ,"m_layer_index not set correctly"
+                ,"BackgroundFileSerializer::readObject" );
+        }
+        pDest.width = pDest.height = size;
+
         readShort( stream, pDest.palette_page );
         readShort( stream, pDest.depth );