Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libGDX - make border around an image

Can I make a border around an image? I have lots of blocks in 2D, and when the player hovers with the mouse on one of them I want to display the border around the texture / image.

Thats how I draw the block actually ( I don't think its relevant, but maybe it will help ) :

batch.draw(map.map[mapPos].TEXTURE, (mapPosX * Block.WIDTH), (mapPosY * Block.HEIGHT), Block.WIDTH, Block.HEIGHT);

Is it possible with code or should I make a separate image with the texture and border around it? Any ideas?

like image 948
julian Avatar asked Dec 26 '13 15:12

julian


1 Answers

You could try using a ShapeRenderer to draw the border first, then batch.draw over it. This is done purely in code, without using a texture. The code below adds a blue border.

In render() add the following after your batch.end().

batch.end(); // Add the following after this line

sr.setProjectionMatrix(camera.combined);
sr.begin(ShapeType.Line);
sr.setColor(new Color(0,0,1,0));
sr.rect((mapPosX * Block.WIDTH), (mapPosY * Block.HEIGHT), Block.WIDTH, Block.HEIGHT));
sr.end();

Of course, you need to initialize ShapeRenderer in your Screen or ApplicationListener implementation. Just do it in the code where you declare and initialized batch.

In your Game class:

SpriteBatch batch; //Put the following below this line
ShapeRenderer sr;

In your constructor :

batch = new SpriteBatch(); //Put the following below this line
sr = new ShapeRenderer();

edit: i have rewrote the function so that you can draw the shape after drawing the texture batch.

like image 88
nedR Avatar answered Sep 21 '22 23:09

nedR