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

Some optimization tips for game programmers

I’m enjoying myself with some optimizing today (yeah I’m weird like that). So I thought I’d jot down some of my tips for making your game faster. These are general, not language-specific tips.

Never run code you don’t have to run

Seems obvious but few people actually do this. For example, in democracy 3, the simulation calculates the popularity of each policy by asking every voter if they benefit from it. That question is complex, and there are a few hundred policies and 2,000 voters. This takes time. Solution: I only ask them about a policy if I need the answer right now. Some policies can go a dozen turns without the player ever checking their popularity, so why keep calculating it?

Batch Stuff

If you have a dozen icons that are always drawn one after another on the same screen, stick them in a texture atlas. If you are 100% they will never overlap, then draw them in a single draw call. the less texture swaps and draw calls, the faster your code. This is trivial to do in 3D, an absolute nightmare to do properly in 2D, but it’s worth it.

Cache Stuff

If you have a variable that is complex to evaluate, evaluate it once, then cache it until it changes (we tend to call that setting it ‘dirty’). If there is some data that is going to be accessed a LOT, then make a local copy of it. And if you have a lot of stuff to write to disk, to the same file, buffer it. Writing to or reading from a file is slow, especially if you are going to do it a lot. Reading in a single file is much quicker than opening 200 of them one after another.

Don’t use sqrt()

Do you ever use sqrt()? never realised how scarily slow it was? Most of the time you can keep the squared result and use some clever tricks to not actually need the sqrt() result. If you were going to get the sqrt() and compare it against a value, just multiply the comparison value by itself and check it that way instead. it’s amazingly faster.

Use the right container class

Sometimes you will use a list where a vector will do. The vector is MUCH faster. And you know what is faster still. I mean REALLY fast? An array. If you really need speed, and the array size won’t change that often, allocating an array of items is much faster and is worth the overhead.

Re-use objects

If you have a collection of objects that keep getting created and destroyed, you want to wrap that up. Stick a factory object around them to handle their construction and destruction. That way, you can just set them inactive on destruction, and save yourself the hassle of the creation and destruction when it comes time to re-use them. Setting a single flag to say an object is ‘dead’ is way faster than calling a destructor, and resetting is way faster than a constructor.

Obviously there are lots more tips, and you should get a decent commercial profiler. That PC you develop on is a super-computing beast. if your game takes more than a few seconds to load, you are being sloppy.

 

Dumb multithreading question (how to safely quit a multithreaded app)

I’m not a multi-threading expert. my main process trundles along nicely then when loading the game it calls CreateThread() to do some loading stuff. Then, while the main thread is rendering my lovely loading screen, and the new thread is loading in data, some evil player hits the top right X button, or alt+F4s the game…

Instant crash!

Unless I add code which calls my thread manager and then calls TerminateThread(). This works…but then the app does not actually properly quit, so it can’t work right. Also… I now read that TerminateThread() is not the way to exit a thread anyway, and I’ve been using it all this time… arghhh!

How do you handle quitting a multithreaded app when the player arbitrarily exist it? Nobody seems to have a definitive answer…

Democracy 3 and rendering crisp text in directx

Democracy 3 is a text heavy game. Text and stats, and graphs. So it’s pretty important it is all legible. I’ve spent the best part of two days monkeying around with my text rendering code, which is part of my library (all written by yours truly), trying to get better results. My aim is obviously easy readability. here is some text from the current (Democracy 2) game: (excuse the bitmap format but obviously I don’t want to add artifacts here…)

oldtext

And here is the current implementation in Democracy 3 (different color scheme too).

newtext

I think the second example is marginally better, but frankly that’s just a better color scheme and bigger text. the actual *quality* of the text is certainly nothing special. What I want, clearly is something as good as the text I see windows drawing in webpages and text documents outside of games. Unfortunately, that isn’t as easy as it sounds.

Most LCD monitors are set up to use cleartype, information on wikipedia. This is a cunning system that effectively increases the screen resolution by using colored ‘sub-pixels’, You can see the effect by taking a screen grab of this page (for example) and then zooming in really close. You will notice this text isn’t really black, or indeed one color. Some sort of cool display/monitor-driver cleverness handles this when it displays text. The trouble is, you can’t just ‘enable’ that for a game drawn the way I do things, where each character is effectively a tiny sprite. It just can’t be done.

Now there ARE some clever solutions to get amazing text, I know valve have some clever system, but this all involves implementation of a bunch of third party code and shaders. That isn’t a big deal *as such*, but it means that I have to cannibalize my extensive current GUI library and font loading and text-wrapping/aligning/processing stuff to work with all new systems. This would be a ton of work, and increase the spaghettiness of my library, something I am keen not to do.

I have tried just doubling my bitmap font size and using mip-maps to scale it down, but if anything that looks worse, so I’m trying to keep to my current system, which uses a 1:1 pixel to texel mapping with point filters engaged to get exactly what I see in my font bitmap file.I have a horrible feeling that swapping to black text on white screens is just going to look far worse than white text on black, at least from a  text-crispness POV.

There is of course, also the issue of opportunity cost. Is more time spent on this a good idea when I could spend that time elsewhere in the GUI, or for that matter, the simulation or game balance. Democracy 2 never earned me anyone bemoaning it’s crappy text, maybe the new text is good enough? Developers – What system do you use for text-rendering in games?

edit: maybe this black version looks better?

blacktext

Saving and Loading

How I envy developers working on arcade games, or anything that has a very simple game ‘state’ that can be easily saved and loaded with no grief. For the game I am currently working on (announcement later this month), it’s a bit of a pain, because the actual game ‘state’ is horrendously involved, and every single byte of it has to be loaded and saved perfectly. The problem is mainly due to my tendency to code systems that have a huge number of objects that all have pointers to each other, in complex and arcane ways.

In theory, stitching everything back together after a load is fairly easy, but in practice, it’s a bit of a pain, and the REAL pain is to know that it has all worked perfectly. if 99% of the pointers are right, and 1 isn’t, then I have a horrible dangling bug somewhere…

My current process for preventing this is to have the game completely save out it’s state and load it’s state back in every turn (it’s a turn based game). At the moment, due to arcane debugging issues this is horribly slow, but eventually will be super fast. At least doing it this way means that during development I’ll spot any save/load inconsistencies really quickly.

I’m aching to get on with some GUI stuff to make it look nice, but I’ve learned the hard way that I have to put that off until the foundations work better. Better to have it stable now, rather than fix it later.

Stuck in a world of legacy code

I use the odd bit of code that has char* in it. There I’ve said it. It feels like a big confession. I bet some programmers just sprayed their coffee everywhere. I’m sorry. They say the first step is admitting that you have a problem.

I’m a very very productive game developer. I churn out a lot of very very big and ambitious games. One of the ways I manage that is that I don’t change my ways unless I can make a really good argument for doing so. I never learned what on earth .Net was, no I don’t mind it’s passing. I don’t really know C#. I have no idea what design patterns exist other than singleton. I have no idea what a UML diagram is (is that even a thing? I don’t know…)

You have no idea what a huge big deal it was for me to switch from directx7 to directx9. That took some doing*, and it’s why I still use DX, not OpenGL (which would make apple mac ports far easier and cheaper).

While I’m in confession mode, here are a few other howlers you might be amused by:

  • I manually type out for() loops without using for_each()
  • I use FILE* not the stream stuff
  • I use my own GUI library, and Input code, not third party stuff. In fact I wrote my own hash table code too. The only stuff I rely on is STL, and only partially use std::string

Scary huh?

Why am I such a luddite? Well it comes down to time. If I had regular bugs that I could attribute to passing around char* now and then rather than a std::string, then things would be different, but I know all the functions that handle chars better than I know STL, so I code much faster that way. I have a slight niggling doubt that for_each() is as fast as it should be. I can’t remember the iostream syntax, so I use FILE*, because frankly my file handling code was written years ago and still works fine, so why change?

I see a lot of coders jump on every code bandwagon, and learn new stuff all the time. They are constantly rewriting their whole engine, and constantly going back and changing the code they wrote a month ago. They rarely ship games, and very rarely ship large ones. To me, it always makes sense to make occasional big jumps, rather than constantly fight to keep up with the latest stuff. I have no idea what was in Directx8, it passed me by totally. I’ll maybe move to directx12 (more likely opengl).

It may make me seem old fashioned, but it’s incredibly productive.

*it’s not learning the API, but building a bug-free FAST engine that correctly encapsulates the API that takes time.