Useful Bitwise tips

When it comes to maintaining state of an entity, bitwise operator plays an important role.Consider that you are developing a game and your character can have jump, walk, fire and run states. The characters can be in more than 2 states at any point of time.You can store this information easily using bitwise operators.

Define states of the player.

typedef enum {
eNone = 0, (0000 0000)
eRunning =1, (0000 0001)
eJumping =2, (0000 0010)
eWalking =4, (0000 0100)
eShooting =8 (0000 1000)

Consider you want to set the player state to Running, you can do this by ORing.

0000 0000
0000 0001     (eRunning)
------------  (Bitwise OR)
0000 0001

You can set another state like Shooting.

0000 0001     (Current state)
0000 1000     (eShooting)
------------  (Bitwise OR)
0000 1001

We have captured both the states of the player.
To query the state ‘x’ of the player, you simply need to AND it.
To check if the player is Running or not,

0000 1001     (Current State)
0000 0001     (eRunning)
-----------   (Bitwise AND)
0000 0001

To reset any state, you need complement the state and then AND it.
To reset ‘Shooting’ state,

Complement the Shooting state.

0000 1000 -> 1111 0111 (complement)

Now AND it. This will reset only the ‘Shooting’ state and retain all the other states of the player.

0000 1001     (Current State)
1111 0111
-----------   (Bitwise AND)
0000 0001

These are the convenient methods.

_playerSate = _playerSate|state;

_playerSate = _playerSate & (~state);

BOOL isSet = _playerSate & state;
return isSet;


Connecting to Amazon EC2 server on Mac using Terminal app

Go to the folder where your .pem file is stored. You can retrieve the .pem file from your amazon account.

Launch the terminal app and run the  command

cd /folder/where/perm/file/stored/
ssh-add filename.pem

This should work fine most of the time and you should get a response similar to this.

Identity added:xxxxx

If the permission set on the .pem file aren’t correct then terminal will show this error

Permissions 0444 for 'filename.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
ec2-user@xx.xx.xx.xx:22: No such file or directory

To fix it, set the appropriate permissions.

chmod 400 filename.pem

Now, it’s time to connect to ec2 server.

ssh-add filename.pem
ssh ec2-user@xx.xx.xx.xx:22

If evrything works fine, then you should see this response on your terminal screen:

Last login: Tue Apr 23 03:34:27 2013 from xx.xx.xx.xx
__| __|_ )
_| ( / Amazon Linux AMI
There are 15 security update(s) out of 141 total update(s) available
Run "sudo yum update" to apply all updates.
Amazon Linux version 2013.03 is available.
-bash: warning: setlocale: LC_CTYPE: cannot change locale (UTF-8)
[ec2-user@ip-xx-xx-xx-xx ~]$

Installing OGRE XCode template for iOS

This is the continuation of my previous blog Building OGRE for iOS. To install the template, you need to follow these steps.

OGRE XCode template

This will install the OGRE template for you. Let us create our first sample OGRE demo using the template we installed it now. To do that, open XCode and select the OGRE template and hit next.

OGRE XCode template

If you see the next attached screenshot carefully, for some reason, the XCode was not letting me add ‘/’ at the beginning of the root SDK path of OGRE. Just ignore it for now. Lets correct this later by making few changes in the XCode project settings.XCode project creation

Now, select the target ‘DemoApp’. Now, select ‘Header Search Paths’ under ‘Build Settings’ and add the missing ‘/’ . Also, make the same changes for ‘Library Search Paths’ too 🙂

OGRE demo app project settings

Now. it’s time to compile the app. If you have done everything right then you should see this error 🙂


Undefined symbols for architecture i386:
"boost::system::system_category()", referenced from:
__GLOBAL__I_a in main.o
__GLOBAL__I_a in OgreDemoApp.o
__GLOBAL__I_a in OgreFramework.o
boost::unique_lock<boost::recursive_mutex>::lock() in libOgreMainStatic.a(OgreConfigFile.o)
boost::recursive_mutex::recursive_mutex() in libOgreMainStatic.a(OgreConfigFile.o)
__GLOBAL__I_a in libOgreMainStatic.a(OgreConfigFile.o)
__GLOBAL__I_a in libRenderSystem_GLESStatic.a(OgreGLESPlugin.o)
"boost::system::generic_category()", referenced from:
__GLOBAL__I_a in main.o
__GLOBAL__I_a in OgreDemoApp.o
__GLOBAL__I_a in OgreFramework.o
__GLOBAL__I_a in libOgreMainStatic.a(OgreConfigFile.o)
__GLOBAL__I_a in libRenderSystem_GLESStatic.a(OgreGLESPlugin.o)
__GLOBAL__I_a in libOgreMainStatic.a(OgreLogManager.o)
__GLOBAL__I_a in libOgreMainStatic.a(OgreQuaternion.o)
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

To fix this error, add -lboost_system in the Other Linker Flags. That’s it…Compile and Run the program now. You should see the OGRE beast staring at you 🙂

OGRE Simulator beast

Compiling OpenCV source to support iOS 5.0

The latest version of the OpenCV framework isn’t compatible with anything other than iOS 6.0. This is due to the script what they used to build the framework. Since they have not specified the deployment target, the XCode takes iOS 6.0 (the latest SDK) as the default deployment target. This is very annoying as there are many users who still use iOS 5.0.

The latest OpenCV framework[v2.4.5] for iOS can be downloaded from here. If you try to build the application using this framework and try to run the compiled app on iOS 5, you will encounter this error.

dyld: lazy symbol binding failed: Symbol not found: _objc_setProperty_nonatomic
Referenced from: /var/mobile/Applications/9C52EB2F-9A11-4656-BBAC-0EE6960A323B/
Expected in: /usr/lib/libobjc.A.dylib
dyld: Symbol not found: _objc_setProperty_nonatomic
Referenced from: /var/mobile/Applications/9C52EB2F-9A11-4656-BBAC-0EE6960A323B/
Expected in: /usr/lib/libobjc.A.dylib

After googling, I ended up with this link where they have addressed the problem as well as the fix for the problem. I had to download the entire OpenCV source from github.Though this wasn’t the latest code [v2.4.3], I still downloaded the source as this had deployment target correction script.

After you download the source, you need to execute the following 2 commands in the terminal.

cd /Users/praveencastelino/Downloads/opencv-ios_deployment_target_722dedb/ios
python ./ /Users/praveencastelino/Downloads/ 5.0

1] go to the iOS folder in the OpenCV source.
2] Excecute the python script python ./ <build directory> <deployment target>

