6
\$\begingroup\$

I'm using this code to set a 800x420 image to be full screen:

public void show() {
    float w = Gdx.graphics.getWidth();
    float h = Gdx.graphics.getHeight();
    camera = new OrthographicCamera(1, h / w);
    batch = new SpriteBatch();
    texture = new Texture(Gdx.files.internal("images/splashatlas/splashatlas1.png"));
    texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    TextureRegion region =
    new TextureRegion(texture, 0, 0, 800, 420);
    sprite = new Sprite(region);
    sprite.setSize(0.9f,
            0.9f * sprite.getHeight() / sprite.getWidth() );
            sprite.setOrigin(sprite.getWidth() / 2,
            sprite.getHeight() / 2);
            sprite.setPosition(-sprite.getWidth() / 2,
            -sprite.getHeight() / 2);
}


public void render(float delta) {
    Gdx.gl.glClearColor( 1f, 1f, 1f, 1f );
    Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    sprite.draw(batch);
    batch.end();
}

But on real device it looks like this:

On a real device

I tried to setSize with different parameters:

    sprite.setSize(1.0f,
            1.0f  );

Now it's fullscreen, but it's really stretched:

fullscreen, but stretched

What is the way to set a background to be full screen? I want it to look like this:

what I want

\$\endgroup\$
8
  • \$\begingroup\$ change new TextureRegion(texture, 0, 0, 800, 420); to new TextureRegion(texture, 0, 0, w, h); \$\endgroup\$
    – user39686
    Mar 1 '14 at 13:03
  • \$\begingroup\$ this gives me a white screen. \$\endgroup\$
    – Matt
    Mar 1 '14 at 13:06
  • \$\begingroup\$ this zoom the picture too much ... its looks blue now \$\endgroup\$
    – Matt
    Mar 1 '14 at 13:13
  • \$\begingroup\$ i updated the question .. \$\endgroup\$
    – Matt
    Mar 1 '14 at 13:14
  • \$\begingroup\$ ah ok, I had a chance to look at the documentation. try sprite.setSize(1.0f, 1.0f); \$\endgroup\$
    – user39686
    Mar 1 '14 at 13:18
4
\$\begingroup\$

the problem was in my images size , i set the viewport to be 800x480 but the images size was 800x420 , i just changed the image size to 800x480 and in setSize() i changed it like the following :

sprite.setSize(1f,
            1f * sprite.getHeight() / sprite.getWidth() );

and its work perfectly now.

\$\endgroup\$
3
  • \$\begingroup\$ Consider using Gdx.graphics.getWidth() and Gdx.graphics.getHeight() as that will set the sprite to the size of the device's screen, regardless of the PPI. \$\endgroup\$
    – user959631
    Mar 4 '14 at 17:58
  • \$\begingroup\$ this will stretch the screen too much . \$\endgroup\$
    – Matt
    Mar 6 '14 at 20:51
  • \$\begingroup\$ but anyway the method i used above should scale the splash screen according to the device width and height and regardless of the ppi \$\endgroup\$
    – Matt
    Mar 6 '14 at 20:53
3
\$\begingroup\$

In your render() method, you could set Gdx.gl.glClearColor(0, 0, 0, 0); which will set the background of your game to Black

Then you don't need to resize the sprite and the texture won't distort.

Hope this helped.

\$\endgroup\$
2
  • 2
    \$\begingroup\$ but this method will not give a good results if i decided to change my splash screen to colored sprite because it will be obvious that the splash in not full screen . but anyway thank you I've already solved my problem. \$\endgroup\$
    – Matt
    Mar 4 '14 at 12:49
  • \$\begingroup\$ Oh, sorry, I wasn't aware that you would want to change the splash screen's color. \$\endgroup\$
    – user959631
    Mar 4 '14 at 17:57
0
\$\begingroup\$

In screen constructor I use OrthographicCamera and Stage with StretchViewport:

camera = new OrthographicCamera(800,420);
camera.setToOrtho(false,800,420);
stage = new Stage(new StretchViewport(800,420, camera));

Then in render method of screen class I update the viewport:

@Override
public void resize(int width, int height) {
    stage.getViewport().update(width,height,false);
}

And it fits the screen perfectly. Then you just draw your picture in the render method with your width and height (800 and 420). So it should work. Also I suggest using constants for width and height.

Don't forget to use super.render(); in your game class.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .