2012-02-25 2 views
0

나는 화면 비율에 따라 애플 리케이션의 크기를 조정하는 솔루션으로 libgdx를 사용하고 싶습니다. 나는이 링크를 발견했습니다, 그리고 난 그것이 정말 유용 : http://blog.acamara.es/2012/02/05/keep-screen-aspect-ratio-with-different-resolutions-using-libgdx/android - 시작 지점을 왼쪽 상단에 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으로 변경하십시오. 사진이나 특정 사항이 없으면 "잘 작동하지 않는 것"을 알기가 어렵다는 것을 의미합니다. 그래도 새로운 질문을 할 때가 있습니다. –

관련 문제