## Friday, October 30, 2009

### Besting a Rhino in hand to hand combat

One of the things I've always wanted to be able to do is create nice game creation tools. A nice syntax highlighted editor etc. So a while back I started writing something simple using Scintilla. Then at work a lot of Lua coding was required so last Sunday night, yesterday night and a bit this morning I made my own lua editor / IDE and released it open source http://code.google.com/p/monmon/ (it's still undergoing development)

I had been using UltraEdit but today might that day I can start eating my own dog food, as they say.

The editor is called MonMon it does rather a lot with very little code - check it out!

## Thursday, April 23, 2009

### Sounds for Games

Recently I wrote a quick post about the awesome sfxr. A great little utility for creating bleeps and bloops.

But that's not all today I stumbled on http://www.freesound.org. It has loads of sounds, all free and easily searchable.

## Wednesday, April 22, 2009

### Quickie Rotations

In my C# codebase I have no rotation code - which may be a little surprising but as I'm making a 2D game it's not been a issue until now.

I just want to be able to rotate my sprites around other sprites. A nice general solution would be to allow matrices to be be applied to the four vertices that make up each corner of my sprite.

But I didn't want to write a matrix class. So I just implemented the rotation manually:

public void Rotate(double angle, EinPoint origin)
{
for (int i = 0; i < _rawVerts.Count(); i++)
{
float cosAngle = (float)Math.Cos(angle);
float sinAngle = (float)Math.Sin(angle);
float newX = (_rawVerts[i].x - origin.x) * cosAngle + (_rawVerts[i].y - origin.y) * sinAngle;
float newY = (_rawVerts[i].y - origin.y) * cosAngle - (_rawVerts[i].x - origin.x) * sinAngle;
_rawVerts[i].x = newX + origin.x;
_rawVerts[i].y = newY + origin.y;
}
}

I'll be the first to say it's not pretty and there's probably a way to avoid the casting. But for my needs it's perfect. I've promised myself I'll implement all the matrix code once I finished my current game - which is coming along quite nicely.

Here's a good rotation reference.

I'm using OpenGL so of course I could use the OpenGL rotation commands - push a matrix, do the rotation, pop the matrix. But doing it this way sends extra commands to the graphics card. If I do the translation manually them I can skip those commands and I only need to pay for the cost of doing the rotation once instead of every frame. I'm thinking of also doing some rotations in my particle system so it's worth considering these issues.

## Saturday, April 18, 2009

### Sound for all

I'm not really a sound person, I know nothing of it. I liked fruity loops when it first came out but that's it. Like so many children the recorder was forced upon me when I was very young ... perhaps these things are related.

But games need sounds!

That's why Dr Petter's sounds effects tool is so awesome. You press one button and it makes a sound. If you like the sound you save it. That's the kind of program and user instructions I can throw my weight behind. It's name is the somewhat unwieldy 'sfxr'.

I only wish there was a little more variety. It seems to be based on what the NES sound hardware can produce - or some similar system. Still very good gamey sounds for very little effort. Thumbs up!

## Saturday, April 11, 2009

### Data bindings

Data bindings in C# are something I've totally ignored because I've always assumed they're boring database related code.

Yesterday I ported my simple particle system from C++ to C#. Previously I'd defined particle systems in my own special file format and then used that to create them. So I'd edit the file then load the test program and see what it looked like. (I may even have had a quick reload key, that reset the particle system with the new data)

In C# I thought I may as well have a second window with a load of controls on it. This is about two lines of code in C# and some mouse clicking. I remembered when I was doing some MFC stuff I could have a text box that had a pointer to a value of an object. So the data was totally real time, the number in the box was the number in the object.

I wondered if databindings would let me do something similar in C# and cut down on the amount of code I'd need to write. Here's what I've got at the moment:

public partial class ParticleTester : Form
{
ParticleSystem _system;
public ParticleTester(ParticleSystem system)
{
_system = system;
InitializeComponent();

}
}

