桌面和Android项目之间的libgdx像素问题

时间:2013-09-07 22:54:39

标签: android libgdx

更新由于缺少硬件按钮,平板电脑上的静态通知栏看起来像是一个问题。我只是没想到这一点。无论如何,在TF101的情况下,它返回的分辨率为1280x752,因此大约为1.702(80:47)。如果我使用合适的单位尺寸,如33.5或11.75垂直,我得到适当的缩放,这似乎解决了像素偏斜的问题。 END UPDATE

我一直在使用16x16单位为我的平铺地图设置游戏。我在我的桌面和Android项目上使用分辨率1280x800,我正在测试它以了解它在TF101华硕平板电脑上的外观。我目前使用单位为20x12.5(wxh)的相机并注意到我的桌面项目没有像素缩放,但是当我在我的android上运行游戏时,我得到了奇怪的缩放和绿色水平线。我还可以沿着平板电脑上的x轴进一步移动四分之一单元格,如屏幕截图所示。 android项目中的像素看起来并不均匀。

我将verticalTiles数量设置为12.5f,然后将horizo​​ntalTiles数量计算为

verticalTiles = 12.5f;
...
horizontalTiles = (float) width / (float) height * verticalTiles;
camera = new OrthographicCamera(horizontalTiles, verticalTiles);

我的目标是使用不同宽高比的设备,只是看到更多或更少的地图,但似乎无法正常工作。任何帮助将不胜感激。

Android Capture - http://imageshack.us/f/7/dsvg.png/ - 注意屋顶边缘的高光,它们根本不均匀。

桌面捕获 - http://imageshack.us/f/853/5itv.png/

当前的MainGame类

package com.bitknight.bqex;

/* Bunch of imports */

public class MainGame implements ApplicationListener {
  private OrthographicCamera camera;
  private SpriteBatch spriteBatch;
  private TiledMap map;
  private OrthogonalTiledMapRenderer mapRenderer;
  private Texture texture;
  private Texture clothArmor;
  private Sprite sprite;
  private BitmapFont font;

  private float horizontalTiles = 0;
  private float verticalTiles = 12.5f;
  private int hoverTileX = 0;
  private int hoverTileY = 0;

  private TiledMapTileLayer layer;
  private Cell cell;
  private TiledMapTile canMoveToTile;
  private TiledMapTile cannotMoveToTile;

  private AnimatedTiledMapTile animatedStopTile;
  private AnimatedTiledMapTile animatedGoTile;

  private Texture spriteSheet;
  private TextureRegion region;

  private Player player;

  float h, w;
  float ppuX, ppuY;

  @Override
  public void create() {    
    // Setup the animated tiles
    Array<StaticTiledMapTile> tileArray;
    // Start position on the sheet
    int startX = 192;
    int startY = 1568;

    spriteSheet = new Texture(Gdx.files.internal("data/maps/tilesheet.png"));
    spriteSheet.setFilter(TextureFilter.Nearest, TextureFilter.Nearest);
    // We are trying to load two strips of 4 frames, 8 total
    for( int i = 0; i < 2; ++i ) {
      tileArray = new Array<StaticTiledMapTile>(4);
      for( int j = 0; j < 4; ++j ) {
        region = new TextureRegion(spriteSheet, startX, startY, 16, 16);
        tileArray.add(new StaticTiledMapTile(region));
        startX += 16;
      }

      if( i == 0 ) {
        animatedStopTile = new AnimatedTiledMapTile(1/10f, tileArray);
      } else {
        animatedGoTile = new AnimatedTiledMapTile(1/10f, tileArray);
      }
    }

    // Load the map
    map = new TmxMapLoader().load("data/maps/base.tmx");
    // Setup the two tiles that show movable and not movable sprites
    canMoveToTile = map.getTileSets().getTileSet(0).getTile(1959);
    canMoveToTile.setBlendMode(BlendMode.ALPHA);
    cannotMoveToTile = map.getTileSets().getTileSet(0).getTile(1958);
    cannotMoveToTile.setBlendMode(BlendMode.ALPHA);

    // Manually create the layer used to show the cursor sprites
    layer = new TiledMapTileLayer(100, 100, 16, 16);
    layer.setName("display");
    cell = new Cell();
    cell.setTile(canMoveToTile);
    layer.setOpacity(1f);
    mapRenderer = new OrthogonalTiledMapRenderer(map, 1/16f);
    spriteBatch = new SpriteBatch();

    font = new BitmapFont(Gdx.files.internal("data/consolas.fnt"), false);
    font.setScale(0.6f);

    texture = new Texture(Gdx.files.internal("data/maps/tilesheet.png"));
    texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);

