Friday, December 30, 2011

C++, SDL and oPong

During the peaceful days between Christmas and New Year, I set myself the task of learning how to use the low-level graphics library SDL. My interest was piqued by playing a few games that have used SDL to good effect to cross platforms, especially to work smoothly on linux distributions.

SDL is written in C, which I know fairly well, but because most games and so online tutorials are now in C++ I decided to use that, even though I'm less familiar with it. Since I'm using a full install of Slackware Linux 13.1 (which is fairly old now, but works fine) on my fast desktop machine, I was pleased to find that GNU compiler g++ was already installed, along with SDL and all the other tools such as make and text and graphic editors that I needed to get started.

After a bit of surfing I found myself following this excellent set of tutorials from Lazy Foo - and did them dutifully - I read each one, downloaded the source code, compiled it and ran it and sometimes tweaked it to try out other ideas. I found them easy to read and the code compiled and worked flawlessly each time. Much kudos to Lazy Foo for the effort s/he put in there (despite claiming to be lazy). I also gained a good boost from this tutorial for C++ for C programmers.

So, what is oPong? Well, I decided that after reaching Lazy Foo's lesson 18 that I could try out creating an old game - pong - but with a new twist. In my version, there would be only one bat and it would move around a circle. Also, every time you hit the ball you get a point, but the bat also gets shorter, making the game increasingly more difficult as you progress. I spent almost zero time on how it looks, as you can see by this screenshot:

I found the process of writing it quite straightforward, with two stumbles along the way. I was surprised at just how low-level SDL was - it didn't have any functions for drawing circles or lines. All SDL can do is fill rectangles and draw sprites (i.e. graphics loaded from image files ). The upside is that SDL is fast and gives you fine control over the graphics.

The second problem I encountered was with handling the angles, which caused a hard to diagnose bug where the collision routine failed and allowed the ball to pass through the bat at its left most position. This was caused by the fact that the angles flipped from -PI to PI at this position. I remedied the problem with some extra code that carefully handled the difference in angles.

I'm not sure what, if anything, I may do next with SDL, but I enjoyed getting to grips with it and C++. Also, I am pleased that fairly low-level graphics access is still readily available on our increasingly complex computer systems. Clearly, I draw pleasure from some odd things.

You can download the source code for oPong from here.

Sunday, September 4, 2011

Birth, death and ongoing life

It's been a while since I blogged here and as the dramatic title may suggest, that's not because I've had a shortage of life events to write about; I was busy dealing with them all.

My mother died in April and although her health had been troubled for some time none of us expected it might be fatal. So it was fairly sudden which is shocking, but preferable to the slow, lingering and degenerative exit that my wife's grandmother is enduring along with close family. I miss my mother very much, but the memories are now starting to be fond and comforting instead of just painful.

We welcomed my sister's second child into the world in late January and my mother met him the week before she died. She also knew that in July my wife and I were expecting a daughter. The new life arrived, almost two weeks late at the end of July and, forgive me for gushing, she is just beautiful and a joy to have around.

This is our second child, our son being well into his seventh year. This time we are much more relaxed as parents and perhaps that's why our daughter seems content. That said, our son was a fairly content baby too, so I suppose there is nature, nuture and luck involved.

Late spring and summer coincided with a particularly troubled time at work, not just for me, but for some friends too. Life sometimes seems to throw everything at you at once, but in a way that can be a good thing. For me it helped keep things in relative perspective. The important thing is to press on and, as a local, Kipling-esque expression goes, keep the head.

Saturday, February 26, 2011

Old phone, new life - donut to froyo

Desire Z and G1 - both on Android 2.2 "froyo"
 I got my G1 phone (also known as an HTC Dream) in early 2009, a few months after it first came out. I recently replaced it with an HTC Desire Z (known as the G2 in the US) which I think is great. But, I really liked my old G1 and was a little sad to let it sit in the corner gathering dust. But, after 2 years, surely it was time for the G1 to retire, I mean, it can't keep up with Android Desires and iPhone 4s?

The truth is that HTC decided not to take the operating system on the G1 beyond Android 1.6 "donut". But could its admittedly meagre CPU clock rate or limited internal memory let it run Android 2.2 "froyo" in a way that in any way compared to my new phone?

To find out I decided to root the phone (gain complete and low level access to it) and install something called the Cyanogen mod. This is a build of the operating system made possible by the fact that Android is an open source project. I've heard people claim Android isn't properly open source, but I think that's just  plain wrong. The google apps (gmail, calendar, maps etc) that usually come with Android are not open source, but they're not part of the operating system; they can easily be replaced.

The installation of the Cyanogen mod is not that straightforward, but it doesn't require anything truly hardcore, such as modifying the hardware or editing source code. Have a read of my detailed account if you'd like to know more. My experience was made more troublesome because my phone was locked to T-Mobile, which means that it will only accept a T-Mobile SIM card. This meant that the phone refused to do anything without a fair bit of hacking right at the start, which I found very frustrating.

