Search code examples
androidgraphicslibgdxtexturesscale

What is Gdx.graphics.getWitdth() measured in?


I'm using getWidth and getHeight a lot for scaling textures in my Libgdx project. What units are these in (pixels?) Also, if I want the texture to look consistent on different phones, should I use getWidth/Height to scale, or some number value like width = 100, height = 50?


Solution

  • Yes, getWidth and getHeight are both measured in pixels. Although you can use these for your dimensions, usually what you'll want to do to get a consistent look across phones is to use a camera of some sort (for 2d games, generally an OrthographicCamera).

    What you'll probably want to do is give the camera some fixed width and height, and then do all of your drawing through the camera's transformations. Something like:

    SpriteBatch batch = new SpriteBatch();
    OrthographicCamera camera = new OrthographicCamera(800, 600);
    //some code here
    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    //draw your textures here
    batch.end();
    

    This should keep the scale of your images consistent across phones. Keep in mind that if there is too much of a stretch/compress, your textures may look distorted.