Saturday, December 17, 2011

ccColor3B constants.... A whole bunch of them.

This is bound to save everyone some time when looking at defining custom colors for cocos2d. Found this list on the cocos2d forums (thanks to a member there called Yeti):

Tuesday, December 6, 2011

Cached Property File Enum

We had a little need to dynamically enable and disable features on our current project and I sorta left it for the last day, came up with a simple little solution that ended up quite neat.

A cached property file enum:


 How it looks being used:
Code:
Test:
The .properties file:
FEATUREA=true
FEATUREB=false
FEATUREC=true

How it works:

The Pragmatic Programmer - Review / Summary Notes.

I recently finished The Pragmatic Programmer, to be completely honest this had been the 3rd attempt to read it, although the book is good and well worth the read, I had unfortunetly learnt most of the lessons the hard way on my own over the last decade or so of being a software developer, so I found often myself easily distracted.

I have to add, had I read this when it was published it would have undoubtedly saved me some pain, but even with that it's always good to reaffirm some of your good habits, and keep your bad ones in check. It covers 46 sections with 70 different tips that are generally useful to any programmer. Jeff Atwood - coding horror made a list of all 70 tips.

As with most things some tips are "more equal" than others, the ones that really stick out and probably can't be emphasized enough are:

  Care About Your Craft
Why spend your life developing software unless you care about doing it well?
  Think! About Your Work
Turn off the autopilot and take control. Constantly critique and appraise your work.
  Don't Live with Broken Windows
Fix bad designs, wrong decisions, and poor code when you see them.
  Invest Regularly in Your Knowledge Portfolio
Make learning a habit.
  DRY - Don't Repeat Yourself
Every piece of knowledge must have a single, unambiguous, authoritative representation within a system.
  Make It Easy to Reuse
If it's easy to reuse, people will. Create an environment that supports reuse.
  Prototype to Learn
Prototyping is a learning experience. Its value lies not in the code you produce, but in the lessons you learn.
  Don't Panic When Debugging
Take a deep breath and THINK! about what could be causing the bug.
  "select" Isn't Broken.
It is rare to find a bug in the OS or the compiler, or even a third-party product or library. The bug is most likely in the application.
  Design with Contracts
Use contracts to document and verify that code does no more and no less than it claims to do.
  Crash Early
A dead program normally does a lot less damage than a crippled one.
  Minimize Coupling Between Modules
Avoid coupling by writing "shy" code and applying the Law of Demeter.
  Don't Program by Coincidence
Rely only on reliable things. Beware of accidental complexity, and don't confuse a happy coincidence with a purposeful plan.
  Refactor Early, Refactor Often
Just as you might weed and rearrange a garden, rewrite, rework, and re-architect code when it needs it. Fix the root of the problem.
  Design to Test
Start thinking about testing before you write a line of code.
  Some Things Are Better Done than Described
Don't fall into the specification spiral at some point you need to start coding.
  Don't Be a Slave to Formal Methods.
Don't blindly adopt any technique without putting it into the context of your development practices and capabilities.
  Don't Use Manual Procedures
A shell script or batch file will execute the same instructions, in the same order, time after time.
  Test Early. Test Often. Test Automatically
Tests that run with every build are much more effective than test plans that sit on a shelf.
  Coding Ain't Done 'Til All the Tests Run
'Nuff said.
  Test State Coverage, Not Code Coverage
Identify and test significant program states. Just testing lines of code isn't enough.
  Find Bugs Once
Once a human tester finds a bug, it should be the last time a human tester finds that bug. Automatic tests should check for it from then on.

Friday, November 25, 2011

Determining iOS Hardware / Device

I am currently working on my second iOS game in which I am creating dynamic Open GL ES reflections, and noticed on the older devices it would cause a drop in frames per second. It doesn't on the iPad 2, I do not have iPhone 4S yet, but I assume it won't have a visible effect there either with the new CPU.

So to not diminish the user experience I decided to remove those reflections, on the interactive CCSprite, on the older devices.

I went searching and found : UIDeviceHardware - Determine iOS device being used

I ended up just using: (platformString wasn't useful to me) I then just created a convenience method for myself to check for the version that have the Dual-core Apple A5 processor.

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.

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...