Monday, September 16, 2013

LibGDX Tutorial 1: Hello World using the default Font

Installing LibGDX
  1. Java Development Kit, from Oracle site
  2. ADT Bundle, from Android Developer site, unzip it (includes eclipse)
  3. Open up Eclipse from the eclipse folder.
  4. Go to Help menu and Install New Software...
  5. In Work with:, enter site: http://dist.springsource.com/release/TOOLS/gradle
  6. Download Eclipse Integration Gradle.
  7. From http://libgdx.badlogicgames.com/download.html, download setup app (a jar file).
The setup app is run (java -jar gdx-setup.jar). Select the options.



After clicking on Generate and getting the successful message, these are the contents:


Now I can open eclipse included in the ADT bundle, and select File -> Import Grade > Import Gradle Project and select the files:



Running the file right now, right clicking on the desktop package and running as Java application, we get:


This is the default java file displaying a Texture:

package com.tutorial.helloGame;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class HellloGame extends ApplicationAdapter {
 SpriteBatch batch;
 Texture img;
 
 @Override
 public void create () {
  batch = new SpriteBatch();
  img = new Texture("badlogic.jpg");
 }

 @Override
 public void render () {
  Gdx.gl.glClearColor(1, 0, 0, 1);
  Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
  batch.begin();
  batch.draw(img, 0, 0);
  batch.end();
 }
}

This is the HelloWorld program using the BitmapFont:

package com.tutorial.helloGame;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class HellloGame extends ApplicationAdapter {
 OrthographicCamera camera;
 SpriteBatch batch;
 BitmapFont font;
 float w,h;
 
 @Override
 public void create () {
  w = Gdx.graphics.getWidth(); // width of screen
         h = Gdx.graphics.getHeight(); // height of screen
         camera = new OrthographicCamera(); // 2D camera
         camera.setToOrtho(false, w, h); // y increases upwards, viewport = window
         batch = new SpriteBatch(); // batch drawing
         font = new BitmapFont(); // no parameters - thus default 15-pt Arial
         font.setColor(0.0f, 0.0f, 1.0f, 1.0f); // blue font
         font.setScale(5); // font scaled up
 }
  
 @Override
 public void render () {
    Gdx.gl.glClearColor(1, 1, 0, 1); // Clear color is yellow
         Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // Clear buffer with Clear color
         batch.setProjectionMatrix(camera.combined); // Set Projection Matrix
         batch.begin(); // begin drawing
         font.draw(batch, "Hello World", w/2-180, h/2+50); // Draw the Hello World text
         batch.end(); // end drawing
 }
 
 @Override
     public void dispose() {
  batch.dispose(); // remove batch when app ending
  font.dispose(); // remove font when app ending
 }
}
 
Result:
 
 


1 comment:

  1. Very useful, thanks a lot. A little offer is that give explanation of your code.
    For example; "OrthographicCamera is to be used in 2D environments only as it implements a parallel (orthographic) projection and there will be no scale factor for the final image regardless where the objects are placed in the world."
    I took this definition on LibGDX github wiki :)

    ReplyDelete