user1001506
user1001506

Reputation:

Libgdx fitting game inside all resolutions

OK, I have read every possible Google entry on this subject, and am still not sure what path to take. At the moment here is my setup:

static final float WIDTH = 800;
static final float HEIGHT = 600;

@Override
public void resize(int width, int height) {
    camera = new OrthographicCamera(WIDTH, HEIGHT);                            
    camera.position.set(WIDTH / 2, HEIGHT / 2, 0);                             
    glViewport = new Rectangle(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());  
    ... 
}

 public void render(float delta) {      

GL20 gl = Gdx.graphics.getGL20();  
gl.glClearColor(1, 0, 0, 1);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);  
gl.glViewport((int) glViewport.x, (int) glViewport.y,
                (int) glViewport.width, (int) glViewport.height);       

camera.update();  
    ....
 }

This does a fine job stretching across my screen but has huge drawbacks. The proportions do not get stretched. For example when I define a sprite in my 800x600 world it stays exactly the same on a 1280x720 screen. Also this affects sprites placed at specific locations because the location 400px on 800x600 is not the same on 1280x720 screen. Also have specific regions that if touched something happens, but my solution and the ones I have found on the internet do not work for it either. Lets not talk about aspect ratio, because every time I get to think about it my head hurts.

There must be a logical way of doing this with libGdx as many games have been released but I can not seem to find something that will work. Any on have any suggestions on how to go on handling this?

Upvotes: 0

Views: 649

Answers (2)

aug13
aug13

Reputation: 369

The possible problem could be that you are not translating the mouse coordinates to world coordinates.This can be done in the following manner

if (Gdx.input.justTouched())

{

camera.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));

}

Here touchPoint is a Vector3..now you can use touchPoint anywhere in your code..

Upvotes: 0

aug13
aug13

Reputation: 369

First you have to make an object of the SpriteBatch class to draw things. After you set the position of your camera..you must update it..add the following line

camera.update(); after updating it's position.

Next,the problem of scaling can be solved by adding

batcher.setProjectionMatrix(camera.combined); after gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

This is very useful since game involves a lot of calculations,specially when you integrate physics(BOX2D) into them. So,try to have a smaller camera like 800/40,600/40 and do all the calculations according to that camera.Then batcher.setProjectionMatrix(camera.combined) will handle the scaling thing. Make sure that you are doing all the calculations according to the camera. Here batcher is the object of the SpriteBatch class. After this..now to draw things..

batcher.begin()

//draw things...

batcher.end()

You can refer to these links https://github.com/libgdx/libgdx/tree/master/tests/gdx-tests/src/com/badlogic/gdx/tests

http://www.youtube.com/watch?v=GeyUCOCmW8k

Upvotes: 1

Related Questions