Sunday, November 23, 2014

Starting Android Game with libgdx

I started with libgdx as a game engine to see how easy it to write code with. My initial impression is very good. The libgdx website gives you a jar to download. When you run the following command on the downloaded jar:


java -jar gdx-setup.jar --dir testGame --package --mainClass MyGame --sdkLocation

a UI is presented. I was able to select Android Studio as a my IDE, and was able to generate the project. It generates two modules - android and core. The android module has your application launcher. The core is where your game logic resides. 


Once created, it comes with a default sample code. This displays a spooky bad logic icon on a red background. I was able to run the android module on emulator to see the result. 

The following code was auto-generated by the jar. Once, you have this code base, you can use it to further extend your game logic.



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 MyGdxGame 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();
 }
}

No comments:

Post a Comment