[英]LibGDX - My Image isn't filling the width and height of the screen
I'm having a problem getting my image to fill the whole screen without stretching itself. 我的图像无法填满整个屏幕时遇到问题。 Currently I have an orthographic Camera set up, but the issue is the origin (0,0) of the image displays in the middle of the screen.
目前,我已经设置了正交摄影机,但问题是屏幕中间显示的图像的原点(0,0)。 How can I make this work?
我该如何进行这项工作? Here is the code that I have.
这是我的代码。
public class GameScreen implements Screen {
SlingshotSteve game;
public void Menu(SlingshotSteve game){
this.game = game;
}
OrthographicCamera camera;
SpriteBatch batch;
TextureRegion backgroundTexture;
Texture texture;
GameScreen(final SlingshotSteve gam) {
this.game = gam;
camera = new OrthographicCamera(800, 480);
batch = new SpriteBatch();
Texture texture = new Texture(Gdx.files.internal("background.jpg"));
backgroundTexture = new TextureRegion(texture, 0, 0, 500, 500);
}
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(backgroundTexture, 0, 0, SlingshotSteve.WIDTH, SlingshotSteve.HEIGHT);
batch.end();
}
@Override
public void dispose() {
batch.dispose();
texture.dispose();
}
How is your Orthographic camera implemented? 您的正交摄影机如何实施? It sounds like you might be interested in the "pixel-perfect" transform, which allows screen co-ordinates to correlate directly to world co-ordinates.
听起来您可能对“像素完美”转换感兴趣,该转换允许屏幕坐标直接与世界坐标相关。 You can use the parameters below to implement this transformation.
您可以使用下面的参数来实现此转换。
final float lOrthoLeft = 0;
final float lOrthoRight = pScreenHeight;
final float lOrthoTop = 0;
final float lOrthoBottom = pScreenWidth;
final float lOrthoNear = Float.MIN_VALUE;
final float lOrthoFar = Float.MAX_VALUE;
In this regard, it is possible that your values of lOrthoLeft
, lOrthoRight
, lOrthoTop
and lOrthoBottom
of your Orthographic projection are (-Width/2)
, (+Width/2)
, (-Height/2)
, (+Height/2)
(or some variation) in order to position the world's origin in the centre of the screen. 在这方面,
lOrthoLeft
, lOrthoRight
, lOrthoTop
和lOrthoBottom
的值可能是(-Width/2)
, (+Width/2)
, (-Height/2)
(+Height/2)
, (+Height/2)
(或某些变化形式) ,以便将世界原点定位在屏幕中央。
However, this does also have to do with how libgdx renders to the screen. 但是,这也与libgdx渲染到屏幕的方式有关。 OpenGL ES, the hardware-accelerated graphical API under the hood of libgdx uses a special co-ordinate system where the centre of the screen is used as the origin.
OpenGL ES是libgdx之下的硬件加速图形API,它使用特殊的坐标系,其中以屏幕中心为原点。 You'll find these resources useful in understanding how to work within these domains.
您会发现这些资源有助于理解如何在这些域中工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.