Drawing text in LibGDX - java

I am having a hard time drawing text in LibGDX. Every time I run it, i get a black window. I want the text to be white, but I dont see anything. I have tried finding numerous tutorials and vieos, and still cannot get it to work. I do have a vaild fnt and png file in my assets folder / fonts.
package org.alexwebber.frc.stalk;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Main extends ApplicationAdapter
{
public class HelloWorld implements ApplicationListener
{
private SpriteBatch batch;
private BitmapFont font;
#Override
public void create()
{
batch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("fonts/main.fnt"),
Gdx.files.internal("fonts/main.png"), false);
}
#Override
public void dispose()
{
batch.dispose();
font.dispose();
}
#Override
public void render()
{
batch.begin();
font.setColor(255.0f, 255.0f, 255.0f, 255.0f);
font.draw(batch, "Hello World", 25, 160);
batch.end();
}
#Override
public void resize(int width, int height)
{
}
#Override
public void pause()
{
}
#Override
public void resume()
{
}
}
}

Something that might be impacting you is the removal of the screen clearing:
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
font.setColor(Color.WHITE);
font.draw(batch, "Hello world", 25, 160);
batch.end();
}
You can also try passing Color.WHITE as above. If the Android device you're using has an extreme screen resolution you may not be seeing the line of text.
Testing via a Desktop application may be a good idea as well, as you can tweak the screen resolution so the text is a larger size.

Related

IntelliJ can't find "GL10" library for "Hello World" LibGDX Program

I am trying to teach myself libGDX, but the tutorial I was following gave an error when I tried to run it. I don't know enough LibGDX to make an MCVE, so I tried to find a simple "Hello World" program just to see if the problem was in my code or something else.
The problem is, the only "Hello World" program I was able to find can't seem to import a necessary libGDX library.
The library it can't import is com.badlogic.gdx.graphics.GL10. My IDE, IntelliJ-IDEA, says can't resolve symbol 'GL10' when I hover the cursor over it.
How can I make this program run?
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class HelloWorld implements ApplicationListener {
private SpriteBatch batch;
private BitmapFont font;
#Override
public void create() {
batch = new SpriteBatch();
font = new BitmapFont();
font.setColor(Color.RED);
}
#Override
public void dispose() {
batch.dispose();
font.dispose();
}
#Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.begin();
font.draw(batch, "Hello World", 200, 200);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
}
As far as I know, OpenGL ES 1.x support was removed from libgdx so the interfaces GL10 and GL11 are no more. Try replacing GL10 with GL20.
You can find the update notification at OpenGL ES 1.x support removed from libgdx

Libgdx tweening issue

