Sunday, May 26, 2013

Importing/moving a git project into a directory

Recently I worked on a project which obviously used a git repository. Then the project needed to be integrated in a bigger project, as a directory. The simple method was to copy-paste the project to it's final destination, but that would mean loosing all the commits. Fortunately, there is a solution to do this without loosing the commits: filter-branch.

Let's assume that we want to copy/move project A into project B, in directory "a". The idea is simple enough and it involves 2 steps:
  1. Modify the structure of project A in order to have all the content in directory "a"
  2. Import project A in project B
In order to do the first step, we use filter-branch like this:
git clone a a_tmp # work on a copy of project A, just to be safe
cd a_tmp
git filter-branch -f --prune-empty --tree-filter '
mkdir -p .tmp-dir
mv * .tmp-dir
mv .gitignore .tmp-dir/
mv .tmp-dir a
' -- --all 

This command does the following:
- The "-f" switch will force filter-branch to continue, even in the case of an error.
- The "--prune-empty" switch will skip empty commits.
- The "--tree-filter" switch has a shell command as the argument. This command is composed from the following 4 lines.

As can be seen, we create a directory, but the name of this directory starts with a dot. This is done so that mv will not try to move a directory inside itself. The "mv" command skips names that start with a dot (are hidden). This is why we move the .gitignore file explicitly.

We now proceed to step 2, importing this restructured git repository inside of project B.
cd ../b
git remote add subproj_a ../a_tmp
git fetch subproj_a
git merge subproj_a/master
git remote rm subproj_a
git gc --aggressive
These commands add a new remote (named subproj_a) with the path ../a_tmp
When we fetch this remote, we will copy the content of that repository (not that you may receive a warning since there are no common commits).
We merge the 2 projects and delete the remote.
The "git gc" command is used in order to delete unused objects.

Resources:
Moving One Git Repository Into Another

Sunday, February 17, 2013

JSON in Android

In this post I will show you how to get JSON data from a HTTP server and parse it in android.
The JSON data that we will parse in this tutorial is the following:

{
  "status" : "ok",
  "score" : 101,
  "friends" : [{"id" : 321, "score" : 100}, {"id" : 320, "score" : 105}]
}

We will assume that you can access this data from the URL: http://server.com/json

The first step is to get the data in you android application. Since we will download it from a server, you will first need to specify the internet permission in you Android Manifest file:

<uses-permission android:name="android.permission.INTERNET" />

Now that we set this permission, we can download the data. Since we can't use the main thread for networking, we will create a new thread to download our data and parse it. Add the following code in your onCreate method, in your Activity class.

new Thread() {
  public void run() {
    helloJson();
  }
}.start();

Next, we will create the helloJson method, containing the following code:

public void helloJson() {
    StringBuilder builder = new StringBuilder();
    
    try {
      HttpClient client = new DefaultHttpClient();
      HttpGet httpGet = new HttpGet("http://server.com/json");
  
      HttpResponse response = client.execute(httpGet);
      int statusCode = response.getStatusLine().getStatusCode();
      
      if (statusCode == 200) {
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        InputStreamReader reader = new InputStreamReader(content)
        BufferedReader reader = new BufferedReader(reader);
        String line;
        while ((line = reader.readLine()) != null) {
          builder.append(line);
        }
        JSONObject jobj = new JSONObject(builder.toString());
        builder = new StringBuilder(jobj.get("status").toString());
        JSONArray jar = jobj.optJSONArray("friends");
        builder.append(jar.length());
      }
    }
    catch(Exception e) {
      e.printStackTrace();
      builder.append(e);
    }
    
    final String str = builder.toString();
    runOnUiThread(new Runnable() {
      @Override
      public void run() {
        Toast.makeText(MainActivity.this, str, Toast.LENGTH_LONG).show();
      }
    });
  }

This will display a message using Toast, containing the status string and the length of the friends array.

That's it! :)

Wednesday, February 6, 2013

Admob with Andengine

If you have a game developed with Andengine you probably don't use a XML layout file and your activity simply extends the BaseGameActivity class or the SimpleBaseGameActivity class.
I assume that you know hot to add the Admob jar to your project and to set the required permissions in the AndroidManifest file. If you don't, then check the tutorials on the Admob web site first.