Anyway, after a  bit of a fight I got it installed and I was very impressed. My G1 is noticeably snappier running 2.2 than it was with 1.6 and, although not as fast and smooth as my Desire Z, it's not that far off. The browser is quite zippy, the pinch zoom works (perhaps with a slight stutter) and google maps work fine - it's all quite usable. More than that, it looks better too and even has window effects and animations (if you're into that kind of thing).

In fact, I'm left wondering why my Desire Z isn't much faster than it is. That could be because the Sense UI that HTC put on it is inefficient, but I suspect it's the other way around: the specs of our phones haven't increased as much as marketing and anecdotal "look how cool my shiny new phone is" comments have lead us to believe, instead any gains in hardware specs are swallowed up by new features to "sell" the product. Some features are useful, but others like pointless apps and stupidly high screen resolutions are not.

Well, my gratitude to Mr Cyanogen and his merry band and to the open source-ness of Android. I was amused to see that the little robot widget on my G1's new home screen sprouted a speech bubble that contained the few commands needed to extract the latest version of Android from a code repository and build it from source. One day I'll get around to building the Android source code myself.

Tuesday, February 22, 2011

On the sleeper

I've found myself travelling around the UK quite a bit this last week, taking in three of my favourite haunts: Glasgow, London and Cambridge.

The majority of the travelling was on the sleeper train. The sleeper train departs Glasgow a little before midnight, when central station is huge, hollow, smooth and clean. You walk along platform 10 and are greeted by a conductor (I'm not sure if that's the right title, perhaps carriage concierge is more appropriate) who checks your ticket and who records your preference for tea or coffee and cooked or continental breakfast. I’m not sure why they ask you. I always get both tea and coffee, and even if I ask for continental, I get handed a hot packet with MEAT written on the label.

The carriages are definitely in the British Rail slam-door era, circa 1990. It's a bit like a working museum in some ways. Perhaps I give the impression that I don't like the sleeper. Nothing could be further from the truth, I love it. It’s clean and functional but is most definitely not “shiny”. In the toilets, I'm delighted to find that the foot button approach to flushing toilets and operating taps is still employed.

And, possibly my favourite bit: there's a restaurant car. Well, it's more like a dimly lit bar car. Again, it's not decked out in the latest fashions - it's all a bit early 1990s - but I like that. The stewards are endearingly grumpy. One tall, bald-headed Glaswegian steward entertained me upon one of my first trips on the sleeper by explaining how he was hit on the head with a brick upon entering a public house in Kilmarnock. "How delightful!" I chirped as I clasped my hands together excitedly, "and what of the other scars upon your face, what stories do they hold?" OK, I didn’t say that; he might have thrown me through the window.

On my most recent trip I was genuinely pleased to spend a good half an hour talking to a lovely old lady whom I knew from my student days at Glasgow University Observatory. She's 80 this year, but full of energy and was charging down to London to exhibit a collection of original letters and photographs sent between various famous astronomers of yester-century. I handled each carefully mounted and protected A4 wallet with great care as she handed them to me over our table of wine, sandwiches and Laphroaig. Marvellous.

She told me her habit was to finish her refreshments and retire when the train reached Carstairs, where (I think) the train gets shunted together with the Edinburgh sleeper, rather clusmily. I didn’t have the staying power of this near-octogenarian, so bade her goodnight before Carstairs.

I made my way down the narrow corridors past the row of small cabins. It’s so narrow that you have to angle your body and walk slightly sideways. But, as I was walking down the corridor a hand reached out from a cabin doorway and grabbed me, pulled me in and threw me to the floor. It was the henchmen of my evil nemesis, Professor Moriarty. We struggled for a while until I bested one by trapping him inside a folding bunk. The other I managed to eject through the window after electrocuting his metal teeth with a broken table lamp bulb. I then climbed into bed with Eva Marie Saint. I may be getting this bit confused with a few films. Hmmm, hard to be sure.

If you get fed up faffing around at security at airports or are bothered by short haul air travel's pollution and wastefulness, or just fancy something different, give the sleeper a go. And do so soon before they make it all intolerably shiny like everything else these days.

Saturday, January 1, 2011

I was very pleased to get a subscription to as a Christmas from my wife. Magnatune is a bit like fair trade for musicians in that 50% of its revenue goes to its musicians and their terms and conditions are equitable and open. For example, magnatunes does not demand an exclusive deal from its musicians.

While you read this blog post, why not hit play on the little embedded player below and have a listen to an album I recently enjoyed from Magnatune?

Between Worlds by LYNX and Janover

It's not a competitor for iTunes though (but that didn't stop Apple rejecting the Magnatune app for a while) and as such don't expect to find any Lady Gaga amongst its 10,000 tracks. It does have a very wide variety of music that is of high quality in terms of recording and musicianship; this is to the credit of the site's founder John Buckman. Apparently he sifts through a large number of submissions and rejects more than 95% of them. At first this sounds like a weakness, but after listening to a dozen or so albums, I'm none-the-wiser as to what John Buckman's taste in music must be, except possibly that it must be very broad.

You don't need to subscribe to listen to the music, you can access it all for free via the website or via a variety of offline players including apps for both Android phones and iPhones. I mostly listen via an application called Amarok which is part of the KDE desktop environment. Magnatune donates some of its 50% to help with the development of such open source or free (FOSS) applications.

Before getting my subscription I enjoyed the messages that played between the songs. One of them went somthing like "Hi, I'm John Buckman's 76 year old neighbour. I don't much like most of the music on this site but you probably do because you're listening to it. Why not consider paying $15 for a subscription...".  In addition to freeing you from such announcements, the subscription also permits you to download as many albums as you wish with zero DRM and even to share each album with three of your friends.

I applaud the good (i.e. not evil) folk behind and wish them every success.