I am trying really hard to draw a sprite with LibGDX, but I fail. If you need more than the snippets, the source is here: https://github.com/reisi007/Planes2DLibGDX/blob/master/Main/src/com/reisisoft/Planes2D/Planes2D.java
So, what am I doing? I am loading a PNG (1024x1024) and creating sprites, which I put in a Map. This map I put in a map again (later different resolutions)
private void putTextures(Resolutions resolutions) {
Map<GObjects, Sprite> map;
switch (resolutions) {
case MidRes:
map = new EnumMap<GObjects, Sprite>(GObjects.class);
map.put(GObjects.PlaneR, new Sprite(txtMidRes, 0, 0, 600, 278));
map.put(GObjects.PlaneG, new Sprite(txtMidRes, 0, 278, 600, 278));
map.put(GObjects.PlaneB, new Sprite(txtMidRes, 0, 556, 600, 104));
map.put(GObjects.Grass, new Sprite(txtMidRes, 0, 834, 600, 104));
map.put(GObjects.Bullet, new Sprite(txtMidRes, 600, 834, 299, 138));
all.put(Resolutions.MidRes, map);
break;
}
}
There is a function I get a sprite from the current resolutions:
private boolean setCurrentResolutions(Resolutions resolution) {
if (resolution == null)
return false;
currentResolution = resolution;
currentResolutionSprites = all.get(Resolutions.MidRes);
return true;
}
private Sprite requestSprite(GObjects gObject) {
return currentResolutionSprites.get(gObject);
}
I am testing the Winows build, whichs size is 800x480. The origin of the sprite I want to draw is: X 300.0 Y 139.0 The width is 300, height is 140. Next the render function:
public void render() {
camera.update();
spriteBatch.setProjectionMatrix(camera.combined);
Update();
spriteBatch.begin();
Draw();
spriteBatch.end();
}
As this is not really helping, here the Draw method:
public void Draw() {
s.draw(spriteBatch);
}
What I see is just black. I tried with OpenGL 1,1.1,2 and various solution from StackOverflow
Finally the "create" method:
// On creation
public void create() {
curH = Gdx.graphics.getHeight();
curW = Gdx.graphics.getWidth();
camera = new OrthographicCamera(curW, curH);
spriteBatch = new SpriteBatch();
// Load texture
txtMidRes = new Texture(Gdx.files.internal("planes.png"));
txtMidRes.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
// Create Sprites for EVERY supported resolution
putTextures(Resolutions.MidRes);
// Set resolution according to H/W
setCurrentResolutions(Resolutions.MidRes);
s = requestSprite(GObjects.PlaneR);
s.setSize(300, 140);
s.setPosition(Gdx.graphics.getWidth() / 2 - s.getWidth() / 2,
Gdx.graphics.getHeight() / 2 - s.getHeight() / 2);
}
Thanks for helping
Florian
First of all, the link you provided to the full source doesn't match the snippets there. Maybe there is a forgotten push ?
Now some ideas on your issue :
You haven't set the camera position. Try :
camera.position.set(curH / 2f, curW / 2f, 0);
You don't need to build your sprites manually, libgdx provides features to do that. See Texture packer on the official doc.
You can also handle resolutions automatically with AssetManager . See this post for more information.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.