Skip to content

Instantly share code, notes, and snippets.

@fkytks
Created April 20, 2014 00:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fkytks/11101392 to your computer and use it in GitHub Desktop.
Save fkytks/11101392 to your computer and use it in GitHub Desktop.
libGDXのViewport設定の謎 ref: http://qiita.com/fslasht/items/c0830eaf05b7a91b8e85
// 良くない例:画面いっぱいになってしまうので実際の画面の縦横比に影響されて、縦長or横長になってしまう
camera = new OrthographicCamera();
camera.setToOrtho(false, 640, 400);
batch.setProjectionMatrix(camera.combined);
camera = new PerspectiveCamera();
viewport = new FitViewport(camera, 800, 480);
batch.setProjectionMatrix(camera.combined);
package com.dokokano.gdxviewporttest1;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Scaling;
import com.badlogic.gdx.utils.viewport.ExtendViewport;
import com.badlogic.gdx.utils.viewport.FitViewport;
import com.badlogic.gdx.utils.viewport.ScalingViewport;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
public class GdxViewportTest extends ApplicationAdapter {
static final int LOGICAL_WIDTH = 640; // ゲーム内の論理座標 幅
static final int LOGICAL_HEIGHT = 480; // ゲーム内の論理座標 高さ
Stage stage; // ViewportとCameraの管理をさせる
SpriteBatch batch;
private ShapeRenderer renderer;
Texture img;
BitmapFont font;
@Override
public void create () {
renderer = new ShapeRenderer();
batch = new SpriteBatch();
img = new Texture("badlogic.jpg"); // 256x256 px
font = new BitmapFont();
// Initialite Stage & View port
stage = new Stage(new FitViewport(LOGICAL_WIDTH,LOGICAL_HEIGHT));
// stage = new Stage(new ExtendViewport(LOGICAL_WIDTH,LOGICAL_HEIGHT));
// stage = new Stage(new ScalingViewport(Scaling.fill , LOGICAL_WIDTH,LOGICAL_HEIGHT));
// stage = new Stage(new ScreenViewport());
Matrix4 cameraMatrix = stage.getViewport().getCamera().combined;
batch.setProjectionMatrix(cameraMatrix);
renderer.setProjectionMatrix(cameraMatrix);
}
@Override
public void render () {
// 背景塗りつぶし(全体を黒)
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
// 論理表示領域を青で塗りつぶし
renderer.begin(ShapeType.Filled);
renderer.setColor(0,0,0.5f,1);
renderer.rect(0, 0,LOGICAL_WIDTH,LOGICAL_HEIGHT);
renderer.end();
batch.begin();
// ビットマップ描画(0,0)-(256,256)
batch.setColor(1,1,1,0.5f);
batch.draw(img, 0, 0);
// 100x100グリッドにあわせて座標の数値描画
for ( int x=-1000; x<=1000; x+=100) {
for ( int y=-1000; y<1000; y+=100) {
font.setScale(1f);
font.setColor(Color.WHITE);
font.draw(batch, "("+x + ","+y +")" , x, y);
}
}
batch.end();
renderer.begin(ShapeType.Line);
// 原点からの100pxごとに四角形描画(緑)
for ( int i=-1000; i<=1000; i+=100 ) {
renderer.setColor(Color.GREEN);
renderer.rect(0, 0, i , i);
}
// 100x100グリッドを描画(白いクロス)
for ( int x=-1000; x<=1000; x+=100) {
for ( int y=-1000; y<1000; y+=100) {
renderer.setColor(Color.WHITE);
renderer.x(x, y, 10);
}
}
renderer.end();
}
@Override
public void resize(int width, int height) {
stage.getViewport().update(width, height,true);
}
}
stage = new Stage(new FitViewport(LOGICAL_WIDTH,LOGICAL_HEIGHT));
Matrix4 cameraMatrix = stage.getViewport().getCamera().combined;
batch.setProjectionMatrix(cameraMatrix);
renderer.setProjectionMatrix(cameraMatrix);
stage.getViewport().update(width, height,true);
stage = new Stage(new ScalingViewport(Scaling.fill , LOGICAL_WIDTH,LOGICAL_HEIGHT));
stage = new Stage(new ExtendViewport(LOGICAL_WIDTH,LOGICAL_HEIGHT));
stage = new Stage(new ScreenViewport());
if (Gdx.input.isTouched()) {
Vector3 touchPos = new Vector3();
touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
stage.getViewport().getCamera().unproject(touchPos);
renderer.begin(ShapeType.Filled);
renderer.setColor(Color.RED);
renderer.circle(touchPos.x, touchPos.y, 50);
renderer.end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment