Quantcast
Channel: Question and Answer » libgdx
Viewing all articles
Browse latest Browse all 434

Libgdx desktop windows resize

$
0
0

I have tried to make game with libgdx

I want to create 3×3 grid in the middle of screen
I ran the desktop project, it seems that not much problem at all,
but if I resize the windows the 3×3 grid move towards the lower left of the screen
(I want to make it always in the center of screen regardless of the screen size change)

What are the problem in my code, I am a new to game developing.

public class GameScreen implements Screen {

    MyGdxGame game;
    SpriteBatch batch;
    Texture img, item;

    public GameScreen(MyGdxGame game) {
        this.game = game;

        batch = new SpriteBatch();
        // a 640x480 image for testing background position
        img = new Texture(Gdx.files.internal("640Wx480H.png"));
        // a 64x64 image for grid item
        item = new Texture(Gdx.files.internal("badlogic.jpg"));
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        Gdx.gl.glClearColor(1, 1, 1, 1);
        batch.begin();
        batch.draw(img, 0, 0);
        float itemSize = item.getWidth();
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                batch.draw(item, itemSize * i + (Gdx.graphics.getWidth() - (itemSize * 3)) / 2, itemSize * j + (Gdx.graphics.getHeight() - (itemSize * 3)) / 2);
            }
        }

        batch.end();
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void show() {
    }

    @Override
    public void hide() {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }

    @Override
    public void dispose() {
        img.dispose();
        item.dispose();
    }

}


public class MyGdxGame extends Game {

    GameScreen mainGameScreen;
    private Viewport viewport;
    private OrthographicCamera camera;

    @Override
    public void create() {
        camera = new OrthographicCamera();
        camera.setToOrtho(false, 640, 480);
        viewport = new FitViewport(640, 480, camera);
        mainGameScreen = new GameScreen(this);
        setScreen(mainGameScreen);
    }

    public void resize(int width, int height) {
        viewport.update(width, height);

    }

    public void render(float delta) {
    }

    public void dispose() {

    }
}

Viewing all articles
Browse latest Browse all 434

Trending Articles