I've started to develop a small roguelike game in LibGDX. I've also made a minimap class, which creates and updates the minimap as the player explores the dungeon, by using a Pixmap
. The minimap class has an OrthographicCamera
(currently unused), which will be used for centering the minimap on the player. The minimap is currently rendered with it's own SpriteBatch
. Now I'd like to put the minimap on the upper right corner, and clip it, because it is so big, that it'd obscure the half of the screen. I'm not really sure how to do this clipping part. I assume I should use the ScissorStack
class, but the LibGDX wiki is a bit uninformative for me.
2 Answers
first, you are using more than one SpriteBatch? Don't do that. Share one instance.
Clipping works really simple that you push a Rectangle to the stack, render what you need (only stuff in your rectangle will be drawn) and afterwards pop the rectangle.
However, the best way to achieve a minimap is with the viewport class.
your render queue should look something like this:
- viewport of your game (might be more than 1 camera for parallax)
- viewport of the minimap
- scene2d or whatever you are using for ui
The default implementations of the viewport are setting the lower left corner to 0, 0 by default, so you need your own implementation:
public class InteractiveViewport extends ScreenViewport{
public InteractiveViewport(OrthographicCamera camera){
super(camera);
}
public void update (boolean centreCamera) {
setScreenBounds(getScreenX(), getScreenY(), getScreenWidth(), getScreenHeight());
setWorldSize(getScreenWidth() * getUnitsPerPixel(), getScreenHeight() * getUnitsPerPixel());
apply(centreCamera);
}
}
set world width, height and x,y position of the lower left corner to whatever position in screen cords you want. With some more work you can make minimap interactive as in able to pan and zoom; and even resizing. And if needed clear the background of the viewport with 0.5f alpha for a cool effect.
The advantage of the viewport is that it sets the gl_viewport to the size of the viewport and you dont have to bother with complicated coordinate transformation to make your map work.
You might want to take a look at something similar I wrote. I hope it helps. https://github.com/Heerbann/Box2dCamera
As you are using a pixmap you can clip it easily. I am not on desktop so I am giving a pseudo code for now.
for( int x = player.x - desiredMapWidth / 2, i = 0; x < player.x + desiredMapWidth / 2; ++x, ++i) {
for( int y = player.y - desiredMapHeight / 2, j = 0; y < player.y + desiredMapHeight / 2; ++y, ++j){
Color c = fullMinimap.getPixel( x, y );
clippedMinimap.setPixel( i, j);
}
}
Note: code may be different from exact code. I just tried to show the way.