Starting Game Development in Android with Libgdx

This post talks about the basics of game development in Android. In this post we would be assuming that you have eclipse and ADT plugin installed. Checkout the official link to see how.

For setting up libgdx a video tutorial is available in the developers’ site along with written instructions. You can download the library from their google code page.

Video tutorial Link:

Written Instructions are here.

They also have really good tutorials in their wiki.

It takes 5-10 minutes to setup libgdx and it saves lots of time later while making the game.

Basic Game Structure:

Usually the game has roughly 4 parts in the code -

  • Initialize
  • Update
  • Render
  • Dispose

Initialize:

First part is to load all the content and initialize all the values of the game models and logic. We load all the textures here.

Update:

This is the part where we update all the objects’ state and logic state depending on their interactions and state of the game. Either you can pass the current time or the change in the time since the last frame (which on inverse gives Frames Per Second). In Libgdx we get the delta time between frames and update the movement by using it. Make sure while calculating motion of objects that you don’t forget to multiply velocity/acceleration by time delta to get the change in position/velocity, else objects will move fast/slow on faster/slower processors. So in a nutshell, the game state, AI, Physics all get updated here.

Render:

This part renders what is present in the viewport (size of the viewing region of the game). There are many ways to determine if an object should be drawn in the viewport but we will discuss that later.

Dispose:

When the game finishes you free all the memory allocated to textures and unload them.

Apart from these there are two extra features which are quite useful for most of the games.

Restarting a Game:

This part usually depends on the kind of game we have but if, after the player dies or level ends, and textures do not need to be unloaded, then we can just reset the objects to a new state, whether it is for starting a new level or restarting the current.

Popup Menus:

This is also an optional part but its nice to show the player the result of the game and the options to play again/exit. Also, an option of pausing the game in between makes a lot of difference to the player’s experience. This part of game development is usually not that interesting (at least for us) but its these small details that can maybe make or break your game. What could be worse than all your effort in the game going to waste because of not implementing some menus (even though it is tedious work )?

This is the structure of the gameplay screen of the game.  This is obviously not fixed and you can always try something different, but keeping a structure helps in later stages when the code size increases and you start forgetting names of your functions.

Libgdx

This page explains the application lifecyle in libgdx. We just give a brief description here:

Implement the interface “ApplicationListener” in the game class which gives Six functions:

create: Put the initialize code in here

render: We usually don’t have an explicit update function in libgdx’s interface so we call both update and render functions of our game here.

resize: In Android phones resize happens when the orientation of phone is changed, e.g. POTRAIT to LANDSCAPE and vice versa so this function is called when the orientation changes.

pause: In Android whenever the application loses focus pause is called. It maybe either closing down of the application or a new application opening in front of it.

resume: In Android when an application gains focus after pause.

dispose: This is called when we want to close the application and dispose of all the allocated memory.

Desktop Application Code:

public class GameNameDesktop {
        public static void main (String[] argv) {
                new JoglApplication(new Game(), "My Game Name", 480, 320, false);              
        }
}

In this code line - new JoglApplication(new GameClass(), “My Game Name”, 480, 320, false); , the first parameter is the name of the Game class which implements the ApplicaionListener we mentioned above. The second parameter is The title which appears in the window of the game. Third and fourth are Width and Height of the window. Fifth one specifies if we want to use OpenGLES 2.0. For now we will use GLES 1.0/1.1.

Android Application Code:

public class AndroidGame extends AndroidApplication {
        public void onCreate (android.os.Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                initialize(new Game(), false);
        }
}

onCreate is the function called when application is started . In initialize(new Game(),false) first parameter is the Game class which implements the ApplicaionListener and second specifies if we want to use OpenGLES 2.0. For now we will use GLES 1.0/1.1.

Thats all for today. This was a very basic introduction to game development using libgdx. Most of the information of this post came from their page, where you would get more detailed description of the functions in the library. In the next series of tutorials we would go about making a game from start to finish.

Let us know any of your doubts/suggestions; please put them in the comments section and we would try to answer them.

Thank you for the patience.

Tagged with: , , ,
Posted in Tutorials
10 comments on “Starting Game Development in Android with Libgdx
  1. wood says:

    bookmarked your site. Very good tutorial. Hoping to see the whole series.

    Thanks!

  2. godslave says:

    yah, very simple and good turorial.
    thanks

  3. at around 10:35 in the video when he types gdx,a dialog box appears telling the available methods in gl interface…how to bring up that dialog box?

  4. pawanpp says:

    Wat a thinking series

  5. Vishal Kumar says:

    Know India is an Educational App that helps you know about India. This is my first game in LibGDX using Scene2D …… plz download and give ur reviews….

    https://play.google.com/store/apps/details?id=com.jaansi.knowindia

  6. LOLN00b says:

    I stated to get everything setup and i dont have the backend-jogl.jar files. Help i have sept 18 2013 nightly build.

  7. LOLN00b says:

    there are only jglfw and lwjgl .jars please help

  8. Vikas Nigam says:

    gr8 tutorials.. I was looking to get into indie gaming multiplatforms and your articles help a lot for a newbie like me.. I’m not a newbie to coding but newbie for mobile development.. Again thanks a lot!! :)

Try our games



Error: Twitter did not respond. Please wait a few minutes and refresh this page.