| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- /*
- -----------------------------------------------------------------------------
- The MIT License (MIT)
- Copyright (c) 2013-08-10 Tobias Peters <tobias.peters@kreativeffekt.at>
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- -----------------------------------------------------------------------------
- */
- #include "data/QGearsBackgroundFileSerializer.h"
- #include "data/QGearsPaletteFileSerializer.h"
- #include <OgreException.h>
- #include <OgreLogManager.h>
- #include <OgreStringConverter.h>
- namespace QGears
- {
- //---------------------------------------------------------------------
- const String BackgroundFileSerializer::SECTION_NAME_PALETTE("PALETTE");
- const String BackgroundFileSerializer::SECTION_NAME_BACK ("BACK");
- const String BackgroundFileSerializer::SECTION_NAME_TEXTURE("TEXTURE");
- const String BackgroundFileSerializer::TAG_FILE_END ("END");
- const Ogre::Real BackgroundFileSerializer::unknown_24_SCALE( 10000000.0 );
- //---------------------------------------------------------------------
- BackgroundFileSerializer::BackgroundFileSerializer() :
- Serializer()
- {
- }
- //---------------------------------------------------------------------
- BackgroundFileSerializer::~BackgroundFileSerializer()
- {
- }
- //---------------------------------------------------------------------
- void
- BackgroundFileSerializer::importBackgroundFile( Ogre::DataStreamPtr &stream
- ,BackgroundFile *pDest )
- {
- readFileHeader( stream );
- read1ByteBool( stream, pDest->getLayers()[0].enabled );
- readPallete( stream, pDest );
- stream->skip( 2 * 2 ); // 2 * uint16 unused;
- readBackground( stream, pDest );
- readTexture( stream, pDest );
- readEnd( stream );
- }
- //---------------------------------------------------------------------
- void
- BackgroundFileSerializer::readFileHeader( Ogre::DataStreamPtr &stream )
- {
- uint16 data[2];
- readShorts( stream, data, 2 );
- m_header.unused = data[0];
- m_header.sort_sprites_by_palette = data[1];
- }
- //---------------------------------------------------------------------
- void
- BackgroundFileSerializer::readSectionHeader( Ogre::DataStreamPtr &stream
- ,const String §ion_name )
- {
- String actual( readString( stream, section_name.size() ) );
- if( actual != section_name )
- {
- Ogre::LogManager::getSingleton().stream()
- << "Warming: Section didn't start with"
- << " expected String '" << section_name << "'"
- << " actual was '" << actual << "'";
- }
- }
- //---------------------------------------------------------------------
- void
- BackgroundFileSerializer::readEnd( Ogre::DataStreamPtr &stream )
- {
- readEndString( stream, TAG_FILE_END );
- }
- //---------------------------------------------------------------------
- void
- BackgroundFileSerializer::readPallete( Ogre::DataStreamPtr &stream
- ,BackgroundFile *pDest )
- {
- readSectionHeader( stream, SECTION_NAME_PALETTE );
- stream->read( pDest->getPalette(), BackgroundFile::PALETTE_ENTRY_COUNT );
- }
- //---------------------------------------------------------------------
- void
- BackgroundFileSerializer::readBackground( Ogre::DataStreamPtr &stream
- ,BackgroundFile *pDest )
- {
- readSectionHeader( stream, SECTION_NAME_BACK );
- Layer *layer( pDest->getLayers() );
- for( size_t i( 0 ); i < BackgroundFile::LAYER_COUNT; ++i, ++layer )
- {
- if( i != 0 )
- {
- read1ByteBool( stream, layer->enabled );
- }
- readLayer( stream, layer, i );
- }
- }
- //---------------------------------------------------------------------
- void
- BackgroundFileSerializer::readLayer( Ogre::DataStreamPtr &stream
- ,Layer *pDest, size_t layer_index )
- {
- if( !pDest->enabled ) return;
- uint16 tmp[4], sprite_count;
- readShorts( stream, tmp, 4 );
- pDest->width = tmp[0];
- pDest->height = tmp[1];
- sprite_count = tmp[2];
- pDest->unknown_06 = tmp[3];
- switch( layer_index )
- {
- case 1:
- readShorts( stream, pDest->unknown_08, 3 );
- case 2:
- case 3:
- readShorts( stream, pDest->unknown_0E, 4 );
- }
- stream->skip( 2 * 2 ); // 2 * uint16 unused;
- readVector( stream, pDest->sprites, sprite_count );
- }
- //---------------------------------------------------------------------
- void
- BackgroundFileSerializer::readObject( Ogre::DataStreamPtr &stream, SpriteData &pDest )
- {
- readObject( stream, pDest.dst );
- readShorts( stream, pDest.unknown_04, 2 );
- readObject( stream, pDest.src );
- readShorts( stream, pDest.unknown_0C, 4 );
- readShort( stream, pDest.palette_page );
- readShort( stream, pDest.depth );
- stream->read( pDest.flags_18, sizeof( pDest.flags_18 ) );
- uint8 flags[2];
- stream->read( flags, sizeof( flags ) );
- pDest.flags_20[0] = flags[0] > 0;
- pDest.flags_20[1] = flags[1] > 0;
- readShort( stream, pDest.unknown_1C );
- readShort( stream, pDest.data_page );
- readShort( stream, pDest.data_page2 );
- // when data_page2 != 0, it must be used instead of data_page
- if ( pDest.data_page2 )
- {
- pDest.data_page = pDest.data_page2;
- }
- readShort( stream, pDest.colourDepth );
- readObject( stream, pDest.unknown_24 );
- pDest.unknown_24 /= unknown_24_SCALE;
- stream->skip( 2 * 2 ); // 2 * uint16 unused
- }
- //---------------------------------------------------------------------
- void
- BackgroundFileSerializer::readTexture( Ogre::DataStreamPtr &stream
- ,BackgroundFile *pDest )
- {
- readSectionHeader( stream, SECTION_NAME_TEXTURE );
- Page *pages( pDest->getPages() );
- for( size_t i(0); i < BackgroundFile::PAGE_COUNT; ++i )
- {
- readObject( stream, pages[i] );
- }
- }
- //---------------------------------------------------------------------
- void
- BackgroundFileSerializer::readObject( Ogre::DataStreamPtr& stream, Color& pDest )
- {
- uint16 colour;
- readShort( stream, colour );
- pDest.r = static_cast<float>(( colour & BIT_MASK_RED ) >> 11);
- pDest.g = static_cast<float>(( colour & BIT_MASK_GREEN ) >> 6);
- pDest.b = static_cast<float>(colour & BIT_MASK_BLUE);
- pDest /= BIT_SIZE;
- pDest.a = 1.0f;
- if ( colour == 0 )
- {
- pDest.a = 0.0f;
- }
- }
- //---------------------------------------------------------------------
- void
- BackgroundFileSerializer::readObject( Ogre::DataStreamPtr &stream, Page &pDest )
- {
- read2ByteBool( stream, pDest.enabled );
- if( !pDest.enabled ) return;
- readShort( stream, pDest.unknown_02 );
- readShort( stream, pDest.value_size );
- if( pDest.value_size != 1 && pDest.value_size != 2 )
- {
- OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS
- ,"Page value_size other then 1 and 2 is not supported"
- ,"BackgroundFileSerializer::readObject" );
- }
- size_t color_count( BackgroundFile::PAGE_DATA_SIZE );
- pDest.colors.clear();
- pDest.data.clear();
- if( pDest.value_size == 2 )
- {
- pDest.colors.reserve( color_count );
- for( size_t i( color_count ); i--; )
- {
- Color colourDest;
- readObject( stream, colourDest );
- pDest.colors.push_back( colourDest );
- }
- }
- else
- {
- pDest.data.resize( color_count );
- stream->read( &pDest.data[0], color_count );
- }
- }
- //---------------------------------------------------------------------
- }
|