LIBGDX Healthbar not showing up - java

I'm making a game as a small project.
I want to add 2 healthbars(2 players one for each player)
I got an hud where I want to put a healthbar now its a number, which is doll. In the picture below the black box is the HUD.
I found this on here on the site but doesnt work.
SpriteBatch batch;
Texture texture, texture2;
#Override
public void create() {
batch = new SpriteBatch();
initTestObjects();
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 0);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(texture, 10, 10, 3000, 20);
batch.draw(texture2, 10, 10, 2500, 20);
//batch.end();
}
private void initTestObjects() {
int width = 1;
int height = 1;
Pixmap pixmap = createProceduralPixmap(width, height, 255, 1, 0);
Pixmap pixmap2 = createProceduralPixmap(width, height, 255, 255, 0);
texture = new Texture(pixmap);
texture2 = new Texture(pixmap2);
}
private Pixmap createProceduralPixmap(int width, int height, int r, int g, int b) {
Pixmap pixmap = new Pixmap(width, height, Format.RGBA8888);
pixmap.setColor(r, g, b, 1);
pixmap.fill();
return pixmap;
}
as you can see https://www.dropbox.com/s/r1qnwruyr2c1k5h/NoHP.PNG?dl=0 there is no hp drawn.
The rest of the code of the class. http://pastebin.com/AqNFarZ1
Thanks in before hand :)

Related

could not add an image in the right place [duplicate]

I have an issue in LibGDX where when i call upon Gdx.input.getY(), it selects a pixel that's on the other side of the application relative to the center of the screen.
public class Main extends ApplicationAdapter {
private SpriteBatch batch;
private Texture img;
private OrthographicCamera camera;
int xPos;
int yPos;
private Vector3 tp = new Vector3();
BitmapFont font;
#Override
public void create () {
batch = new SpriteBatch();
img = new Texture("crosshair.png");
camera = new OrthographicCamera();
camera.setToOrtho(false, 1280, 720);
font = new BitmapFont();
}
#Override
public void render () {
yPos = Gdx.input.getY();
xPos = Gdx.input.getX();
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.unproject(tp.set(xPos, yPos, 0));
batch.begin();
font.draw(batch,xPos + " , " + yPos, Gdx.input.getX() - 25, Gdx.input.getY() - 5);
batch.draw(img, xPos, yPos);
batch.end();
}
#Override
public void dispose () {
batch.dispose();
img.dispose();
}
Subtracting the viewport height with the touch location won't work, because that would be subtracting world coordinates with touch coordinates. (and even for a pixel perfect projection it would be height - 1 - y). Instead use the unproject method to convert touch coordinates to world coordinates.
There are two problems with your code:
You are never setting the batch projection matrix.
Even though you are using the unproject method, you are never using its result.
So instead use the following:
#Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
camera.unproject(tp.set(Gdx.input.getX(), Gdx.input.getY(), 0));
font.draw(batch,tp.x+ " , " + tp.y, tp.x - 25, tp.y - 5);
batch.draw(img, tp.x, tp.y);
batch.end();
}
I would suggest to read the following pages, which describe this and the reasoning behind it in detail:
https://github.com/libgdx/libgdx/wiki/Coordinate-systems
https://xoppa.github.io/blog/pixels/
https://github.com/libgdx/libgdx/wiki/Viewports
It's better to try this
yPos = camera.viewportHeight - Gdx.input.getY();

Set Libgdx FrameBuffer size in world units

I'had like to use a FrameBuffer and to set his size in world units instead of pixel.
When I set the FrameBuffer size in pixel, i have the expected result. But when I use another personal units. The result is messed up.
Here a snippet of the working code:
public class FBOTestLibGDX implements ApplicationListener {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.useGL30 = false;
cfg.width = 640;
cfg.height = 480;
cfg.resizable = false;
new LwjglApplication(new FBOTestLibGDX(), cfg);
}
Texture atlas;
FrameBuffer fbo;
TextureRegion fboRegion;
Matrix4 projection = new Matrix4();
SpriteBatch batch;
OrthographicCamera cam;
#Override
public void create() {
atlas = new Texture(Gdx.files.local("src/test/resources/fboData/testTexture.jpg"));
fbo = new FrameBuffer(Format.RGBA8888, atlas.getWidth(), atlas.getHeight(), false);
fboRegion = new TextureRegion(fbo.getColorBufferTexture(), atlas.getWidth(), atlas.getHeight());
fboRegion.flip(false, true); // FBO uses lower left, TextureRegion uses
projection.setToOrtho2D(0, 0, atlas.getWidth(), atlas.getHeight());
batch = new SpriteBatch();
batch.setProjectionMatrix(projection);
cam = new OrthographicCamera(5, 5);
cam.setToOrtho(false);
renderTextureInFBO();
}
protected void renderTextureInFBO() {
fbo.begin();
Gdx.gl.glClearColor(0f, 0f, 0f, 0f);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.setProjectionMatrix(projection);
batch.draw(atlas, 0, 0);
batch.end();
fbo.end();
}
#Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 0f);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(cam.combined);
batch.begin();
batch.draw(fboRegion, 0, 0, 5, 5);
batch.end();
}
#Override
public void resize(int width, int height) {
cam.setToOrtho(false, 5, 5);
//batch.setProjectionMatrix(cam.combined);
}
#Override
public void pause() {}
#Override
public void resume() {}
#Override
public void dispose() {}
}
Using pixel I obtain this result:
But if I use world units for FrameBuffer like this:
public void create() {
...
fbo = new FrameBuffer(Format.RGBA8888, 5, 5, false);
fboRegion = new TextureRegion(fbo.getColorBufferTexture(), 5, 5);
fboRegion.flip(false, true);
projection.setToOrtho2D(0, 0, 5, 5);
...
}
protected void renderTextureInFBO() {
...
batch.draw(atlas, 0, 0,5,5);
...
}
I obtain a low scaled result:
The question is, it is possible to set the FrameBuffer size in world unit as we do for batch and viewport or it is mandatory to set it in pixel?
As specified in the documentation, a FrameBuffer size have to be defined in pixel. cf:
FrameBuffer specification
FrameBuffer
public FrameBuffer(Pixmap.Format format,
int width,
int height,
boolean hasDepth,
boolean hasStencil)
Creates a new FrameBuffer having the given dimensions and potentially a depth and a stencil buffer attached.
Parameters:
format - the format of the color buffer; according to the OpenGL ES 2.0 spec, only RGB565, RGBA4444 and RGB5_A1 are color-renderable
width - the width of the framebuffer in pixels
height - the height of the framebuffer in pixels
hasDepth - whether to attach a depth buffer
Throws
GdxRuntimeException - in case the FrameBuffer could not be created

Why is my background image stretched out in libGDX?

This is my first try in libGDX and I've not seen an issue like this before, googling didn't help either. What I'm trying to to display a background, Later on I'll make this move, but for me it was a great start to actually display the image. It displays, but it's streched out (See picture below)
And my code is:
private BombArrangement game;
private OrthographicCamera gameCamera;
private Viewport gamePort;
private Texture backGroundTexture;
public PlayScreen(BombArrangement game) {
this.game = game;
gameCamera = new OrthographicCamera();
gamePort = new FitViewport(BombArrangement.V_WIDTH, BombArrangement.V_HEIGHT, gameCamera);
backGroundTexture = new Texture("startbackground.png");
}
#Override
public void show() {
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
game.batch.begin();
game.batch.draw(new TextureRegion(backGroundTexture, 0, 0, BombArrangement.V_WIDTH, BombArrangement.V_HEIGHT), 0, 0);
game.batch.end();
}
#Override
public void resize(int width, int height) {
gamePort.update(width, height);
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void hide() {
}
#Override
public void dispose() {
}
}
I tried several things like textureregions, sprites and more but all of them give this result.
not exactly sure what your want to do but i use this to render my background in my main menu:
//Camera
camera = new OrthographicCamera();
camera.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
//Viewport
ScreenViewport viewport = new ScreenViewport(camera);
//Background
backgroundImage = new Texture(pathToImage);
//Stage
stage = new Stage();
stage.setViewport(viewport);
(this is located in my constructor and camera, backgroundImage and stage are fields in my class)
in render method
(ConfigData holds data of settings applied to the game; DEFAULT_WIDHT and -HEIGHT are just some values I use to initialize the window when not in fullscreen mode; Replace them with your values used in the DesktopLauncher for
config.widht
and
config.height
):
#Override
public void render(float delta) {
//Clear screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.getBatch().begin();
stage.getBatch().draw(backgroundImage, 0, 0, ConfigData.DEFAULT_WIDTH, ConfigData.DEFAULT_HEIGHT);
stage.getBatch().end();
stage.draw();
}
my resize method:
#Override
public void resize(int width, int height) {
camera.setToOrtho(false, width, height);
stage.getViewport().update(width, height);
}
hopes this helps somehow someone because i figured out this by myself and it costed some effort (:
As a response to your last comment:
You are now scaling down the height correctly but the width remains the same. Try to multiply the width by the same amount you scale the height down, so with some alterations to the code you linked in the comment (not tested):
private Texture texture;
private int x1, x2, speed, scaledHeight, scaledWidth;
public StartBackground() {
texture = new Texture("startbackground.png");
x1 = 0;
x2 = texture.getWidth();
speed = 5;
float imageRatio = Gdx.graphics.getHeight() / texture.getHeight();
scaledHeight = (int) (texture.getHeight() * imageRatio);
scaledWidth = (int) (texture.getWidth() * imageRatio);
}
public void updateAndRender(float deltaTime, SpriteBatch spriteBatch) {
x1 -= speed;
x2 -= speed;
// If image is off screen and not visible
if (x1 + texture.getWidth() <= 0) x1 = x2 + texture.getWidth();
if (x2 + texture.getWidth() <= 0) x2 = x1 + texture.getWidth();
// Render
spriteBatch.draw(texture, x1, 0, scaledWidth, scaledHeight);
spriteBatch.draw(texture, x2, 0, scaledWidth, scaledHeight);
}

Libgdx Pixmap drawCircle now drawing

I'm trying to draw a hollow circle on the screen. I got a class the implements Screen and this is it's render method:
#Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(1,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
myGame.batch.setProjectionMatrix(gamecam.combined);
myGame.batch.begin();
Pixmap pixmap = new Pixmap(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), Pixmap.Format.RGBA8888);
pixmap.setColor(Color.BLACK);
pixmap.drawCircle(100, 100, 15);
Texture texture = new Texture(pixmap);
myGame.batch.draw(texture, 0, 0);
myGame.batch.end();
}
But for some reason I get a red screen
A Pixmap stores its data in native heap memory, don't create in render() method. Don't create Texture too in render method.
private Pixmap pixmap;
private Texture texture;
#Override
public void show() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
gamecam = new OrthographicCamera(30, 30 * (h / w));
gamecam.position.set(gamecam.viewportWidth / 2f, gamecam.viewportHeight / 2f, 0);
gamecam.update();
pixmap= getPixmapCircle(10, Color.WHITE,false);
texture=new Texture(pixmap);
texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
}
#Override
public void render() {
Gdx.gl.glClearColor(0,0,0,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
myGame.batch.setProjectionMatrix(gamecam.combined);
myGame.batch.begin();
myGame.batch.draw(texture,100,100);
myGame.batch.end();
}
public static Pixmap getPixmapCircle(int radius, Color color, boolean isFilled) {
Pixmap pixmap=new Pixmap(2*radius+1, 2*radius+1, Pixmap.Format.RGBA8888);
pixmap.setColor(color);
if(isFilled)
pixmap.fillCircle(radius, radius, radius);
else
pixmap.drawCircle(radius, radius, radius);
pixmap.drawLine(radius, radius, 2*radius, radius);
Pixmap.setFilter(Pixmap.Filter.NearestNeighbour);
return pixmap;
}
#Override
public void dispose() {
pixmap.dispose();
texture.dispose();
}
It is mandatory to call dispose() when the pixmap or texture is no longer needed, otherwise memory leaks will result

Libgdx Texture is flickering when being moved

I have an image that I want to move to the right. To do this I increase its x position by 200f*Gdx.graphics.getDeltaTime(); every time render is called. I've read other posts where people had similar problems but were they were not multiplying by delta or Gdx.graphics.getDeltaTime(). Because I obviously do this why is my image flickering/vibrating when its moving???
My code:
public class gm extends Game{
OrthographicCamera cam;
SpriteBatch batch;
Texture img;
float x = 0;
#Override
public void create () {
batch = new SpriteBatch();
img = new Texture("badlogic.jpg");
cam = new OrthographicCamera();
cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
}
#Override
public void render () {
super.render();
//x += 200f*Gdx.graphics.getDeltaTime();
Gdx.gl.glClearColor(1, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
batch.setProjectionMatrix(cam.combined);
batch.begin();
batch.draw(img, x += 100f*Gdx.graphics.getDeltaTime(), 0);
batch.end();
}
}

Categories