2013-10-22 37 views
2

我正在開發使用libGDX的塔防遊戲。我剛剛開始,我能夠展示路徑,「環境」和沿着路徑行走的敵人。libGDX SpriteCache:無法正確顯示圖像的透明度

我顯示使用SpriteBatch-對象的環境中,這樣的:

public LevelController(Level level) { 
    spriteBach = new SpriteBach(); 
    atlas = new TextureAtlas(Gdx.files.internal("images/textures/textures.pack")); 
} 

public void setSize() { 
    spriteBatch.setProjectionMatrix(this.cam.combined); 
} 

public void render() { 
    spriteBatch.begin(); 
    drawTowerBases(); 
    spriteBatch.end(); 
} 

private void drawTowerBases() { 
    // for each tower base (=environment square) 
    TextureRegion towerBaseTexture = atlas.findRegion("TowerBase"); 

    if(towerBaseTexture != null) { 
     spriteBatch.draw(towerBaseTexture, x, y, 1f, 1f); 
    } 
} 

這是正常工作和紋理顯示得好:Tower Defense using spriteBatch

現在,我想知道是否有可能緩存背景。因爲它保持不變,所以不需要每次計算它。我通過Google搜索找到了SpriteCache。所以我改變了代碼如下:

public LevelController(Level Level) { 
    spriteCache= new SpriteCache(); 

    atlas = new TextureAtlas(Gdx.files.internal("images/textures/textures.pack")); 

    spriteCache.beginCache(); 
    this.drawTowerBases(); 
    this.spriteCacheEnvironmentId = spriteCache.endCache(); 
} 

public void setSize() { 
    this.cam.update(); 
    spriteCache.setProjectionMatrix(this.cam.combined); 
} 

public void render() { 
    spriteCache.begin(); 
    spriteCache.draw(this.spriteCacheEnvironmentId); 
    spriteCache.end(); 
} 

private void drawTowerBases() { 
    // for each tower base (=environment square) 
    TextureRegion towerBaseTexture = atlas.findRegion("TowerBase"); 

    if(towerBaseTexture != null) { 
     spriteCache.add(towerBaseTexture, x, y, 1f, 1f); 
    } 
} 

現在的遊戲是這樣的:Tower Defense using spriteCache

對我來說好像透明度不正確地呈現。如果我不透明地拍攝圖像,一切正常。有沒有人有一個想法,爲什麼發生這種情況,我該如何解決這個問題?

預先感謝您。

回答

1

SpriteCache documentation摘自:

注意SpriteCache不管理融合。您需要啓用混合(Gdx.gl.glEnable(GL10.GL_BLEND);),並在調用draw(int)之前或之間根據需要設置blend func。

我想沒有什麼可說的了。

+0

謝謝。我可以在spriteCache.begin()之前調用這兩行來處理它:Gdx.gl.glEnable(GL10.GL_BLEND); Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA,GL10.GL_ONE_MINUS_SRC_ALPHA); – Tineler

+0

我對SpriteCache有點困惑。在SpriteCache文檔中沒有清楚地說明它,但似乎SpriteCache是​​一種'存儲在GPU內存中'的「你所渲染的內容的快照」。您可以使用其ID獲取此快照,並將其放在屏幕上,而無需再次發送所有內容。那是對的嗎?這也意味着你可以在屏幕上的任何位置渲染快照 - 移動它,對吧?這個文檔有點令人迷惑,'幾何不變'。 – Dzik

+0

我不確定你的問題到底是什麼。是的,您應該能夠通過操縱其投影矩陣來「緩存」任何位置。 – noone