If everything works fine then you should have the OpenCV framework compiled to run on iOS 5.0.
OpenCV build folder structure

You can also download the OpenCV framework that I have compiled from here.

Building OGRE for iOS

I was looking out for a 3D Engine (OpenSource) to render 3D models. I wanted to use Cocos3d but this has few limitations. It is not a cross platform engine. [ I can’t ignore my friend Android 😦 ]. Also, it lacks many of the cool features when compared to sophisticated 3D engines. The next engine was OGRE. This is written C++ and a cross platform engine. Moreover, this is exactly what I was looking for. After downloading the source, the next big task was to compile the source. There are many tutorials available in the website but most them are obsolete. So I thought of writing one myself to help someone to quickly get started with OGRE 🙂

Follow the following steps to compile OGRE on iOS platform:

  • Get the latest source from here(v1.8.1)
  • Download the OGRE iOS dependancy and XCode template from here(1.8)
  • Open the iOS dependancies dmg and copy the folder iOSDependencies to the source folder ogre_src_v1-8-1. Ensure that the iOSDependencies and OgreMain folder are at the same hierarchical level.
  • OGRE folder structure
  • Now, navigate to the SDK -> iOS folder. You should find a script.iOS script
  • Open the terminal and navigate it to the above folder where the script resides and run the script ./ in the terminal.
  • If everything worked fine then you should see a build folder in the same location where the script was. This folder contains a XCode project. XCode Build folder
  • Launch the  XCode project and set the target to SampleBrowser. Run the project. Wait for the Simulator to launch the OGRE beast 🙂XCode OGRE Project

PS: XCode Version 4.6.1 (4H512) and OS X (10.8.3)

iOS Resolution

Useful info about iOS devices:

iPhone Resolution
iPhone 3GS 480-by-320-pixel resolution at 163 ppi
iPhone 4S 960-by-640-pixel resolution at 326 ppi
iPhone 5 1136-by-640-pixel resolution at 326 ppi
iPad 4 2048-by-1536 resolution at 264 ppi
iPad mini 1024-by-768 resolution at 163 ppi
iPad 2 1024-by-768 resolution at 132 pip

Cool utility method to display HH:MM:SS from seconds

When I was developing video recording application, I had to display the time in HH:MM:SS format.
I wrote a utility method which does the same. It takes seconds as input and returns a NSString. Hope this helps iOS community 🙂
NSString * formatHHMMSSStringFromSeconds(int seconds)
    NSString *timeString = @"";
    int minutes = seconds / 60;
    seconds = seconds % 60;
    int hours = 0;
    if(minutes > 60)
        hours = minutes / 60;
        minutes = minutes % 60;
    timeString = [timeString stringByAppendingFormat:@"%02d:%02d:%02d",hours,minutes, seconds];
    return timeString;

svnX FileMerge does not work with XCode 4.3 upgrade

Many of us who upgraded their XCode to 4.3 must be facing an issue with FileMerge application which is part of svnX. After the XCode 4,3 upgrade, FileMerge application location has been changed. This is why it fails to launch from svnX.

You can fix this running the following command on terminal.
sudo /usr/bin/xcode-select-switch/Applications/

Also, you can launch the FileMerge directly from Xcode.
Xcode -> Open Developer Tool -> FileMerge.

The application binary can be found here:


UIImage From Movie File

Simple utility method to create a image from video file. Specify the size of the image and the time interval.

UIImage *getImageFromMovieAtTime(AVURLAsset *videoAsset, CGSize imageSize,CMTime frameTime)
NSError *error =nil;
CMTime actualTime;

AVAssetImageGenerator *imageGenerator = [AVAssetImageGenerator assetImageGeneratorWithAsset:videoAsset];
imageGenerator.appliesPreferredTrackTransform = YES;
imageGenerator.maximumSize = imageSize;
CGImageRef imageRef = [imageGenerator copyCGImageAtTime:frameTime actualTime:&actualTime error:&error];
UIImage *movImage = [UIImage imageWithCGImage:imageRef];

if(error || movImage == nil)
DEBUGLOG(@"Error: %@",error);
DEBUGLOG(@"imagesize: %@",NSStringFromCGSize(movImage.size));

return movImage;


    UIImage *videoFrame = getImageFromMovieAtTime(videoAsset, CGSizeMake(width, height),kCMTimeZero);