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

Monday, October 20, 2014

Solve Android Studio crash for Yosemite Mac update

The Android Studio will start crashing once you update Mac to Yosemite.

The fix is to point android studio to 1.7* from 1.6*.

Here is what you need to do -

cd to Content to AndroiD studio -

cd /Applications/Android\ Studio.app/Contents

open Info.plist using a text editor like Sublime. (If you have Sublime in Path, you can open Info.plist
by command - Sublime Info.plist )

Update  

JVMVersion
1.6*

to 

JVMVersion
1.7*

Close the editor. Now launch the Android Studio.

Tuesday, September 23, 2014

# KitKat NDK APP_PLATFORM Values : android/asset_manager.h: No such file or directory -

You might get an error android/asset_manager.h: No such file or directory
while making an NDK project.

The APP_PLATFORM has to be updated to android-14, which may fix the problem.


android-3      -> Official Android 1.5 system images
android-4      -> Official Android 1.6 system images
android-5      -> Official Android 2.0 system images
android-6      -> Official Android 2.0.1 system images
android-7      -> Official Android 2.1 system images
android-8      -> Official Android 2.2 system images
android-9      -> Official Android 2.3 system images
android-14     -> Official Android 4.0 system images
Also, make sure that both Application.mk and Android.mk is present in jni folder. 
Supported headers are at $NDK/platforms/android-/arch-arm/usr/include

OPENGL NDK build problem using Android Studio on Mac OS

While building NDK application that consumes OPENGL, may not compile properly.
First make sure you have
ndk.dir= in local.settings
Also, the ndk folder has to be in PATH.
echo $PATH will show whats there in path and what is not.


You may get error like -
error: undefined reference to 'glGetUniformLocation'
error: undefined reference to 'glGetAttribLocation'
error: undefined reference to 'glUseProgram'
error: undefined reference to 'glBindTexture'
error: undefined reference to 'glUniform2f'
error: undefined reference to 'glVertexAttribPointer'
.......

I also saw that the Headers files for OPENGL framework in Mac OS was missing. You can update that using:
xcode-select --install

However, the error will not go.

The solution is:
Deactivate the ndk compilation from gradle by setting in build.gradle of your module. ( the inner one).
Place it inside under android tag:, below the build tool version

sourceSets.main {   
jni.srcDirs = []
    jniLibs.srcDir 'src/main/libs'
}
to be able to call ndk-build yourself and integrate libs from libs/.
Do a ndk-build at app/src/main, where jni folder resides.
The compiled jni code will be in 'src/main/libs'

Then install the app by gradle iD 

Tuesday, August 26, 2014

Code snippet for Setting Alarm using fragements

           //Main Activity

            public static final String FRAGTAG = "RepeatingAlarmFragment";

            if (getSupportFragmentManager().findFragmentByTag(FRAGTAG) == null ) {
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        RepeatingAlarmFragment fragment = new RepeatingAlarmFragment();
                        transaction.add(fragment, FRAGTAG);
                        transaction.commit();
            }



            //RepeatingAlarmFragment - setting intent and alarm

            Intent intent = new Intent(getActivity(), MainActivity.class);
            intent.setAction(Intent.ACTION_MAIN);
            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
  
            PendingIntent pendingIntent = PendingIntent.getActivity(getActivity(), REQUEST_CODE,
                    intent, 0);

            int alarmType = AlarmManager.ELAPSED_REALTIME;
            final int FIFTEEN_SEC_MILLIS = 15000;

            AlarmManager alarmManager = (AlarmManager)
                    getActivity().getSystemService(getActivity().ALARM_SERVICE);

            alarmManager.setRepeating(alarmType, SystemClock.elapsedRealtime() + FIFTEEN_SEC_MILLIS,
                    FIFTEEN_SEC_MILLIS, pendingIntent);

Monday, August 4, 2014

Python code to search array of strings in particular folder

import os

#list of strings you want to find
apis = ["sandeepshabd@gmail.com",
"sandeep.shabd"
]

def find(word):
    def _find(path):
        with open(path, "rb") as fp:
            for n, line in enumerate(fp):
                if word in line:
                   yield word, n+1, line
    return _find

def search(word, start):
    finder = find(word)
    for root, dirs, files in os.walk(start):
        for f in files:
            path = os.path.join(root, f)
            for word, line_number, line  in finder(path):
                yield word, path, line_number, line.strip()

if __name__ == "__main__":
    import sys
 
    result =set()
    #Path of the document folder
    start= "/Users/sandeepshabd/Documents/"
    for api in apis:
        for word, path, line_number, line in search(api, start):
           result.add(word)
 
    print "**************************"
    for filterApi in list(result):
        print filterApi
    print "**************************"


Monday, April 28, 2014

#Android #chromium code base language distribution

Tried to find what all consist of android chrome code base. The one I downloaded has these details