1

This is just a really quick post wondering what i have done wrong?

I want the background to be filled on the whole screen with whatever screen sizes i have but this does not make it work. This is the result when i run the programHere is all the code:

// Variables

Jump game;

OrthographicCamera cam;

SpriteBatch sb;

 // Variables

public SplashScreen(Jump game)
{
    this.game = game;

    cam = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    cam.translate(cam.viewportWidth / 2, cam.viewportHeight / 2);

    sb = new SpriteBatch();
}

public void show() 
{

}

public void render(float delta) 
{
    Gdx.gl20.glClearColor(0.2F, 0.6F, 1F, 1F);

    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

    cam.update();

    sb.begin();

    sb.setProjectionMatrix(cam.combined);

    sb.draw(Assets.splash_spr_background, 0, 0);

    sb.end();

}

Thanks! :)

Adam Brodin
  • 63
  • 1
  • 2
  • 13

1 Answers1

3

The problem is your asset is not the same size as the viewport. See the links below. Libgdx uses the Viewport class to handle this.

How to deal with different aspect ratios in libGDX?

How to scale image according to different screen resolutions in libgdx

https://github.com/libgdx/libgdx/wiki/Viewports

Community
  • 1
  • 1
Jeremy Barnes
  • 657
  • 4
  • 15
  • I thought that the camera/sb or whatever would notice that the sprite is smaller than the screen size and resize it somehow? xD – Adam Brodin May 13 '15 at 15:18
  • Not automatically. Spritebatch has a draw method that allows you to manually set the width and height if you want to do it by hand, otherwise Viewport for you. http://libgdx.badlogicgames.com/nightlies/docs/api/com/badlogic/gdx/graphics/g2d/SpriteBatch.html#draw-com.badlogic.gdx.graphics.Texture-float-float-float-float- – Jeremy Barnes May 13 '15 at 15:20
  • Okay i will try the viewport thingy and if it works you will get +REP & marked as answer for your help :) – Adam Brodin May 13 '15 at 15:21
  • - I cannot seem to get it to work! Im so frustrated, the background covers the whole screen but i do not see the text as in the image i posted, it's just the background color. This is the code im using: – Adam Brodin May 13 '15 at 16:12
  • public SplashScreen(Jump game) { this.game = game; BG_WORLD_WIDTH = 100; BG_WORLD_HEIGHT = 50; aspectRatio = (float)Gdx.graphics.getHeight() / (float)Gdx.graphics.getWidth(); cam = new OrthographicCamera(BG_WORLD_HEIGHT * aspectRatio, BG_WORLD_HEIGHT); cam.position.set(BG_WORLD_WIDTH / 2, BG_WORLD_HEIGHT / 2, 0); sb = new SpriteBatch(); Assets.splash_spr_background.setSize(BG_WORLD_WIDTH, BG_WORLD_HEIGHT); } – Adam Brodin May 13 '15 at 16:13
  • public void render(float delta) { Gdx.gl20.glClearColor(0.2F, 0.6F, 1F, 1F); Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT); cam.update(); sb.begin(); sb.setProjectionMatrix(cam.combined); sb.draw(Assets.splash_spr_background, 0, 0); sb.end(); } – Adam Brodin May 13 '15 at 16:13
  • There are errors in your math. If you use a Viewport, you won't need to worry about the math. – Tenfour04 May 13 '15 at 17:30