2012-02-25 10 views
0

私はスクリーンアスペクト比に基づいてアプリをスケーリングするソリューションとしてlibgdxを使いたいと思っています。 私は、このリンクを見つけた、と私はそれが本当に役に立つ: http://blog.acamara.es/2012/02/05/keep-screen-aspect-ratio-with-different-resolutions-using-libgdx/アンドロイド - libgdxの開始点を左上に設定する

私はOpenGLと非常にさびたよ(年間でそれを書いていない)と私はそのように、このリンク上の例を使用したいです画像や形を入れるのは簡単でしょう。 悲しいことに、出発点は真ん中にあります。私は他の多くのプラットフォームのように使用したい - 左上隅は(0,0)、右下隅は(targetWidth-1、targetHeight-1)、 でなければなりません。 )、それを達成するためにカメラを回転させるが、わからない。

は、ここでのonCreateメソッドのリンクの例の私の修正コードです:

@Override 
public void create() 
    { 
    camera=new OrthographicCamera(VIRTUAL_WIDTH,VIRTUAL_HEIGHT); 
    // camera.translate(VIRTUAL_WIDTH/2,VIRTUAL_HEIGHT/2,0); 
    // camera.rotate(90,0,0,1); 
    camera.update(); 
    // 
    font=new BitmapFont(Gdx.files.internal("data/fonts.fnt"),false); 
    font.setColor(Color.RED); 
    // 
    screenQuad=new Mesh(true,4,4,new VertexAttribute(Usage.Position,3,"attr_position"),new VertexAttribute(Usage.ColorPacked,4,"attr_color")); 
    Point bottomLeft=new Point(0,0); 
    Point topRight=new Point(VIRTUAL_WIDTH,VIRTUAL_HEIGHT); 
    screenQuad.setVertices(new float[] {// 
     bottomLeft.x,bottomLeft.y,0f,Color.toFloatBits(255,0,0,255),// 
      topRight.x,bottomLeft.y,0f,Color.toFloatBits(255,255,0,255),// 
      bottomLeft.x,topRight.y,0f,Color.toFloatBits(0,255,0,255),// 
      topRight.x,topRight.y,0f,Color.toFloatBits(0,0,255,255)}); 
    screenQuad.setIndices(new short[] {0,1,2,3}); 
    // 
    bottomLeft=new Point(VIRTUAL_WIDTH/2-50,VIRTUAL_HEIGHT/2-50); 
    topRight=new Point(VIRTUAL_WIDTH/2+50,VIRTUAL_HEIGHT/2+50); 
    quad=new Mesh(true,4,4,new VertexAttribute(Usage.Position,3,"attr_position"),new VertexAttribute(Usage.ColorPacked,4,"attr_color")); 
    quad.setVertices(new float[] {// 
    bottomLeft.x,bottomLeft.y,0f,Color.toFloatBits(255,0,0,255),// 
     topRight.x,bottomLeft.y,0f,Color.toFloatBits(255,255,0,255),// 
     bottomLeft.x,topRight.y,0f,Color.toFloatBits(0,255,0,255),// 
     topRight.x,topRight.y,0f,Color.toFloatBits(0,0,255,255)}); 
    quad.setIndices(new short[] {0,1,2,3}); 
    // 
    texture=new Texture(Gdx.files.internal(IMAGE_FILE)); 
    spriteBatch=new SpriteBatch(); 
    spriteBatch.getProjectionMatrix().setToOrtho2D(0,0,VIRTUAL_WIDTH,VIRTUAL_HEIGHT); 
    } 

があるこれまでのところ、私はスケールされた座標を使用するためには、このコードを使用することに成功してきたが、まだ(縦横比を維持します素晴らしい)、しかし、私は開始点(0,0)を左上に動かすことに成功しなかった。

私を助けてください。


EDIT:ok、いくつかのテストの後、私はspriteBatchを使用している理由が動作していないことを知りました。私はそれがカメラを無視すると思います。このコードはレンダリング部分で発生します。カメラに何をしても、同じ結果が表示されます。

@Override 
public void render() 
    { 
    if(Gdx.input.isKeyPressed(Keys.ESCAPE)||Gdx.input.justTouched()) 
    Gdx.app.exit(); 
    // update camera 
    // camera.update(); 
    // camera.apply(Gdx.gl10); 
    // set viewport 
    Gdx.gl.glViewport((int)viewport.x,(int)viewport.y,(int)viewport.width,(int)viewport.height); 
    // clear previous frame 
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); 
    // 
    final String msg="test"; 
    final TextBounds textBounds=font.getBounds(msg); 
    spriteBatch.begin(); 
    screenQuad.render(GL10.GL_TRIANGLE_STRIP,0,4); 
    quad.render(GL10.GL_TRIANGLE_STRIP,0,4); 
    Gdx.graphics.getGL10().glEnable(GL10.GL_TEXTURE_2D); 
    spriteBatch.draw(texture,0,0,texture.getWidth(),texture.getHeight(),0,0,texture.getWidth(),texture.getHeight(),false,false); 
    spriteBatch.draw(texture,0,VIRTUAL_HEIGHT-texture.getHeight(),texture.getWidth(),texture.getHeight(),0,0,texture.getWidth(),texture.getHeight(),false,false); 
    font.draw(spriteBatch,msg,VIRTUAL_WIDTH-textBounds.width,VIRTUAL_HEIGHT); 
    spriteBatch.end(); 
    } 

答えて

0

これらの行:

camera.translate(VIRTUAL_WIDTH/2,VIRTUAL_HEIGHT/2,0); 
camera.rotate(90,0,0,1); 

はそれが何をしたいんようにカメラを移動する必要があります。ただし、私のコードには、翻訳が呼び出された後に、

camera.update() 

コールが追加されています。 doc for Camera.updateは言う:

update 
public abstract void update() 

Recalculates the projection and view matrix of this camera and the Frustum planes. Use this after you've manipulated any of the attributes of the camera. 
+0

動作しないようです - 緑の四角形は、ウィンドウ全体のスペースを取りません。また、テキストや画像を正しい位置に表示されません。 –

+0

アンドロイドでもうまく動作していないようです。私は何が欠けているのか分からないのですか?私はまた、現在のもの(左下)ではなく、左上隅に開始点を置くことを望みます。 –

+0

あなたのカメラは90度回転しています...それはXとYを台無しにしていますか?その行を削除するか、180に変更してみてください。写真や特定のものがなければ、「うまくいかない」とは分かりません。しかし、新しい質問に時間がかかるかもしれません。 –

関連する問題