0
\$\begingroup\$

I have created a class called Node which I would like to (currently) contain a single Actor (later I will be adding additional actors, but for now I'm sticking with just one). The actor is called "body" and is just a container for the position/size of the "Node" actor.

I have also created a Label which I am drawing with a custom font file.

What I am having issues with is that when I RESIZE my app window (on desktop, I'm not currently using Android or any other targets), the LABEL works just fine (stays the same size on my screen), but the Node gets resized with the main window. That is, if I make the window twice as wide, the square stretches to be twice as wide as it was before. Why is this? The coordinates I am using work exactly as I expect UNTIL I resize the screen. Is there something else I have to do in the resize() method?

App class:

public class App implements ApplicationListener {
    private Stage stage;
    private ShapeRenderer shapeRenderer;

    @Override public void pause() {}
    @Override public void resume() {}

    @Override
    public void create() {
        stage = new Stage(new ScreenViewport());

        shapeRenderer = new ShapeRenderer();

        //  FONT stuff  //  https://github.com/libgdx/libgdx/wiki/Gdx-freetype
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/arial.ttf"));
        FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
        parameter.color= Color.ORANGE;
        parameter.size = 24;

        BitmapFont arialFont = generator.generateFont(parameter);
        generator.dispose(); 

        Skin skin = new Skin();

        Label.LabelStyle labelStyle = new Label.LabelStyle();
        labelStyle.font=arialFont;
        skin.add("default", labelStyle);

        Label label = new Label("DOOT", skin);
        label.setX(110.5f);
        label.setY(110.5f);


        stage.addActor(label);

        stage.addActor(new Node(shapeRenderer));
    }

    @Override
    public void resize(int width, int height) {
        // See below for what true means.
        stage.getViewport().update(width, height, true);
        shapeRenderer.updateMatrices();
        System.out.println(""+width+"x"+height);
    }

    @Override
    public void render(){//} {
        Gdx.gl.glClearColor(0.3f, 0.3f, 0.3f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act(delta);
        stage.draw();
    }

    @Override
    public void dispose() {
        stage.dispose();
    }
}

Node class:

public class Node extends Group {
    private Actor body;
    private ShapeRenderer shapeRenderer;

    public Node(ShapeRenderer sr) {
        body = new Actor();
        body.setBounds(10.0f, 10.0f, 100.0f, 100.0f);
        shapeRenderer = sr;
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);

        shapeRenderer.begin(ShapeRenderer.ShapeType.Line);
        shapeRenderer.box(body.getX(), body.getY(), 0.0f, body.getWidth(), body.getHeight(), 0.0f);
        shapeRenderer.end();
    }
}
\$\endgroup\$
3
\$\begingroup\$

You need to set the shapeRenderer projectionMatrix everytime you change your camera or viewport.

You could try to start by getting the projectionMatrix being used in the Batch.

shapeRenderer.setProjectionMatrix(batch.getProjectionMatrix());

Just like it's described here: https://github.com/libgdx/libgdx/wiki/Scene2d

\$\endgroup\$
  • \$\begingroup\$ That works great - thanks! I just grabbed the camera from the stage: shapeRenderer.setProjectionMatrix(stage.getCamera().combined) - seems to work fine. Any reason that's not a good way to do it? \$\endgroup\$ – loneboat Oct 14 '16 at 18:44
  • \$\begingroup\$ @loneboat That's perfect! And indeed the right way to do it :) \$\endgroup\$ – Gustavo Maciel Oct 14 '16 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.