Sunday, October 16, 2011

Cocos2d CCSprite Reflection

With yet another 2 months of Wild West Development behind me, I can hopefully get back to a somewhat normal life again, blogging, learning and a little iOS game development.

What I want to share today is my little hack to reflect a CCSprite used in to cocos2d. I say hack because it does feel a little dirty, i didn't go and learn about the details of Open GL and the like, simply patched things together to get a working solution.

Below is the code for my ImageReflector, the basic idea and how it got it work was simple:

The best looking reflection example I found was Apples' example. So the methods createBitmapContext, createGradientImage and reflectedImage are directly from the above example. I wanted to use it in cocos2d so I used CCRenderTexture to create a UIImage, passed that to Apples' example code and then simply converted back to a CCSprite with the help of the CCTexture2D class.

Usage: Class:

Friday, September 2, 2011

iPhone Gmail Username / Password Error

This morning, I all of a sudden could not get my gmail mails on my iPhone, constantly got:
Cannot Get Mail - The user name or password for imap.gmail.com is incorrect.

After a bit of searching, I found the solution to be simple, logon and input the Captcha phrase.

Link Below:
https://www.google.com/accounts/UnlockCaptcha

Wednesday, August 31, 2011

Mac OSX Global Git Ignore - XCode Project and State Files.

With my recent switch to Mac, Objective C and XCode and XCode using Git as it's default VCS (Version control system), I figured it would probably be a good time to switch over to using Git. I registered with GitHub purchased the base 5 private repo plan and uploaded my projects and images. I didn't realize at the time then what pain I would cause myself.

A little later, my wife joined me in working with those projects only to discover that under certain scenarios we could never actually merge or sync the branches from XCode. There were always couple files that would be "uncommited" in XCode. Nasty XCode project state files.

I had not worked with Git before this actually, so after reading up a bit about project files and the like and how a ".ignore" file would do the trick.
After I little bit of struggling, I realized that for ignore to work, the files needed to be removed from the repository first. Thankfully I did not have any important history so I could simply recreate them. You can also remove them from git with: git rm --cached

To go about creating, editing configuring a global ignore file on OSX I did the following:

First problem on a Mac, any "." files are seen as system files and those are automatically hidden from you.

