Archive for January, 2010

posted on Friday 29th January 2010 by Dave

Setting up Git on CentOS 5 server

I’m currently setting up Git for our company. The reason is that Git is better than X. This post is all about how to get Git setup on CentOS 5. There are other posts on this topic, of course, but this one is better!

Two minute intro to Git

I’ve come from an SVN background; you checkout a copy of a central repository, make some changes and commit. Git is a slightly different beast in that it is a distributed Source Control Management system. What this means is that you have your own local repository where you can happily commit changes (whether online or offline). To share your changes with others, you can then push your changes to another repository (either their repository or some central repository if you’d prefer). Similarly, to work on someone else’s code, you can create your own cloned version of their repository and then pull updates as required.

The reason I’m switching to Git is all about branching – I find this a real pain in SVN. If you’re not convinced you can click here to find out Git is better than your current SCM.

Installing Git on CentOS 5

Installing Git on CentOS 5 is easy if you make use of the EPEL (Extra Packages for Enterprise Linux) repository. You’ll know if you’ve got this installed if the following fails:

yum install git

To setup EPEL all you need to do is create a file /etc/yum.repos.d/epel.repo and then paste in the following:

[epel]
name=Extra Packages for Enterprise Linux 5 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/5/$basearch
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-5&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 5 - $basearch - Debug
#baseurl=http://download.fedoraproject.org/pub/epel/5/$basearch/debug
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-debug-5&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 5 - $basearch - Source
#baseurl=http://download.fedoraproject.org/pub/epel/5/SRPMS
mirrorlist=http://mirrors.fedoraproject.org/mirrorlist?repo=epel-source-5&arch=$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL
gpgcheck=1

Now you can install using:

yum install git git-daemon

Creating and sharing a repository

Creating a repository is easy! Simply create a folder and type git init.

mkdir newrepo
cd newrepo
git init

Once created, we can copy/create our files (think svn import) and then do:

git add .
git commit

Once you’ve created a repository, you’ll probably want to share it. This means that other people can pull and push changes. There are a number of ways of accomplishing this (this blog post lists 8 possibilities). My usual method for sharing SVN repositories is via Apache; Git supports this as well. I think one of the simplest solutions is to use the Git Deamon. To allow others to pull and push you can share your Git repository using the following command:

git daemon --reuseaddr --base-path=/path/to/repos --export-all --verbose --enable=receive-pack

This command will share all repositories found within the folder /path/to/repos (so we would have created our “newrepo” folder within this location). Once shared you can clone the repository using the git resource locator syntax:

git clone git://remote.computer.hostname/newrepo

Or you can just use the IP address if you’d prefer.

You should now have a repository setup on a CentOS 5 server which you can clone and then pull/push updates.

Tortoise; the familiar client for Windows

When I’ve used Mercurial in the past (another distributed SCM), I’ve actually found the command line tools significantly easier to use than the GUI (Tortoise). However there is a level of familiarity that a TortoiseSVN-like frontend provides.

Tortoise Git

TortoiseGit has all the features you’d be used to from using TortoiseSVN plus Pull, Push and all the other Git-specific stuff.

Git hub

It’s worth making a quick mention of Git Hub. According to the website, “GitHub is the easiest (and prettiest) way to participate in that collaboration: fork projects, send pull requests, monitor development, all with ease.”.

Git Hub provides a handy way of visualising a Git project (listing commits, branches and pretty-printed code). It avoids the need to setup your own central Git repository and mess about setting the server up. A lot of projects seem to be moving this way, for example Symfony.

Finding out more – useful links

http://whygitisbetterthanx.com/

Some well thought out and concise arguments as to why Git is better than other SCM systems.

http://gitready.com/

Excellent site – “learn git one commit at a time”. Lots of help and advice clearly laid out.

http://git.or.cz/course/svn.html

Crash course for SVN users – really good comparison of SVN commands and the equivalent GIT commands.

http://www.jedi.be/blog/2009/05/06/8-ways-to-share-your-git-repository/

