根据视口和相机绘制纹理 [英] Draw texture according to viewport and camera

查看:106
本文介绍了根据视口和相机绘制纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法绘制纹理,因此无法根据屏幕分辨率调整它们的大小.

I don't manage to draw my textures so that they are resized according to the screen resolution.

我正在使用ScreenViewport(因为我希望字体平滑且不变形).

I am using ScreenViewport (as I want my font to be smooth and without distorsion).

float w = Gdx.graphics.getWidth();                                       
float h = Gdx.graphics.getHeight();   
gameCam = new OrthographicCamera(2f * h/w, 2f);
gamePort = new ScreenViewport(gameCam);

奇怪的是,无论相机的高度和宽度如何,结果都是相同的.

Weirdly, whatever the height and width of the camera the result is the same.

在调整大小的方法中,我尝试了很多事情:

In the resize method, I tried a lot of things :

@Override
public void resize(int width, int height) {
gamePort.update(width, height, true);
renderer.stage.getViewport().update(width, height, true);
gameCam.viewportWidth = width;
gameCam.viewportHeight = height;
gameCam.update();

float aspectRatio = (float) width / (float) height;
gameCam = new OrthographicCamera(2f * aspectRatio, 2f);
gameCam.update();

最后,在render方法中:

Finally, in the render method :

public void render() {                                              
    sb.setProjectionMatrix(cam.combined);   
    sb.draw(block.getTexture(), block.getPosition().x, block.getPosition().y, BLOCK_WIDTH, BLOCK_HEIGHT);

BLOCK_WIDTH和BLOCK_HEIGHT = 48 我的纹理是512像素,所以我希望它们更小.

BLOCK_WIDTH and BLOCK_HEIGHT = 48 My textures are 512px so I want them to be smaller.

如下图所示,当设备具有高分辨率时,即使我调整窗口大小,纹理也将保持相同的大小和位置.舞台上的分数表没有这个问题.

As you can see in the image below, when the device has a big resolution, even when I resize the window, the textures keep the same size and position. I don't have this problem with the table of scores on top of the stage.

推荐答案

在此处回答了有关调整精灵大小的问题:

A question about resizing sprites was answered here:

如何在Libgdx中调整Sprite的大小?

因此,请不要使用batch.draw(sprite,...),而应尝试使用sprite.draw(batch).

So instead of using batch.draw(sprite,...), try sprite.draw(batch) instead.

我严格使用LibGDX的Stage和Actor,因为它使调整大小和定位组件(以及其他操作)非常容易.使用ScreenViewport时,我将组件的大小设置为屏幕大小的某个百分比.可以完美地缩放以适应不同的屏幕尺寸.

I strictly use LibGDX's Stage and Actors as it makes resizing and positioning components (among other things) very easy. When using ScreenViewport I set the size of my components to be some percentage of the screen size. This scales perfectly to different screen sizes.

例如,我没有将按钮硬编码为正好为100像素宽,而是将其设置为Gdx.graphics.getWidth()* 0.10f,这会将按钮宽度设置为屏幕宽度的10%,而与屏幕的大小无关分辨率.

For example, instead of hard coding a button to be exactly 100 pixels wide, I set it to be Gdx.graphics.getWidth() * 0.10f and this sets the button width 10% of the screen width regardless of the screen's resolution.

您也可以在职位上做同样的事情.不用将位置设置为50像素,而是将其设置为Gdx.graphics.getWidth()* 0.5f,然后x位置将为屏幕宽度的%5.

You can do the same thing with position also. Instead of setting the position to 50 pixels set it to Gdx.graphics.getWidth() * 0.5f and then the x position will be %5 of the screen width.

如果您的块要在某种网格中对齐,则另一种定位方法是使用LibGDX图像而不是Sprite.图像基于Actor,它将允许您将所有块放入表格中,表格在调整屏幕大小时会自动重新定位.因此,这些区块的工作方式与屏幕顶部的分数完全一样.

If your blocks are going to be aligned in some kind of a grid, then another option for positioning would be to use LibGDX Image instead of Sprite. The Image is based on Actor and it would allow you to put all your blocks into a Table which would automatically re-position itself when resizing the screen. So the blocks would work exactly like the scores at the top of the screen.

这篇关于根据视口和相机绘制纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