2

I've tried to get my text to appear, but I cannot get it on screen. I've played with the numbers a few times and found out that LibGDX drew the font to abnormally large sizes, but I don't exactly know if I still need to scale the font size smaller, or if I'm drawing them offscreen. I've copied the entire code and pasted it below.

I used Hiero to make a 256x256 bitmapfont of ariel black on white background.

package com.me.manners;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.me.input.Input;
public class Choice implements Screen
{
private PerspectiveCamera camera;
private SpriteBatch batch;
private Texture arrowT;
private Sprite arrowS;
private BitmapFont font;
private String str1 = "Hello World!";
private Input input = new Input();
private Manners game;

public Choice(Manners game)
{
    this.game = game;
    camera = new PerspectiveCamera();
    batch = new SpriteBatch();
    font = new BitmapFont(Gdx.files.internal("data/choice/ariel.fnt"), Gdx.files.internal("data/choice/ariel.png"),false);
    Gdx.input.setInputProcessor(input);

    arrowT = new Texture(Gdx.files.internal("data/choice/arrow.png"));
    arrowT.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    arrowS = new Sprite(new TextureRegion(arrowT, 0, 0, 64, 64));
    arrowS.setSize(0.125f, 0.25f);
    arrowS.setPosition(-0.75f, 0);
}

@Override
public void dispose() {
    batch.dispose();
    arrowT.dispose();
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    arrowS.draw(batch);
    font.setColor(1.0f,1.0f,0.0f,1.0f);
    font.setScale(0.5f,0.5f);
    font.draw(batch, str1, Gdx.graphics.getWidth()*0.5f, Gdx.graphics.getHeight()*0.5f);
    batch.end();
}
}
putty174
  • 305
  • 5
  • 19

2 Answers2

1

I think your game is 2D game, so using camera = new OrthographicCamera(viewportWidth, viewportHeight) can fix your problem.

phucvin
  • 73
  • 5
1

phucvin makes a very good observation. It seems your game is 2d ,so you should use an OrtographicCamera. I would recommend you to use big numbers, because the BitmapFont will render using its correct pixel size (thus thats the reason you saw very big letters).

camera = new OrthographicCamera(480, 320); //for example.

Of course you will need to change the Sprite size to something bigger

arrowS.setSize(100, 20); //for example

Or using a second camera with low viewport values for the sprites.

Community
  • 1
  • 1
Daahrien
  • 10,190
  • 6
  • 39
  • 71