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

Fun with 2D game engine rendering algorithms

Sooo…consider the image below (click to enlarge) it’s from GSB 2. A ship has blown apart (no bright fire pixels yet, just clouds of smoke) and there are smoke plumes. So far so good right?

screenshot_27-02-2014_10-42-25

Sadly no. The problem is, that ship was in the distance, and behind all the other ships, but if you are really eagle eyed you will notice the smoke plumes get drawn above the ships next to it, and this is a HIDEOUS RENDERING ARTIFACT. For complex reasons to do with the way I build up lightmaps and composite them back, I can’t just draw the smoke cloud after I draw that far-off ship, and then let other stuff render over it. I do some nonsense with multiple render targets that means despite using a ton of transparency and ruling out a conventional Z-Buffer system, I have to render some things out of order. So those smoke plumes are actually rendered last, just before I do my (slight in this screenshot) bloom effect. This lets me have them work sensibly when it comes to obscuring lights behind them, and avoids other artifacts.

The problem is…I need a way when drawing them to prevent them appearing in front of far off objects. Thankfully I have z-positions for everything, so in theory this could be done, although reading up on it, it seems like a conventional z-buffer is not going to cut it, due to it being all translucent smoke particles. So… right now I’m considering the experiment or the day is for me to create my own z-buffer, fill it with the ships (and asteroids) and their z-positions, and then manually compare the z-positions in a special shader I can use for drawing the smoke plumes. In my head, I know that even if this works perfectly, it won’t work perfectly…because there is nothing to stop a smoke plume going ‘through’ a ship. But hey… we can live with that, as long as ones in the background don’t get obviously drawn on top of things in the foreground. Because GSB 2 uses a lot of parallax, it looks slightly 3D-ish, and I don’t want to ruin that effect.

This probably will all go wrong, it’s one of the many experiments you go through when you have an unusual game and your own graphics engine. In my head it seems easy. Create new offscreen render target (yikes…performance), render all of the items I want to sort to it (yikes…performance), using their z-position as the value written to it (it could be a simple and small 8bit format), then set that as a texture I can read from my smoke-plume shader, pass in the Z value to the shader and compare the two, setting alpha to be 0 if the pixel should be obscured. Job done right?

Place yer bets!

Gratuitous Space Battles 2 Lighting