That's the entire class. What the data binding means is - get the 'Text' field from the textbox 'particleSizeBox' and fill it up with data from _system.ParticleSize. This works both ways - if I change that value in the text box the value in my particle system changes. Very neat and quick - it's handles changing from a number to string and back again automagically.

Obviously I'm a little late to the party on databindings but it's nice to find something new and useful.

Just in case you want to know - this is how I did a combo box:

comboType.DataSource = System.Enum.GetValues(typeof(ParticleSystem.eParticleType));

## Sunday, April 05, 2009

### Fiddly bits

Finally, I've written some code that I've been meaning to for a quite some time - the frame.

By frame, I mean an in-game window. A good example is the final fantasy screen shot below. It has a blue box with text in it. The blue box is a frame.

If you are using 3d acceleration the way to write this is to split the frame image up into a number of quads. So the corners are each a quad, so are columns and cross bars and the middle is a quad as well.

The columns and crossbars repeat their texture vertically and horizontally respectively. This allows the frame to be of arbitrary size but still look nice.

Here's a screen grab of the image I made in photoshop.

I have a small drop shadow, so the upper cross bar and the lower cross bar are different. The same for the columns. In total I have nine images each 16 x 16. I've packed all my images in a texture atlas.

Tiling or repeating a texture from a texture atlas using OpenGL isn't as simple as it is without a texture atlas. Without a texture atlas, you set the texture co-ords past 1.0 and for each unit over 1.0 it will repeat the texture. You also need to set a texture repeat flag somewhere. So a quad that has texture coordinates from 0,0 to 2,2 will repeat the texture four times over it's surface.

Using a texture atlas you can't alter the texture coordinates - as you're using them to cut a chunk out of the texture. So to get round this I just increased the vertex count on my frame mesh. Each time I want to repeat a texture I apply the same texture to a new quad and put the new quad next to the old one.

Here's the final result:

My frame has black border and I'm rendering on black so it appears a little strange - but at least it's working nicely now!

In the above each corner is a quad, the crossbars and columns and several quads in a row. The middle is a big mesh of quads - though often this middle section could be single large quad.

## Sunday, March 29, 2009

### Let's do something C++ can't

Recently I was writing some code to load levels. This is a common task but I think I ended up with some pretty neat code that can be used in a variety of ways.

My levels are rather unusual. A level can be generated totally by my code. So I can be as a vague as 'make a level' or I can say 'I want a monster here and here and walls here' and describe everything down to the last detail. I'm still somewhat in the early stages of development, so the layout of the level file is likely to change a lot as I add new things.

My first thought was XML. I'd write something like:

<level>
<name>Level One</name>

<simpleDescription>
<setting>
<stone/>
<wood/>
<setting>

<enemies>
<orc/>
<dragon/>
<enemies>
</simpleDescription>

</level>

Obviously this is code that's going to change quite a bit as I develop the game. Writing an XML parser would mean everytime there's a code change - the data files and the parser must change. Not only that but I'd be editing these files by hand. XML isn't the most pleasant medium to write in.

XML Alternatives

So what can be used instead of XML? Dimly, ever so dimly I recalled an XML alternative. The way I remember this alternative was that it used whitespace in a novel way. The above xml code would be rendered:

Level
Name : LevelOne
SimpleDescription
Setting
Wood
Stone
Enemies
Orc
Dragon

This seemed great - unfortunately I couldn't find any reference to it, perhaps my google-foo is weak. All I could find was JSON, which is a subset of Javascript laid out to look like XML. I toyed with JSON for a while but in the end it was just as unpleasant as XML for me.

