Wednesday, 24 July 2013

There are many game engines available for mobile platforms like:-

1 AndEngine.(2D Only)
2 Cocos2d.(2D Only)
3 Min3d.(3D)
4.jMonkey(3D)
..etc

But most programmers prefer libgdx due to following advantages:-
1. Open source.
2. Supports 2D and 3D(3D is still Updating with new features).
3. Free since it is open source.
4. Many sample tutorials available on home site of libgdx.
5. Supports various platforms(Android/ios/Html5/Desktop).



So here we will learn to simply configure our libgdx project and write our first libgdx program.

*Assuming you have some basic knowledge of programming before.
(SYSTEM SETUP)
Steps involved :-

1 Install java development toolkit (jdk). here.
2 Download and extract Android SDK (ADT Bundle) from here.
3 Download libgdx library (you can select any one from release build or nightly build ).
*what is difference between both libraries?
 :- 
There is nothing much difference you can choose any - Release build is updated only when considerable changes are done while nightly build is updated almost daily.
4 Unzip the contents of library in some directory (say:- c:\game_engine\libgdx).

(PROJECT SETUP)
(Main base project can be used in android,desktop,html5 project)
1. Open Eclipse come with ADT Bundle.
2. Create a new java project.(name: mylibgdx).
3. Create a package in src folder(com.suheb.libgdx_example).
4. Create a class in this package (mylibgdx_class).
5. Now we have to import libgdx libraries.
    1> Right click on project folder and go to build path->configure build path.
    2> Select "Add External JARs".
    3> Browse to location where you have extracted libgdx libraries (say:- c:\game_engine\libgdx). 
         and select these jars:-
                                           1.gdx.jar
                                           2.gdx-natives.jar
                                           3.gdx-backend-lwjgl.jar
                                           4.gdx-backend-lwjgl-natives.jar
   click ok.

6. Now open our class (mylibgdx_class) implements it with ApplicationListener.
7. Press ctrl+shift+o and all unimplemented methods will auto generated.
                                          OR 
    You can type/copy the following code as it is in your class.

//CODE
package com.suheb.libgdx_example;

import com.badlogic.gdx.ApplicationListener;

public class mylibgdx_class implements ApplicationListener{

@Override
public void create() {
// TODO Auto-generated method stub
}

@Override
public void dispose() {
// TODO Auto-generated method stub
}

@Override
public void pause() {
// TODO Auto-generated method stub
}

@Override
public void render() {
// TODO Auto-generated method stub
}

@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
}

@Override
public void resume() {
// TODO Auto-generated method stub
}

}

I am not describing whole code skeleton right now
Just a quick description....
  1. create method :- Called when project executes and its instance is created mostly all initializations  and variables goes here.
  2. dispose :- when program is going to terminated.releasing of resource goes here.
  3. pause :- when when program is paused or is not in focus all rendering and screen updates should also be paused.
  4. render :- called repeatedly all updation to screen goes here.
  5. re-size :-called when screen size is changed or orientation is changed.
  6. resume :- called after pause is removed.all rendering and updation also resumes.

Desktop project:-

  1. Create a new Java project in Eclipse.
  2. Project name-mylibgdx_desktop.
  3. Right click project folder and create a new folder named "libs". copy following jars in that folder
    1. gdx-natives.jar
    2. gdx-backend-lwjgl.jar
    3. gdx-backend-lwjgl-natives.jar
  4. In Eclipse, right click the project -> Refresh. Right click again -> Properties -> Java Build Path -> Libraries -> Add JARs, select the three JARs and click OK.
  5. Click the Projects tab, click Add, check the main project and click OK.

Android project:-

  1. Create a new Android project in Eclipse.
  2. Project name-mylibgdx_android. 
  3. Build target android 1.5 or more.
  4. Package name - com.suheb.libgdx_example_android then click Next.
  5. Create activity MyLibgdx_Example_Android.
  6.  Right click project folder and create a new folder named "libs". copy following jars in that folder
    1. gdx-backend-android.jar 
    2. gdx.jar
    3. both armeabi and armeabi-v7a folders.
  7. In Eclipse, right click the project -> Refresh. Right click again -> Properties -> Java Build Path -> Libraries -> Add JARs, select gdx-backend-android.jar and gdx.jar click OK.
  8. Click the Projects tab, click Add, select the mylibgdx project and click OK twice.
  9. Click the Order and Export tab, check the mylibgdx project.

Run project on desktop

Right click the desktop project -> New -> Class. Name it Desktop_Sample and specify a package (eg, "com.suheb.desktop_example"). Use following code:
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
public class Desktop_Sample{
        public static void main (String[] args) {
                new LwjglApplication(new mylibgdx_class(), "My_Libgdx_Example", 480, 320, false);
        }
}
This code creates an LwjglApplication, gives it an instance of your class, a title, and size. The "false" means we aren't using OpenGL ES 2.0 (we will use 1.0/1.1).
To run the game on the desktop, right click the project -> Debug As -> Java Application. You should get a black window titled "My_Libgdx_Example".

Run project on Android

Open the MyLibgdx_Example_Android class in the Android project that was automatically created and make it look like this:
import com.badlogic.gdx.backends.android.AndroidApplication;
public class MyLibgdx_Example_Android extends AndroidApplication {
        public void onCreate (android.os.Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                initialize(new Game(), false);
        }
}

8 comments:

  1. I wanted to thank you for this excellent read!! I definitely loved every little bit of it. I have bookmarked your web site to check out the latest stuff you post.
    Mobile Application Development Company

    ReplyDelete
  2. This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post.

    small business outsourcing

    ReplyDelete
  3. Thanks for sharing your view on this.I am glad it helps you.

    ReplyDelete
  4. Excellent information, thanks for sharing great information. We deal with Android Game Development this information is quite unique and interesting.

    ReplyDelete
  5. The article posted was very informative and useful. You people are doing a great job. Keep going.

    Web Design In Houston

    ReplyDelete
  6. Today people like Smartphone due to its most amazing features and unique applications. There are number of mobile game available, some of games are free as others are paid. Among all Smartphone Android OS based phone is most favorite device in all over the world. Because of increasing use of Android phones. Android Game Development is becoming fastest growing field in app development market.

    ReplyDelete
  7. One of the best article to getting more knowledge information, thanks for shared and enjoyed to reading this post.

    game app developer

    ReplyDelete