void Sprite::Load( RageTextureID ID ) { if( !ID.filename.empty() ) LoadFromTexture( ID ); LoadStatesFromTexture(); };
bool Sprite::Load( RageTextureID ID ) { bool result; if( ID.filename == "" ) result = true; if( ID.filename.Right(7) == ".sprite" ) result = LoadFromSpriteFile( ID ); else result = LoadFromTexture( ID ); return result; };
bool CGUIStaticImage_Impl::LoadFromFile ( const char* szFilename, const char* szDirectory ) { std::string strPath = szDirectory ? szDirectory : m_pGUI->GetWorkingDirectory (); strPath += szFilename; // Load texture if ( !m_pTexture ) { m_pTexture = new CGUITexture_Impl ( m_pGUI ); m_bCreatedTexture = true; } if ( !m_pTexture->LoadFromFile ( strPath.c_str () ) ) return false; // Load image return LoadFromTexture ( m_pTexture ); }
void Sprite::LoadFromNode( const XNode* pNode ) { /* Texture may refer to the ID of a render target; if it's already * registered, use it without trying to resolve it. */ RString sPath; pNode->GetAttrValue( "Texture", sPath ); if( !sPath.empty() && !TEXTUREMAN->IsTextureRegistered( RageTextureID(sPath) ) ) ActorUtil::GetAttrPath( pNode, "Texture", sPath ); if( !sPath.empty() ) { // Load the texture LoadFromTexture( sPath ); LoadStatesFromTexture(); // Read in frames and delays from the sprite file, // overwriting the states that LoadFromTexture created. // If the .sprite file doesn't define any states, leave // frames and delays created during LoadFromTexture(). vector<State> aStates; const XNode *pFrames = pNode->GetChild( "Frames" ); if( pFrames != NULL ) { /* All attributes are optional. If Frame is omitted, use the previous state's * frame (or 0 if the first). * Frames = { * { Delay=1.0f; Frame=0; { x=0, y=0 }, { x=1, y=1 } }; * } */ int iFrameIndex = 0; for( int i=0; true; i++ ) { const XNode *pFrame = pFrames->GetChild( ssprintf("%i", i+1) ); // +1 for Lua's arrays if( pFrame == NULL ) break; State newState; if( !pFrame->GetAttrValue("Delay", newState.fDelay) ) { newState.fDelay = 0.1f; } pFrame->GetAttrValue( "Frame", iFrameIndex ); if( iFrameIndex >= m_pTexture->GetNumFrames() ) { LuaHelpers::ReportScriptErrorFmt( "%s: State #%i is frame %d, but the texture \"%s\" only has %d frames", ActorUtil::GetWhere(pNode).c_str(), i+1, iFrameIndex, sPath.c_str(), m_pTexture->GetNumFrames() ); } newState.rect = *m_pTexture->GetTextureCoordRect( iFrameIndex ); const XNode *pPoints[2] = { pFrame->GetChild( "1" ), pFrame->GetChild( "2" ) }; if( pPoints[0] != NULL && pPoints[1] != NULL ) { RectF r = newState.rect; float fX = 1.0f, fY = 1.0f; pPoints[0]->GetAttrValue( "x", fX ); pPoints[0]->GetAttrValue( "y", fY ); newState.rect.left = SCALE( fX, 0.0f, 1.0f, r.left, r.right ); newState.rect.top = SCALE( fY, 0.0f, 1.0f, r.top, r.bottom ); pPoints[1]->GetAttrValue( "x", fX ); pPoints[1]->GetAttrValue( "y", fY ); newState.rect.right = SCALE( fX, 0.0f, 1.0f, r.left, r.right ); newState.rect.bottom = SCALE( fY, 0.0f, 1.0f, r.top, r.bottom ); } aStates.push_back( newState ); } } else for( int i=0; true; i++ ) { // deprecated RString sFrameKey = ssprintf( "Frame%04d", i ); RString sDelayKey = ssprintf( "Delay%04d", i ); State newState; int iFrameIndex; if( !pNode->GetAttrValue(sFrameKey, iFrameIndex) ) break; if( iFrameIndex >= m_pTexture->GetNumFrames() ) LuaHelpers::ReportScriptErrorFmt( "%s: %s is %d, but the texture \"%s\" only has %d frames", ActorUtil::GetWhere(pNode).c_str(), sFrameKey.c_str(), iFrameIndex, sPath.c_str(), m_pTexture->GetNumFrames() ); newState.rect = *m_pTexture->GetTextureCoordRect( iFrameIndex ); if( !pNode->GetAttrValue(sDelayKey, newState.fDelay) ) break; aStates.push_back( newState ); } if( !aStates.empty() ) { m_States = aStates; Sprite::m_size.x = aStates[0].rect.GetWidth() / m_pTexture->GetSourceToTexCoordsRatioX(); Sprite::m_size.y = aStates[0].rect.GetHeight() / m_pTexture->GetSourceToTexCoordsRatioY(); } } Actor::LoadFromNode( pNode ); RecalcAnimationLengthSeconds(); }
void Sprite::LoadFromNode( const CString& sDir, const XNode* pNode ) { retry: CString sTextureFile; CString sPath; if( pNode->GetAttrValue( "Texture", sTextureFile ) ) { sPath = sDir + sTextureFile; CollapsePath( sPath ); } if( !sPath.empty() ) { vector<CString> asElementPaths; GetDirListing( sPath + "*", asElementPaths, false, true ); if( asElementPaths.size() == 0 ) { CString sMessage = ssprintf( "The sprite file '%s' points to a texture '%s' which doesn't exist.", m_sSpritePath.c_str(), sPath.c_str() ); switch( Dialog::AbortRetryIgnore(sMessage) ) { case Dialog::abort: RageException::Throw( "Error reading value 'Texture' from %s.", m_sSpritePath.c_str() ); case Dialog::retry: goto retry; case Dialog::ignore: return; default: ASSERT(0); } } if( asElementPaths.size() > 1 ) { CString message = ssprintf( "There is more than one file that matches " "'%s'. Please remove all but one of these matches.", sPath.c_str() ); RageException::Throw( message ); } sPath = asElementPaths[0]; // Load the texture LoadFromTexture( sPath ); // Read in frames and delays from the sprite file, // overwriting the states that LoadFromTexture created. // If the .sprite file doesn't define any states, leave // frames and delays created during LoadFromTexture(). for( int i=0; true; i++ ) { CString sFrameKey = ssprintf( "Frame%04d", i ); CString sDelayKey = ssprintf( "Delay%04d", i ); State newState; if( !pNode->GetAttrValue( sFrameKey, newState.iFrameIndex ) ) break; if( newState.iFrameIndex >= m_pTexture->GetNumFrames() ) RageException::Throw( "In '%s', %s is %d, but the texture %s only has %d frames.", m_sSpritePath.c_str(), sFrameKey.c_str(), newState.iFrameIndex, sPath.c_str(), m_pTexture->GetNumFrames() ); if( !pNode->GetAttrValue( sDelayKey, newState.fDelay ) ) break; if( i == 0 ) // the ini file defines at least one frame m_States.clear(); // clear before adding m_States.push_back( newState ); } } Actor::LoadFromNode( sDir, pNode ); }
/** Loads font-data from specified path. If the path length is 0 it will attempt to load it from the same location as the given texture for the font. */ bool TextFont::Load(String path) { this->source = path; std::fstream f; /// Check what kind of path we received first of all. if (path.Contains(".font")){ /// Check to see if the file exists first! f.open(path.c_str(), std::ios_base::in | std::ios_base::binary); // If not successfully opened, abort! if (!f.is_open()){ assert(f.is_open() && "Unable to open file stream to specified font file in Font::Load!"); return false; } } // If we got a texture passed, check if a .font exists already! else if (path.Contains(".png")){ textureSource = path; path = path.Tokenize(".")[0]; // Cut off everything after the first decimal point, yes? path = path + ".font"; // Add other file-ending o-o /// Check to see if the file exists first! f.open(path.c_str(), std::ios_base::in | std::ios_base::binary); // If not successfully opened, abort! if (!f.is_open()){ path = path.Tokenize(".")[0] + ".png"; return LoadFromTexture(TexMan.LoadTexture(path)); } } // Create .font-version of path if possible else { path = textureSource; return this->LoadFromTexture(); } // f.open(path.c_str(), std::ios_base::in | std::ios_base::binary); assert(f.is_open()); // WRite general font data first! f.read((char*)&this->fontWidth, sizeof(int)); f.read((char*)&this->fontHeight, sizeof(int)); // Then character-specific data for (int i = 0; i < this->MAX_CHARS; ++i){ // Width, height, stuff f.read((char*)&this->charWidth[i], sizeof(float)); f.read((char*)&this->charHeight[i], sizeof(float)); } f.close(); // Invisible characters have no width! charWidth['\n'] = 0; charWidth['\0'] = 0; texture = TexMan.LoadTexture(textureSource); /// Set name accordingly. if (texture){ List<String> tokens = texture->source.Tokenize("/\\"); name = tokens[tokens.Size()-1]; /// Remove extension, if any. if (name.Contains(".")) name = name.Tokenize(".")[0]; } return true; }