Where to Start

Posted: September 17, 2010 in Development
Tags: , , ,

One of the most frequently asked questions I get from beginners in programming, is that of where to start. This is very common amongst beginners, that they are given the tools to do almost anything, but they lack the inspiration of what these tools can be used for. As with anything, practice makes perfect, and programming is no different, so anything you can think of is really good exercise.

Besides practicing the use of the tools you are given though, I recommend a gradual move into practicing techniques, rather than syntactical practice. I look at programming as problem-solving, and each time you solve a problem you learn a technique for a given task, with practice you learn more and more techniques — some even for the exact same problem, just more efficient than the last. So, I suggest setting yourself goals, modest goals to begin with, practice the goal until you have a thorough understanding of the problem and solution, even try different ways of accomplishing the same goal — this way you will not only have understood the problem, but also gain a deep knowledge of various techniques to solve it, which will come in handy later on.

From Beginner To Intermediate
These are a few suggestions for you to practice, that get increasingly difficult to solve. If you finish all, go back to the first project and try to solve it with any new technique learned as you progressed. And if you are learning an object oriented programming language, such as C++, make sure you make everything an object in the project, as it will ensure a very good understanding of how OOP (Object Oriented Programming) works. Once you get familiar with OOP and comfortable working with it, you will not be wanting to go back to procedural programming — and then you can call yourself an intermediate programmer.

  • Hello World — Printing a string to the console.
  • Guessing Game (eg. guess a number) — Generate a random number, have the player try to guess the number.
  • Tic-Tac-Toe — Slightly more complicated game, will challenge you on several aspects of basic programming.
  • Pong — Very simple game, but requires animation, which can be quite difficult at first. Will get you familiar with time-based programming, real-time input interpretation and event handling.

Anytime you feel like you don’t know exactly how to solve a problem, write it out in pseudo-code, as such:

create keyboardInputVariable
generate aRandomNumber

while keyboardInputVariable does not equal aRandomNumber
     ask user for input
     set keyboardInputVariable equal to keyboardInput

// once outside this loop, the number has been guessed
print game-over string

By writing out pseudo-code, things become much easier to solve, as you have the solution in —almost— plain english.

I hope this helped you get started on your first projects. If i neglected to mention something, that you as a beginner, would like to know on the subject of getting started, let me know by commenting and I’ll update the post accordingly.

Count in Binary

Posted: September 10, 2010 in Tips & Tricks
Tags: ,

I felt like doing a quick post on something this evening, and so tonight I’ll be teaching you how to count in binary. Regardless of whether or not you are a programmer, being able to count in binary is quite fun.

What is Binary?
Binary refers to a system consisting of two components, and in the realm of computing we recognize these two as ones and zeroes.

1 .. 2 .. 4 ?
Counting in binary is quite simple. Consider a block of zeroes, say 8 (eg. 00000000), each of these numbers are called bits, and 8 bits make up one byte.

0	00000000
1	00000001
2	00000010
4	00000100

Notice the pattern here, that each time you move the 1 one over toward the left, you increase the value by a factor of 2 (multiplication by 2). So, 8 would be 00001000.

Where did 3, 5, 6 and 7 go ?

0	00000000
1	00000001
2	00000010
3	00000011
4	00000100
5	00000101
6	00000110
7	00000111
8	00001000

Notice that on each pass of multiplication by 2 (1, 2, 4, 8, 16), we fill the gap of zeroes behind it in a pattern similar to how we did the multiplication. The only difference is that we are counting on top of it. If you look away from the bit that make up the number 8 (fifth bit from the left), you are just counting 1, 2, 3, etc. all over again — this is illustrated below.

8	00001000
9	00001001
10	00001010
11	00001011
12	00001100
13	00001101
14	00001110
15	00001111
16	00010000

There is also a visual pattern going on here, if you look at each column of the numbers 0 through 8, look at the rightmost column, and you will see the alternating pattern of even and odd numbers (0 is even, while 1 is an odd number). The same pattern emerges when you look at the next column, which alternates in a pattern of 2/2 (2 zeroes, 2 ones, etc.). As you may have guessed, these patterns are found all throughout the columns, as the next column will then be 4/4 — this makes a nice rule of thumb to remember binary counting by, you can also immediately tell whether or not it is an odd or even number, just by looking at the rightmost column.

If this still doesn’t make a whole lot of sense, try adding two binary numbers together, as shown below (overly simplified, for illustrating the correlation).

8 + 2 = 10	00001000 + 00000010 = 00001010
16 + 5 = 21	00010000 + 00000101 = 00010101

Hopefully this was understandable. It is quite easy to count in binary, once you know the pattern. With 8 bits (one byte), you can count from 0 to 255, of course you can always extend the range to 16, 32 or even more bits if you wish. Knock yourselves out, have fun.

I bet you have been wanting to put your own custom ringtones on your iPhone, free of charge, for quite a while — I have a solution for you.

Ever since I got my first iPhone, I have found the lack of easy custom ringtones to be quite a pain. Apple will tell you to use iTunes if you wish to rip a piece of a song and use that as your ringtone, however they will charge a small fee for that and only a select few of the iTunes store songs are eligible for ringtone creation — in other words, their solution is not very handy, at all.

The Solution
It is actually quite simple and I don’t really know why I only just found out about this, but it seems GarageBand does the trick for us and you are not constricted to use song rips with this approach.

Open up GarageBand and create a new project, set the loop bar to 40 seconds or lower (this is the time limit of ringtones on the iPhone). Over the span of your loop, start creating your very own ringtone, and when you are happy with your marvelous creation go to the menu ‘Share’ and click on ‘Send Ringtone to iTunes’. Sync your iPhone and voila!

Review: iPhone 4

Posted: August 23, 2010 in Reviews
Tags: , , , ,

So, I have had the iPhone 4 for about two weeks now and I’d like to share my impressions of it with you guys, in an effort to separate nonsense from the facts about it.

First Impressions
When you pick up this phone and turn it on, you immediately notice the astonishing ‘Retina Display‘. The pixel density of this display is incredibly beautiful, but more importantly it makes a lot of things work better on such a small display, like e-book reading, internet surfing, etc. — Initially I thought this feature to be purely for looks, but found it to be much more than that.

Antennagate
There has been a lot of chatter around the antenna design of the iPhone 4 and it has been pointed out as a technical flaw. In my experience, this has been blown completely out of proportion, as I have had no problems with it at all — in fact, the very first call I made from my iPhone, I held it in the so-called ‘death-grip’. There is some truth to the fact that it drops in signal strength when held in a certain way, however this doesn’t affect whether or not you can make a call, as long as there is at least one bar left.

Why Did I Get It ?
My old iPhone 3G was beginning to feel a bit sluggish whenever I wanted to try out new things that were slightly more CPU intensive. I also really wanted a video-capable phone, for the occasional YouTube upload, and the iPhone 4 fits the bill quite nicely with 720p recording.

If there is anything I did not cover, that you would like to know about, please leave a comment and I will update the post as soon as possible.

Retina Display

Posted: August 22, 2010 in Thoughts

With the incredibly dense ppi of the new iPhone 4, will we we this filter through to the Mac? — I definitely think so.

Sooner or later, we will see an industry-wide transition from standard ppi to “HD-ppi”. However, there are a few technical issues with handling the transition, the most prominent of them being the content that already exists. If the ‘Retina Display’ pixel density of the iPhone 4 were to be applied to say a Macbook Pro 15.3” screen, like the one I have, I would probably not be able to see what is going on — everything would become very tiny, the resolution would have gone up by over a factor of 2. This means that whenever this is going to happen, the transition won’t be easy in comparison to the HDTV transition, as the entire web would have to account for this change.

I’ve never really used the Interface Builder all that much when developing, and frankly I don’t like the “magic” it does for me. I prefer being able to visualize what my application does in the code by avoiding the NIB altogether — and I’m going to show you how I go about doing this.

Why ?
Well, there are projects where it just doesn’t make sense to use Interface Builder, rendering the NIB obsolete to the project itself. This is true for projects that have little or no GUI based on the AppKit, like fullscreen games. Games in particular usually incorporate their own GUI, which is managed by the engine.

How ?
As far as I know, there is no Apple-sanctioned method of approach of doing nibless development under cocoa, and the method I will show you is probably not the only way — or the best, for that matter.

The standard main.m looks something like this:

#import <Cocoa/Cocoa.h>

int main(int argc, char *argv[]) {

    return NSApplicationMain(argc,  (const char **) argv);
}

The return NSApplicationMain(argc, (const char **) argv) instantiates your application by creating the singleton object NSApplication, loading the MainMenu.xib (the nib) and calling the run method on your NSApp. In order to avoid the nib, we need to circumvent this by initiating the application ourselves.

The first thing we do, is to get rid of the NSApplicationMain function call. In it’s place we simply return zero, as cocoa will call exit() before reaching the return instruction of the main function, so this never gets executed anyway. Now that our main function looks like a generic C program, we can begin recreating what the NSApplicationMain did for us.

We will need to wrap everything in an autorelease pool, and make sure we have an instantiated application — we do this as such:

#import <Cocoa/Cocoa.h>

int main(int argc, char *argv[])
{
    // create an autorelease pool
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    // make sure the application singleton has been instantiated
    NSApplication * application = [NSApplication sharedApplication];
    
    // drain the autorelease pool
    [pool drain];
	
    // execution never gets here ..
    return 0;
}

Now that we have made sure we have an application to run, we can go on and create an application delegate — if not already present in your project.

