1

I set my texture to resize to Gdx.graphics.getWidth(), Gdx.graphics.getHeight() every time render function calls. In fact when I launch the app, texture sets full screen (as I want). But when I change the size of the app, the texture draws in some dumb way, while I want it to be full screen all the time.

Here is the code:

    package com.leopikinc.bobdestroyer;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL20;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class BobDestroyer extends ApplicationAdapter {
    SpriteBatch batch;
    Texture mainscreen;
    Music intromusic;
    float VOLUME;

    @Override
    public void create () {
        VOLUME = 1f;
        batch = new SpriteBatch();
        mainscreen = new Texture(Gdx.files.internal("data/FirstLevel.png"));
        intromusic = Gdx.audio.newMusic(Gdx.files.internal("data/IntroMusic.mp3"));
        intromusic.setLooping(true);
        intromusic.setVolume(VOLUME);
        intromusic.play();
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 1, 1, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(mainscreen, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        batch.end();
    }

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

Nice way

Dumb way

Cœur
  • 37,241
  • 25
  • 195
  • 267
Leopik
  • 303
  • 2
  • 14

1 Answers1

0

You should use viewport to deal with different aspect ratios.

Here is an example of StretchViewport which can display a fullscreen texture.

public class MyGdxGame extends ApplicationAdapter {
    SpriteBatch batch;
    Texture img;
    private Viewport viewport;
    private Camera camera;

    @Override
    public void create() {
        camera = new OrthographicCamera();
        viewport = new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        viewport.setCamera(camera);
        batch = new SpriteBatch();
        img = new Texture("badlogic.jpg");

    }

    @Override
    public void render() {
        camera.update();
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.begin();
        batch.draw(img, 0, 0, viewport.getWorldWidth(),viewport.getWorldHeight());
        batch.end();
    }

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

    }
}

Before changing screen sizes :

before changing screen sizes After changing screen sizes:

after changing screen sizes

Saeed Masoumi
  • 8,746
  • 7
  • 57
  • 76