The first thing you need to do is create a new XML layout file, let's call it game_layout.xml
The content of this file should be similar to the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <org.andengine.opengl.view.RenderSurfaceView
        android:id="@+id/SurfaceViewId"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:gravity="center" />

    <com.google.ads.AdView
        android:id="@+id/adViewId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="YOU_ID_HERE"
        ads:loadAdOnCreate="true" />
    
</RelativeLayout>

In this case, the Layout is RelativeLayout, because we want the ad to be drawn over the game surface. Based on your requirements, you could try to use a other layouts, but i find the relative one to be the best. Inside the layout there are 2 components, the surface view and the ad view, each with it's own id (adViewId and SurfaceViewId). Also, don't forget to define the namespace for the Admob components, in my case "ads:".


Now that we have our layout file, we must include it in the activity. In a normal android application you could do this by calling setContentView(R.layout.game_layout);  in the onCreate method of the activity. In Andengine, we need to use a different approach.
I will assume that you extend the SimpleBaseGameActivity class because the first step you need to do is adapt the class to extend the BaseGameActivity class.
The class hierarchy for the activity classes in Andengine is presented in the following figure:


In order to display ads we must extend the LayoutGameActivity class.
Let's assume that you have the following class that extends the SimpleBaseGameActivity:
public class MainActivity extends SimpleBaseGameActivity {

  public static final int CAMERA_HEIGHT = 720;
  public static final int CAMERA_WIDTH = 480;

  private Camera camera;
  private BitmapTexture blocksTexture;

  @Override
  public EngineOptions onCreateEngineOptions() {
    this.camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    EngineOptions engineOptions = new EngineOptions(true,
        ScreenOrientation.PORTRAIT_FIXED,
        new FillResolutionPolicy(), camera);
    return engineOptions;
  }