8 ways to share your git repository – file share, Git daemon, plain SSH server, SSH server git-shell, Gitosis, Apache http + gitweb, github.

posted on Wednesday 27th January 2010 by Dave

Getting started with Flash AS3 from a PHP developer’s perspective

They say that to become a better programmer, you should learn a new programming language. Exposure to a new language allows you to step outside of the patterns and workflow you’re used to and tackle a problem from a different perspective. There are extremes to this, for instance learning Haskell (an advanced purely functional programming language). I’ve decided to postpone my learning of Haskell, for now, and instead learn Adobe Flash AS3.

This blog post aims to give an introduction to Flash AS3. One good thing about Flash is the large volume of tutorials out there; however this is a double-edged sword. Seperating the wheat from the chaff can be hard work. This post aims to give an introduction to Flash from the perspective of an experienced PHP programmer; someone who already understands the key ideas of OOP. It covers:

  1. Getting started; how to organise code
  2. Adding Movie Clips to the scene
  3. Handling events
  4. The power of arrays
  5. Download source code

Once you’ve put it all together, you’ll have a super-duper rocket-firer (the big white space below is the finished article – hint click on the milk bottle bottom left to launch a rocket)

You’ll need FLASH to see this! Makes sense being that this is a Flash tutorial!

One more thing… a big shout out to other tutorials I’ve found helpful, especially the concise and engaging tutorials from untoldentertainment.com. You can check out all my AS3 links on Delicious.

How to organise code

As Ryan explains in this tutorial, Flash beginners tend to put Actionscript code everywhere.

On different frames of the timeline, embedded in nested MovieClips – if i can write code on it, i do”

The fast path to Flash Pro is to put all your Actionscript in seperate files and break your application down into objects (classes). To get started, fire up Flash (I’m using CS4) and create a new “Flash File (ActionScript 3.0)”. Get the Properties window up (click “Window” > “Properties” if you can’t see it). It should look like this:

Document properties

Type “Main” into the Class box. This is the name of the class that will be attached to main movie; the entry-point of the application. Now we can create this class. Click “File” > “New” and choose “ActionScript File”. Paste in the following code:

package
{
  import flash.display.MovieClip;

  /**
   * Main class - entry point for App
   */
  public class Main extends MovieClip
  {
    /**
     * Constructor
     */
    public function Main()
    {
      trace('It works! - blatantly ripped off from the other tutorial');
    }

    /**
     * Another method
     */
    public function myFirstMethod():void
    {
    }
  }
}

This is a basic “Main” class. We could create other classes in a similar fashion. However we should put each class in its own file. Press CTL-Enter to compile and run; you should find the OUTPUT tab shows the “It works” message.

package

Namespacing basically; I’m just leaving this blank for now. Could be package myExcellentPackage

import flash.display.MovieClip

Similar to PHP’s require_once. We need to make sure that everything this class needs is imported. This starts to be a pain when you use various libraries / built-in Flash classes as you need to know what to import. For example, need to change colour? You’ll need import flash.geom.ColorTransform. Easiest way of knowing what to import is simply to Google it.

public class Main extends MovieClip

The class declaration.

trace(’some text’);

The Flash equivalent of print_r, ish. Useful for outputting console messages for debugging.

public function myFirstMethod():void

Another method declaration. Notice the :void bit which defines the return type explictly; I quite like this feature. Another important thing to note is that the constructor Main must not have any return type defined.

Adding Movie Clips to the scene

To more further down the Flash Pro road, we need to be able to setup the scene (Flash canvas) programatically. We need to be able to add things and remove things as needed, for example adding a high-score table at the end of a game or a splash screen at the beginning. In this example we’ll add everthing to the scene using code. It’s dead simple.

For my rocket firing example, I want to fire the rockets from a milk bottle. We’ll create this as a Symbol. According to the first thing I found on Google (it must be right), a Symbol is simply a reusable object. From a code sense, a Symbol is like a class. We can create copies (instances) of the Symbol and place them in our scene; we can do this from code or from within the Flash GUI. To create the Sybmol, from the FLA, click “Insert” > “New Symbol”. I’ve added a JPEG picture of a milk bottle on the Symbol.

