Friday 22 February 2013

Android Game Development tutorial [ Part 4 ]

Hello Friends,
            Welcome you all in the Android Game Development tutorial [part 4].

Let’s take a quick review of what we have learnt in the part3.
1)    How to setup the ADT (Android Development Tool)
2)    We have created our first Android Application.
3)    We have learnt how to run the application on Android Emulator.
4)    We have learnt how to port the android application on an Android Device.

Let’s have a look what we are going to learn in this part.
1)    Directory structure of the android application.
2)    AndroidManifest.xml file
3)    Setup the Game Architecture.

Let’s start with the Directory Structure of an Android Application.


Directory Structure of an Android Application:-

Android project are get build into the .apk file. You can install this file onto your Android Device.
The following directories and files comprise android project:-
·        src/
It contains your source code.
·        bin/
This is the output directory of build. Here you can find all the compiled files and .apk file.
·        jni/
It contains the native code developed using the Android NDK.
·        gen/
It contains the java file generated by the ADT such as R.java. The R.java file assigns a numerical value to the each resource what we use in our project.
·        assets/
This is an empty directory. We can use this directory to store the files which we need in our game development.
·        res/
It contains the application resources such as layout files, string values etc.
·        libs/
It contains the various dependent libraries required by the application.


AndroidManifest.xml :-

         This is the very important file of the Android Application.
Every application must have an AndroidManifest.xml file.
This file contains the essential information about the application like version number of the application, minimum SDK version, Main Activity, permissions etc.

Main Activity is like a main method of our application.
You can modify all these information provided by the AndroidManifest.xml file also according to your need.

The AndroidManifest.xml file looks like as follows :-



The AndroidManifest.xml file xml version look like as follows :-



Setup the Game Architecture :-
            There are the following steps you have to follow :-

            1)    Create new Android Application Project.
File à New à Android Application Project.
Give the project name PocoGame, package name com.pocogame.gamecode and click next.

  2)    Unchecked the create custom launcher icon and create activity. We are going to create it later manually.


3)    Right click on the src directory  à New à package.
Give the package name,  com.pocogame.framework

You can give any name whatever you would like to give.
4)    Right Click on the package directory com.pocogame.framework  à New à Interface.
Give the interface name Audio.java

Like that you have to create the following interface class.
a.     Audio.java
b.     FileIO.java
c.      Game.java
d.     Graphics.java
e.     Image.java
f.       Input.java
g.     Music.java
h.     Pool.java
i.        Sound.java

5)    Right Click on the package directory com.pocogame.framework  à New à Class.
Create an abstract class, give Screen.java to this class.


         6)    Copy paste the code into the respective java file.
         
/********************************************************/
          a.     Audio.java 
 /*********************************************************/
package com.pocogame.framework;

public interface Audio {
    public Music createMusic(String file);

    public Sound createSound(String file);
}


    /********************************************************/
          b.     FileIO.java
/********************************************************/
package com.pocogame.framework;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.SharedPreferences;

public interface FileIO {
    public InputStream readFile(String file) throws IOException;

    public OutputStream writeFile(String file) throws IOException;
    
    public InputStream readAsset(String file) throws IOException;
    
    public SharedPreferences getSharedPref();
}


/********************************************************/
          c.      Game.java
/********************************************************/
package com.pocogame.framework;

public interface Game {

    public Audio getAudio();

    public Input getInput();

    public FileIO getFileIO();

    public Graphics getGraphics();

    public void setScreen(Screen screen);

    public Screen getCurrentScreen();

    public Screen getInitScreen();
}

/********************************************************/
          d.     Graphics.java
/********************************************************/
package com.pocogame.framework;


import android.graphics.Paint;

public interface Graphics {
 public static enum ImageFormat {
  ARGB8888, ARGB4444, RGB565
 }

 public Image newImage(String fileName, ImageFormat format);

 public void clearScreen(int color);

 public void drawLine(int x, int y, int x2, int y2, int color);

 public void drawRect(int x, int y, int width, int height, int color);