  @Override
  protected void onCreateResources() {
    try {
      this.blocksTexture = new BitmapTexture(this.getTextureManager(),
            new IInputStreamOpener() {
        @Override
        public InputStream open() throws IOException {
          return getAssets().open("gfx/block.png");
        }
      });
      blocksTexture.load();
      
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  @Override
  protected Scene onCreateScene() {
    GameScene scene = new GameScene(blocksTexture, this);
    this.mEngine.registerUpdateHandler(scene);
    return scene;
  }

}

This is a minimalist class, with all the logic defined in the GameScene class, for the sake of simplicity.
After extending the LayoutGame class, the file will look like this:
public class MainActivity extends LayoutGameActivity {

  public static final int CAMERA_HEIGHT = 720;
  public static final int CAMERA_WIDTH = 480;

  private Camera camera;
  private BitmapTexture blocksTexture;

  @Override
  public EngineOptions onCreateEngineOptions() {
    this.camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
    EngineOptions engineOptions = new EngineOptions(true,
        ScreenOrientation.PORTRAIT_FIXED,
        new FillResolutionPolicy(), camera);
    return engineOptions;
  }

  @Override
  public void onCreateResources(
      OnCreateResourcesCallback pOnCreateResourcesCallback)
          throws Exception {
    try {
      this.blocksTexture = new BitmapTexture(this.getTextureManager(),
            new IInputStreamOpener() {
        @Override
        public InputStream open() throws IOException {
          return getAssets().open("gfx/block.png");
        }
      });
      blocksTexture.load();

    } catch (IOException e) {
      e.printStackTrace();
    }
    pOnCreateResourcesCallback.onCreateResourcesFinished();
  }

  @Override
  public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback)
      throws Exception {
    GameScene scene = new GameScene(blocksTexture, this);
    this.mEngine.registerUpdateHandler(scene);
    pOnCreateSceneCallback.onCreateSceneFinished(scene);
  }

  @Override
  public void onPopulateScene(Scene pScene,
      OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
    pOnPopulateSceneCallback.onPopulateSceneFinished();
  }

  @Override
  protected int getLayoutID() {
    return R.layout.game_layout;
  }

  @Override
  protected int getRenderSurfaceViewID() {
    return R.id.SurfaceViewId;
  }

}

There are 2 methods related to the layout file. The first method is getLayoutID, that must return the id of the file containing the layout. The other method is getRenderSurfaceViewID, that must return the id of the surface view.
The other methods simply do what the SimpleBaseGameActivity class does for the BasicGameActivity.

This is all you need to do in order to display ads in an Andengine game.

Friday, February 1, 2013

Nvidia Optimus support in Ubuntu


If you have a laptop with nVidia Optimus and you run Linux (in my case Ubuntu) it will drain your battery very fast (in my case 2-3 times faster than on Windows).

In order to counter this, I install Bumblebee on my laptop. This will allow you to use the discrete graphics card when you need it and turn it off when you don't.
Bumblebee 3.0 is supported up to Ubuntu version 12.10.


In order to install Bumblebee enter the following commands in a terminal:
  1. sudo add-apt-repository ppa:bumblebee/stable
  2. sudo apt-get update
  3. sudo apt-get install bumblebee bumblebee-nvidia linux-headers-generic
  4. Reboot
To run a program using the nVidia card run:
optirun firefox
If you have problems please check the references.

References:
https://wiki.ubuntu.com/Bumblebee
http://bumblebee-project.org/
https://github.com/Bumblebee-Project/Bumblebee

Tuesday, January 29, 2013

Analyze memory fragmentation with VMMap

VMMap is a process virtual and physical memory analysis utility. It shows a breakdown of a process's committed virtual memory types as well as the amount of physical memory (working set) assigned by the operating system to those types. Besides graphical representations of memory usage, VMMap also shows summary information and a detailed process memory map

 

If you have an application that runs out of memory and you only see 800MB of memory used you probably have fragmentation problems. You can use VMMap to see how the memory is allocated. If you use VirtualAlloc for less than 64 KB you will generate unusable memory regions. This is because the address must be aligned to 64KB. By allocating 10 chunks of 1KB you will have 10KB of used memory and 630KB of unusable memory. If you have 32-bit application you may run out of memory (you only have 2 GB of virtual address space available).

References:
http://technet.microsoft.com/en-us/sysinternals/dd535533.aspx

Monday, January 28, 2013

Patching with git

In this post I will show you how to make a patch with git and then apply it.
The first thing we will need is a test project.
We will begin by cloning a repository from github.com, with read only access.
git clone git://github.com/gulyan/gulyan.github.git test
cd test
This will clone a very simple git project.
The first thing we will do is make a new branch. It's considered best-practice to commit your fixes in a separate branch:
git checkout -b my_fix_branch
To view your current branch, you can type:
> git branch
master
* my_fix_branch
Now you can play around and add a couple of commits to this branch.
When you are read to make a patch, you will use the format-patch command.
If you run it without the --stdout argument, it will create a patch file for every commit.
We want to put all the commits in a single file so we use --stdout and we redirect it to a file like this:
git format-patch master..HEAD --stdout > my_fix.patch

Now let's try and apply this patch. First we go back to the master branch:
git checkout master

Before we apply the path, we want to make sure it will not cause problems.
The first thing we should do is run git apply with the --stat argument. This command will show us what the patch will modify:
> git apply --stat my_fix.patch
README |    4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
After this we run git apply with the --check argument. This will not apply the patch. If we don't get any errors we can safely apply the patch.
git apply --check my_fix.patch
In order to apply the patch we will use the git am command:
git am my_fix.patch
This will apply the patch and the commit messages.
If we use git am, we can also signoff on the commits by adding the --signoff argument.

You could also apply the patch with git apply:
git apply my_fix.patch
This will apply the patch to the files, but it will not commit the changed to the branch.
You need to add the files and commit them manually.
For most use cases, I recommend you use git am.

If you have any problems, with git am you can fix the files, add them to the index and then run git am --resolved. To abort a patch use git am --abort.

Reset linux root password

This post will show you how to reset a linux root password. I assume that you are using GRUB 2 as the bootloader.
  1. Select the linux boot option from the grub menu for which you want to reset the password.
  2. Press e to edit your boot option entry. Note that the editing is not persistent, the next time you boot all these changes will be lost so you don't have to worry about reverting them.
  3. In the line starting with something like this: linux /boot/vmlinux-3.5.0-22-generic ... ro quiet slpash replace ro with wr. This will give you write access instead of read only.
  4. Also add init=/bin/bash at the end of the line. This will start a bash process instead of your desktop environment.
  5. When the line looks like this: linux /boot/vmlinux-3.5.0-22-generic ... rw quiet slpash init=/bin/boot press Ctrl+X to boot.
  6. The system will boot and will give a terminal with root access. You can use this terminal to change the password of any users or run any command you want with root access.
  7. To change the password for user gulyan type: passwd gulyan
  8. After you type the new password 2 times, you need to reboot.
Done :)

