我的地图尺寸为40000X20000,我想只渲染2048X2048并在屏幕上只显示1200X600像素,我该怎么做?
换句话说,认为我有大地图,我不想渲染所有40000X20000我只想渲染较小的地图2048X2048
我的屏幕允许我显示1200X600 Pixle。
我的代码:
public class LibgdxGame extends ApplicationAdapter {
static final float WORLD_WIDTH = 40000.0f;
static final float WORLD_HEIGHT = 20000.0f;
// Define viewoport
private static final float VIRTUAL_WIDTH = 1200.0f;
private static final float VIRTUAL_HEIGHT = 600.0f;
// Move camera around
private static final float CAMERA_SPEED = 60.0f;
private OrthographicCamera camera;
private Viewport viewport;
private TiledMap map;
private TmxMapLoader loader;
private OrthogonalTiledMapRenderer renderer;
private Vector2 direction;
@Override
public void create () {
camera = new OrthographicCamera(15000, 15000);
viewport = new FitViewport(VIRTUAL_WIDTH, VIRTUAL_HEIGHT, camera);
camera.position.set(10900, 10800, 0);
loader = new TmxMapLoader();//
map = loader.load("40000X20000.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
direction = new Vector2();
}
@Override
public void dispose() {
map.dispose();
renderer.dispose();
}
@Override
public void render () {
System.out.println("Render.........");
Gdx.gl.glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
updateCamera();
renderer.setView(camera);
renderer.render();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
System.out.println(width);
System.out.println(height);
}
private void updateCamera() {
direction.set(0.0f, 0.0f);
int mouseX = Gdx.input.getX();
int mouseY = Gdx.input.getY();
int width = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();
if (Gdx.input.isKeyPressed(Keys.LEFT) || (Gdx.input.isTouched() && mouseX < width * 0.25f)) {
direction.x = -1;
}
else if (Gdx.input.isKeyPressed(Keys.RIGHT) || (Gdx.input.isTouched() && mouseX > width * 0.25f)) {
direction.x = 1;
}
if (Gdx.input.isKeyPressed(Keys.UP) || (Gdx.input.isTouched() && mouseY < height * 0.25f)) {
direction.y = 1;
}
else if (Gdx.input.isKeyPressed(Keys.DOWN) || (Gdx.input.isTouched() && mouseY > height * 0.25f)) {
direction.y = -1;
}
direction.nor().scl(CAMERA_SPEED * Gdx.graphics.getDeltaTime());;
camera.position.x += direction.x;
camera.position.y += direction.y;
TiledMapTileLayer layer = (TiledMapTileLayer)map.getLayers().get(0);
float cameraMinX = viewport.getWorldWidth() * 0.5f;
float cameraMinY = viewport.getWorldHeight() * 0.5f;
float cameraMaxX = layer.getWidth() * layer.getTileWidth() - cameraMinX;
float cameraMaxY = layer.getHeight() * layer.getTileHeight() - cameraMinY;
camera.position.x = MathUtils.clamp(camera.position.x, cameraMinX, cameraMaxX);
camera.position.y= MathUtils.clamp(camera.position.y, cameraMinY, cameraMaxY);
camera.update();
}
}
答案 0 :(得分:-1)
有很多方法可以做到这一点,尽管没有人完全优化。我几天前试着这样做。我试过用:
Spritecache;如果你有一个静态地图,没有更改瓷砖,这是最好的选择。虽然你有缓冲限制,我猜是5640瓦/纹理。
使用Scessior Stack方法,这对我来说很复杂,你可以简单地裁剪所需的纹理区域并将其显示为地图。但这不是一个好的解决方案。
我最好采用这种方法。我拿了两个tmx文件,一个是large_map.tmx,包含所有详细的tile和layer;然后拿另一个小的small_map.tmx,它只包含没有瓷砖的1/2层。然后使用两组加载器,渲染器,只需将小区域(2048X2048)中的需求单元格从large_map复制到small_map,并仅渲染小地图。现在,您可以使用此地图动态更改切片并显示动画,因为它只加载小型tmx文件。