From: <cn...@us...> - 2009-11-03 08:20:53
|
Revision: 594 http://hgengine.svn.sourceforge.net/hgengine/?rev=594&view=rev Author: cnlohr Date: 2009-11-03 08:20:45 +0000 (Tue, 03 Nov 2009) Log Message: ----------- wooh state changers Added Paths: ----------- Mercury2/src/StateChanger.cpp Mercury2/src/StateChanger.h Added: Mercury2/src/StateChanger.cpp =================================================================== --- Mercury2/src/StateChanger.cpp (rev 0) +++ Mercury2/src/StateChanger.cpp 2009-11-03 08:20:45 UTC (rev 594) @@ -0,0 +1,220 @@ +#include <StateChanger.h> +#include <MercuryNode.h> +#include <GLHeaders.h> + +using namespace std; + +//////////////////////////////////////STATE CHANGERS////////////////////////////////////////// + +///State changer for color changing. +class ColorChange : public StateChange +{ +public: + ColorChange( const MVector< MString > & sParameters ) : StateChange( sParameters ) + { + if( sParameters.size() < 3 ) + { + LOG.Write( ssprintf( "Error: ColorChange state has invalid number of parameters(%d).", sParameters.size() ) ); + return; + } + + r = StrToFloat( sParameters[0] ); + g = StrToFloat( sParameters[1] ); + b = StrToFloat( sParameters[2] ); + + if( sParameters.size() > 3 ) + a = StrToFloat( sParameters[3] ); + else + a = 1.; + } + + void Stringify( MString & sOut ) + { + sOut = ssprintf( "%f,%f,%f,%f", r,g,b,a ); + } + + void Activate() + { + glColor4f( r,g,b,a ); + } + + STATECHANGE_RTTI( ColorChange ); + float r,g,b,a; +}; + +REGISTER_STATECHANGE( ColorChange ); + + +//////////////////////////////////////STATE CHANGE CHUNK////////////////////////////////////// +StateChangeRegister * StateChangeRegister::m_Instance; + +StateChangeRegister & StateChangeRegister::Instance() +{ + if( !m_Instance ) + m_Instance = new StateChangeRegister(); + return *m_Instance; +} + +int StateChangeRegister::RegisterGenerator( const MString & name, StateChange*(*gn)( const MVector< MString > &sParameters ) ) +{ + m_Generators[name] = gn; + m_hStateIDs[name] = m_iStateCount++; + return m_iStateCount-1; +} + +MAutoPtr< StateChange > StateChangeRegister::Create( const MString & name, const MVector< MString > & sParameters ) +{ + StateChange*(**tgn)( const MVector< MString > &sParameters ); + if( !( tgn = m_Generators.get( name ) ) ) + return 0; + + return (**tgn)(sParameters ); +} + +int StateChangeRegister::GetStateID( const MString & spar ) +{ + int * r = m_hStateIDs.get( spar ); + if( r ) + return *r; + else + return 0; +} + + +//////////////////////////////////////STATE CHANGER ASSET///////////////////////////////////// + +REGISTER_ASSET_TYPE(StateChanger); + +StateChanger::StateChanger() + :MercuryAsset() +{ + //Make sure our state stack is correctly sized + if( m_StateSet.size() < (unsigned)StateChangeRegister::Instance().GetStateCount() ) + m_StateSet.resize( StateChangeRegister::Instance().GetStateCount() ); +} + +StateChanger::~StateChanger() +{ + REMOVE_ASSET_INSTANCE(TEXTURE, m_path); +} + +void StateChanger::Render(const MercuryNode* node) +{ + for( unsigned i = 0; i < m_vStates.size(); i++ ) + { + MAutoPtr< StateChange > & k = m_vStates[i]; + k->Activate(); + m_StateSet[k->sID].push_back( k ); + } +} + +void StateChanger::PostRender(const MercuryNode* node) +{ + for( unsigned i = 0; i < m_vStates.size(); i++ ) + { + MAutoPtr< StateChange > & k = m_vStates[i]; + MVector< MAutoPtr< StateChange > > & l = m_StateSet[k->sID]; + + unsigned ilpos = l.size() - 1; + + if( ilpos <= 0 ) + continue; + + l.resize( ilpos-- ); + + if( ilpos >= 0 ) + l[ilpos]->Activate(); + } +} + +bool StateChanger::LoadFromString( const MString & sFile ) +{ + int f = sFile.find( ":", 0 ); + if( f <= 0 ) + { + LOG.Write( ssprintf( "Error loading new StateChanger node. File: \"%s\" improperly formatted, required \"[type]:[parameters]\".", sFile.c_str() ) ); + return false; + } + + MString sType = sFile.substr( 0, f ); + MString sParameters = sFile.substr( f+1 ); + MVector< MString > vsParameters; + + SplitStrings( sParameters, vsParameters, ",", " ", 1, 1 ); + + MAutoPtr< StateChange > s = StateChangeRegister::Instance().Create( sType, vsParameters ); + if( s.Ptr() ) + m_vStates.push_back( s ); + else + { + LOG.Write( ssprintf( "Error: Could not make new StateChanger from: \"%s\"", sFile.c_str() ) ); + return false; + } + + + return true; +} + +void StateChanger::LoadFromXML(const XMLNode& node) +{ + MercuryAsset::LoadFromXML(node); + if ( !node.Attribute("file").empty() ) + { + MString sFile = node.Attribute("file"); + LoadFromString( sFile ); + } +} + +void StateChanger::SaveToXMLTag( MString & sXMLStream ) +{ + if( m_vStates.size() ) + { + MString sStr; + m_vStates[0]->Stringify( sStr ); + sXMLStream += "file=\"" + sStr + "\" "; + } + + MercuryAsset::SaveToXMLTag( sXMLStream ); +} + +StateChanger* StateChanger::Generate() +{ + return new StateChanger(); +} + +MVector< MVector< MAutoPtr< StateChange > > > StateChanger::m_StateSet; + + +/**************************************************************************** + * Copyright (C) 2008 - 2009 by Joshua Allen * + * Charles Lohr * + * * + * * + * All rights reserved. * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions * + * are met: * + * * Redistributions of source code must retain the above copyright * + * notice, this list of conditions and the following disclaimer. * + * * Redistributions in binary form must reproduce the above * + * copyright notice, this list of conditions and the following * + * disclaimer in the documentation and/or other materials provided * + * with the distribution. * + * * Neither the name of the Mercury Engine nor the names of its * + * contributors may be used to endorse or promote products derived * + * from this software without specific prior written permission. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * + ***************************************************************************/ + Added: Mercury2/src/StateChanger.h =================================================================== --- Mercury2/src/StateChanger.h (rev 0) +++ Mercury2/src/StateChanger.h 2009-11-03 08:20:45 UTC (rev 594) @@ -0,0 +1,107 @@ +#ifndef STATE_CHANGER_H +#define STATE_CHANGER_H + +#include <MercuryAsset.h> +#include <RawImageData.h> + + +#define STATECHANGE_RTTI(x) const char * GetType() { return #x; } + +class StateChange : public RefBase +{ +public: + StateChange( const MVector< MString > & sParameters ) { } + void Stringify( MString & sOut ) { } + + virtual void Activate() = 0; + + STATECHANGE_RTTI( StateChange ); + int sID; +}; + +#define REGISTER_STATECHANGE( x ) \ + extern int sID##x;\ + StateChange*CreateNew##x( const MVector< MString > & sParameters ) { x * ret = new x( sParameters ); ret->sID = sID##x; return ret; } \ + int sID##x = StateChangeRegister::Instance().RegisterGenerator( #x, CreateNew##x ); + +class StateChangeRegister +{ +public: + static StateChangeRegister & Instance(); + int RegisterGenerator( const MString & name, StateChange*(*gn)( const MVector< MString > &sParameters ) ); + MAutoPtr< StateChange > Create( const MString & name, const MVector< MString > & sParameters ); + + int GetStateID( const MString & spar ); + int GetStateCount() { return m_iStateCount; } +private: + MHash< StateChange*(*)(const MVector< MString > &sParameters) > m_Generators; + static StateChangeRegister * m_Instance; + MHash< int > m_hStateIDs; + int m_iStateCount; +}; + +///State Changer Node +/** + This node is for things like changing the color of everything, or turning on/off + blending modes, etc. in the overall system. + Note: While this node can handle multiple states at once - it is currently + only set up to be able to load one from the incoming XML. */ +class StateChanger : public MercuryAsset +{ +public: + StateChanger(); + virtual ~StateChanger(); + + virtual void Render(const MercuryNode* node); + virtual void PostRender(const MercuryNode* node); + + virtual void LoadFromXML(const XMLNode& node); + virtual void SaveToXMLTag( MString & sXMLStream ); + static StateChanger* Generate(); + + bool LoadFromString( const MString & sDescription ); + + GENRTTI( StateChanger ); +private: + + MVector< MAutoPtr< StateChange > > m_vStates; + + //Actually... It's faster if we use MercuryVectors here. + static MVector< MVector< MAutoPtr< StateChange > > > m_StateSet; +}; + +#endif + +/**************************************************************************** + * Copyright (C) 2008 - 2009 by Joshua Allen * + * Charles Lohr * + * * + * * + * All rights reserved. * + * * + * Redistribution and use in source and binary forms, with or without * + * modification, are permitted provided that the following conditions * + * are met: * + * * Redistributions of source code must retain the above copyright * + * notice, this list of conditions and the following disclaimer. * + * * Redistributions in binary form must reproduce the above * + * copyright notice, this list of conditions and the following * + * disclaimer in the documentation and/or other materials provided * + * with the distribution. * + * * Neither the name of the Mercury Engine nor the names of its * + * contributors may be used to endorse or promote products derived * + * from this software without specific prior written permission. * + * * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR * + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * + ***************************************************************************/ + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |