2013-01-23 49 views
3

我正在爲使用libGDX的Android開發遊戲。我實現了「聚光燈效果」,突出了教程級別中的一些場景元素。我確實使用Pixmap類來實現這一點。所以我做了這樣的事情:Android LibGDX Pixmap問題

public class ComplexSpotlight implements MoveObserver{ 

// somewhere in class 

    public void update(){ 
     black.setColor(0, 0, 0, 0.7f); 
     black.fill(); 

     black.setColor(0, 0, 0, 0); 

     for(Map.Entry<MoveObservable, Vector2> entry : currentObservablePositions.entrySet()){ 
      Vector2 position = entry.getValue(); 

      int x = (int)(Gdx.graphics.getWidth()/2+position.x); 
      int y = (int)(Gdx.graphics.getHeight()*1.5f-position.y); 

      int radius = 50; 

      black.fillCircle(x, y, radius); 
      System.out.println("at "+x+" "+y); 
     } 

     overlay.dispose(); 
     overlay = new Texture(black); 
    } 

    public Texture getOverlayTexture(){ 
     return overlay; 
    } 

快速的解釋 - 我創建一個點陣圖與顏色(0,0,0,0.7f)加油吧。在此之後,我使用Pixmap.fillCircle()方法繪製透明圓。然後我使用這個Pixmap創建新的紋理。我已經使用以下設備測試了此代碼:HTC One V(480 * 800),Sony XPeria Neo 15i(480 * 854),HTC Desire S(480 * 800),並且效果不錯;

enter image description here

但今天我發現,有關於HTC One X的(1280 * 720)和Gamsung的Nexus(1280 * 720)的問題 - 我只看到黑屏。

所以如果有人給我一些關於這個問題的解釋會很好。鏈接,想法 - 任何事情都可能有幫助。 在此先感謝!

+1

有問題的手機是相當大的屏幕。你用完紋理記憶或類似的東西了嗎? (假設您以全分辨率繪製)。你可以嘗試渲染到一個較小的視口,看看是否解決了這個問題。還要確保它的覆蓋層是真正的問題(如果禁用覆蓋層,背景顯示是否正確?如果你用紅色或白色填充它) –

+0

我也注意了屏幕尺寸,但沒有任何建議能夠 。創建和呈現我的疊加層時沒有發生任何異常/錯誤。在屏幕上輕敲疊加刪除(根據我的遊戲邏輯),然後看到我的背景和遊戲對象。此外,我顯示彈出式文本消息上方此覆蓋,它呈現正確。 我會嘗試測試你提出並更新我的帖子的結果。 – Viacheslav

+0

@ P.T。對於大屏幕尺寸,您完全正確 - 由於某些原因,我使用了2 *屏幕尺寸,將紋理尺寸增加到4倍!將Pixmap大小更改爲屏幕大小後,所有效果都很好。如果您發表評論作爲答案,我會批准它作爲主題解決方案。謝謝! – Viacheslav

回答

1

有問題的手機都是相當大的屏幕。你用完紋理記憶或類似的東西了嗎? (假設你正在以全分辨率進行繪製)。你可以嘗試渲染一個較小的視口來查看是否解決了這個問題。

現在我知道這個猜測是正確的,我很好奇,如果有一個在應用程序(或Android的?)日誌或在現在指向這個問題了OpenGL錯誤狀態的任何證據,你知道原因是什麼。

1

我不確定,但我一直在使用Pixmap在我的應用程序中填充Alpha的問題。 我發現pixmap中的fillCircle只是沒有正確設置alpha。當我仔細研究它時,我發現更好的方法是使用FrameBuffer。

我發現的所有細節在以下 libgdx SpriteBatch render to texture

我的一些代碼,我一直在使用的

public class SplashScreen extends GameScreen { 

SpriteBatch batch; 
BitmapFont font; 

FrameBuffer frmBuff; 
TextureRegion frm; 
OrthographicCamera frmCam; 

int frmSizeX = 256; 
int frmSizeY = 256; 

public SplashScreen(ReactorApp game) { 
    super(game); 
    setClearColor(new Color(1,1,1,1)); 
} 

@Override 
public void show() { 
    // TODO Auto-generated method stub 
    super.show(); 
    batch = new SpriteBatch(); 
    font = new BitmapFont(); 

    frmBuff = new FrameBuffer(Format.RGBA8888, frmSizeX, frmSizeY, false); 
    frm = new TextureRegion(frmBuff.getColorBufferTexture()); 
    frmCam= new OrthographicCamera(frmSizeX, frmSizeY); 
    frmCam.translate(frmSizeX/2, frmSizeY/2); 
} 

@Override 
public void dispose() { 
    // TODO Auto-generated method stub 
    super.dispose(); 
} 


public void drawToTexture(){ 
    frmCam.update(); 
    ShapeRenderer shape = new ShapeRenderer(); 
    shape.setProjectionMatrix(frmCam.combined); 
    frmBuff.begin(); 

    Gdx.gl.glClearColor(0, 0, 0, 0); 
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
    shape.begin(ShapeType.FilledRectangle); 
    shape.setColor(Color.BLACK); 
    shape.filledRect(0,0,frmSizeX/2, frmSizeY/2); 
    shape.end(); 

    shape.begin(ShapeType.FilledCircle); 
    shape.setColor(Color.GREEN); 
    shape.filledCircle(MathUtils.random(frmSizeX), MathUtils.random(frmSizeY), 20); 
    shape.end(); 

    frmBuff.end(); 
} 

int pos = 0; 

@Override 
public void render(float delta) { 
    super.render(delta); 
    drawToTexture(); 

    batch.setProjectionMatrix(cam.combined); 


    batch.begin(); 
    batch.draw(frm, pos++, 30,frmSizeX,frmSizeY); 
    if(pos>Gdx.graphics.getWidth()-frmSizeX)pos=0; 
    font.setColor(Color.RED); 
    StringBuilder message = new StringBuilder(); 
    message.append("Time :"+(int)(delta*1000)+"\n"); 
    font.drawMultiLine(batch, message.toString(), 10, Gdx.graphics.getHeight()); 
    batch.end(); 
} 

} 

這給了我一直在玩的想法,但它似乎工作得很好。

+0

謝謝你的例子,喬伊!我會嘗試實施這種方法,因爲我認爲它會有更好的表現。 – Viacheslav