Monday 7 September 2009

How to use Playstation Controllers on PC

Last week I posted how useful I'd found the wired SingStar mic's USB adaptor to be outside of the SingStar game. Writing that post got me thinking about the old PS2* controllers and how useful these could be. Like many PS3 owners, I wasn't keen to shell out £35 on a second wireless controller and purchased a PS2/PS3 USB adaptor. This works pretty well on Little Big Planet or any other games not reliant on SIX-AXIS motion.

Because I had purchased this adaptor for the PS3, it had not occured to me before now to try plugging this USB adaptor into my PC. I tried this today and was pleased to see it automatically install as "HID-compliant game controller" on both XP and Vista. The next thing I did was fire up TrackMania Nations Forever on the hunch that it would be fun to play with a game controller. The controller was instantly recognised although all actions seemed to be assigned to the buttons on the left side of the controller. I prefer steering on the left and acceleration on the right, so I went into the Inputs configuration.

TMNF Inputs for game controller

This is the point where things started to get a little confusing. I had no idea which button was Button 0 or 1 or 4, etc. So after a little but of experimentation I put together this handy reference image.
PS2 button numbers

I even tried this with an old PS1 controller and it also performed well (apart from the controller's buttons being a little sticky). The only difficulty I found (apart from mapping the buttons) was finding games that support game controller input, TrackMania is the only game I have installed that does. It is nice to play it with a controller though.

* Connecting a PS3 controller is a completely different process which is nicely addressed by davies lim and HowToGeek, although you map still find the the button map above to be useful.

Friday 4 September 2009

Short rant about quotes

It seems to me that a high percentage of PHP programmers are making a simple mistake in their code that increases the execution time of their scripts. I certainly seem to be frequently fixing the problem in other people's code, whether I am maintaining a website, or using an open source class or plugin, etc.

To some, the following two statements might appear to be functionally identical, but in the second statement PHP has to parse the double-quoted content and check for any variables to evaluate before outputting it.


echo 'Hello World, so long and thanks for all the ghoti!';
echo "Hello World, so long and thanks for all the ghoti!";


If you're thinking, "So what? It's just one line..." then you are not thinking like a programmer. "Expect the unexpected" is one of the first rules of defensive programming. Imagine using this statement in a function or method that is executed many times in one script, or if it's called a few hundred times from within a loop, and maybe on a page which suddenly gets ten times the traffic you expected. All those extra CPU cycles quickly start to add up.

Now, think about all the times that static text is used inside your scripts outside of echo statements, because they will be parsed in exactly the same way. It's easy to imagine how this can start making a difference to the resources your site uses.

In a nutshell, if you're not evaluating anything in the string, use single quotes.

NB: ghoti = fish.