1
\$\begingroup\$

I am trying to make a tile based game in LibGDX and I have run into some problem. In earlier versions of LibGDX you were able to draw a certain section of a .tmx map instead of a whole and even a section of a layer. Even though , in the nightly builds I cannot find anything about this methods the only thing I have found was drawing of a whole map or one of its layers.

Ideally I would like to draw portion of a map something like draw nao from coords (5,5) to (25,25) . Meaning only the square 20x20 tiles starting from tile in the 5th row/column at position no. 5.

Is it even doable in the newer builds ?

\$\endgroup\$

1 Answer 1

0
\$\begingroup\$

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;
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .