android libgdx font isn't drawing - android

I have got a background picture and want to draw text on it, but it isn't working.
There is only the picture on the screen.
private SpriteBatch spriteBatch;
private Texture splsh;
private BitmapFont font;
public void render(float arg0)
{
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
spriteBatch.begin();
spriteBatch.draw(splsh, 0, 0);
font.draw(spriteBatch, "test", 10, 480);
spriteBatch.end();
}
public void show()
{
spriteBatch = new SpriteBatch();
splsh = new Texture(Gdx.files.internal("bg2.png"));
font = new BitmapFont();
font.setColor(Color.RED);
}

you should scale your font
font.setScale(0.2f);
change size according to your need...
and one more thing try drawing your font in center of screen, that way you can check that whether it is drawing or not

Related

LibGDX Android: nothing to see on loading screen (render method does not draw)

When I launch my application on Android, nothing is being shown on the loading screen, it is just black.
That screen is supposed to be shown while some assets are loading when you start the application.
Everything works fine in the Desktop application.
How to make the loading screen show on Android?
I do the following in show() of the loading screen:
Assets.load(); //assets are being loaded there
cam = new OrthographicCamera();
viewport = new StretchViewport(width, height, cam);
viewport.apply();
cam.position.set(cam.viewportWidth / 2, cam.viewportHeight / 2, 0);
cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.setProjectionMatrix(cam.combined);
sprite = new Sprite(new Texture(texturePath));
sprite.setPosition(100, 200);
I do the following in render() of the loading screen:
Gdx.gl.glClearColor(.17f, .17f, .25f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
batch.setProjectionMatrix(cam.combined);
batch.begin();
sprite.draw(batch);
batch.end();
if(Assets.manager.update())
game.setScreen(nextScreen);
I've found a way to make it work:
make another empty screen show up before the loading screen.
I really don't understand why that works, but I hope this will help someone.
(If you know what part of that made it work, then it would be nice if you help:D)
Part of my empty screen:
public class EmptyScreen implements Screen {
OrthographicCamera cam;
Viewport viewport;
SpriteBatch batch;
MyGame game;
public EmptyScreen(SpriteBatch batch, MyGame game) {
this.game = game;
cam = new OrthographicCamera();
viewport = new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), cam);
viewport.apply();
this.batch = batch;
}
#Override
public void show() {
}
#Override
public void render(float delta) {
game.setScreen(new LoadingScreen(game, batch));
}
Part of my (now working on android as well) loading screen
public class LoadingScreen implements Screen {
MyGame game;
Sprite sprite;
OrthographicCamera cam;
Viewport viewport;
Stage stage;
SpriteBatch batch;
public LoadingScreen(MyGame game, SpriteBatch batch) {
this.game = game;
cam = new OrthographicCamera();
viewport = new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), cam);
viewport.apply();
cam.position.set(cam.viewportWidth / 2, cam.viewportHeight / 2, 0);
this.batch = batch;
batch.setProjectionMatrix(cam.combined);
sprite = new Sprite(new Texture(texturePath));
//you may need to set the position of the sprite here
}
#Override
public void show() {
stage = new Stage(viewport, batch);
Assets.load();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor(.1f, .2f, .25f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(cam.combined);
batch.begin();
sprite.draw(batch);
batch.end();
if(Assets.manager.update())
game.setScreen(nextScreen);
}

Android: Change size of canvas / drawable in custom drawable

I'm trying to implement a custom drawable which should have the shape of a speechbubble. Therefore I use two paths, one draws the rect and the other should draw the triangle for the bubble.
My class looks like the following:
public class SpeechBubbleView extends Drawable {
private Paint mBubblePaint;
private Paint mBubblePaint2;
private Path mRectPath;
private Path mBubblePath;
public SpeechBubbleView() { }
public void initPaint() {
mBubblePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mBubblePaint.setStyle(Paint.Style.FILL);
mBubblePaint.setColor(Color.GREEN);
mBubblePaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
mBubblePaint2.setStyle(Paint.Style.FILL);
mBubblePaint2.setColor(Color.RED);
int width = getBounds().width();
int height = getBounds().height();
mRectPath = new Path();
mRectPath.addRoundRect(new RectF(0, 0, width, height), 8, 8, Path.Direction.CW);
mRectPath.close();
mBubblePath = new Path();
mBubblePath.moveTo(50, height);
mBubblePath.lineTo(100, height + 50);
mBubblePath.lineTo(150, height);
mBubblePath.close();
}
#Override
public void draw(Canvas canvas) {
if(mRectPath == null && mPathValues == null) {
initPaint();
}
canvas.drawPath(mRectPath, mBubblePaint);
canvas.drawPath(mBubblePath, mBubblePaint2);
}
#Override
public void onBoundsChange(Rect bounds) {
Rect customBound = new Rect(0, 0, bounds.width(), bounds.height() + 50);
super.onBoundsChange(customBound);
}
The problem now is, that I take the width and height from the drawable to draw the rect of the speechbubble. The full space of the canvas is taken and there is no more room for the triangle to display below the rect.
My question now is: Is it possible to change the size of canvas or the drawable, so that I am able to display the small triangle below the rect?
I already tried the method onBoundsChange, but it takes no effect. In the draw-method the size is still the same.
If possible, it would be nice to change the size directly in the custom drawable class, shown above, because I do not have the size of the view, when I call it. Also I cannot make the size of the rect smaller, because in the drawable there is content and if the rect is smaller, some of the content will be outside of the drawable. I use a drawable, so that I can simple call setBackgroundDrawable of my layout or TextView and it matches always the content size.
If anyone of you got an idea on how to do the size change, this would be very great. Thank you :D

Dynamic texture creation with font using libgdx

I'm working on a word game and I was dynamically creating the textures for the letter tiles when the game loads, comprising of a background image and a font.
To do this I was drawing pixmaps onto pixmaps, this was all fine until I started working on scaling. The font scaling on the pixmaps was terrible, even with bilinear filtering turned on (left image below) even though my scaled fonts were looking pretty good elsewhere.
So I decided to get round this I'd use a frame buffer, render everything to that and then copy that out to a pixmap and create a texture from that. That way I could use the gpu filtering and it should look exactly the same as my other fonts, (middle image below) but it still didn't look quite as nice as the other fonts. A slight dark line round the outside, it looks like the alpha blending isn't working properly.
I then tried drawing straight over the tiles with the font at runtime to make sure it wasn't my imagination, and this definitely looks better with smooth blending into the image below (right image below), but this impacts my frame rate quite a lot.
So my question is, why is drawing to the frame buffer not producing the same result as when I draw to the screen? Code below.
Texture tx = Assets.loadTexture("bubbles/BubbleBlue.png");
tx.setFilter(TextureFilter.Linear, TextureFilter.Linear);
SpriteBatch sb = new SpriteBatch();
FrameBuffer fb = new FrameBuffer(Format.RGBA8888,
LayoutManager.getWidth(), LayoutManager.getHeight(), false);
fb.begin();
sb.begin();
sb.draw(tx, 0, 0, LetterGrid.blockWidth, LetterGrid.blockHeight);
Assets.candara80.font.getRegion().getTexture()
.setFilter(TextureFilter.Linear, TextureFilter.Linear);
Assets.candara80.setSize(0.15f);
TextBounds textBounds = Assets.candara80.getBounds(letter);
Assets.candara80.drawText(sb, letter,
(LetterGrid.blockWidth - textBounds.width) / 2,
(LetterGrid.blockHeight + textBounds.height) / 2);
sb.end();
Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0,
(int) LetterGrid.blockWidth, (int) LetterGrid.blockHeight);
Pixmap flipped = flipPixmap(pm);
result = new Texture(flipped);
fb.end();
pm.dispose();
flipped.dispose();
tx.dispose();
fb.dispose();
sb.dispose();
set PROJECTION is the problem.
EXAMPLE
public Texture texture(Color fg_color, Color bg_color)
{
Pixmap pm = render( fg_color, bg_color );
texture = new Texture(pm);//***here's your new dynamic texture***
disposables.add(texture);//store the texture
}
//---------------------------
public Pixmap render(Color fg_color, Color bg_color)
{
int width = Gdx.graphics.getWidth();
int height = Gdx.graphics.getHeight();
SpriteBatch spriteBatch = new SpriteBatch();
m_fbo = new FrameBuffer(Format.RGB565, (int)(width * m_fboScaler), (int)(height * m_fboScaler), false);
m_fbo.begin();
Gdx.gl.glClearColor(bg_color.r, bg_color.g, bg_color.b, bg_color.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
/**set PROJECTION**/
Matrix4 normalProjection = new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
spriteBatch.setProjectionMatrix(normalProjection);
spriteBatch.begin();
spriteBatch.setColor(fg_color);
//do some drawing ***here's where you draw your dynamic texture***
...
spriteBatch.end();//finish write to buffer
pm = ScreenUtils.getFrameBufferPixmap(0, 0, (int) width, (int) height);//write frame buffer to Pixmap
m_fbo.end();
// pm.dispose();
// flipped.dispose();
// tx.dispose();
m_fbo.dispose();
m_fbo = null;
spriteBatch.dispose();
// return texture;
return pm;
}

Fullscreen resolution on Android for libgdx games

I was wondering if there was a way to make Android games in libgdx so they all share the same resolution of 480x800. Using Gdx.graphics.setDisplayMode(480, 800, true) doesn't seem to change anything. Creating an OrthographicCamera of 480 by 800 makes it so the game is 480 by 800, but doesn't zoom into fullscreen and take up the entire screen like I expected it would. When I tested it out on my phone, the phone just used blank space to fill up the rest of the screen while the game in 480x800 resolution. Here is the code that I'm using.
public class GameScreen implements Screen {
private Game game;
private OrthographicCamera guiCam;
private SpriteBatch batch;
private Texture texture;
private Rectangle glViewport;
public GameScreen (Game game)
{
this.game = game;
guiCam = new OrthographicCamera(GAME_WIDTH, GAME_HEIGHT);
guiCam.position.set(GAME_WIDTH / 2, GAME_HEIGHT / 2, 0);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/c2.png"));
glViewport = new Rectangle(0, 0, GAME_WIDTH, GAME_HEIGHT);
}
#Override
public void render(float delta) {
if (Gdx.input.justTouched()) {
texture = new Texture(Gdx.files.internal("data/c1.png"));
}
GL10 gl = Gdx.graphics.getGL10();
gl.glClearColor(1, 0, 0, 1);
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glViewport((int) glViewport.x, (int) glViewport.y,
(int) glViewport.width, (int) glViewport.height);
guiCam.update();
guiCam.apply(gl);
batch.setProjectionMatrix(guiCam.combined);
batch.begin();
batch.draw(texture, 0, 0, 0, 0, 142, 192);
batch.end();
}
private static final int GAME_WIDTH = 480;
private static final int GAME_HEIGHT = 800;
}
Thanks in advance.
You've set your glViewport to 480x800, that means you're asking the phone hardware for a 480x800 window to draw on. Because most (all?) phone hardware doesn't do screen scaling (like your desktop monitor does), they just give you a 480x800 area on the screen.
You need to get OpenGL to "zoom" your screen, and you do that by setting the glViewport() to the physical resolution of your device (modulo aspect ratio caveats!). You should leave the camera at the 'virtual' resolution you prefer (so 480x800 in this case). Now OpenGL will scale all your primitives up to the screen's resolution.
As a quick test, try this:
gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
The next problem is that if the hardware aspect ratio doesn't match your 'virtual' aspect ratio. In that case you need to decide between stretching, leaving black bars on one side, or changing your virtual aspect ratio. See this blog post for more details on the camera/viewport setup, and some solutions to the aspect ratio issue: http://blog.acamara.es/2012/02/05/keep-screen-aspect-ratio-with-different-resolutions-using-libgdx/

How do I create an orthographic camera that correctly scales my sprites/textures with libgdx?

I'm creating a game with libgdx that I want to run at a higher resolution on the desktop, but I want it to scale everything down correctly when I run it on android at smaller resolutions. I've read that the best way to do this is to not use a pixel perfect camera, and instead to use world coordinates, but I'm not sure how to correctly do that.
This is the code I have right now:
#Override
public void create() {
characterTexture = new Texture(Gdx.files.internal("character.png"));
characterTextureRegion = new TextureRegion(characterTexture, 0, 0, 100, 150);
batch = new SpriteBatch();
Gdx.gl10.glClearColor(0.4f, 0.6f, 0.9f, 1);
float aspectRatio = (float)Gdx.graphics.getWidth() / (float)Gdx.graphics.getHeight();
camera= new OrthographicCamera(aspectRatio, 1.0f);
}
#Override
public void render() {
GL10 gl = Gdx.graphics.getGL10();
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
camera.update();
camera.apply(gl);
batch.setProjectionMatrix(camera.combined);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
draw();
}
private void draw() {
//batch.getProjectionMatrix().set(camera.combined);
batch.begin();
batch.draw(characterTextureRegion, 0, 0, // the bottom left corner of the box, unrotated
1f, 1f, // the rotation center relative to the bottom left corner of the box
0.390625f, 0.5859375f, // the width and height of the box
1, 1, // the scale on the x- and y-axis
0); // the rotation angle
batch.end();
}
The texture I'm use is 256x256 with the actual image in it being 100x150.
This is the result I get when I run the game: http://i.imgur.com/HV9Bi.png
The sprite that gets rendered is massive, considering this is the original image: http://i.imgur.com/q1cZT.png
What's the best way to go about making it so that the sprites get rendered at their original size while still keeping the ability to have the game scale correctly when played in different resolutions?
I've only found two solutions, both of which I don't like.
The image showed up how it was supposed to if I used pixel coordinates for the camera, but then that didn't scale at all when I put it on my phone with a different resolution.
I can scale the texture region down when I draw it, but it seems like there is a better way because it is extremely tedious trying to figure out the correct number to scale it by.
Have you ever used the Libgdx setup tool? When you create a project with it, it has a sample image that is displayed. It seems to keep it's ratio correct no matter what size you change the screen to.
public class RotationTest implements ApplicationListener {
private OrthographicCamera camera;
private SpriteBatch batch;
private Texture texture;
private Sprite sprite;
Stage stage;
public boolean leonAiming = true;
#Override
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("data/libgdx.png"));
texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
TextureRegion region = new TextureRegion(texture, 0, 0, 512, 275);
sprite = new Sprite(region);
sprite.setSize(0.9f, 0.9f * sprite.getHeight() / sprite.getWidth());
sprite.setOrigin(sprite.getWidth()/2, sprite.getHeight()/2);
sprite.setPosition(-sprite.getWidth()/2, -sprite.getHeight()/2); }....
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
batch.begin();
sprite.draw(batch);
batch.end();
First of all you need to fix boundaries to the world (I mean to your game ). In that world only you actors(game characters) should play. If you are crossing boundaries, manage it with camera like showing up, down, left and right.

Categories

Resources