Archive for the ‘Blog’ Category

The Strangler – attempting a point-and-click game

Wednesday, August 24th, 2011

Last month (July) I’ve been participating in a challenge, to make a game in a month, using any technology and tools. The only restriction was the theme: Justice. And even that was open to interpretation.

I saw this as an opportunity to practice a bit of game design and, more importantly, telling a story, so I decided to make some sort of classic point-and-click. I used my game engine Pixie, and the coding was quite straightforward, but it was rather difficult to come up with what was to happen in the game.

To make it a bit more interesting, I decided to try and make it in a bright and happy visual style, while keeping the story itself a bit more dark and sinister. There was something about those two conflicting ideas that appealed to me.

The game can be downloaded here  (for windows, but should run on Linux through Wine):

And here are some screenshots:

The name of the game is "The Strangler", and is about Tom O’Reilly, Private Investigator:

The game starts with you, playing as O’Reilly, getting a visit…

… from a lady who has a case for you – to find out if her husband is a killer.

So you have to hit the streets in a search for clues, but you’re not very popular with the local PD:

There’s a couple of cut-scenes in the game, giving you another perspective on what’s happening:

I didn’t have time to  add much interactive dialogue (so that’s something I’ll be adding over the next couple of weeks). But there’s a little of it at least:

When you discover a new location, you get a marker on a map, which is used to move around in the game:

I had to come up with a bunch of characters for the game. Ten of them in total.

As I only had a month to do it, and everything took longer than expected (especially the animations), I didn’t have time to do the last scene of the game within the time set out for the challenge, but in the couple of weeks following it, I finished it off.

If you do try it out, please let me know what you think :-)

Also, as I usually do, I’ve released the source code for the whole thing:

It includes both the code for the engine and the code for the game, but not the data-files for the game (you’d have to get them from the games zip file). The code is public domain. The source code for the game itself is not particularly pretty – time limitations meant doing quite a few nasty hacks. But sometimes it can be fun to see that other people hack their code too, so I’m releasing it as is :-)

Simple and efficient management of shared resources

Wednesday, June 22nd, 2011

Recently, Cliff Harris of Positech Games made a blog post called Optimising my ‘dumb’ textures, where he describes the optimisations he is doing to his latest game, to increase the load times when dealing with large number of textures that needs to be managed automatically, but also be shared between different game systems.

This made me think of resource managers in general, and how I’ve been planning for a while to replace the one in my Pixie Game Engine with something more robust, efficient and generic. It also struck me that this is a topic that often comes up, and I think all developers eventually reach a point where they need to solve this problem, so I thought I might as well give it a shot and share the resulting code as public domain, as I usually do.

So, my requirements for this system is that it should be really easy to use, but at the same time very efficient both in terms of memory use and performance.

In the solution I came up with, I use efficient hash tables to store the resources, but I wrap the access to them in a convenient resource class, which can be used as simply as this:

Resource<Texture> myTexture("myBitmap.tga");

In this example, Texture is the class that we want our resource manager to manage, and Resource is the templated wrapper class that we use to have it managed automatically.

There’s no problem having multiple instances referencing the same physical resource, like this:

Resource<Texture> myTexture("myBitmap.tga");
Resource<Texture> myOtherTexture("myBitmap.tga");

Even though we have two different resources, the fact that they’re created from the same filename means that they will both be pointing to the same Texture instance. The first of these two lines will cause the Texture instance to be created, while the second line will merely add an additional reference to the same instance.

Resources are automatically reference counted, so as resources are destroyed, the reference count goes down, and when it reaches zero (meaning there are no longer any Resource referencing the instance in question), the instance will be deleted automatically.

A good thing about this resource implementation is that Resource instances take very little memory – only 4 bytes – as they only store a pointer to the instance they reference. Also, they can be freely copied and assign, and the reference count will be adjusted accordingly, like this:

Resource<Texture> myTexture("myBitmap.tga"); // New instance created
Resource<Texture> myTexture2("myBitmap2.tga"); // New instance created
Resource<Texture> myTexture3(myTexture); // Ups ref count on "myBitmap.tga"
myTexture2 = myTexture3; // Ups ref count on "myBitmap.tga”
// and also causes "myBitmap2.tga" instance to be destroyed