Sunday, January 27, 2013

C preprocessor

I will mention 3 useful features of the c preprocessor that can be used to make a program more customizable:
  1. Stringification
  2. Concatenation
  3. Varadic macros
Stringification can be used to turn an expression into a string, surrounded by double quotes.
To do this we use # in front of the macro argument that we want to stringify:
#define STRINGIFY(S) #S
This will turn the expression STRINGIFY(2+3) into "2+3".
You can use it for something even more complex like this:
#define STRINGIFY(S) #S
const char str[] = STRINGIFY(int main()
{
  return 0;
});
It should be noted that all the whites-paces will be trimmed intro a single space character.

Concatenation is useful when used with the stringify feature. We can use it to merge 2 strings into a single one by using ## like so:
#define CONCAT(A,B) A ## B
This will turn the expression CONCAT("The result is ", STRINGIFY(2+3)) into "The result is 2+3".

Varadic macros are macros with a variable number of arguments.
These can be useful is we want a custom debug function:
#ifndef DEBUG
  #define dprintf(...) do{}while(0)
#else
  #define dprintf(fmt, ...) \
    fprintf(stderr, fmt, __VA_ARGS__)
#endif
In this example, __VA_ARGS__ will represent the parameters given to the dprintf macro. Based on the definition of DEBOG, calls to dprintf will either be converted to fprintf or be completely removed by the compiler.
We can go a step further with our debug function by including the line and file of the call.
#ifndef DEBUG
  #define dprintf(...) do{}while(0)
#else
  #define dprintf(fmt, ...) \
    fprintf(stderr, "[ERROR %s at %d] " fmt, __FILE__, __LINE__, __VA_ARGS__)
#endif
The __LINE__ and __FILE__ macros are expanded to represent the file and line number of the call of dprintf, NOT the declaration. It should also be noted that there is no coma before the fmt argument. This is done so that the C compiler will concatenate the 2 strings.

Global variables in android

Global variables are variables that can be accessed from more than one activity.
If you only need primitive values than you can pass them in the Intent, but if you have more complex data types this is not an option.
The first thing you might try is to declare the members as static.
This is a VERY BAD idea.
Android can set static instances to null without you knowing and you will spend hours debugging the application.

The correct way to do this is by extending the Application class.
As stated in the Android documentation:
Base class for those who need to maintain global application state.

Bellow is an example of how to use the application class:
package gulyan.gametoolkit.app;

import android.app.Application;

public class GameApplication extends Application {

 private static GameApplication instance = null;

 @Override
 public void onCreate() {
  super.onCreate();
  instance = this;
 }

 public static GameApplication getInstance() {
  return instance;
 }

}
Next, you must edit the AndroidManifest file in order to use the custom application class, by setting the name attribute.
<application
     android:name="gulyan.gametoolkit.app.GameApplication"
     android:icon="@drawable/ic_launcher"
     android:label="@string/app_name"
     android:allowBackup="true"
     android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
You can now declare all the variables as members of this class (non-static members).
If you need to access them you can use the getInstance method in order to obtain the application instance. This instance is the same in all the activities.
GameApplication inst = GameApplication.getInstance();
MyVar x = inst.getMyVar();
inst.setMyVar(x);
Hope you find this useful.

git bisect

The bisect command does a binary search through the commit history in order to help you identify which commit introduced an issue.
To begin a git bisect, the first command is:
git bisect start
After this, you must tell git a bad and a good commit:
git bisect bad # or git bisect bad HEAD
git bisect good HEAD~10
In this example, the HEAD is broken and that 10 commits before the HEAD is a good point.
Git will then begin the binary search and ask you about the state (good/bad) of the current revision.
You tell this by running:
git bisect good
or
git bisect bad
At the end, git will tell you the commit that introduced the issue.
In order to stop the git bisect you must run:
git bisect reset
You should run this command after finishing the search.

Finally, if you have a script that returns 0 if a commit is good and a non-zero result otherwise, you can find out the bad commit in 2 lines:
git bisect HEAD last_good_revision
git bisect run ./script.sh

References:
http://git-scm.com/book/en/Git-Tools-Debugging-with-Git