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

Lazy Execution in games (pseudocode)

I’ve been testing a bit of a worst case situation for the battle screen in the game where there are just TONS of fighters, and they all have contrails and running lights and you try to see as many of them as possible onscreen.

Not surprisingly the game chugs pretty slowly during that. I’ve been looking at ways to speed the rendering up and have realised something I haven’t been doing enough of is lazy execution.

So what is lazy execution in non coding terms?

Imagine your program is designed to tell a robot how to cook (hey why not?). You might have a bit of code that says “Go to the fridge and grab some butter”. That involves a ton of steps, it would involve moving towards the fridge, locating the door, opening the door, removing the butter, etc.

So what you probably do is write some code like this:

FetchItemFromFridge(item)
{
Find fridge
Open Door
Take Item(item)
Close Door
}

And all is lovely and wonderful. Now later on in the programs development you realise you need to do that a lot of items, so your code in another area of the game looks like this:

FetchItemFromFridge(butter);
FetchItemFromFridge(milk);
FetchItemFromFridge(ketchup)

Weird recipe huh? Anyway. this sort of code gets written a lot, especially on big games, because the guy writing the fetchitem code is a different guy to the one writing the recipe code. Often recipe-coder has no idea what happens inside the fetchitem code, so he can’t see any reason to not call it multiple times.

Of course, the sensible re-written code is this:

Find fridge
Open Door
TakeItem(butter)
TakeItem(milk)
TakeItem(ketchup)
Close Door

Now the downside is, that’s a pain to code, because you need to be aware of the circumstances and write code in each case to handle it. This is what lazy execution solves.

Lazy execution means that when I write the code that says “open door”, I don’t actually open the door at all. I just make a mental note that someone wanted the door opened, for reasons we can only speculate about. The good news is, if someone else asks to open the door immediately afterwards, I don’t need to do anything at all.

The real ‘action’ happens when someone calls the ‘takeitem’ code. At that point, the clever lazy stuff says “whoah! you cant take an item without the door being open, I better open that now. At this point, the previous instructions that had been ignored actually get put into action.

The drawback is that the fridge door close needs to be lazy too, which would impact on your carbon emissions :D

Obviously its more complex than that. Incidentally, this is how a lot of directx works. When you call SetRenderState or SetTexture, sod all happens. It only actually does anything if you render something.

The really good news about using lazy code, is that you can optimise anyway all redundancy. In my case the pseudo code causing some issues was stuff like this:

LockVertexBuffer
for each particle
CopyToVertexBuffer
UnlockVertexBuffer

By making the Lock() lazy, the lock is never called (nor is unlock) in cases where no particle needs to be drawn. On big maps, with most of them off screen, this is quite common.

Back to optimising

So the fullscreen / windowed toggling is still shaky, but the game now runs in both windows and fullscreen and in pretty much any resolution, including my own 1920×1200 res. Listening to music from star wars whilst testing the game fullscreen at that res with a big battle is a flipping joy.

I’m in ‘lets minimise the number of textures used’ mode. Even at the start of a battle before much fighting, I have this, and it’s not pretty. (click to enlarge).

The problem is the running lights use a different ‘blend mode’ so putting them in the same texture as the ship saves me nothing :(. Lots to think about here. SetTexture() can be pretty slow, and you ideally don’t want to be doing hundreds of them every frame. Of course, in games programming, everything is a compromise. The good news is I’m only using up 70% of the CPU to do this stuff, given a minimum 60 FPS.

Insane days work just to swap screen res :D

Today, I was awake at 6am as it was holiday morning. Not for me, I’m staying here, but it means I was awake at 6am, and naturally, ended up programming by 7.30am.

Sooo… I thought I’d get the screen res code done today, and here it is at 11.55PM and it’s still not 100% what it should be. I’ve stopped for food twice, and didn’t linger over it. Bah.

Now granted, I spent maybe two hours dealing with the upcoming German translation of Democracy 2, but the rest of the time I’ve had my head buried in the directx documentation trying to find out just how the hell it’s supposed to handle a screen res change under directx9. I finally got there, and I’m still ironing out the bugs in certain circumstances, but my main objective now at least looks doable, which was to retain the feature all my games have of ‘in-game’ resolution toggling.

I HATE it when games ship with a separate ‘launcher’ app that you use to set the screen res. It’s just so fiddly. I much prefer being able to fiddle with such things, especially windowed / fullscreen toggling ‘on the fly’. I doubt it will be possible mid-battle, but at least it will be a part of the main app itself. Plus it makes it trivial for me to test that the GUI works in all resolutions. 768 really pushes it height wise, but I’ll sort it.

Today was not fun, it took longer than it should had, and it was seriously frustrating and involved. The idiot who knocked on my door to try and doorstep-sell me energy supplies got told where he could shove his special offer :D

explosions, texture swaps, allsorts…

I spent part of today adding new explosion effects to the game. Until now, all my particles have had random rotation. This makes them look natural, but for some explosion effects you want the particles to be ‘stretched’ and thus for them to be angled in the direction they face. Anyway, that’s in and looking good.

Also today I added some basic physics to debris so that when there is a big white explosive flash, it’s applied as a force to the debris which makes the explosion seem more 3D and impressive.

I’ve also been thinking about techniques to reduce the number of texture-swaps per frame, which is currently scarily high in some cases. I can’t easily use z-buffers for the game because the huge amount of alpha-blending that I do. (I tried it, and its slower). One thing I’m considering is more use of (manually assembled) texture atlases, another is to make greater use of concurrency by scheduling some tasks for processing by the CPU while the GPU is busy swapping textures.

I havent implemented resolution changing yet, but I’ll be taking everyone’s suggestions for doing so on-board on monday when I do it. I suspect I’ll display all options above a certain minimum height.

Website updated with FAQ and new screenshots

Yesterday I got the GSB website updated a bit, adding the final logo for the game and some small new screen shots and a FAQ. You can see it here:

http://positech.co.uk/gratuitousspacebattles/

And the faq is here:

http://positech.co.uk/gratuitousspacebattles/faq.html

As well as that, I also got the last few bits of GUI done, so the in-game GUI now is generally the final one, not the crappy one in the BBC video, although I’m hoping to add various bits of polish to it here and there. I also have a tutorial to do, and need to code in support for switching screen resolutions as well. From then on it’s back to play balancing and final weapons and data for the simulation.

The screen resolution system will probably support a number of fixed resolutions to choose from, if your card supports them. In the past, I’ve coded games that basically ask the card what it can do and let the player pick, but that can be hellishly awful to support, as some cards return about 100 options, and some can be obscure. Right now, the game needs 1024×768 minimum, but I might try and squash it to 600 for netbooks.

I definitely aim to support at least one stupidly big res, probably 1900 1200 res. If your video card can do it, the game will look real nice at that size.