Create a new Objective-C NSObject subclass, name it AppDelegate. In the @interface section found in the header of your new NSObject subclass, add the NSApplicationDelegate protocol to it as such: @interface AppDelegate : NSObject <NSApplicationDelegate>

Import the AppDelegate’s header to your main.m as such: #import "AppDelegate.h"

Now that your AppDelegate implements the NSApplicationDelegate protocol and your main.m knows about it, we can create an instantiation of the application delegate and assign it to the NSApplication, and then all we really need to do is to call the run method:

#import <Cocoa/Cocoa.h>

#import "AppDelegate.h"

int main(int argc, char *argv[])
{
    // create an autorelease pool
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    // make sure the application singleton has been instantiated
    NSApplication * application = [NSApplication sharedApplication];
    
    // instantiate our application delegate
    AppDelegate * applicationDelegate = [[[AppDelegate alloc] init] autorelease];
    
    // assign our delegate to the NSApplication
    [application setDelegate:applicationDelegate];
    
    // call the run method of our application
    [application run];
    
    // drain the autorelease pool
    [pool drain];
    
    // execution never gets here ..
    return 0;
}

This takes care of the main function. Now all we need to do, is implement how our application delegate handles our application. You can handle this however you want, I’m just going to show you how to create a window with a content view. Keep in mind, that for the sake for keeping the demonstration short and to the point, I do not use custom classes for these, in real projects I would definitely write a class for the content view.

Inside of the class declaration of the AppDelegate, we put an NSWindow and a NSView as such:

@interface AppDelegate : NSObject  {

    NSWindow * window;
    NSView * view;
}

Because the AppDelegate implements the NSApplicationDelegate protocol, there are quite a few methods that it will now respond to and that will get called during run-time. So in the definition of our application delegate, we add the following methods and implementations as such:

@implemention AppDelegate

- (id)init {
    
    if ( self = [super init] ) {
        
        // create a reference rect
        NSRect contentSize = NSMakeRect(0.0f, 0.0f, 480.0f, 320.0f);
	
        // allocate window
        window = [[NSWindow alloc] initWithContentRect:contentSize styleMask:NSTitledWindowMask backing:NSBackingStoreBuffered defer:YES];
        
        // allocate view
        view = [[NSView alloc] initWithFrame:contentSize];
    }
    return self;
}


- (void)applicationWillFinishLaunching:(NSNotification *)notification {
    
    // attach the view to the window
    [window setContentView:view];
}

- (void)applicationDidFinishLaunching:(NSNotification *)notification {
    
    // make the window visible.
    [window makeKeyAndOrderFront:self];
}

- (void)dealloc {
    
    // don’t forget to release allocated objects!
    [view release];
    [window release];
    
    [super dealloc];
}

@end

.. and that concludes my approach of doing nibless development. Notice that I did not implement the main menu, so when you run this the main menu will not work. But there is a solution for that as well, I might write an article about that some time — please do let me know if this is something you would like to see.

Phew, that got a little lengthier than I originally expected it to.

Hopefully you found this useful. If you have any questions or thoughts on this, please leave a comment — Thank you.

Download Xcode Project
I’ve made an xcode project available for download at, just click the link above to get it. It has been build, tested and it works.

UPDATE Fixed a typo; in AppDelegate.m it said ‘@implemention’, while it should have been ‘@implementation’. Thank you albert7546, for pointing out the source wouldn’t compile.

Adobe makes great products, and Flash is definitely one of them. I used Flash back in the day when it was very popular on the internet and I loved using it, web-content became an interactive experience with very little effort. However, my vote is not for dying technologies and Flash is indeed living its lasts days.

I can understand why Adobe is struggling to keep Flash alive as it has been playing such a major role in the industry — and for so long that this may have come as a shock for Adobe. And while they probably won’t let Flash die without a fight, I do not think they will succeed in keeping it alive for much longer.

The Apple Issue
On this matter, we supposedly have two sides of story to worry about; the end user and the ethical aspect of Apple not implementing Adobe’s technologies.

Besides the drastic movement by Apple to simply cut out Flash from its iDevices, I don’t think the average end user will ever really notice the transition, as long as the content they enjoy is still available to them. Flash is easily substitutable by several other —and newer— technologies, and as Apple points out, HTML5 is indeed a great substitute. In the end, transitions usually pave the way for new exciting things and the battle of who takes over the main stage when Flash dies is on — my bets are placed on HTML5 and WebGL.

I do think Apple could have handled this with a little more professional courtesy, however I understand and appreciate their move, even though it was a little out of line. Adobe provides some of the most important applications for the Macintosh platform and Apple needs to recognize and cherish that, so this was a very bold move to make as it could potentially hurt a great partner.

All in all, Adobe I’m sorry for your loss and Apple … behave!

Read the rest of this entry »