Yes, using a FrameBuffer
, a TextureRegion
and a OrthogonalCamera
You set the camera so that the bottom left of the "screen" (this is to the buffer, not the actual screen) is at the bottom left of the bottom left tile you want. Then you draw to a FrameBuffer
and then extract the region you need.
This implementation will only work if the amount you're trying to pull off will fit on the screen you actually have.
Try making the FrameBuffer
width and height bigger if you need a bigger region (I haven't tested this)
In init
//Set up the framebuffer (NOTE: RGBA8888 might not work on some divices)
fb = new FrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);
//Attach a texture so you can draw a portion of the frame buffer
mapRegion = new TextureRegion(fb.getColorBufferTexture());
In the render thread:
cam.position.set(TILE_W * TILE_X, TILE_H * TILE_Y,0);
cam.update(); batch.setProjectionMatrix(cam.combined);
//to the buffer
fb.begin();
batch.begin();
renderTileLayer(tiledMapTileLayer);
batch.end();
fb.end();.
//Extract the portion from the buffer
mapRegion.setRegion(0, 0, TILE_W * NUM_TILES_ACROSS, TILE_H * NUM_TILES_VERTICALLY);
//to screen
cam.position.set(/*where the camera is supposed to be*/);
cam.update(); batch.setProjectionMatrix(cam.combined);
batch.begin();
//Draw the region you extracted from the tilemap to the screen
batch.draw(mapRegion, x, y + mapRegion.getRegionHeight(),
mapRegion.getRegionWidth(), -mapRegion.getRegionHeight());//Needs to be flipped rightside u
//Draw more stuff
batch.end();
To match your example you would have
TILE_X = 5;
Tile_Y = 5;
TILE_W = whatever your size is per tile (often 16 or 32 px);
TILE_H = same sort of thing as TILE_W;
NUM_TILES_ACROSS = 20;
NUM_TILES_VERTICALLY = 20;