2012-11-12 30 views
0
void TileSheetManager::setTileSheet(const string textureName) 
{ 
    texture.loadFromFile(textureName); 
} 

sf::Sprite TileSheetManager::getTile(int left, int top, int width, int height) 
{ 

    sf::IntRect subRect; 
    subRect.left = left * 32; 
    subRect.top = top * 32; 
    subRect.width = width; 
    subRect.height = height; 

    sf::Sprite sprite(texture, subRect); 

    return sprite; 
} 

我需要getTile()返回一個sf ::紋理但我不知道我該怎麼做。
我正在使用SFML2.0。如何讓這個函數返回一個sf :: Texture? SFML2.0

+0

是sfml簡單而快速的多媒體庫?如果是這樣,你使用的是哪個版本? http://www.sfml-dev.org/documentation/1.6/annotated.php不包含一個Texture類。這是你自己開發的課程嗎? – emartel

+0

我安裝了庫,這就是我有sf :: Sprite和sf :: IntRect。我說的是,無論如何裁剪像sf :: Sprite sprite(texture,subRect);但用sf :: Texture而不是sf :: Sprite? – user1816388

+0

基本上我想將剪裁紋理的sf ::紋理返回到子目錄。 – user1816388

回答

2

根據文檔herehere你應該能夠

sf::Image fullImage = texture.copyToImage(); 
sf::Image croppedImage(width, height); 
croppedImage.copy(fullImage, 0, 0, subRect); 
sf::Texture returnTexture(); 
returnTexture.LoadFromImage(croppedImage); 
+0

經過一些調整,我一次都沒有錯誤,但現在我需要等待它編譯幾分鐘,直到我看到它是否工作或沒有! – user1816388

+0

這工作,非常感謝! – user1816388

2

的方法getTile,你必須在此刻做什麼它應該。你有一個tile管理類加載一個完整的spritesheet,並將裁剪後的區域轉換爲sprites。不要爲了解決這個問題而改變這個方法,你的TileSheetManager類的結構很好。

如果您想將其中一個精靈轉換爲紋理,可以嘗試以下操作。

// Get a sprite from the Tile Manager. 
sf::Sprite mySprite = tileSheetMgr.getTile(1,2,3,4); 
// Create a new texture for the sprite returned. 
sf::Texture spriteTexture; 
// Generate the texture from the sprite's image. 
spriteTexture.loadFromImage(*mySprite.getImage()); 
相關問題