Game Design, Programming and running a one-man games business…

Big project code architecture

I actually wrote some new code for Gratuitous Space Battles today, and in doing so, I had to do a bit of chin-stroking and thinking about the general code structure of the game. GSB is not my latest game, so the code I write now for new stuff is more polished and better designed, but even so, I’ve coded about a dozen games from scratch before GSB, so it should be fairly good.

..and yet…

One of the best lessons you learn, mostly from experience, is how to arrange classes and objects in your code so that its fairly modular and not ‘closely-coupled’. In C++, what this amounts to is you shouldn’t have a lot of classes constantly using get() and set() calls to talk to the private members of other classes. Separate objects and basically separate for a reason. The hardest decisions to make in code are the big architectural decisions about what objects sit inside others, and who inherits from what. I’ve been coding in C++ for decades now, and I still know I often get it wrong. This stuff is really important.

For example, there is a class is GSB called SIM_Ship, which contains all the gameplay related stuff for a ship (totally separate from the visual representation of the ship which is entirely different and in a totally separate class). In an ideal world, that ship class would be pretty self-contained, without a ton of connections to other objects.

The problem is, over time, I’ve ended up adding stuff to that class that bloats it and makes the code messy. For example, it has a function in there to shake off limpet mines. This is a bad idea. The limpet mine code should all be within SIM_LimpetMine. SIM_Ship not only shouldn’t get involved with the limpet code directly, it shouldn’t even be aware that the concept of a limpet mine exists. That stuff should be modular, residing elsewhere and making use of generic functions and hooks within the ship code from a distance…

Sometimes that doesn’t work in practice because of optimisation and speed considerations, but often, the reason stuff ends up in the wrong place and the code gets confused and messy is that the coder is just adding ‘one more thing’ and doesn’t think its worth going to the trouble right now…

But when you are starting a big new project, its good to keep these issues in mind. Assume you will have 5x the code you think you will have, and then plan the code architecture accordingly.

Intels performance analysis stuff

So intel sent me a new laptop, because they are wonderful nice people, and one of the reasons for this is to get game developers to use their amazing performance tools, and to see how modern games run on the very latest intel hardware. The laptop in question runs GTB very very well, and the graphics card is apparently an intel HD Graphics 4000 . I have to say, that if I plugged a mouse in, I could probably quite happily use it as a main gaming PC, which is a vast surprise to me.

Anyway, the main thing that impresses me is this intel graphcis performance analyzer stuff. You run a small program on the target PC (the laptop) and then run the analyzer stuff on another PC, connecting trivially by IP address. You run a game, and then you can connect to it on the second PC and view dozens of real time graphs on all sorts of crazy stats as you play. It’s pretty impressive stuff. AQTime can do this after-the-event, and it’s great for really working out exact lines of code that need optimizing, but this is a great way to get insight into the GPU, rather than the CPU.

Clickable full size screenshot below:

If you have a PC with an intel graphics chip, give it a go. Their performance analyzer stuff seems to be free, which is also awesome. I think for the first time in ages, intel video cards may well start to become serious rivals to ATI/Nvidia. Also the contrast between nvidias attitude to indie game developers (totally ignore me, even when reporting a bug), and intels is night and day.

Heres a video showing some of the stuff:

Pushing 2D engines further

I have dabbled very slightly in 3D (Don’t be alarmed), and have retreated in terror at the additional grief it requires, and the compromises required, and the general ickiness as it seems to me.

So I am pretty unlikely to release a proper 3D game in the next few years, at least. I personally do prefer 2D games. However, that doesn’t mean I do not enjoy nice graphics, and certainly there are a ton of nice looking 2D games out there. The majority of them are side-scrolling or similar style games that rely more on a very good piece of art direction (Braid, World of Goo), than they do any sheer horsepower or rendering muscle. I’m not aware of many 2D games, indie or otherwise that tax a CPU/GPU as much as Gratuitous Space or Tank Battles.

However, having just bought a new PC, AND watched the latest cryengine demos, it’s pretty clear that modern gamers have enough firepower to render the bejezus out of anything in 2D, so we are in the happy situation of twiddling our thumbs thinking what crazy stuff to add next time-around.

One idea is is to push particle counts through the roof and have seriously complex explosions and smoke trails etc. This could certainly be ramped up, although smoothly transitioning to it from low-spec PC’s is a nightmare

Another idea is to use a LOT of different layers and components to build up individual units to give them a more unique feel. Obviously GTB is a big step up from GSB in that respect, but it’s got way, way more scope.

Shadows and lighting are two other possibilities. GTB infantry uses blob shadows. Animated shadows are certainly doable, but involve a crazy amount of texture RAM to do right. Maybe worth investigating. Faked 3D lighting using that clever deferred rendering thingy is another (fairly tricky) option.

More detailed environments is another. The problem here is art budget. It’s all very well saying we need 120 different bush or pebble models. Someone has to make them, and get paid for it.

More cunning explosions, using some sort of clever physics modelling, or procedural whatnots, or clever multi-layered shader thingies, is another option.

Of course the real problem is TIME. I already have this big scary change to Gratuitous Space Battles waiting for me to release for everyone. It’s just sat in-between holidays, GTB updates, redshirt, and talking to builders. Plus another new thing I haven’t revealed yet. I am quite tempted to just push out my GSB update very soon ‘as-is’. I was hoping to find a cunning way to use it to raise money for charity, but that’s a whole other story…

That steam stutter bug, and tracking it down…

Right from it’s initial launch on steam, there was a bug reported in Gratuitous Tank Battles for a tiny subset of steam players. The bug was basically that the game ran horribly slowly, with stuttering and skipping music.

Regular blog readers will know I am a bit obsessive about optimisation, so naturally this seemed weird to me. Right from the start I doubted that this was actually the games graphics engine or core gameplay code running slow. Not given the specs of the users reporting it, which was a wide cross-section of hardware types. Happily, this was confirmed by the realisation that running the game from the .exe, outside of steam, enabled it to run perfectly. A good workaround, but not actually a fix.

This was followed by weeks, maybe even months of me investigating what on earth it could be.

Initially, I assumed it must be something about the steam integration code. So that resulted in a ton of going through it with a fine tooth comb and checking every parameter, asking valve if I was doing it right, checking with other devs how I was doing it compared with them etc. I ended up moving the code that initialised steams systems from one point in my code to another, as they recommended, but there was no change. I also changed a callback to run every 100 frames rather than every frame, and again, no difference.

Then, finally we found some helpful players who not only had the bug, but were happy to run diagnostic code on their PC for valve to analyze. A very helpful guy from valve then crunched through it, and found that an alarming amount of time was spent in the sound library, and in Heapvalidate(), called from it. This then led to another 48 hours of me looking at heap validation code, memory errors, installing xperf, the application verifier, trying (and failing) to download Microsoft platform SDKs, lots more poking and prodding…

and then eventually  I got a reply from the sound library people suggesting that yes, it *might* be their code occasionally calling HeapValidate() all the time, and to upgrade to the latest build.

So I did that and it has fixed it…(afaik)

How annoying. The one piece of code in the game not written by me, and it had a bug in it! At least it’s fixed now, so all is well. I bet my code has bugs too :D We still don’t know why it was actually caused by the interaction of the game with steam, but that just seemed to be the randomish trigger for the errant memory checking.

I have a new ninja PC on order. I dread re-installing anything. Is there a magic new way to do this, or will I sit there downloading and installing stuff for 2 or 3 days again?