How to scale sprites in libgdx according to screen resolutions?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


How to scale sprites in libgdx according to screen resolutions?



I am trying to scale the texture to fit the screen width. This is what I tried, but it simply repeats the texture. It does not scale it.



In the init method:


TextureLoader.TextureParameter param = new TextureLoader.TextureParameter();
param.minFilter = Texture.TextureFilter.MipMapLinearLinear;
param.genMipMaps = true;
param.wrapU = Texture.TextureWrap.ClampToEdge;
param.wrapV = Texture.TextureWrap.ClampToEdge;
manager.load("textures/texture.png", Texture.class, param);



In the render method:


Texture tex = manager.get("textures/texture.png", Texture.class);
float scale = (float)( (float)Gdx.graphics.getWidth() / (float)(tex.getWidth()));

batch.begin();
Sprite s = new Sprite(tex, 0,0,tex.getWidth(),tex.getHeight());

s.setPosition(0, 0);
s.setOriginCenter();
//s.setScale(scale);
s.setSize(Gdx.graphics.getWidth(), scale * tex.getHeight());
s.setOrigin(0,0);
s.draw(batch);
batch.end();



Does anyone have an idea what I am doing wrong? 





Looks like you are using imaginary "pixel" units, read this: blog.xoppa.com/pixels
– Xoppa
Dec 28 '15 at 17:25





Why is this so complicated?
– Z0q
Dec 28 '15 at 18:18





What is complicated?
– Xoppa
Dec 28 '15 at 18:28





Well, do I need to create a separate camera or viewport? Why isn't there a simple function like setScale() in libgdx? I mean, 3D objects simply scale the way I want, but 2D seems to make it bloated. I find the article difficult to understand too.
– Z0q
Dec 28 '15 at 19:01





@Z0q there is nothing complicated about camera and/or Viewports. Once you understood them they can make many things a lot easier. One thing you always have to remember: You DON'T want to use pixels. Devices have different resolutions and you really don't want to have a resolution-dependent game, especially not, when you are developing for android devices! So read the camera/viewport tutorials and you will see it's advantage
– Springrbua
Dec 30 '15 at 8:17


camera


Viewport


camera


viewport




1 Answer
1



Apparently this happened because I was using mipmaps. Without mipmapping, the sprites scale appropriately.


param.minFilter = Texture.TextureFilter.MipMapLinearLinear;
param.genMipMaps = true;








By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

C++ virtual function: Base class function is called instead of derived