 public void drawImage(Image image, int x, int y, int srcX, int srcY,
   int srcWidth, int srcHeight);

 public void drawImage(Image Image, int x, int y);

 void drawString(String text, int x, int y, Paint paint);

 public int getWidth();

 public int getHeight();

 public void drawARGB(int i, int j, int k, int l);

}

          e.     Image.java
/********************************************************/
package com.pocogame.framework;

import com.kilobolt.framework.Graphics.ImageFormat;

public interface Image {
    public int getWidth();
    public int getHeight();
    public ImageFormat getFormat();
    public void dispose();
}




/********************************************************/
          f.       Input.java
/********************************************************/
package com.pocogame.framework;

import java.util.List;

public interface Input {
    
    public static class TouchEvent {
        public static final int TOUCH_DOWN = 0;
        public static final int TOUCH_UP = 1;
        public static final int TOUCH_DRAGGED = 2;
        public static final int TOUCH_HOLD = 3;

        public int type;
        public int x, y;
        public int pointer;


    }

    public boolean isTouchDown(int pointer);

    public int getTouchX(int pointer);

    public int getTouchY(int pointer);

    public List getTouchEvents();
}
 

/********************************************************/
          g.     Music.java
/********************************************************/
package com.pocogame.framework;

public interface Music {
    public void play();

    public void stop();

    public void pause();

    public void setLooping(boolean looping);

    public void setVolume(float volume);

    public boolean isPlaying();

    public boolean isStopped();

    public boolean isLooping();

    public void dispose();

    void seekBegin();
}


/********************************************************/
          h.     Pool.java
/********************************************************/
package com.pocogame.framework;

import java.util.ArrayList;
import java.util.List;

public class Pool {
    public interface PoolObjectFactory {
        public T createObject();
    }

    private final List freeObjects;
    private final PoolObjectFactory factory;
    private final int maxSize;

    public Pool(PoolObjectFactory factory, int maxSize) {
        this.factory = factory;
        this.maxSize = maxSize;
        this.freeObjects = new ArrayList(maxSize);
    }

    public T newObject() {
        T object = null;

        if (freeObjects.size() == 0)
            object = factory.createObject();
        else
            object = freeObjects.remove(freeObjects.size() - 1);

        return object;
    }

    public void free(T object) {
        if (freeObjects.size() < maxSize)
            freeObjects.add(object);
    }
}
//

/********************************************************/
          i.        Sound.java
/********************************************************/
package com.pocogame.framework;

public interface Sound {
    public void play(float volume);

    public void dispose();
}


/********************************************************/
          j.   Screen.java
/********************************************************/
package com.pocogame.framework;

public abstract class Screen {
    protected final Game game;

    public Screen(Game game) {
        this.game = game;
    }

    public abstract void update(float deltaTime);

    public abstract void paint(float deltaTime);

    public abstract void pause();

    public abstract void resume();

    public abstract void dispose();
    
 public abstract void backButton();
}
           
In the next part we are going to create java classes that will use these interfaces. 

Stay tuned ....

5 comments:

  1. Hi,,

    Where come from T object in Pool.java?
    My project indicates error message..

    Can you help me to resolve it?

    Thanks. :)

    ReplyDelete
  2. This is a way of creating a Generic Type Class.

    Example as follows :-
    public class Box {
    // T stands for "Type"
    private T t;

    public void set(T t) { this.t = t; }
    public T get() { return t; }
    }

    Can you tell me what type of error message you are getting?

    ReplyDelete
  3. I got it +Habibie what you are pointing in Pool.java class.

    replace t with T. Your code will work fine.
    I don't why small t is coming here....

    ReplyDelete
  4. Can you tell me how to install android programming tool in windows and steps to start from basic?

    ReplyDelete
    Replies
    1. Yeah Sunni, please read the Android Game Development Tutorial Part1. Part1 tells you every thing about how to install ADT(Android Development Tool) and Part2 will tell how to develop and test your first android application.

      Please let me know, if you face any issue ...

      Thanks and Happy learning

      Delete