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

Optimising my ‘dumb’ textures

I have this class in my code called ‘smart texture’. I’ve had it for several games now. It basically lets me tell a sprite to use “gun.dds”, and it transparently converts that to the LPDIRECT3DTEXTURE9  and doesn’t mention it. It keeps ‘gun.dds’ in memory, and if I ever lose the screen (alt+tab), and need to rebuild stuff, it will replace the invalid surface pointer the next time it gets drawn with a fresh copy.

MAGIC.

Trouble is, that means keeping a list of every smart texture, so that I don’t miss any when recovering from alt+tab. That’s easy, but I ended up using texturedsprites (with a built-in smart texture) everywhere, and thus the list, for a full battle could be 20,000 textures long.

F**k.

So here I am, effectively keeping a record of me trying to fix this…

It’s 5.35PM Sunday. The wonders of aqtime show me that when I ditch the current level and load in a new one, I am killing off about 8,000 smart textures. Every one is going through it’s destructor and removing itself from the smart textured list. This sucks. I need this to be faster. First instinct is to speed up the destructor, but obviously that’s treating the symptom, not the cause. The real problem is 8,000 smart textures. That isn’t so smart, when 2,000 of them are probably pointing to the exact SAME surface in directx… I need to rethink this system, and NOT break any other code…

I *do* have a system called GUI_TextureCache which does some ‘fixed’ storing of textures I use all the time, like UI stuff, buttons etc. This is obviously a similar task to what I want to do here, in that I need a dynamic dumping ground for commonly used textures pointers. I’m concluding that the cleanest way to handle a fix would be within my SmartTexture itself. it can do some clever caching, and then nobody will ever know any code changed!

Realisation that this means I still keep all these smart textures knocking about, with their ‘gun.dds’ strings. That offends me, as a programmer, but tbh, even ‘longcomplextexturefilename.dds’ is only 30 characters, so with 2,000 units I’m wasting 60k here. Big deal.

Right, so the plan is when I call SmartTexture::SetTexture(“gun.dds”) it may, or may not add itself to a list of textures that would need rebuilding, based on if its already in the list.

Problem: I can’t do that, because it means checking the whole list every time I call SetTexture(). That might even be *slower*. However, my current ‘SetTexture’ goes through a list to grab the pointer, how slow is that? It looks like currently its about half as much time as all those smarttexture destructors, so it might be a win. Plus my SetTexture() stuff uses a MRU caching system which could make ti super fast when loading in 100 identical units…

5.50PM Actually realising I’ll need a totally new class to handle this. it needs to be done a different way, not with existing smart textures. Balls. Surely it can be done? Hold on.. Surely its just a matter of indirection. The purpose of a smart texture is to hold a surface pointer it can rebuild if needed, but it doesn’t have to hold a direct link. It could actually hold an indirect link.  One more piece of pointer indirection is trivial. I just add a new CLoadedTexture class that handles rebuilds, and the smarttextures can point at those, meaning no need to keep a list of them at all, as their pointers never go invalid or need rebuilding.

6.25PM everything coded except the actual rebuild() calls for alt+tab. I’ll comment the errors out for a quick test… GAH, it crashes immediately. fixed easily enoguh, and the code RUNS! hurrah, but it doesn’t seem AMAZINGLY faster,. Quick! to aqtime! what the hell… it’s slower?…

6.35OM. Hah! looking at it backwards, it’s about 75% faster. how much time does that knock off loading in a new level now? 5.28 seconds down to 2.1 seconds. That’s pretty good. I’ll code in the rebuilding stuff and give it a quick test…

6.50PM Alt+tab doesnt work, I get just a black window, but that may be something else. I’ve checked in my current code, and am investigating alt+tab issues now. It looks like the restore code hasn’t worked on this new game at all yet. I consider my (dumb) smart texture code fixed :D

 

AQTime

Yesterady I bought AQTime. It’s $600. That ain’t cheap. It’s profiling software, so basically its something that helps me write faster code. I’ve used it before, and it’s extremely good. I hope I can make my new game faster and smaller-memory-footprint than GSB by using it.

Already I’ve found out that some code that I thought was fast (searchlights) is in fact scarily slow. What’s more, I know the exact lines of code that cause the problem and it’s likely easily fixed. yay!

Plus fog of war code is done and dusted. Double yay.

I hope to have some interesting screenshots to show off in a few weeks. I’ve done the flashy graphics stuff before the gameplay balancing and level design, which means early screenshots of GTB will look nicer than the frankly awful early GSB ones :D

Also… there was a fox in the field opposite the house yesterday. It seemed to be just prowling about, midday, in the sun. Who needs springwatch when you have a fox? :D

Rendering to a texture

My new game does more rendering-to-a-texture stuff than the others have done. I’m very pleased with it so far.

What am I talking about?

Say you have a sprite, and that sprite is a ninja hamster wearing a bullet proof vest and wearing a crash helmet. This is a character in your game, and an icon used in various places. To allow customisation, the player has chosen the helmet and the vest, and maybe tweaked their colors. This gives them a slightly customised hamster.