After failing to find the whitespace xml alternative my mind wandered to kjAPI a C++ game programming library (it's wonderful though the public release is somewhat tricky to compile.) kjAPI has this great reflection system, you mark a number of fields to be 'metaclassed'. Once the field's are metaclassed they can be directly manipulated by it's GUI editor - and they can be serialized to XML. This is done automatically no parser needs writing just automatic load and save functionality. Excellent.

Of course C# has this in the form of the serialize attribute - but this generates computer XML. I'm going to be writing these level descriptions - I have a hard enough time with human xml! Never mind writing my XML so that the serialize methods of C# will correctly map it to my class data.

Next I considered going the C# serialize route but doing all my editing in a GUI. Then I started to think about how such a GUI would need to appear, and I started to look at datagrid controls and things ... soon my initial enthusiasm waned. Also if I'm going the GUI route - why bother with the XML? I could just use a binary file. Deciding the GUI option would be a unreasonable time sink I came upon my current solution.

I'd do what all programmers love to do - roll my own!

A general solution that fits my needs

First here's the data file.

+Level
{
_name = "Level One"
_simpleDescription = +SimpleDescription
{
}
}

Ok - it looks a lot like the others, but it's got a clever trick. I have some general code that searches the current C# assembly and creates an object the type 'Level' then it assigns it's fields _name and _simpleDescription. The code can do this for any class in the C# assembly provided the data files names match up correctly.

So if I have a class item.

class Item
{
string _name;
string _description;
}

I can write

+Item
{
_name = "Some toast."
_description = "A piece of toast, it's cold and slightly burnt."
}

Then in C# code I can write something like (I've called my generic object loader 'dresser'):

Dresser dresser = new Dresser("the text of the data file goes here.");
Item item = dresser.GetNextObject<Item>();

And that's it. That item is now filled up, it's name is set to 'Some toast' and the description is set too.

How it works

The '+' sign means create a new object. The object type is whatever follows the +[that is what goes here]. I use C#'s reflection system to find the class with the name, then using various trickery I create an instance of that class at runtime. The name in the data file and the name in the C# file must be the same. Then I go through the fields and set them in the same way, I search the class using reflection for a field's name and then set it directly (using reflection private fields can be set).

The @ symbol is something I've added only for this example (my level's aren't of the orc, dragon kind). It merely means calls a function of this name. So once again I search for the method and call it with the appropriate arguments. Neat right?

This means I can change and extend the code as I like and I only need change the data where the two don't match. There's no parsing code to rewrite.

## Saturday, March 28, 2009

The errors are slowly trickling in. This time Nunit. I tried to load some of my assemblies to unit test and recieved this exception: "system.badimageformatexception". So I ensured all the projects in my solution where compiling using the same CPU. Still I got the same error.

It turns out this is another symptom of developing on 64bit Vista system. The assemblies I was testing were x86. The nunit was 64bit, and that's were the problem was. This can be resolved by using nunit-x86.exe in the bin directory.

Another problem down.

## Friday, March 27, 2009

### An attempt was made to load a program with an incorrect format.

I'm updating here until I resolve what I'm going to do withgodpatterns.

Recently I setup a new dev machine, while I'm back in the UK searching for a new job (I'm hoping to get something to do with C# if possible). Anyway I moved my SVN repository over along with my most recent code check outs.

As I mentioned in my last post I'm developing using C# and Tao (which, among other things, is an OpenGL wrapper).

On running my code I got a runtime crash. An exception

public SoundManager()
{
Alut.alutInit();

When the program tries to start up OpenAL I get the error message "An attempt was made to load a program with an incorrect format."

It's worth noting I'm now developing on Vista (this machine had it preinstalled) and the machine is 64 bit. Using this post, I found out the problem is that the Tao dlls are x86 (32 bit) and I was trying to compile my code as 64 bit. This seemed to be default in visual studio.

If you want to fix this, then for each project in your solution go to Project > Properties. This will bring up a dialog. Select BUILD, then find the combo box next to the label 'Platform Target'. Change this from any to x86.

That should fix any problems.

(One other issue I seem to be having is a number of dlls are missing in Vista. So for now I'm just including copies with my executable)

Happy hacking.

Note: If you're still having problems and want some more voodoo. I also remove all my Tao references and readded them