    clothArmor = new Texture(Gdx.files.internal("data/img/native/clotharmor.png"));
    region = new TextureRegion(clothArmor, 32, 256, 32, 32);
    sprite = new Sprite(region);
    sprite.setOrigin(0.5f, 0.5f);
    sprite.setPosition(0f - 0.5f, 0f);
    sprite.setSize(2, 2);

    // Setup player and associated animations
    Array<TextureRegion> regions = new Array<TextureRegion>();

    player = new Player();
  }

  @Override
  public void dispose() {
    spriteBatch.dispose();
    texture.dispose();
    clothArmor.dispose();
    spriteSheet.dispose();
  }

  @Override
  public void render() {
    player.update(Gdx.graphics.getDeltaTime());

    camera.update();

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    if( Gdx.input.isKeyPressed(Input.Keys.ESCAPE) ) {
      Gdx.app.exit();
    }

    // Clear the last cell
    layer.setCell(hoverTileX, hoverTileY, null);
    // Convert screen coordinates to world coordinates
    Vector3 worldCoordinates = new Vector3(Gdx.input.getX(0), Gdx.input.getY(0), 0);
    camera.unproject(worldCoordinates);
    hoverTileX = (int)(worldCoordinates.x);
    hoverTileY = (int)(worldCoordinates.y);

    TiledMapTileLayer layer = (TiledMapTileLayer)map.getLayers().get("collision");

    if( Gdx.input.isTouched(0) ) {
      //sprite.setPosition(hoverTileX - 0.5f, hoverTileY);
      player.pos.x = hoverTileX - 0.5f;
      player.pos.y = hoverTileY - 0.25f;
      cell.setTile(animatedGoTile);
    } else {
      if (layer.getCell(hoverTileX, hoverTileY) != null) {
        cell.setTile(cannotMoveToTile);
      } else {
        cell.setTile(canMoveToTile);
      }
    }
    layer.setCell(hoverTileX, hoverTileY, cell);

    mapRenderer.setView(camera);
    mapRenderer.render();

    mapRenderer.getSpriteBatch().begin();
    mapRenderer.renderTileLayer(layer);
    mapRenderer.getSpriteBatch().end();

    spriteBatch.setProjectionMatrix(camera.combined);
    spriteBatch.begin();
    player.render(spriteBatch);
    spriteBatch.end();
  }

  @Override
  public void resize(int width, int height) {    
    horizontalTiles = (float) width / (float) height * verticalTiles;
    camera = new OrthographicCamera(horizontalTiles, verticalTiles);
    w = width;
    h = height;
  }

  @Override
  public void pause() {
  }

  @Override
  public void resume() {
  }

}

1 个答案:

答案 0 :(得分:1)

由于缺少硬件按钮,平板电脑上的静态通知栏看起来像是一个问题。我只是没想到这一点。无论如何,在TF101的情况下,它返回的分辨率为1280x752,因此大约为1.702(80:47)。如果我使用合适的单位尺寸,如33.5或11.75垂直,我得到适当的缩放,这似乎解决了像素偏斜的问题。

另外,虽然这对我的TF101平板电脑有好处,但它并不是一个很好的解决方案。这是一个Gemserk系列,讨论了一个很好的解决方案。

http://blog.gemserk.com/2013/01/22/our-solution-to-handle-multiple-screen-sizes-in-android-part-one/