When you draw this onscreen, you need to draw 3 layers (hamster, vest, helmet). That means drawing 3 sprites on top of each other, and 3 times as many pixels as normal. That can mount up quick, and is called overdraw. It also means changing texture 3 times, and changing textures is sloooowwww.

The solution, before the game starts, is to create a new blank texture in memory, and ‘offscreen’ (while the player isn’t looking) you draw the hamster, with all 3 layers and colors. You then save that texture to disk, with a unique name, Later, in the game, you just blap that single sprite, already composed, in a single draw call. If you are really obsessed, you could, at run-time, stitch together all the units textures into a single sprite-sheet to make it even simpler, and faster.

I have no idea if people tend to do this any more. Most people make 3D games with little or no alpha-blending, where you don’t worry much about texture swaps because you sort by Z and use a Z Buffer. My games can’t be done like that because it’s all soft-edged alpha-blended 2D, so these things start to matter. The code to do all this is a bit fiddly, but I’m already massively glad I’ve got it working. It will let me do all kinds of cool stuff in the next game, and means it will run fast as a race horse wearing rocket-skates.

You wouldn’t start from here

So many programming, and technical things are bodged, not just in games or software, but everywhere, because the ideal best-case academic method is totally useless given that we are always working from an imperfect base.

The railway network, telephone infrastructure, road network, sewers and god-knows-what-else here in England are all rubbish. Why? Because we got these things very early in history due to being economically and technologically quite advanced, esp during victorian times. As a result, as pioneers, we have what I guess is ‘first-mover disadvantage’. Our countries infrastructure is like excite vs today’s google :D

I definitely find this problem in my own code, my own methods of working etc. I have an engine, it doesn’t really have a name, the folder is called ‘Positech Engine’. I save my imagination for the actual game… The engine isn’t like the unreal engine or other fully-featured thing, but it has all my library code which handles stuff like directinput, rendering, vertex buffer and texture management, file loading and so on. Bizzarely, 90% of the my UI code is NOT in the library, I copy it to each new game. How sucky.

Not only that, but I have a bunch of long standing balls-ups that I am now stuck with, pending a major rewrite for the next game. I should have done them for this one, really… Here are the ones that spring to mind.

STRINGS: I can’t decide if I like char* buffers or std::string. I pretty much use std::string, but because historically I know functions like sprintf and strcpy really well, I still use them in places. As a result some of my library stuff is designed to accept either std::string or char* as input. I can type ‘.c_str()’ so fast now it’s scary

UNICODE: I don’t use unicode. This means a Japanese GSB is not going to happen any time soon. I should probably use unicode, but I don’t

TOOLTIPS: I know this seems minor, but my tooltip class isn’t integrated with my window class. This means if I create a new window, and it needs a tooltip, I need to add a tooltip object, and call a destructor, and make it respond to the mouse and get drawn. This sucks. I should have defaulted to all windows having tooltip functionality

BUTTONS: Buttons should be a subset of windows, but mine aren’t. Both windows and buttons at least derive from the same class, but a window thus now derives from a buttonlist. That means a window has a list of buttons, and a list of child windows, and they are handled seperately. THIS SUCKS.

I know this all sounds crap, and if you are a coder writing a new engine, or a student, it sounds like I’m a n00b, but the thing is, you wouldn’t start from here. Unfortunately I have, and re-using my existing engine saves me hundreds if not thousands of hours for each game, so it’s tough to make a business case for changing a lot of this. Both big studios I worked on had code that was at least as messy as this, so I know it’s not just me…

Next game I’ll fix it all, sure…   maybe…

Work For Idle Hands

One thing GSB does that I’m quite proud of, is run an ‘idle manager’ to smooth out the frame rate. In concept, it’s pretty simple. There are some jobs that need doing in the near future, but not *NOW*, and some that are optional. The idle manager works out that we have some spare time, and does them accordingly. In code terms it’s a lot more involved.

GSB has a target frame rate, and checks the time since last frame in the main game loop. If there is some ‘spare’ time before the next frame, it tells the idlemanager, and checks again once the idlemanager has finished. The idle manager has a list of tasks, and it cycles through them in turn. Two of the common tasks are these:

1) Check that we aren’t running low on any pre-cached particle effects, and if we are, pre-cache some new ones ready for future use

2) Check if any laser blasts which are missing their target happen to intersect with some debris. If they do, make the debris explode.

task 1) is vital for performance, task 2) is optional graphical fluff.

The implementation of an idle manager is cool because it allows you to use the fluctuating rendering-demands per frame to your advantage. It also means you eek as much usage as possible out of a single processor core. In multi-core, multi-threaded systems, this is all done much better by having a separate thread, which can spin off and do this stuff at your leisure. There can be synchronisation issues in that case though (I don’t want to change the list of cached particles while another thread is altering them too etc).

If you can’t be bothered with the hassle of multithreading, I recommend implementing something like this idle manager at the very least, assuming performance is vaguely an issue for you. I’m writing the idle code for my new game right now, and it will be a bit cleverer, and more involevd and possibly multithread stuff too. I hate to think the game would drop a frame when it could be avoided by doing this sort of stuff.