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

Basic principles of game optimisation

Making games run faster is a pet topic of mine. Code samples are too specific to help many people, so here are some general principles I’ve learned.

1) Design to avoid the code.

Sounds horrid doesn’t it, but people do it all the time. ‘Skinned’ animation at points like shoulders and knees is CPU intensive and tricky. Ever noticed how many characters in games have big shoulder-pads? Makes life much easier. GSB is a 2D game, mainly because I prefer 2D gameplay, but I can’t ignore the fact that it would be horrendously more draining on the CPU/GPU to be in 3D

2) Run the code offline.

If the output of your code is a one-off, only ever do it once, and before you ship the game. The GSB campaign game code has some really inefficient slow brute force stuff for calculating planet-travel distances. It’s done as a one-off, and the results loaded in at run time. If there are calculations your game makes at run time that are based on data that never changes, just pre-calc them and load in a table, assuming the table is small enough

3) Run the code once, and cache it.

This is the ultimate big win. In the old days, people used tables of cosine/sine lookups, rather than calculating them. Hold on! GSB does that for some stuff too (nothing gameplay critical). Never make a calculation twice in a  function if you can do it once and reference it later. It’s amazing what a difference this makes. even simple stuff like STL list end() calls can mount up with really large containers

4) Don’t run the code every frame.

Not much code has to be run every frame. The minimap in GSB isn’t updated every frame. Can you notice? I can’t, because most frames a ships movement would be ‘sub-pixel’ anyway. If stuff like this seems to jitter, update half the code one frame, the other half the next frame. A frame should be 1/60th of a second. Nobody will notice.

5) Don’t process what isn’t seen.

GSB does some cunning code for all kinds of cosmetic things that only applies if that activity happens onscreen. In theory you can pause the game, scroll around and spot very minor inconsistencies (good luck). With strategy games, most of the time, most of the action isn’t being viewed, so you can skip allsorts of stuff. I don’t update timers for running lights offscreen, for example.

6) Re-order and group stuff.

Graphics cards like to do things in big clumps. give them a single texture, and 50 sprites to draw and they are happy. Give them 50 sprites with different textures and they are not. This is why all the GSB debris is clumped into a single texture, ditto the laser bolts. I also re-order ships in the game code so that similar ones are drawn one after the other, even when drawn as icons. This minimizes texture swaps. Because GSB doesn’t use a z-buffer(for blending reasons), that makes for some spaghetti like code, but it’s worth it. The easiest system I’ve found is to maintain spritelists for different textures. You still draw sprites as normal, but when they draw they just get thrown into the relevant list, and the list gets drawn later.

7) Save stuff for when you are not busy

GSB does this. It’s very tricky, but you can have code that only runs when the CPU is idle. If a laser bolt hits some debris in GSB, the debris explodes. This ONLY happens if the CPU is idling. People spot it occasionally and think it’s l33t. They never spot that it doesn’t happen all the time.

8) Optimize the UI

People get a fast engine then slap some super-slow UI on top. Madness! With a lot of text, icons, dialog box backgrounds and multi-part buttons, you can have crazy overdraw, crazy texture-swaps and huge inefficiency. Keep an eye on it.

Don’t forget that you can also optimize textures too. Some textures are ripe for compression, others not, but you can also do crazy tricks with some stuff, like chop a circle texture into one quarter, and raw it as 4 flipped and mirrored sprites (I do that a lot). Not everyone has 512 MB video cards, and its quicker to load a small texture than a big one anyway.

Most coders probably know all this already, but it doesn’t hurt to recap. It’s easy to forget about optimising and just worry about features, but gamers will thank you for it. I get a lot of people remarking how surprised they are at how well GSB runs on their machine. This is no accident :D.

patch 146

I released patch 1.46 for GSB today. It’s up and running now. This is quite a minor patch, but I thought it worth doing. I must make a mental note to tell steam, stardock etc about it tomorrow. The big feature of the patch is it lets you toggle between the new and the old post-battle stats now with a mere click of a button. Hurrah. The rest is just bug fixes. Apostrophes in usernames cause errors and are no longer allowed, etc…

Tomorrow I’ll be back working on the campaign. There is some new GUI artwork, and a list of bugs to fix. Plus I had some crazy ideas for some gameplay balancing code.

In other news, today was spent partly pacing around the outside of the house seeing where there is room to put solar panels. I’ve long long wanted solar panels, and it would be kinda cool to have positech solar-powered at my end :D It’s pretty scary when you add up how amny units of electricity we use, and then pace out how much space (and cash) is needed to provide all that. Currently, 12 square meters of space is possibly viable, and that’s maybe a third of the requirement. Bah.

One day maybe…

New GSB Website

I’ve updated the gsb website. For the first time since positech started, I’ve actually got a proper web-designer to do  a webpage, rather than me knocking it up myself in basic html and crappy coder-art. Even then, it was a single page, which I cloned and fiddled with for the rest of the site, because I’m so cheap :D

I think it looks way better than before. There are a ton of minor formatting things to fix, and no doubt a lot of the graphics on the ‘other’ pages need tweaking now. If you spot any broken links, then let me know. I’m aware that ‘about’ and ‘faq’ are the same thing. Maybe ‘about’ should go to this blog?

here it is:

http://www.gratuitousspacebattles.com

Campaign Battle Frontiers

My latest playtesting has convinced me to change the way ai attacks happen in the upcoming GSB campaign. Previously, there was a complex system involving local threat levels, which changed over time based on the strength of your fleet combined with the number of enemy-controlled systems linked to your world by hyperspace warp tunnel thingies.
And that works fine, and its cool, and mostly staying.

But what was happening was that you would conquer a system, push forwards and conquer the next system, and there was still a threat ‘behind the lines’ to recently conquered worlds. That was fine too, but I also coded a little ‘unlikely but possible sneak attack’ system whereby any of your worlds could get attacked at any time. If you didn’t have a big fleet sat there, this would be unopposed, you would lose the system, and maybe now have a gap in your supply lines.

Frankly, in game terms, this is a pain in the exhaust-port. It’s frustrating and annoying to lose a system behind the lines, and it’s wasteful to keep a fleet in every system just in case. The good old ‘pushing-back the frontier’ system is better.

I’ll keep the gradually lowering threat level thing, but ditch the sneak attacks. Once you have conquered a world, and parked a big-ass fleet there for a few turns, you can mvoe on and not fear losing it. It is, after all, a big map to conquer.

On an unrelated note, can whoever codes the cursor stuff at ATI get their shit together please? Multiple monitor setups in windows 7 are basically chaos with an ATI card. Random cursor corruption when swapping monitors, and an invisible cursor if it goes into text carat mode and back again on the secondary montior… These are not new bugs, from what I read, so why are you tweaking drivers to get an extra 1 FPS on starcraft when you should be fixing basic windows functionality? Bah!

Gratuitous Mac Battles

IT IS DONE!

At last, you get to blow up spaceships with the approval of steve jobs. We wouldn’t do anything without the approval of steve would we?

Clicky here to go to redmarblegames website and grab the Mac version of Gratuitous Space Battles.

http://www.redmarblegames.com/gsb.html

Tell your friends! Tell your enemies! Tell random people in the street who look like mac owners! (feel free to retweet this too…