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.

1 comment:

  1. I am continually amazed at what can be done in games and special effects these days - it must be fascinating to be able to dable/explore this stuff - I am consigned to the ranks of people who point at these wonders ooing and ahing! Good luck with your faulty bat - I had one like that in high school - couldn't hit a damn thing :-)