0

I'm testing my sprite that has the game title, and on my Motorola Moto G 2nd generation the dimensions of the sprite looks good but I'm testing also on my mothers phone, a Samsung GT-S5830i, and the height of the sprite looks stretched out. I'm also trying to understand the concept of Viewport (I'm using the StretchViewport), but I don't know if I'm doing right. My game are designed for mobile, not desktop.

I did that to my SplashScreen:

this.gameTitle = new Sprite(new Texture(Gdx.files.internal("images/GameTitle.png")));
this.gameTitle.setSize(Configuration.DEVICE_WIDTH - 50, this.gameTitle.getHeight() * Configuration.DEVICE_HEIGHT / Configuration.DEVICE_WIDTH);

The DEVICE_HEIGTH and DEVICE_WIDTH are constants about the dimension of the screen. And the "-50" is a margin to the sprite

In my Viewport I used the real size of the screen for the dimensions, or should I use a virtual dimension? But how it works?

This is a part of my main class, what can I change?

// Create the Orthografic camera
this.orthoCamera = new OrthographicCamera(Configuration.DEVICE_WIDTH, Configuration.DEVICE_HEIGHT);
this.orthoCamera.setToOrtho(false, Configuration.VIRTUAL_GAME_WIDTH, Configuration.VIRTUAL_GAME_HEIGHT);
this.orthoCamera.position.set(this.orthoCamera.viewportWidth / 2f, this.orthoCamera.viewportHeight / 2f, 0);
this.orthoCamera.update();

// Combine SpriteBatch with the camera
this.spriteBatch.setTransformMatrix(this.orthoCamera.combined);

// Create the ViewPort
this.viewPort = new ExtendViewport(Configuration.DEVICE_WIDTH, Configuration.DEVICE_HEIGHT);

I updated my viewport to the ExtendViewport as you said.


Main class render method:

public void render() {
    super.render();

    // Update Orthographic camera
    this.orthoCamera.update();

    // Combine SpriteBatch with the camera
    this.spriteBatch.setTransformMatrix(this.orthoCamera.combined);
}

Screen class render method:

@Override
public void render(float delta) {
    // OpenGL clear screen
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(Gdx.gl.GL_COLOR_BUFFER_BIT | Gdx.gl.GL_DEPTH_BUFFER_BIT);

    // SpriteBatch begins
    this.game.spriteBatch.begin();

    // Display the ClimbUp logo
    this.gameTitle.draw(this.game.spriteBatch);
    this.character.draw(this.game.spriteBatch);

    // SpriteBatch ends
    this.game.spriteBatch.end();
}
Jazzguy
  • 147
  • 1
  • 11

1 Answers1

0

If you don't want stuff to look distorted on some devices and you don't want black bars (which none of your customers will like), you need to use an ExtendViewport instead of StretchViewport. And the dimensions you give it should be virtual dimensions based on whatever units you would like to work with.

For example, assuming a landscape orientation game, you could use 800 x 480 as virtual dimensions, and then you know that anything within that area (in world units) will be shown on the screen and you can design your game for that. On narrower devices (4:3 ratio) there will be more than 480 vertical units shown, and on wider devices (16:9 ratio) there will be more than 800 horizontal units shown.

There's one other option that avoids black bars and distortion, and that's FillViewport. But I think in general that's not a good option because you have no easy way to predict how much of your virtual dimensions are going to get cropped off.


Based on your edited question, here's what I would change in your code:

//No need to create your own camera. ExtendViewport creates its own.

// Pointless to call this now before resize method is called. Call this in render
//XXX this.spriteBatch.setTransformMatrix(this.orthoCamera.combined);

//This is where you give the viewport its minimum virtual dimensions
this.viewPort = new ExtendViewport(Configuration.VIRTUAL_GAME_WIDTH, Configuration.VIRTUAL_GAME_HEIGHT);

//Get reference to the viewport's camera for use with your sprite batch:
this.orthoCamera = (OrthographicCamera) this.viewport.getCamera();

Then in the resize method:

orthoCamera.setPosition(/*wherever you want it*/);
viewport.update(width, height, false); //these are actual device width and height that are provided by the resize method parameters.

You might want to position your camera in relation to the size calculated by the viewport. Then you should omit the setPosition line above, and instead calculate it after calling viewport.update. For example if you want 0,0 in the bottom left of the screen:

viewport.update(width, height, false);
orthoCamera.setPosition(orthoCamera.viewportWidth/2f, orthoCamera.viewportHeight/2f);

In your render method you can put this before spriteBatch.begin():

orthoCamera.update();  //good idea to call this right before applying to SpriteBatch, in case you've moved it.
spriteBatch.setProjectionMatrix(orthoCamera.combined);
Tenfour04
  • 83,111
  • 11
  • 94
  • 154
  • I updated my question. I never know where do I put virtual dimension or the device dimension. – Jazzguy Feb 12 '15 at 20:30
  • Ok, I did that, but now my screen shows all black now (gl clears everything). I didn't mentioned that I have the main class (the game class) and my screen classes. – Jazzguy Feb 13 '15 at 00:56
  • Post your whole render method of the Screen class and of the Game class if you have modified it. – Tenfour04 Feb 13 '15 at 02:46
  • I can't see any reasons for a black screen there, but I don't know the size and location of your sprites, or you virtual screen size. It is not correct to apply your matrix before super.render() because then you are drawing the screen before applying the correct camera matrix. But since your camera isn't moving, that would only be a problem in the first frame. – Tenfour04 Feb 13 '15 at 19:26
  • I still didn't change my code, but I discovered that my application crash, that's why my screen just shows black. I just didn't see on my logcat if something got wrong. – Jazzguy Feb 14 '15 at 19:42
  • Check this to try to figure it out. http://stackoverflow.com/q/3988788/506796 Then I suggest going to the Libgdx forum if you want more in depth review of your code. – Tenfour04 Feb 14 '15 at 20:14
  • I don't know whats going on. The android shows the error dialog (sometimes) and it shows black (the app didn't finalize), but now I can see my sprites, the app render the sprites but the glClear() clear the screen, then our sequence is not correct. – Jazzguy Feb 14 '15 at 23:42
  • EVERYTHING ALRIGHT! I just moved the "this.spriteBatch.setTransformMatrix(this.orthoCamera.combined);" to the render() method from the screen instead the render() method from the Game class. Thank you, for now it's working! – Jazzguy Feb 15 '15 at 16:34
  • Cool. I'd still encourage you to post your code at the libgdx forums. I sense from your solution that something is not quite right and could cause you problems later. – Tenfour04 Feb 16 '15 at 00:35
  • Back again!! My orthoCamera position are at 206.64583 / 310. It should be the half of 350 and 620. So just the Y axis are right! I don't know why the X is on this position... – Jazzguy Feb 20 '15 at 00:50