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

updating android studio to 0.8.4 on Mac

While updating Android Studio 0.8.4 on Mac, you might get an error message to remove the sdk out of application.

To resolve the issue, have android sdk copied to some place else and delete the sdk inside the android studio.

cd /Android\ Studio.app 
ls
rm -rf sdk

When launching android studio after update, just point the android studio to location where you have copied the sdk.

Friday, November 7, 2014

Generic return type

Java function that accept a type param  and return a value of this type so  don't have to cast the return type

For Known String type in  and out:

public String stringReturnFunc(String out)
{                                                                
         return out;                                        
}                                                               

The compiler doesn't know anything about the type

public <T> T genricMT(T type)
{
  return type;
}
 If return type is of certain class-CertainClass
public <T extends CertainClass> T genricMT(T type){
  // now you can use YourType methods
  return type;
}

Generic Types in Java

generic type is a generic class or interface that is parameterized over types.


generic class is defined with the following format:
class name { /* ... */ }


The most commonly used type parameter names are:
  • E - Element (used extensively by the Java Collections Framework)
  • K - Key
  • N - Number
  • T - Type
  • V - Value
  • S,U,V etc. - 2nd, 3rd, 4th types

The Diamond:
Replace the type arguments required to invoke the constructor of a generic class with an empty set of type arguments (<>) as long as the compiler can determine, or infer, the type arguments from the context. This pair of angle brackets, <>, is informally called the diamond. For example, you can create an instance of Box with the following statement:
Box integerBox = new Box<>();

Multiple Types Parameter:

public interface Pair {
    public K getKey();
    public V getValue();
}

public class OrderedPair implements Pair {

    private K key;
    private V value;

    public OrderedPair(K key, V value) {
 this.key = key;
 this.value = value;
    }

    public K getKey() { return key; }
    public V getValue() { return value; }
}
The following statements create two instantiations of the OrderedPair class:
Pair p1 = new OrderedPair("Even", 8);
Pair  p2 = new OrderedPair("hello", "world");
The code, new OrderedPair, instantiates K as a String and V as an Integer. Therefore, the parameter types of OrderedPair's constructor are String and Integer, respectively. Due to autoboxing, it is valid to pass a String and an int to the class.
As mentioned in The Diamond, because a Java compiler can infer the K and V types from the declaration OrderedPair, these statements can be shortened using diamond notation:
OrderedPair p1 = new OrderedPair<>("Even", 8);
OrderedPair  p2 = new OrderedPair<>("hello", "world");

Parameterized Types

You can also substitute a type parameter (i.e., K or V) with a parameterized type (i.e., List). For example, using the OrderedPair example:
OrderedPairBox






More details at:
http://docs.oracle.com/javase/tutorial/java/generics/types.html




Tuesday, November 4, 2014

Delete the git branch

Git: Delete a branch (local or remote)

To delete a local branch

git branch -d the_local_branch
To remove a remote branch (if you know what you are doing!)

git push origin :the_remote_branch