libGDX - A Java based Cross-Platform Game Engine - Introduction

LibGDX is a Java Based Cross-Platform game engine. One code base can be used to generate apps for Android, iOS, HTML and Desktop games 

I've published 3 simple apps to the play store now. All of which had simple interfaces. So none of them were really mouth gaping to look at and interact with. For my next app challenge I want to tackle something more meaty. Now since I tend to lean towards Java development, I've been looking for a Java based game engine, and fortunately, I've found one!

LibGDX has support for 2D physics, which will allow you to use and control objects in your game, apply gravity and also caters for collision detection (when 2 or more objects collide with each other). Now those are just some of the features. I'll be using this engine to create a simple platformer or sidescroller type of game. 

My current dev environment consists of Android Studio 3.0.1 IDE. If you don't have use Android Studio, I strongly suggest you go download it. Next, you'll need to download the libGDX setup jar which we'll be using to generate your project with all the libraries needed to get going.

To execute the setup file, run the below in your command prompt.

 java -jar gdx-setup.jar


Configure the destination for your project and your Android SDK location. Ticket the appropriate sub projects. Now click "Generate". Once the generation has completed, you'll need to import your project into android studio (File -> Open -> build.gradle). The below is what this should look like in Android Studio.


Note, that you'll have a separate Launcher class for Android, Desktop and HTML and that there is a package for core. The Core module is where your main code base will go. Double click on the DesktopLauncher. This will be the easiest to use to test your application. Click the run button. You should get the below issue.

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: badlogic.jpg
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:149)
at com.badlogic.gdx.graphics.TextureData$Factory.loadFromFile(TextureData.java:98)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:92)
at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:88)
at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:16)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: badlogic.jpg (Internal)
at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:222)
at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:146)
... 7 more

Process finished with exit code 0

Note, that the assets for each of your modules (Desktop, HTML, Android) will be stored in the Android assets folder. The non-android modules will need to be made aware of this. On the left of the play button, expand the DesktopLauncher drop down and then click on Edit Configurations. Add the below to the Working Directory.

<YourProjectDirectory>\android\assets


You should now see the below if all goes well! Thanks for following, I'll be adding more content to this tutorial at a later stage.

Comments

Popular posts from this blog

How Context Switching is killing your productivity at work

Integrating Code Syntax on a Blog or Website.