Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Java LibGDX) How do I resize my textures in LibGDX?

I have been fooling around with LibGDX for a while now, and wanted to easily port my programs to different systems. I have a background texture, which I want to scale to the currently used resolution. The image is 1920x1080, how do I change it to the currently used resolution at runtime?

like image 810
user2445723 Avatar asked Jun 02 '13 19:06

user2445723


People also ask

What is texture in libGDX?

A Texture is the libGDX implementation of an OpenGL 2D Texture. That is: it represents a single 2D texture in GPU memory, which can be used for rendering. You typically want to use as less textures as possible (and specifically want to avoid switching between textures).


2 Answers

If you want to scale at drawing time use:

Pixmap pixmap200 = new Pixmap(Gdx.files.internal("200x200.png"));
Pixmap pixmap100 = new Pixmap(100, 100, pixmap200.getFormat());
pixmap100.drawPixmap(pixmap200,
        0, 0, pixmap200.getWidth(), pixmap200.getHeight(),
        0, 0, pixmap100.getWidth(), pixmap100.getHeight()
);
Texture texture = new Texture(pixmap100);
pixmap200.dispose();
pixmap100.dispose();

From:

https://www.snip2code.com/Snippet/774713/LibGDX-Resize-texture-on-load

like image 56
rubmz Avatar answered Sep 19 '22 21:09

rubmz


    batch.begin();
    batch.draw(yourtexture, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    batch.end();

Using the camera viewport also works:

    batch.begin();
    batch.draw(yourtexture, 0, 0, cam.viewportWidth, cam.viewportHeight);
    batch.end();
like image 33
Lestat Avatar answered Sep 18 '22 21:09

Lestat