/* $Id: smoke.cpp,v 1.15 2005/11/13 11:35:14 uwe Exp $ */ #include "global.hpp" #include "spriteset.hpp" #include "world.hpp" #include "smoke.hpp" /**********************************************************/ // "nearly 2.0". Since the accumulated time is incremented in each // call of update, there might be some roundoff errors. To make sure // that the condition "if(accumulatedDT >= DELTA_T_PER_ANIMATION_STEP)" // is true whenever it should be true, we use 1.99 instead of 2.0 #define DELTA_T_PER_ANIMATION_STEP 1.99 /**********************************************************/ Smoke::Smoke() : m_Size( 0 ), m_AccumulatedDT( 0.0 ) { // sequence is chosen randomly in setSize() // setSize( size ); m_Colors[SMOKE_COLOR] = TPLCOL_RGB(255,255,255); } /**********************************************************/ Smoke::~Smoke() { } /**********************************************************/ void Smoke::setSize( const Sint32 size ) { DBG( 1 ) ASSERT( getMinSize() <= size && size <= getMaxSize(), "Smoke::setSize: size %d is out of range [%d,%d]\n", size, getMinSize(), getMaxSize() ); // If the world does not yet have a sprite interface, the // call of setSequence below is not possible. This situation // should only occure during deserialization of the world and // should not cause any further problems in this case, since // valid data are deserialized into the object immediately. if( ! m_worldPointer->getSpriteInterface() ) return; // set new size m_Size = size; // choose a sequence randomly // this also sets m_SequenceLength setSequence( m_worldPointer->getRandom().getUint32() % m_worldPointer->getSpriteInterface() ->getNumSequences(*this) ); // start sequence in any case m_Frame = 0; m_AccumulatedDT = 0.0; } /**********************************************************/ void Smoke::update() { if( (m_AccumulatedDT += m_worldPointer->getDT(m_pos)) >= DELTA_T_PER_ANIMATION_STEP ) { m_AccumulatedDT = 0.0; // smoke only live for one cycle if( ++m_Frame >= m_SequenceLength ) { m_removeMeAfterUpdate = true; } } } /**********************************************************/ Uint32 Smoke::getSerializeBufferSize() const { return NonCollidableObject::getSerializeBufferSize() + NUM_COLORS * Serialize::sizeOf( m_Colors[0] ) + Serialize::sizeOf( m_Size ) + Serialize::sizeOf( m_AccumulatedDT ) // adds additional size for debugging (see serialize.hpp), // but only, if the tags are used PLUS_TAG_SIZE( 1 ); } /**********************************************************/ void Smoke::serialize( Uint8*& bufferPointer ) const { // expands to a check of the buffer movement START_OBJECT_SERIALIZED_SIZE_CHECK( bufferPointer ); NonCollidableObject::serialize( bufferPointer ); for( int i = 0; i < NUM_COLORS; i++ ) { Serialize::serialize( m_Colors[i], bufferPointer ); } Serialize::serialize( m_Size , bufferPointer ); Serialize::serialize( m_AccumulatedDT, bufferPointer ); // expands to tag serialization SERIALIZE_OBJECT_TAG( bufferPointer ); // expands to a check of the buffer movement END_OBJECT_SERIALIZED_SIZE_CHECK( bufferPointer, Smoke ); } /**********************************************************/ void Smoke::deserialize( Uint8*& bufferPointer ) { NonCollidableObject::deserialize( bufferPointer ); for( int i = 0; i < NUM_COLORS; i++ ) { Serialize::deserialize( bufferPointer, m_Colors[i] ); } Serialize::deserialize( bufferPointer, m_Size ); Serialize::deserialize( bufferPointer, m_AccumulatedDT ); // expands to tag deserialization DESERIALIZE_OBJECT_TAG( bufferPointer ); } /**********************************************************/