Now we need to set it up so we can tie the Symbol to ActionScript. Get the Symbol properties window up. The easiest way is to find the Symbol from the library, right click on it and click “Properties”. Fill out the details, as shown below:

Symbol Properties

The key thing is the Class property. This is the class it will be tied to in ActionScript.

Now we’ll adjust our Main class to get this added to the scene. Adjust Main.as by adding in the property launchpad and adding to the constructor:

  /**
   * Main class - entry point for App
   */
  public class Main extends MovieClip
  {
    /**
     * Milk bottle (launch pad)
     * @var MovieClip
     */
    private var launchPad:MovieClip;

    /**
     * Constructor
     */
    public function Main()
    {
      launchPad = new milkBottle();
      launchPad.x = 50;
      launchPad.y = stage.stageHeight-25;
      launchPad.height = 40;
      launchPad.scaleX = launchPad.scaleY;
      addChild(launchPad);
    }
  }

private var launchPad:MovieClip;

So you’ll probably recognise this as declaring a private class property. The key things is the :MovieClip which defines what type of variable this is. You’ll see this a lot in Flash.

launchPad = new milkBottle();

Create an instance of our milkBottle class (Symbol). You’ll notice that we don’t need to bother with this. (or $this->) as we would in PHP to access the member property launchPad.

addChild(launchPad);

This adds the object to the stage. It’s a bit like the JavaScript DOM in a lot of ways. This is the simplest way of adding an object to the scene; we can also use other methods like addChildAt(launchPad, 1).

launchPad.height = 40;

Resize the object. launchPad.scaleX = launchPad.scaleY completes the job – adjusting the width automatically to make the aspect ratio correct.

Handling events

Event handling in AS3 will be instantly familiar to anyone who’s used jQuery, or indeed JavaScript, so long as they’ve stuck to the idea of seperating content from functionality (no inline event handlers). For the rocket launching app, we need the launchPad object to handle a mouse click event and fire off a rocket (click on the milk bottle to fire a rocket!) We can attach an event handler by adding the following line to Main().

  launchPad.addEventListener(MouseEvent.CLICK, launchRocket);

To access the event handler functionality, we also have to make sure we import the right libraries.

  import flash.events.Event;
  import flash.events.MouseEvent;

Finally, we need a method to handle this event. We’ll add this to our class:

  /**
   * Launch a new rocket
   * @param Event e
   */
  public function launchRocket(e:Event):void
  {
    trace('Launch!');
  }

The power of arrays

So we’ve made a good start. Next up, we want to be able to launch a rocket. We want to launch one whenever the milk bottle is clicked. Some how we need to keep track of all these rockets. This is where an array proves useful. We can add a new class property.

  /**
   * Rockets
   * @var Array
   */
  private var rockets:Array;

Within the Main class constructor we’ll initialise the rockets array.

	rockets = new Array();

Then we’ll flesh out the event handler to create new rocket objects.

  var rocket = new firework();
  rocket.x = 50;
  rocket.y = stage.stageHeight-25;
  rocket.height = 40;
  rocket.scaleX = rocket.scaleY;
  addChildAt(rocket,1);
  rockets.push(rocket);

Now the rocket class is an interesting one. This is where I’ve started to get a bit more interesting – starting to explore Flash. The key thing to realise is that the rocket class encapsulates the behaviour of a rocket. It sets itself up (via the constructor) and then it adjusts its own behaviour (in this instance location on the stage) each frame. How it does this is another interesting part of Flash: the Event.ENTER_FRAME event. Within the firework class constructor we have:

  addEventListener(Event.ENTER_FRAME, updateState);

This fires off an event every time Flash starts a new “frame” – n times a second, where n is defined within the main Flash file’s properties.

If you dig through the firework class (and indeed the smoke class), you’ll see how they set themselves up, update their state once a frame and eventually remove themselves once a predefined lifetime has completed.

Source code

Click here to download the full source code.