This makes it possible to work freely with Resources, assigning them and copying them around, knowing that it’s all very efficient under the hood, and that the bulk of the data (as would be stored in, say, the Texture class) will be shared among all of them.

Additionally there’s a couple of helper functions to get the filename that a resource was created from, and to get a list of all managed resources of a specific type – which can come in handy if you need to loop through and perform an operation on each of them, such as Cliffski’s case where he needed to restore lost surfaces after Alt+Tab.

The code is public domain, it’s stand-alone and compiles as is – but it does include several of the data containers I originally developed for Pixie, so there’s quite a few files in this package, even though the resource system itself is only four fairly small files.

Download Resource Manager Source Code

I hope someone will find this useful, if nothing else as a starting point for their own system. I’ve done some quick performance tests, and on my computer I can create and destroy one million resources in less than half a second, so the resource system adds very little overhead to the process of loading and managing your assets :-)

Debug Text Render

Monday, January 4th, 2010


When working on games, I often find it useful to be able to quickly and easily display text strings on the screen for debugging purposes. A long time ago I wrote a simple little piece of code which could draw text onto a bitmap, and without having a dependency on anything else – it doesn’t use any other classes, functions or libraries, and it doesn’t need any external data – the font bitmaps are included in the source file, as a static array. This system, in all its simplicity, have turned out to be very, very useful, and I’ve used it for most of my stuff for the last 10 years or so.

I recently spent a little time cleaning it up a little, and thought I’d release it to anyone who’s interested. As usual, it’s public domain, so use it in any way you like.

You can download the DebugFont source code here.

To draw text on any bitmap, you simple include "DebugFont.h", and do something like this:

DebugFont font;
font.Blit(pixelDataPtr,w,h,20,30,"My text"); // 20,30 is the x,y position of the text

Where pixelDataPtr is a pointer to the pixel data of the bitmap we want to draw onto, andw and h are the width and height (in pixels) of that pixel data area. The pixel data of the bitmap to draw on, can be 8-, 16- or 32-bit data.

As I finish the refactoring of more parts of my Pixie game engine, I’ll try and post more of these little useful, stand-alone code snippets which can just be dropped into any project – let me know if you find any of them useful :-)

Using Strings as IDs

Friday, June 26th, 2009

Sometimes, it’s nice to be able to identify things by name, rather than using a number or an enum. The problem is, that if you’re not careful, using strings can take a lot of memory, and LOADS of CPU-power, with all the stricmp’s. When I was hired for the Tycoon City project, and assigned the task of speeding up the game (which was *really* slow at that point), it turned out they were using several hundred megabytes for storing strings (of which most were used as ID:s, and was mostly duplicates of duplicates), and that string comparisons and string copying was right at the top of the profiling results. I won’t go into how we went about solving that particular problem – it was late in the project, and things turned out the way they turned out – but it got significantly faster and smaller, that’s for sure.

Ever since then, I’m always a bit wary about using strings – it can easily get out of hand, and it feels wrong to waste both memory and CPU-power on something if you don’t have to. So, I thought I’d share my own system for solving this problem, extracted from my (recently cleaned up) Pixie Game Engine. It’s a solid, fast solution, ready to just drop into any project, and consists of only two classes. At its core is a class I call StringId, which is easily created by:

StringId myStringIdVariable("myOwnIdNameHere");

Which internally results in the string "myOwnIdNameHere" being located in a global string table (non-case-sensitive), or inserted if it’s not already added. The lookup is done through a pretty fast hash-table. In the StringId object is stored a pointer to the shared string, so you can do:

if (myStringIdVariable==anotherStringIdVariable)

and know that it will result, internally, in just a comparison of two pointers. Nice and fast. And takes less memory too, as a string is only stored in one place – the global string table. There’s also a couple of macros defined (they’re nifty at times), strSwitch and strCase, which can be used to compare StringId’s in a way similar to switch-case statements:

strSwitch (myFruitTypeStringId)
        // code to do stuff here
        // code to do other stuff here

Which is the same as doing this, but looks a bit nicer (though that is, of course, a matter of taste 😀 )

static StringId applesId("Apples");
if (myFruitTypeStringId==applesId)
    // code to do stuff here
static StringId orangesId("Oranges");
if (myFruitTypeStringId==orangesId)
    // code to do other stuff here

The code is well-written and well-commented, and can be downloaded here. It is C++, and as usual it’s public domain – use it anyway you like 😀