To get around that open "Terminal" and use the commands below to show and hide system files (you'll need to either restart or force quit Finder for the changes to take effect).

Show:
defaults write com.apple.finder AppleShowAllFiles -bool true

Hide:
defaults write com.apple.finder AppleShowAllFiles -bool false


Once I had done that I simply went searching for a couple example ignore files and combined them into the example below.

Create a file in your user directory called ".gitignore_global", populate it with the contents required, (below is mine, that does take care of the XCode problem files)
Then in Terminal run:
git config --global core.excludesfile ~/.gitignore_global

My global ignore file:

Wednesday, August 24, 2011

Java / XML UTF-8 marshalling and validating pains

This is a post on the little pains I am sure most people have endured while working with Java and XML from different sources, with 'funny' characters...

If you use a FileWriter instead of a FileOutputStream it will use your OS encoding (So cp1252, ISO-8859-1 or US-ASCII and not UTF-8... ):

A way to ensure that you are using UTF-8 is:

If you are reading XML from a stream, it is probably safer to do something like:

When validating a schema note the .getBytes("UTF-8"):


When marshalling (Use a stream rather than a writer):


Lastly if you are getting XML from some unknown source there are some chars that are outside the legal XML Unicode limits that you can not encode. To remove those:
Code below from another blog :





Tuesday, July 26, 2011

Mac Attack...

I have not created a blog entry in some time, for the last 2 months or so all my weekends and evenings have been spent on my latest addiction, iPhone development.

A couple months back I spent some time deciding what programming language to learn, blogged about it here. At the time I based on Tiobe, job stats, current interest and not taking the final step to actually buy a Mac, I choose C#. I spent a couple weeks messing with it, nothing serious, started a WPF clone for one of my favorite tools of all time. Baretail, but it just didn't catch my imagination, so I lacked the drive to finish.

I then got my iPhone, and that was all she wrote... I was hooked, coming from a lame excuse of a "smart phone" The HTC-Touch-Diamond-2-windows-mobile-thing, to the iPhone was a wonderful experience. It didn't take long before I could not resist the desire to program of my new toy... off to the iStore, out with the credit card, returning triumphantly with a single box, (iMac i5 quadcore 4gb 21.5"), inside the box there is a single cable, plug in the power... (spend a couple minutes looking for the on switch)... basic OS config, user and such.... done.

I had heard something about xCode, off go App Store... wait a day for the monster 4.5gb download. Stumble around hello world, and 2 or 3 other tutorials and that was that. I was hooked and with that 2 months of weekends and evenings were lost infront of the iMac. This last weekend my wife and I eventually submitted the game, and now we have to wait for probably 2 nerve wracking weeks for approval or rejection.

I'll will discuss more specific details once it is released but For now I'll share some experiences and thoughts from the last 2 months.

Objective C
I have to say, I like the language although coming for a Java / .Net background some of the intricacies of the syntax, lack of type enforcement and actually having to do your own memory management can be a little tedious when starting.
All that aside but both my wife (a C# developer) and I (Java) didn't feel too uncomfortable picking it up.

Open Source and Other resources
No. 1 most useful thing I found when writing a game for an iOS platform is:
Cocos2D
These guys have done an awesome job, and there is a decent size community / forum to help out if you do get stuck.
I haven't looked at Sparrow as I only found it a couple days after implementing Cocos2D and decided to stick with that. Another blogger did a comparison here.

Physics Engines, there are a couple out there, I figured I would gave it a skip for the first app, there would be enough to worry about without having to learn one of them either.
The 2 Main contenders I can see are:
Chipmunk
Box2D

XCode 4...
ouch, XCode just feels behind the likes of Eclipse, Visual Studio and IntelliJ. Little things we take for granted, are really missed. I am not going to go into the details, just know if you already use Eclipse, Visual Studio (with Resharper) or IntelliJ and start with XCode, you are going to be a little frustrated. To be fair, some of the frustrations were probably from a lack knowledge on my part and to top it off never having coded in the language before.

After submitting my app I decided to try App Code and loved it straight away. It's still in "beta", but using AppCode after coming from IntelliJ fells very natural. So thankfully there will be a real alternative once they release it officially.

General
I will try post useful Objective C/Game/Apple code and examples as I learn. The first 2 quick things to start with:

1. When Using Cocos2D.... and trying to incorporate UIScrollView from the SDK, prepare yourself for some pain. The CCScrollLayer however, is completely painless. We extended it and completely customized it to our game look and feel, it was very simple to do.

2. If you want to sort an array of integers in descending order use a NSSortDescriptor, very hand class actually:

Monday, May 23, 2011

1 Year and 70 000 views later, the stats...

So it has been just over a year, since my first blog post. Even though the whole work / life thing has kept me overly busy the last couple months, reducing me to 1-2 posts a month, it has been quite a pleasing experience blogging. I would recommend it to all developer types, it's an good way to keep you investigating, learning and having your work out there and visible to people makes you put in that little extra effort.

The actual reason for this post is for some discussion on the stats... There were 1 or 2 interesting percentages I would not have expected.

Views By Country



No big surprise that the US has the most views, this being a English technology based blog, it is however interesting that Germany is second, beating out the UK and India, both with larger populations. South Africa on the list is probably only there because I force my colleagues and friends to read it :)

Views By Browser



With this being an Java-technical-open source oriented blog makes FireFox the obvious choice to lead the browser war. However with Chrome in 2nd by a such large percentage was a bit unexpected.

Views By OS



On OS Windows is still number one. Apple is competing nicely with Linux / Unix combined, this is probably due the large apple following in the US and the boom of the iPad.

Monday, May 16, 2011

Java Compression.

In a recent project, we had to do something I had personally never really had to look at; Compression. We needed to take a couple files and images, zip them up and make them available for FTP, and yes somedays it does feel like we are back in the 90's. Besides the FTP trip into the past its was good opportunity to spend a little bit of time on the subject.

Compressing Files
So above the usual IO classes BufferedInputStream, FileOutputStream and File there are:
ZipInputStream - An input stream for reading files in the ZIP file format. Zip entries are not cached, unlike ZipFile.
ZipOutputStream - An output stream for writing files in the ZIP file format. This has a default internal buffer of 512, a BufferedOutputStream can be used to increase this.
ZipEntry - Represents an entry int a zip file.
ZipFile - Used to read entries from a zip file. The entries are cached.
CRC32 - Used to compute the CRC-32 of a data stream.


Below is an example showing how to compress and decompress files in a folder, with and without a checksum:


Compressing Objects
We didn't end up using object compression but I had a look at it anyways. I did a little generic compress / expand util, don't know if it will ever be useful. I left the input params as OutputStream and InputStream as this could theoretically be used with any stream implementation from socket communication to string manipulation.

The compression related classes being used here:
GZIPInputStream - An input stream filter for reading compressed data in the GZIP file format.
GZIPOutputStream - An output stream filter for writing compressed data in the GZIP file format. Default internal buffer of 512, use BufferedOutputStream if you require more.

Tuesday, April 5, 2011

Little Spring Gem: ReflectionTestUtils

I have recently been slacking on content on my blog, between long stressful hours at work and to the wonderful toy that is an iPhone, I have taken a little break from anything "development" related after leaving the office to maintain my sanity. Now with the project delivered, and things quieting down again I can re-focus my excess neuron energy back to processing more IT related information.

One little thing I discovered in my last project, which I am a little embarrassed about, being a Spring nut, is the little gem that is : ReflectionTestUtils.

I mostly try write unit tests that do not include Spring, for the reason, that you should be testing your code and not Spring config. However sometimes it's really useful and quite beneficial to have your test code wired up with Spring, be that for integration tests or just to extend your test suite.
One issue was, I always found myself adding "setters" to my component interfaces, I always hated doing it, but I would con myself by saying: "It was for more testing, and more testing is always a good thing", and move along. I eventually (while procrastinating on work I didn't really want to do) went searching for a cleaner solution.

1 search and 1 minute later, a little red faced, I kicked myself. I should have guessed straight away that Spring would have thought of this scenario From 2.5 Spring had the ReflectionTestUtils class, which simply lets you set your dependencies / mocks via reflection.

So easy, tell it which object, the name of the field and set the mock / value you want to set. Neater interfaces, good times.
Below is an example using EasyMock interfaces and inject them for my test.

Sunday, March 13, 2011

The simple Big-O Notation Post.

I make no claim to be a "computer scientist" or a software "engineer", those titles alone can spark some debate, I regard myself as a software developer and I generally don't study the math and science behind everything I do. I generally learn what is relevant and useful to my day to day functioning and only rarely go deeper and dabble in the theory behind it. This is one of those occasions, so I decided to scour the internet and see what I could pick up. I hope to keep this simple, practical and to the point.


Big-O:
  • Describes how the algorithm scales and performs, in terms of either the execution time required or the space used.
  • Is relative representation of complexity. This allows you to reduce an algorithm to a variable which in turn allows you to easily compare it to another.
  • Describes an upper limit on the growth of a function, in the other words the "worst case scenario".

There is also Big-Omega notation which looks at the lower bound  / "best case scenario" stating that the algorithm will take at least X amount of time and Big-Theta which is tight bound to both lower and upper / "average".

Some quick observations in determining Big-O:
  • A Sequence of statements, or things like conditional checks are constant: O(1)
  • A loop of statements result in : O(n) n being the number of loop executions.
  • Nested loops are multiplied together: O(n2) where n is the times the outer loop executes and m is the times the inner loop executes.

Comparing the common notation examples:
(Thanks to Algorithms: Big-Oh Notation.)
 

nConstant 
O(1)
Logarithmic
O(log n)
Linear
 O(n)
Linear Logarithmic
O(n log n)
Quadractic
O(n2)
Cubic
O(n3)
1111111
2112248
412481664
81382464512
161416642564,096
1,0241101,02410,2401,048,5761,073,741,824
1,048,5761201,048,57620,971,52010121016


Java code example:
Show examples of notations in the table above. 



Common Data Structures and Relative functions: Lists and Sets:
Structuregetaddremovecontains
ArrayListO(1)O(1)*O(n)O(n)
LinkedListO(n)O(1)O(1)O(n)
HashSetO(1)O(1)O(1)O(1)
LinkedHashSetO(1)O(1)O(1)O(1)
TreeSetO(log n)O(log n)O(log n)O(log n)
* ArrayList Notes:

Thanks to reader comments, the add method on an ArrayList should be: O(1) amortized (and O(n) in worse case). Useful reference links:

Constant Amortized Time

Linked List vs ArrayList

Maps:

StructuregetputremovecontainsKey
HashMapO(1)O(1)O(1)O(1)
LinkedHashMapO(1)O(1)O(1)O(1)
TreeMapO(log n)O(log n)O(log n)O(log n)
References:

Algorithms: Big-Oh Notation.

Algorithmic Complexity and Big-O Notation.

Determining Big O Notation An easier way .

Wikipedia.

Sunday, February 20, 2011

Selecting a new programming language to learn.

I have been itching to learn a new language, but being a Java freak, I always end up convincing myself to spend the time and effort discovering, investigating or playing with something in the Java open source stable, Spring, Hadoop, Joda Time, Hibernate, Maven, Hazelcast, EhCache etc etc. Developing in Java these days is almost purely about knowing and wiring together frameworks, which is both a good and a bad thing. (as well as a topic for another day).

Now to get myself to not redirect the "new language" energy into Y.A.F (yet another framework) I decided to give the languages out there a proper look and see which would be the best fit and most beneficial to my work, marketability and just general 'IT Zen'.

So what do I require from a language:
IDE... my number 1 thing is an IDE, if there isn't a decent IDE for a language it is frankly not worth the time and effort. I don't see myself as a "scientist" where I feel the need to cause myself pain and inconvenience to be "pure". I want a comfortable productive working environment, and VI or Notepad with a command line utility ain't it.

Established... Every couple years someone somewhere tries to define some new language, and most of those die in obscurity for example brainf*** or anything listed on Esolang.

Popular / In Demand... As with most things, popularity is good, it means:
open source community, support and most importantly jobs. If you ever want to see the current popularity of a language Tiobe is the site to visit.

So who are the contenders out there?
Based on Feb 2011 Tiobe index:
Java is still the no.1 most popular, it has awesome IDEs and it's been around for just more than 15 years (January 23, 1996), but thankfully I know Java reasonably well :)... so moving right along... To narrow down the list quickly, I won't look at any languages that are losing popularity, for obvious reasons, so from the top 20 on the Tiobe list that excludes: C, C++, PHP, VB, JS, Perl, Ruby, Delphi, Go.
(C, C++, PHP, VB, JS, Perl, Ruby, Delphi, Go.)

Which leaves behind:
Python, C#, Objective-C, Lisp, NXT-G, Ada, Pascal, Lua, RPG

Now there is a line between established and old, I am going to make a call that could offend some people and say Pascal and RPG are just old. (Pascal, RPG)

Ada, don't know much about it, after reading the ADA overview, it seems okay, going to exclude it based on popularity. (Ada)

Lua, from a quick read it is a scripting language. (Lua)

NXT-G has something to do with lego or some robotics, not very mainstream. (NXT-G)

Lisp again like Ada, at first glace seems fine, just not popular enough. (Lisp)

Then there are the "New, built on other platforms" functional languages: Scala, F#, Clojure. Although very temping being on the bleeding edge, it's not all that profitable or marketable yet. I'll give them some time to standardize, settle down and see if they are widely adopted. They do appeal greatly to my inner geek, so will always be keeping an eye on them.

So this leaves me with:
Python, C#, Objective-C, (and Java).

Straight away based on the above list we can Tick: IDE, Established and Popular / In demand. We all know they have decent IDEs: Eclipse, XCode, Visual Studio, (IntelliJ and Netbeans). They have also been around and are well known.

Now looking at number of jobs:
Found a site (Simply hired) with a graph displays the percentage of jobs with your search terms anywhere in the job listing. Since June 2009, the following has occurred:

Python jobs increased 72%
C# jobs increased 77%
Objective-c jobs increased 268%
Java jobs increased 76%



With the recent boom of iPads and iPhones the Objective-C percentage is not all that surprising. I do have a problem with Apple, Objective-C and XCode and that problem is you need a Mac to run it. Once you start down that road you end up having to change everything to Apple, and I am not ready to do that. So for now I am going to drop Objective-C out of the running. Although if I ever do buy into the whole Apple thing, this will got back to the list.

Leaving me with Python and C#, looking at their salaries compared with Java:
(Data from Payscale).
US Data
Java
PayScale - Java Skill Salary, Average Salaries by Years Experience


Python
PayScale - Python Skill Salary, Average Salaries by Years Experience


C#
PayScale - C# Skill Salary, Average Salaries by Years Experience



South Africa Data

Java
PayScale - Java Skill Salary, Average Salaries by Years Experience


Python
PayScale - Python Skill Salary, Average Salaries by Years Experience


C#
PayScale - C# Skill Salary, Average Salaries by Years Experience


Based on the US data, I would have gone with Python, it's not as popular as C# but the pay is slightly better, I would also get to keep using Eclipse (PyDev) and Spring, but as soon as I looked at the South African data, I realized something, Python is really not big here. I manually went searching for Python positions advertised.. and found a grand total of 2 and the salaries were not good.

(Python)

Leaving C# as the last language standing.

It's got Visual Studio (even a free version Visual Studio Express), It has proven itself over the last couple years, it's out innovating Java at the moment, there's a ton of jobs, a whole range of certifications and the salaries have closed the gap on Java.
Seems quite a logical choice to me.

To top it off, I have also used C# many years back, so it won't not entirely new. Most of the successful Java open source projects (Spring, Hibernate etc etc) have been ported so all that knowledge is reusable, which also counted a little in my decision. Now I just need to stop working 12-14 hours a day, and I can focus on getting back to my Microsoft roots with little C# as a Java developer. Hopefully a couple months after that I can go through this process again, looking at Python, Objective-C, the mobile platforms (iOS, Android, windows) or maybe rather a concept change to functional with the likes of Clojure or Scala.

Monday, February 14, 2011

Validate XML against its XSD

Just a quick code snippet for possible future reference. How to validate a XML against it's XSD.

Building KubeSkippy: Learnings from a thought experiment

So, I got Claude Code Max and I thought of what would be the most ambitious thing I could try "vibe"? As my team looks after Kuber...