Consistently Formatting C

From my own projects, I have noticed a trend – code is never consistently styled. I change my own coding style every once in a while, so over the years I notice a siginificant change in source code – even within the same project! I am of the opinion that syntax is not that important, but it is offputting when you see different conventions, and you want to make them consistent. I ended up spending a lot of time restyling old code to match my new conventions – time I should be spending writing games.

Recently I did a bit of programming in go and I liked their approach to this problem. Go provides a tool called gofmt which automatically styles source code to the de facto go code style. This means all go programs ending up looking fairly consistent.

This looked pretty cool to me, but I do most of my programming in C/C#/python. There is a standard for styling python programs called pep8 which solves this problem nicely, and pylint will help keep you close to the standard.

For C and C#, there does not seem to be any canonical formatting style (Microsoft do give some guidelines on style for C#, but I don’t know of any free tool which enforces these – please comment if you do).The wikipedia page on indentation style alone gives many options for arranging curly braces, all with their own advantages and disadvantages. None of them match with what I consider good style either.

In the absence of a standard, we can come up with our own. As long as we are consistent with the standard, there is no problem. So we need a tool which will style our code to this specification. I found 2 free tools which do this :

  1. gnu indent
  2. astyle

Both of them are good tools, but astyle had more options I cared about and supports both C and C#, so I went with that. After half an hour of experimenting with options I eventually came to this line to format a C file:

Now I put this into a bash script called ”enforce_coding_style.sh” in the root of my repo to style all of our source files:

This will also force unix line endings which is nice. So that we never have to worry about the problem again, we can call this script from a git pre-commit hook in our repo “.git/hooks/pre-commit”:

Make sure everyone who uses the project has this enabled, and your code will look consistent forever more. If you ever want to update the standard, you just need to update the options passed to astyle in the bash script.

Update:

Improved bash script thanks to feedback from claudius on hacker news.

My Ludum Dare 22 Entry – LittleEarth’s Last Stand

I created a game!
For Ludum Dare 22 I created LittleEarth’s Last Stand, a Missile-Command style arcade game with 4 levels + an infinite challenge mode which is unlocked after completing the main 4 levels.

I came 222nd for Humor, which I am pleased about. :D Overall I really enjoyed my first Ludum Dare and can’t wait for the next one. Since my last attempt at a game competition didn’t go so well, I took more time to prepare for this event. I used Flixel, an awesome actionscript 2D game engine, written in actionscript. This helped me to get a game completed quickly, and I had already completed a couple of simple games including a platformer and a shoot ‘em up in the 2 days leading up to the competition.

Links:

This is the game in its entirety, I hope you enjoy it!

Get Adobe Flash player

Also features Kittens! :D

go is awesome

Go is a great language, I have just recently started using it for my website and it is so productive.

Its concurrency model is very interesting – using goroutines it is possible to make a multithreaded high-performance application very easily.
The fact that the language is compiled also makes it very interesting for performance reasons.

Along with the performance benefits, syntactically it is very terse.

This makes it easy to write things quickly.
In the future I will definitely consider using Go for my projects.
That said, for gamedev you can’t beat C99. :D

My GLES hello world

I’ve long been a fan of the http://openpandora.org project, an open source handheld game console which was crowdfunded a few years ago.
Recently I’ve been wanting to look and see how hard it was to port a project over to the pandora.
I’ve written a simple 2D game engine which is written in C99 using SDL and OpenGL.
I thought I’d have a go at porting over this codebase to the pandora.
Looking for hello world examples of portable OpenGL code seem to be hard to come by, so I created my own : https://github.com/df3n5/pandora_sdl_gles_hello_world
Hope this helps anyone who was having the same trouble as me in finding a *simple* example of using GLES without a million dependencies.