Monday, September 16, 2013

LibGDX Tutorial 2: Using Image for Hello World text

I used vector graphics program Inkspace to create image for HelloWorld text. Now this is the program output:

Setup
 The only program that has to be installed in Java. Get ADT bundle from Android develper site. These only have to be extracted to folder, which will have the Eclipse subfolder and Android Development Kit folder. Always start Eclipse from this Eclipse subfolder.

Next get the file libgdx-nightly-latest.zip from this website:



If you extract that file, you can find the zipped java file gdx-setup-ui.jar. Keep both this file and libgdx-nightly_latest.zip on same folder and then enter the command

This will start the Setup Program:

After clicking Create and entering the three fields on the next page

Next It will generate the files:

Next open Eclipse from the subfolder provided by ADT Bundle. Import from the File Menu, Existing Projects from the root folder where the projects are that were just generated. Check the copy files into Project workspace.


HelloImageWorld.java
package com.tutorials.helloImageWorld;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class HelloImageWorld implements ApplicationListener {
    private OrthographicCamera camera;
    private SpriteBatch batch;
    private Texture texture;
    private float w,h;
   
    @Override
    public void create() {       
        w = Gdx.graphics.getWidth();
        h = Gdx.graphics.getHeight();
        camera = new OrthographicCamera();
        camera.setToOrtho(false,w,h);
        batch = new SpriteBatch();
        texture = new Texture(Gdx.files.internal("HelloWorldImage.png")); /** Image loaded into texture **/
    }

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

    @Override
    public void render() {       
        Gdx.gl.glClearColor(1, 1, 0, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.draw(texture, w/2-texture.getWidth()/2, h/2); /** Center text in x and y **/
        batch.end();
    }

    @Override
    public void resize(int width, int height) {
    }

    @Override
    public void pause() {
    }

    @Override
    public void resume() {
    }
}

No comments:

Post a Comment