2014-03-30 6 views
2

나는 libgdx으로 게임을 개발 중이고 다른 장치에서 종횡비가 붙어 있습니다. 많은 생각 후
나는 다음은이 문제에 대한 최선의 해결책이라고 생각 :libgdx에서 가로 세로 비율 처리 android

난 항상 16:9 화면 비율로 고정 카메라를 가지고와 장치 측면은 예를 4:3에 대한 경우 그 카메라
를 사용하여 모든 것을 그리려는 내가 보기의 일부만 보여주고 싶지 않을 때가 있습니다.

가이

aspect ratio

blue 같을한다하면 가상 스크린 (카메라 뷰포트)이고 red는 단말기 화면 (4 표시 영역 3 개 장치), 예를 들면 기기 화면이기도 한 경우
16:9 전체보기가 표시되어야합니다.

문제는 이것을 달성하는 방법을 모른다는 것입니다.

+0

새로운'Viewport' 클래스를 보았습니까? 이 링크를보십시오 : https://github.com/libgdx/libgdx/wiki/Viewports 아마도이 클래스를 사용할 수 있습니다. – Springrbua

답변

1

필자는 세로 화면에서 위와 아래에 빈 공간을 남겼습니다. 다음 그림에서 볼 수 있듯이 게임은 4 : 3의 비율로 유지되고 나머지는 비워 둡니다.

Portrait example of game keeping aspect ratio

이 콘텐츠는 항상 중심과 불균일 콘텐츠 연신하지 않음으로써 그 비율을 유지한다. 그래서 여기에 그것을 달성하기 위해 사용하는 몇 가지 샘플 코드가 있습니다.

public static final float worldW = 3; 
public static final float worldH = 4; 
public static OrthographicCamera camera; 

... 

//CALCULATING THE SCREENSIZE 
float tempCalc = Gdx.graphics.getHeight() * GdxGame.worldW/Gdx.graphics.getWidth(); 
if(tempCalc < GdxGame.worldH){   
    //Adjust width 
    camera.viewportHeight = GdxGame.worldH; 
    camera.viewportWidth = Gdx.graphics.getWidth() * GdxGame.worldH/Gdx.graphics.getHeight(); 

    worldWDiff = camera.viewportWidth - GdxGame.worldW; 
}else{ 
    //Adjust Height 
    camera.viewportHeight = tempCalc; 
    camera.viewportWidth = GdxGame.worldW; 

    worldHDiff = camera.viewportHeight - GdxGame.worldH; 
} 
camera.position.set(camera.viewportWidth/2f - worldWDiff/2f, camera.viewportHeight/2f - worldHDiff/2f, 0f); 
camera.zoom = 1f; 
camera.update(); 

나는 완벽한 솔루션을 제안하지 않도록 메신저 해요,하지만 당신은 당신이 원하는 효과를 달성 할 수 있도록 카메라의 위치와 뷰포트을 계산하는 방법에 값으로 재생할 수 있습니다. 내가 추측하는 것보다 낫다.

또한, 올림픽 개발자의 충돌은 뭔가를 달성하고 여전히 다른 장치에 잘 보이게하는 방법에 대해 이야기 (정말 재미있다,하지만 코드는하지만 없음) : Our solution to handle multiple screen sizes in Android - Part one

1

더 새로운 버전 libGDX는 Viewport라는 glViewport에 대한 래퍼를 제공합니다.

Camera camera = new OrthographicCamera(); 

//the viewport object will handle camera's attributes 
//the aspect provided (worldWidth/worldHeight) will be kept 
Viewport viewport = new FitViewport(worldWidth, worldHeight, camera);  

//your resize method: 
public void resize(int width, int height) 
{ 
    //notice that the method receives the entire screen size 
    //the last argument tells the viewport to center the camera in the screen 
    viewport.update(width, height, true); 
} 
0

다른 크기의 화면과 이미지로 작동하는 함수를 작성했습니다. 가능한 경우 이미지가 확대/축소 및 변환 될 수 있습니다.

public void transformAndDrawImage(SpriteBatch spriteBatch, Texture background, float scl, float offsetX, float offseY){ 
    float width; 
    float height; 

    float _offsetX; 
    float _offsetY; 

    float bh2sh = 1f*background.getHeight()/Gdx.graphics.getHeight(); 
    float bw2sw = 1f*background.getWidth()/Gdx.graphics.getWidth(); 
    float aspectRatio = 1f*background.getHeight()/background.getWidth(); 

    if(bh2sh>bw2sw){      
     width = background.getWidth()/bw2sw * scl; 
     height = width * aspectRatio; 
    } 
    else{ 
     height = background.getHeight()/bh2sh * scl; 
     width = height/aspectRatio;     
    }   

    _offsetX = (-width+Gdx.graphics.getWidth())/2; 
    _offsetX += offsetX; 
    if(_offsetX-Gdx.graphics.getWidth()<=-width) _offsetX=-width+Gdx.graphics.getWidth(); 
    if(_offsetX>0) _offsetX=0; 

    _offsetY = (-height+Gdx.graphics.getHeight())/2; 
    _offsetY += offseY; 
    if(_offsetY-Gdx.graphics.getHeight()<=-height) _offsetY=-height+Gdx.graphics.getHeight(); 
    if(_offsetY>0) _offsetY=0; 

    spriteBatch.draw(background, _offsetX, _offsetY, width, height); 
} 

사용 방법은? 간단합니다 :

@Override 
public void render(float delta) { 

    Gdx.graphics.getGL10().glClearColor(0.1f, 0.1f, 0.1f, 1); 
    Gdx.graphics.getGL10().glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

    spriteBatch.setProjectionMatrix(cam.combined);      
    spriteBatch.begin();  

    //zoom the image by 20% 
    float zoom = 1.2f; //Must be not less than 1.0 

    //translating the image depends on a few parameters such as zooming, aspect ratio of screen and image 
    offsetX+=0.1f; //offset the image to the left, if it's possible 
    offsetY+=0.1f; //offset the image to the bottom, if it's possible 

    transformAndDrawImage(spriteBatch, background, zoom, offsetX, offsetY); 

    //draw something else... 
    spriteBatch.end(); 

} 
관련 문제