Sooo… I’ve been experimenting with lighting of spaceships for GSB 2. If you played the original game you might be aware that although it often looked pretty l33t, it also had a tendency to look a little ‘flat’. The lighting was always the same (apart from the odd ‘global’ shader effect, and it could certainly have had more depth. This is one of the things I wanted to address when re-doing the game. The original game just had simple sprites for ship hulls, and the new version is tons more complex and lets me do lots of magic. Basically, I combine sprites for the ships with normal maps, and specular maps and lightmaps, and use a shedload of different shaders and render targets to do all kinds of compositing voodoo. So here I present some early screenshots showing me monkeying around with the options I now have. It’s a GSB 1 ship (as a test) and it looks like ti has another one stuck to the front of it. This is a test of something else (secret!) but it shows how one ship can now cast a shadow on another (Not correctly positioned yet, but easily fixed…)

So here is a screenshot showing the bloom effect everywhere: (click to enlarge)

1

Here is one with the bloom effect turned down but the 3D bumpiness up a bit:

2

Here I turned down the exterior lighting, and may have moved the lighting direction too:

3

Now I’ve gone full-on moody lighting and likely moved the light again:

4

Now I’m in real ‘dark-battle lit only by the light of our warp engines mood:

5

You really need to see it all big screen (and moving!) to see the full effect of it all. And you also need to compare it to the original flat looking default-shaded sprites in GSB 1 to imagine the final effect with all the battle raging around it. I’m quite pleased with it so far, although there are loads of things that need improving and tweaking, and no doubt needs more optimizing. It’s a start though!

 

Learning to code (with george osborne!)

So there is this new government initiative in the UK where apparently every student in the country will be taught some programming in an effort to increase the technical knowledge of the future workforce. I see this as an extremely good thing. It surprises me that we have gone this long without introducing it as part of the school day anyway. Physics, Biology,Chemistry & programming? They seem like fairly equivalent importance to me, although I think we should add in ‘Engineering’ too. What slightly worries me is that the depth of the courses will not go far enough.

The general theme behind all this is that schools have been teaching people how to use Excel, rather than teaching them programming. This is clearly a bad thing, partly because most kids know how to use excel anyway, but the problem is, the grasp of what ‘programming’ really means seems incredibly weak amongst those who are discussing it. I keep seeing discussions where politicians and campaigners talk about how ‘you too’ can learn the ‘complexities of HTML’, and equate this to an understanding of how computers work.

Don’t get me wrong, I’m all for people understanding how HTML works, but we can surely go further than that right? Shouldn’t we be encouraging kids to go a bit further down the programming rabbit hole? At the very least php and java-script. ideally all the way to C# or even a real-mans language like C++*. I wouldn’t be shocked if everyone understood at least in principle what assembler was. (I know I could do with learning some more there). My point isn’t that we need a million C++ coders, but that understanding ‘a bit’ about how it works means it’s much easier to find the 1% of kids who really do want to study that sort of stuff, and in any case knowing a ‘bit’ about the next-most-complex layer of a technology is always beneficial. I’m not an assembly language programmer, but I’m not horrified to click on the breakdown in aqtime and look at my codes assembler breakdown either. Knowledge is good.

I know you have to start somewhere, and people think that stuff like C++ will scare people off, but hey, lets set some lofty ambitions shall we? At the very least lets not make the mistake of giving an entire generation of kids the idea that Windows 8 was programmed in HTML and that this is as low-level as it gets. There is more to coding than HTML and phone apps. A lot, lot more.

*I’m kidding**

**sort of :D

 

Cleaning the engine

It’s tricky to get the timing right. Some people do nothing but update their engine. they have re-factored it so much and re-implemented everything so often that it is a true work of art, worthy of actually putting in books on how to code. These people never ship a game. The other extreme is people who are still using assembly language routines for loading in ini files because they wrote them in 1996. These people have tech support issues galore, and are probably still using Directx3.

In between there somewhere is common sense, if you *are* going to write your own engine and not use someone else’s. I think, from chatting to devs and surfing a lot on developer forums, that people tend to want to redesign their engine after every game. They consider that quite a major compromise compared to their real hidden urge to do it every morning. The thing is, if you are doing that you aren’t really writing an engine, you are just writing a new game from scratch all the time.

lexus

I tend to go with a system of marginal improvement. I still have some code from about 5 games ago (non critical stuff like ini file loading, text handling, game timers and some math stuff), but a lot of it is fresher. The graphics stuff, as you would imagine gets a major refresh more often, and my engine only contains some pretty ‘raw’ directx wrapper and vertex buffer / text engine stuff. The actual ‘scene management’ for my games is done in the game itself.

Despite the occasional between-project update and maintenance, occasionally you have to step in and clean things up. The last big update was when I went from Directx7 (Democracy 2, Kudos games…) to Directx9 (GSB,GTB,Democracy 3). This time I’m updating almost everything BUT the directx version.

I recently bought Visual Studio 2013, mostly for the concurrency profiler to enable me to experiment with multi-threading more. This was a good opportunity to take a look at some of the flakier things in my engine. I have a lot of warnings in there for data-conversion and other sloppiness. I also have code I never use (I’m culling it), and the worst and most embarrassing thing is that I can’t decide if I like char* or std::string. I Figure that std::string must be at least a bit better, more robust and safe than char*, so I’m trying to purge all that char* from the engine, and eventually, the game. I’m also planning on re-wiring stuff so that the main game code doesn’t have any FILE* or other old fashioned stuff, but uses my file wrapper more.

Why? Mostly because I can see me heading towards cross platform eventually. maybe not with the next game, but baby-steps and all that… Plus it makes life easier if getting my engine ported is a less messy business. I’m sure after a few days of sorting out this stuff I’ll be climbing the walls and wanting to code some explosions again…

Thread Concurrency bug. Any expert advice?

I’m new to multithreading, and the visual studio concurrency visualiser. I have lovely multithreading code working. basically my main thread throws the Threadmanager a bunch of tasks. The threadmanager has a bunch of threads which all spin doing nothing until told to do a task. Once they finished their task, they set an IDLE flag, and the main threads WaitForAllTasks function then assigns them a new one. It works just fine…. but I notice an anomaly.

See below (Click to enlarge enormously)

bug

The highlighted connection shows thread 4828 sat on its fat ass waiting for the main thread, when it clearly idle and ready to do stuff. The things is, the main thread function is just this:

    bool bfinished = false;
    while(!bfinished)
    {
        bfinished = true;
        for(int t = 0; t < MAX_THREADS; t++)
        {
            if(CurrentTasks[t] == IDLE_TASK)
            {
                //maybe get a queued item
                if(!QueuedTasks.empty())
                {
                    THREAD_TASK next_task = QueuedTasks.front();
                    QueuedTasks.pop_front();
                    CurrentTasks[t] = next_task;
                    SetEvent(StartEvent[t]);
                }
            }
            else
            {
                bfinished = false;
            }
        }
    }

Which basically does sod all. How can this take any time? Setting event sets an event which the worker threads function is calling WaitForSingleObject() on. Again…how can this take any time? Is there some polling delay in WaitForSingleObject? Is this the best I can hope for? It’s the same case for all those delays, it’s just this one is the largest. I’m new to this. Any ideas? :D