libGDX: Orthogonal TiledMapRenderer renders just one tile - libgdx

I'm using the OrthogonalTiledMapRenderer in libGDX 0.9.9 to render a tiled map in tmx format.
maprend = new OrthogonalTiledMapRenderer(board.getTiledMap(), sprtbatch);
This renderer renders just one single tile in the bottom left corner.
render() { //(shortened)
sprtbatch.setProjectionMatrix(camera.combined);
maprend.render(); }
Using an IsometricTiledMapRenderer with the same constructor renders the whole map.
Is there a known bug in the orthogonal renderer or am I using it wrong?

You need to call maprend.setView(camera); before calling maprend.render();.
Note: There is no need to set the projection matrix of the spritebach there.

Related

TiledMap Layers causing many extra texture bindings

I'm using Tiled Map Editor to generate a tile map for my game in libgdx. I noticed while creating a simple map that having two tile layers causes many extra texture bindings. I'm using 2 packed tilesets and NOT using individual pngs.
For example, here is map with 2 tile layers with 2 tilesets. The dirt is from 1 tileset, while the rocks, trees, and other objects are on a different tileset.
This causes 25 texture bindings.
However, if I delete the layer with the rocks, trees, etc. and only leave the dirt, I get 1 texture binding.
Is there a better way to achieve this? Again, I'm not using individual pngs for the tiles, they are packed into 2 tilesets. What are the work arounds for this? To just pack everything into 1 tileset? I have some maps that are causing 80+ texture bindings.
Not really doing anything weird to render the map either:
class MapRenderer {
private TiledMapRenderer tiledMapRenderer;
public MapRenderer(TiledMap tiledMap, float tiledMapScale, Camera camera) {
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap, tiledMapScale);
tiledMapRenderer.setView((OrthographicCamera) camera);
}
public void update() {
tiledMapRenderer.render();
}
}
Sort of seems like it is rendering each tile, tile by tile. When a better way to reduce texture bindings would be to render each tile by tileset.
When you create the tiled map, create separate layer for tiles from each tileset. This way you will have only 1 extra binding for each tileset.

Drawing Rectangle images-LibGdx

In my first LibGdx Project,I want to draw some rectangles.
I am not looking for shape rendering purpose.I am aiming to implement a function like what fillRect() in j2me do.I have to draw filled rectangles and need to manipulate it(changing size,rotating.. etc).
When I google about it, always getting shapeRenderer related things only.
Please mention how can I draw and manipulate my own images.
Draw Rectangle by using Pixmap.
Texture texture=getPixmapTexture(Color.WHITE);
Sprite sprite=new Sprite(texture); //Used for drawing 2D sprites.
//or
Image image=new Image(texture); //2D scene graph node.
public static Texture getPixmapTexture(Color color){
return new Texture(PixmapBuilder.getPixmapRectangle(1, 1, color));
}
public static Pixmap getPixmapRectangle(int width, int height, Color color){
Pixmap pixmap=new Pixmap(width, height, Pixmap.Format.RGBA8888);
pixmap.setColor(color);
pixmap.fillRectangle(0,0, pixmap.getWidth(), pixmap.getHeight());
return pixmap;
}
The answer by Abhishek is correct.
However, if you have just started game developement with LibGDX, I would check whether you need at all to perform such operation (draw a rectangle).
In libGDX you can use Scene2D which allow you to create a Stage, Actors and direct them on your stage.
So instead of drawing a rectangle, you create an actor, such as an image, to which you can associate a texture, a button or a TextBox and place it on your screen.
Scene2D allows you to then use things like Action or rotation, scaling..
There are some good visual demos about that on Libgdx.info
I am mentioning this because moving to Scene2D later may be more complicated than if you make that decision early on.

libGDX change tilemap

I am making an 2d rpg game with box2d. So, I've got a problem. When one of my bodies(the character) collides with another(a door) the map needs to change, should I just create new screens for maps and change them? Or is there a more simple solution?
You can change your current map on the same screen only. What you have to do is, Let's say your map variable name is testMap. Now let's say your player just collided with a door. Now let's say you will call a method called changeMap(). Here is what you will put inside changeMap() method. (Assuming you are using tiled maps, you can change logic accordingly here)
void changeMap() {
Gdx.app.postRunnable(() -> { //Post runnable posts the below task in opengl thread
testMap = new TmxMapLoader().load("someMap.tmx"); //load the new map
renderer.getMap().dispose(); //dispose the old map
renderer.setMap(testMap); //set the map in your renderer
});
}

geotools draw symbol on overlay

I'm new to Geotools. I am developing a simple application that shows a map and I would like to dynamically place a bitmap or vector symbol on it (for example to show where the current position is, like in navigation systems).
Now, what I've done already is:
showing a map given one or more shapefile.
center the map on the last inserted position.
add shapes to new layers when needed.
What I need to do is to create an overlay with images at given coordinates on the map area (to be clear, I don't want to generate a raster layer on disk, I just want to paint on the screen).
My guess is that I have to somehow directly use the Graphics2D instance inside JMapPane, is that correct? In this case how can I convert from the geographic coordinates to pixel coordinates on the drawing pane? Or are there some geotools functions/classes that I should be looking for?
Thank you.
Answering myself, since I found the solution.
I did it by extending class org.geotools.map.DirectLayer, which provides a way to define a custom layer by extending its draw(Graphics2D, MapContent) method. Graphic elements are placed at the correct coordinates by using the worldToScreen affine transform provided by geotools. For example, to define a Point2D placed at the current position given real-world coordinates:
AffineTransform worldToScreen = viewport.getWorldToScreen();
Point2D worldPoint = new Point2D.Double(currentPosition.x, currentPosition.y);
Point2D screenPoint = worldToScreen.transform(worldPoint, null);
Once the draw() method is defined, just instantiate and add as a layer to your MapContent instance.

Getting Graphics-Object or Bitmap from BrowserField on Blackberry

I would like to get the Bitmap of the content of a webpage as it is displayed in the BrowserField. Therefore I'd need the Graphic-object of the browser field. But the paint-method is protected unfortunately.
Is there a way to get this?
Thank's
Normally, if you want to do some custom drawing with a field ie. draw into the field's graphics context, you'd subclass Field and override the paint method. However, when it comes to BrowserField, you can't do that because it's declared final.
There is a workaround for this, though. You can subclass a Manager and add your BrowserField to an instance of that manager. So, for example, if you want to add your BrowserField instance to a VerticalFieldManager, you can use the following code to get access to the Graphics object the browser will be drawn into. In this sample code, you'll see that I use the graphics object and manager's superclass implementation to draw into a bitmap. Then, that bitmap is drawn to the screen.
VerticalFieldManager vfm = new VerticalFieldManager() {
// Override to gain access to Field's drawing surface
//
protected void paint(Graphics graphics) {
// Create a bitmap to draw into
//
Bitmap b = new Bitmap(vfm.getVirtualWidth(), vfm.getVirtualHeight());
// Create a graphics context to draw into the bitmap
//
Graphics g = Graphics.create(b);
// Give this graphics context to the superclass implementation
// so it will draw into the bitmap instead of the screen
//
super.paint(g);
// Now, draw the bitmap
//
graphics.drawBitmap(0,
0,
vfm.getVirtualWidth(),
vfm.getVirtualHeight(),
b,
0,
0);
}
};
And, there you have a Bitmap containing the contents of the manager. Should note, though, that this has the potential to consume a lot of memory.