Reputation: 321
I am trying to draw some lines for debugging using libgdx but I failed. this is my code
public class MainMenu extends Screen implements InputProcessor {
private OrthographicCamera camera;
SpriteBatch myBatch;
ShapeRenderer shapeDebugger;
public MainMenu(Game game) {
super(game);
camera= new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.update();}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
myBatch.begin();
stage.draw();
Gdx.gl10.glLineWidth(10);
shapeDebugger.setProjectionMatrix(camera.combined);
shapeDebugger.begin(ShapeType.Line);
shapeDebugger.setColor(1, 1, 1, 1);
shapeDebugger.line(2, 2, 5, 5);
myBatch.end();
}
}
I get an error from line
shapeDebugger.setProjectionMatrix(camera.combined);
@Pranav008
thank you very much. I didn't expect that I need to initiate it. but I've got a true problem. I draw the line to center of the game screen like this.
Gdx.gl10.glLineWidth(2);
shapeDebugger.setProjectionMatrix(camera.combined);
shapeDebugger.begin(ShapeType.Line);
shapeDebugger.setColor(1, 1, 1, 1);
shapeDebugger.line(Gdx.graphics.getWidth()/2, 0,Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight());
shapeDebugger.end();
when I try to resize the screen it doesn't update the to center it goes far away to right.
Upvotes: 4
Views: 13288
Reputation: 45072
Define & initialize ShapeRenderer
ShapeRenderer shapeDebugger;
@Override
public void create() {
shapeDebugger = new ShapeRenderer();
...
Draw line in render callback
@Override
public void render() {
//render scene
Gdx.gl.glClearColor(69f / 255, 90f / 255, 100f / 255, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
...
shapeDebugger.setProjectionMatrix(camera.combined);
shapeDebugger.begin(ShapeRenderer.ShapeType.Line);
Gdx.gl.glLineWidth(10 / camera.zoom);
shapeDebugger.setColor(1, 0, 0, 1);
shapeDebugger.line(screenWidth / 2, 0, screenWidth / 2, screenHeight);
shapeDebugger.line(0, screenHeight / 2, screenWidth, screenHeight / 2);
shapeDebugger.end();
Upvotes: 0
Reputation: 135
My answer is mayby too late for you, but for people how have the same positioning problems.
Your second question about the position after a resize is because the viewport did not change.
Aldo your window size changed your application stil is using the same pixel size set on create by the function camera.setToOrtho
Update the view port on a resize!
//-----------------------------------------------------------------
@Override
public void resize (int width, int height) {
camera.setToOrtho(true, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
camera.update();
}
Upvotes: 1
Reputation: 1223
You must have got nullpointerException
because you haven't made any object of ShapeRenderer.
Insert this line in your constructor.
shapeDebugger=new ShapeRenderer();
Upvotes: 5
Reputation: 737
Remember that nesting Shaperender with a SpriteBatch may cause problems.
Check this link.
Upvotes: 3