From ef80deedc400a9d369eefec63d110104dcb98a75 Mon Sep 17 00:00:00 2001 From: Chris Danford Date: Wed, 23 Feb 2005 02:14:06 +0000 Subject: [PATCH] cleanup: use class registration to create actors --- stepmania/src/ActorFrame.cpp | 1 + stepmania/src/ActorUtil.cpp | 88 +++++++---------------------------- stepmania/src/BGAnimation.cpp | 2 + stepmania/src/BitmapText.cpp | 18 +++++++ 4 files changed, 37 insertions(+), 72 deletions(-) diff --git a/stepmania/src/ActorFrame.cpp b/stepmania/src/ActorFrame.cpp index 5336a63edc..c54a60b4dd 100644 --- a/stepmania/src/ActorFrame.cpp +++ b/stepmania/src/ActorFrame.cpp @@ -10,6 +10,7 @@ // lua start LUA_REGISTER_CLASS( ActorFrame ) // lua end +REGISTER_ACTOR_CLASS( ActorFrame ); ActorFrame::ActorFrame() { diff --git a/stepmania/src/ActorUtil.cpp b/stepmania/src/ActorUtil.cpp index 11411669fa..44416af85e 100644 --- a/stepmania/src/ActorUtil.cpp +++ b/stepmania/src/ActorUtil.cpp @@ -23,6 +23,11 @@ // Actor registration static map *g_pmapRegistrees = NULL; +static bool IsRegistered( const CString& sClassName ) +{ + return g_pmapRegistrees->find( sClassName ) != g_pmapRegistrees->end(); +} + void ActorUtil::Register( const CString& sClassName, CreateActorFn pfn ) { if( g_pmapRegistrees == NULL ) @@ -58,9 +63,6 @@ Actor* ActorUtil::LoadFromActorFile( const CString& sAniDir, const XNode* pNode } } - - Actor* pActor = NULL; // fill this in before we return - // Element name is the type in XML. // Type= is the name in INI. CString sType = pNode->m_sName; @@ -86,62 +88,9 @@ Actor* ActorUtil::LoadFromActorFile( const CString& sAniDir, const XNode* pNode sType = "CourseBanner"; - if( sType == "BGAnimation" ) + if( IsRegistered(sType) ) { - BGAnimation *p = new BGAnimation; - p->LoadFromNode( sAniDir, pNode ); - pActor = p; - return pActor; // we just ran LoadFromNode; don't run it again below - } - else if( - sType == "GenreDisplay" || - sType == "RollingNumbers" || - sType == "ScoreDisplayCalories" ) - { - pActor = ActorUtil::Create( sType, sAniDir, pNode ); - } - else if( sType == "ActorFrame" ) - { - ActorFrame *p = new ActorFrame; - p->LoadFromNode( sAniDir, pNode ); - pActor = p; - return pActor; // we just ran LoadFromNode; don't run it again below - } - else if( sType == "BitmapText" ) - { - /* XXX: How to handle translations? Maybe we should have one metrics section, - * "Text", eg: - * - * [Text] - * SoundVolume=Sound Volume - * TextItem=Hello - * - * and allow "$TextItem$" in .actors to reference that. - */ - /* It's a BitmapText. Note that we could do the actual text setting with metrics, - * by adding "text" and "alttext" commands, but right now metrics can't contain - * commas or semicolons. It's useful to be able to refer to fonts in the real - * theme font dirs, too. */ - CString sAlttext; - pNode->GetAttrValue("AltText", sAlttext ); - - ThemeManager::EvaluateString( sText ); - ThemeManager::EvaluateString( sAlttext ); - - CString sFont = sFile; // accept "File" for backward compatibility - pNode->GetAttrValue("Font", sFont ); - - BitmapText* pBitmapText = new BitmapText; - - /* Be careful: if sFile is "", and we don't check it, then we can end up recursively - * loading the BGAnimationLayer that we're in. */ - if( sFont == "" ) - RageException::Throw( "A BitmapText in '%s' is missing the Font attribute", - sAniDir.c_str() ); - - pBitmapText->LoadFromFont( THEME->GetPathToF( sFont ) ); - pBitmapText->SetText( sText, sAlttext ); - pActor = pBitmapText; + return ActorUtil::Create( sType, sAniDir, pNode ); } else if( sType == "SongBackground" ) { @@ -156,7 +105,8 @@ Actor* ActorUtil::LoadFromActorFile( const CString& sAniDir, const XNode* pNode * with duplicates. */ Sprite* pSprite = new Sprite; pSprite->LoadBG( sFile ); - pActor = pSprite; + pSprite->LoadFromNode( sAniDir, pNode ); + return pSprite; } else if( sType == "SongBanner" ) { @@ -181,15 +131,14 @@ Actor* ActorUtil::LoadFromActorFile( const CString& sAniDir, const XNode* pNode sFile = THEME->GetPathG("Common","fallback banner"); TEXTUREMAN->DisableOddDimensionWarning(); - /* Always load banners with BannerTex. It sets texture properties; * if we load a background without setting those properties, we'll end up * with duplicates. */ Sprite* pSprite = new Sprite; pSprite->Load( Sprite::SongBannerTexture(sFile) ); - pActor = pSprite; - + pSprite->LoadFromNode( sAniDir, pNode ); TEXTUREMAN->EnableOddDimensionWarning(); + return pSprite; } else if( sType == "CourseBanner" ) { @@ -218,8 +167,9 @@ Actor* ActorUtil::LoadFromActorFile( const CString& sAniDir, const XNode* pNode TEXTUREMAN->DisableOddDimensionWarning(); Sprite* pSprite = new Sprite; pSprite->Load( Sprite::SongBannerTexture(sFile) ); - pActor = pSprite; + pSprite->LoadFromNode( sAniDir, pNode ); TEXTUREMAN->EnableOddDimensionWarning(); + return pSprite; } else // sType is empty or garbage (e.g. "1" // 0==Sprite") { @@ -287,18 +237,12 @@ retry: sNewPath = DerefRedir( sNewPath ); - pActor = ActorUtil::MakeActor( sNewPath ); + Actor *pActor = ActorUtil::MakeActor( sNewPath ); if( pActor == NULL ) return NULL; + pActor->LoadFromNode( sAniDir, pNode ); + return pActor; } - - ASSERT( pActor ); // we should have filled this in above - - // TODO: LoadFromNode should be called when we still have a pointer to the derived type. - // (Why isn't it virtual?) - pActor->LoadFromNode( sAniDir, pNode ); - - return pActor; } Actor* ActorUtil::MakeActor( const RageTextureID &ID ) diff --git a/stepmania/src/BGAnimation.cpp b/stepmania/src/BGAnimation.cpp index 1b19a3b583..39eda2f2e3 100644 --- a/stepmania/src/BGAnimation.cpp +++ b/stepmania/src/BGAnimation.cpp @@ -9,6 +9,8 @@ #include "LuaManager.h" +REGISTER_ACTOR_CLASS( BGAnimation ); + BGAnimation::BGAnimation() { } diff --git a/stepmania/src/BitmapText.cpp b/stepmania/src/BitmapText.cpp index 04f983cf15..025527652d 100644 --- a/stepmania/src/BitmapText.cpp +++ b/stepmania/src/BitmapText.cpp @@ -15,6 +15,7 @@ // lua start LUA_REGISTER_CLASS( BitmapText ) // lua end +REGISTER_ACTOR_CLASS( BitmapText ); /* * XXX: Changing a whole array of diffuse colors every frame (several times) is a waste, @@ -86,6 +87,20 @@ BitmapText::~BitmapText() void BitmapText::LoadFromNode( const CString& sDir, const XNode* pNode ) { + /* XXX: How to handle translations? Maybe we should have one metrics section, + * "Text", eg: + * + * [Text] + * SoundVolume=Sound Volume + * TextItem=Hello + * + * and allow "$TextItem$" in .actors to reference that. + */ + /* It's a BitmapText. Note that we could do the actual text setting with metrics, + * by adding "text" and "alttext" commands, but right now metrics can't contain + * commas or semicolons. It's useful to be able to refer to fonts in the real + * theme font dirs, too. */ + CString sText; pNode->GetAttrValue("Text", sText ); CString sAltText; @@ -99,6 +114,9 @@ void BitmapText::LoadFromNode( const CString& sDir, const XNode* pNode ) * loading the BGAnimationLayer that we're in. */ CString sFont; pNode->GetAttrValue("Font", sFont ); + if( sFont.empty() ) + pNode->GetAttrValue("File", sFont ); // accept "File" for backward compatibility + if( sFont == "" ) RageException::Throw( "An object '%s' in '%s' is missing the Font attribute", pNode->m_sName.c_str(), sDir.c_str() );