我在libgdx GitHub上找到了一些代码,用于创建平铺地图并填充它。一切正常,直到你放大相机绘制~50x50瓷砖。此时,我得到~5 fps并且输入几乎没有响应。这是修改后的代码:
public class Core extends ApplicationAdapter {
private AssetManager am;
private SpriteBatch batch;
private BitmapFont font;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera;
private OrthoCamController cameraController;
@Override
public void create() {
am = new AssetManager();
am.load("tiles.png", Texture.class);
am.finishLoading();
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
batch = new SpriteBatch();
batch.disableBlending();
camera = new OrthographicCamera();
camera.setToOrtho(false, (w / h) * 400, 400);
camera.update();
cameraController = new OrthoCamController(camera);
Gdx.input.setInputProcessor(cameraController);
font = new BitmapFont();
map = new TiledMap();
MapLayers layers = map.getLayers();
TiledMapTileLayer layer = new TiledMapTileLayer(200, 200, 8, 8);
TextureRegion[][] splitTiles = TextureRegion.split((Texture)am.get("tiles.png"), 8, 8);
for (int x = 0; x < 1000; x++) {
for (int y = 0; y < 1000; y++) {
int ty = 1;
int tx = 1;
Cell cell = new Cell();
cell.setTile(new StaticTiledMapTile(splitTiles[ty][tx]));
layer.setCell(x, y, cell);
}
layers.add(layer);
}
renderer = new OrthogonalTiledMapRenderer(map, batch);
}
@Override
public void render() {
Gdx.gl.glClearColor(100f / 255f, 100f / 255f, 250f / 255f, 1f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
renderer.setView(camera);
renderer.render();
}
@Override
public void resize(int width, int height) {
}
@Override
public void dispose() {
am.dispose();
map.dispose();
}
}
我严重怀疑Libgdx无法处理那么多瓷砖,所以我的问题是:我如何提高性能......或者我是否过于雄心勃勃?
我能找到的唯一解决方案是here,但看起来他们删除了其中使用的方法。
答案 0 :(得分:1)
您可能希望将layers.add(layer);
置于for (int x = 0; x < 1000; x++)
循环