I am creating a game and trying to set up a splash screen.
Whenever I render the sprite that i want to tween to by using the sprite.draw method which looks like this:
#Override
public void render(float delta)
{
Gdx.gl20.glClearColor(0.2F, 0.5F, 1F, 1F);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
tm.update(delta);
cam.update();
sb.setProjectionMatrix(cam.combined);
sb.begin();
Assets.splash_spr_bg.draw(sb);
sb.end();
}
The tweening works great except i can only see 1/4 of the picture on my screen, it is completely out of position.
And whenever I try to use this code in order to render the sprite by using the spritebatch to draw it, which looks like this:
#Override
public void render(float delta)
{
Gdx.gl20.glClearColor(0.2F, 0.5F, 1F, 1F);
Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);
tm.update(delta);
cam.update();
sb.setProjectionMatrix(cam.combined);
sb.begin();
sb.draw(Assets.splash_spr_bg, 0, 0);
sb.end();
}
I can see the background great, great quality, correct size and position and all that. However, the tweening doesn't work at all; nothing happens.
Why does this not work? How can I fix it?
Here is some other code.
Initializion of the TweenHandler class:
package com.heavenapps.jumpdodge.handlers;
import com.badlogic.gdx.graphics.g2d.Sprite;
import aurelienribon.tweenengine.TweenAccessor;
public class TweenHandler implements TweenAccessor<Sprite>
{
public static final int ALPHA = 1;
#Override
public int getValues(Sprite target, int tweenType, float[] returnValues)
{
switch(tweenType)
{
case ALPHA:
returnValues[0] = target.getColor().a;
return 1;
default:
return 0;
}
}
#Override
public void setValues(Sprite target, int tweenType, float[] newValues)
{
switch(tweenType)
{
case ALPHA:
target.setColor(1, 1, 1, newValues[0]);
break;
}
}
}
Initializion of the sprite/texture:
package com.heavenapps.jumpdodge.handlers;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
public class Assets
{
public static Texture splash_tex_bg;
public static Sprite splash_spr_bg;
public static void init()
{
// Splash Screen
splash_tex_bg = new Texture(Gdx.files.internal("Splash Screen/Background.png"));
splash_tex_bg.setFilter(TextureFilter.Linear, TextureFilter.Linear);
splash_spr_bg = new Sprite(splash_tex_bg);
splash_spr_bg.setOrigin(splash_spr_bg.getWidth() / 2, splash_spr_bg.getHeight() / 2);
splash_spr_bg.setPosition(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
splash_spr_bg.setColor(1, 1, 1, 0);
}
}
Usage of the tweening:
public void fadeSplashScreen()
{
Tween.to(Assets.splash_spr_bg, TweenHandler.ALPHA, 2F).target(1).ease(TweenEquations.easeInBounce).start(tm);
}
You need to set the sprite's position if you want to draw it using your first method. (sprite.draw(spriteBatch)). And you need to use your first method if you want to use the sprite's color, which is being controlled by the tween.
It looks like you did give the background sprite an initial position, but you put it off screen. So change
splash_spr_bg.setPosition(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
to
splash_spr_bg.setPosition(0, 0);
The reason the second method you showed (spriteBatch.draw(sprite, x, y)) draws it in the correct position is that this method of drawing ignores the fact that it's a sprite and just draws the texture region owned by the sprite in whatever position you give it. And this method doesn't fade the sprite because it's ignoring the fact that it is a sprite (which has a color).

Asynchronously Loading Screen Libgdx

I store all of my assets in a class and load them at the start of my game, I am trying to create a loading screen in a separate class.
Asset manager class:
public class Assets {
public static AssetManager manager = new AssetManager();
public static void queueLoading() {
(..)
manager.load("sound/buttonpress.mp3", Sound.class);
(..)
while(!manager.update())
{
System.out.println("Loaded: " + manager.getProgress() *100 + "%");
}
}
public static boolean update() {
return manager.update();
}
}
Loading screen class:
public class LoadingScreen implements Screen{
final Game1 game;
Sprite LdScreen;
OrthographicCamera camera;
public LoadingScreen(Game1 gam){
game=gam;
camera = new OrthographicCamera();
camera.setToOrtho(false, 1920, 1080);
}
public void show() {
Texture LdscreenTexture = new Texture(Gdx.files.internal("data/Background.png"));
LdScreen = new Sprite (LdscreenTexture);
Assets.queueLoading();
}
#Override
public void render(float delta) {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
camera.update();
game.batch.setProjectionMatrix(camera.combined);
game.batch.begin();
game.batch.draw(LdScreen, 0,0);
game.batch.end();
// Assets.queueLoading();
if(Assets.update()){
game.setScreen(new MainMenuScreen(game));
System.out.print("hllasgsgsag");
}
Assets.update();
}
#Override
public void resize(int width, int height) {
}
#Override
public void hide() {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
}
}
Problem:
When i run it in the console i get the progress percentage, but the screen stays black whilst it is loading (stuck at Assets class), then it flashes to the loading screen, and changes to MainMenuScreen.
How do I stop the black screen when the assets are loading?
The problem that you are having is because you are loading all the Assets straight away and this isn't done in a different thread and is run whilst the screen is being shown. I would advise using manager.update(delta) which should be called during your game loop and getting the update percentage. The full source would be a bit difficult for me to explain but I have done something similar in my previous projects, the sources are available on GitHub below:
The Assets class:
https://github.com/basimkhajwal/NinjaTower/blob/master/NinjaTower/core/src/net/net63/codearcade/NinjaTower/utils/Assets.java
The Loading Screen:
https://github.com/basimkhajwal/NinjaTower/blob/master/NinjaTower/core/src/net/net63/codearcade/NinjaTower/screens/MainMenuScreen.java

LibGDX - what do the console errors mean and how can I fix this console error?

Heres the console error I'm getting:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.dakotapederson.slingshotsteve.SlingshotSteve.render(SlingshotSteve.java:30)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:206)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
And here is the code:
package com.dakotapederson.slingshotsteve;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
public class SlingshotSteve implements ApplicationListener {
// Creates our 2D images
private SpriteBatch batch;
private TextureRegion backgroundTexture;
private Texture texture;
#Override
public void create() {
Texture texture = new Texture(Gdx.files.internal("background.jpg"));
backgroundTexture = new TextureRegion(texture, 20, 20, 50, 50);
}
#Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
batch.draw(backgroundTexture, 0, 0);
batch.end();
}
#Override
public void resize(int width, int height) {
}
#Override
public void pause() {
}
#Override
public void resume() {
}
#Override
public void dispose() {
batch.dispose();
texture.dispose();
}
}
What do the console errors exactly mean? Because if theirs an error in the code I need to know what it is. Also, since I'm new at this, please describe the error in the code with as much clarity as possible.
You should read basic tutorial: https://github.com/libgdx/libgdx/wiki/A-simple-game
You are missing this line in create():
batch = new SpriteBatch();

Low framerate on simple libgdx example in desktop application

I just started learning libGDX, and was following the example from http://steigert.blogspot.in/2012/02/2-libgdx-tutorial-game-screens.html (the second link on libGdx help page).
As of now, I just display a logo of 512x512. There is nothing else happening in the application but when I run the application in desktop mode,I get a FPS of 15-16. When I remove the image, I get 60fps for the blank screen. For android its even worse, I get 3-4 fps in Galaxy SL - GT-i9003 (Temple Run runs on playable speed on the device).
My laptop plays World of Warcraft without any hiccups in high quality so its baffling that such a small app would only achieve 15fps.
public class SplashScreen extends AbstractScreen {
private Texture splashTexture;
private TextureRegion splashTextureRegion;
public SplashScreen(MyGDXGame game){
super(game);
}
#Override
public void show()
{
super.show();
// load the splash image and create the texture region
splashTexture = new Texture("splash.png");
// we set the linear texture filter to improve the stretching
splashTexture.setFilter( TextureFilter.Linear, TextureFilter.Linear );
// in the image atlas, our splash image begins at (0,0) at the
// upper-left corner and has a dimension of 512x301
splashTextureRegion = new TextureRegion( splashTexture, 0, 0, 512, 382 );
}
#Override
public void render(float delta ) {
super.render( delta );
// we use the SpriteBatch to draw 2D textures (it is defined in our base
// class: AbstractScreen)
batch.begin();
// we tell the batch to draw the region starting at (0,0) of the
// lower-left corner with the size of the screen
batch.draw( splashTextureRegion, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight() );
// the end method does the drawing
batch.end();
}
#Override
public void dispose()
{
super.dispose();
splashTexture.dispose();
}
}
Here is the relevant section of AbstractScreen class:
public class AbstractScreen implements Screen {
protected final MyGDXGame game;
protected final BitmapFont font;
protected final SpriteBatch batch;
public AbstractScreen(MyGDXGame game ){
this.game = game;
this.font = new BitmapFont();
this.batch = new SpriteBatch();
}
#Override
public void render(float delta) {
Gdx.gl.glClearColor( 0f, 0f, 0f, 1f );
Gdx.gl.glClear( GL20.GL_COLOR_BUFFER_BIT );
}
...
}
And the desktop app:
public class Main {
public static void main(String[] args) {
LwjglApplicationConfiguration cfg = new LwjglApplicationConfiguration();
cfg.title = "First Game";
cfg.useGL20 = false;
cfg.width = 800;
cfg.height = 480;
new LwjglApplication(new MyGDXGame(), cfg);
}
MyGDXGame is as follows:
public class MyGDXGame extends Game {
private FPSLogger fpsLogger;
public SplashScreen getSplashScreen() {
return new SplashScreen( this );
}
#Override
public void create() {
fpsLogger = new FPSLogger();
}
#Override
public void dispose() {
super.dispose();
}
#Override
public void render() {
super.render();
setScreen(getSplashScreen());
fpsLogger.log();
}
#Override
public void resize(int width, int height) {
super.resize(width, height);
}
#Override
public void pause() {
super.pause();
}
#Override
public void resume() {
super.resume();
}
I have read so many good things about libGdx, so this issue seems baffling to me. What am I doing wrong to get such a low frame rate?
I think setScreen(getSplashScreen()); in your render() method might be the problem. Move it to the create() method of MyGDXGame.
Right now you are switching the screen in every single frame and recreate a SpriteBatch every single time which is a very heavy object (see my comment to your question).

Categories

Resources