Nintendo DS, Game writingSeptember 30, 2005 4:26 pm

I bought a Nintendo DS when I was over in the States this summer. I’ve become something of a serial importer and my impatience with European release dates (and prices!) has resulted in ridiculously large DS games collection already. I can’t recommend the console hightly enough - it’s unique, robust, plays all my beloved GBA games, is properly lit and the battery lasts for weeks.

Anyway, the DS homebrew scene seems to be trundling along nicely (although nothing like the PSP) and the architecture of the machine really appeals. I tried some GBA coding a while back but the results always felt like simple stuff I could have done on the PC. The DS and its stylus, however, give me lots of scope for interesting ideas.

So, I’ve just ordered a DS PassMe. It took a fair bit of hoops and jumping - I don’t have a PayPal account and that’s the only payment method the site took. After some pursuading a friendly work colleague ordered a couple using his account - now all I have to do is wait. The guy puts the things together himself, so it may be some time.

GraphicsSeptember 29, 2005 5:50 pm

Bumbling on with the particles engine has led me to pixel shaders in C#, using HLSL. I even have a book. Up until today however, I’d just fiddled with existing examples.

The code already had the alpha blending and coloring of the particles as a very, very simple pixel shader. It needed more per particle variables to find their way down to the shader (e.g. particle lifetime, coloring function etc.) - this requires using a custom vertex type and a corresponding vertex shader that mangles it into a sensible input for the pixel shader.

This is what I ended up with (the odd variables at the top are for EffectEdit, an application that comes with the DirectX SDK and compiles and displays effects. Definitely worth using to find mistakes in the shaders!):

// Stuff for EffectEdit
string XFile = "tiger\\tiger.x"; // model
int BCLR = 0xff202080; // background
float4x4 gTransform : WORLDVIEWPROJECTION;

struct VS_OUTPUT
{
float4 Pos : POSITION;
float4 Color : COLOR0;
};

VS_OUTPUT VS(float4 position : POSITION, float4 color : COLOR0)
{
VS_OUTPUT Out = (VS_OUTPUT)0;
Out.Pos = mul(position, gTransform);
Out.Color = color;
return Out;
}

float4 PS(VS_OUTPUT input) : COLOR
{
float4 result = { input.Color.r, input.Color.g, input.Color.b, 0.5 };
return result;
}

technique Monkey
{
pass P0
{
// shaders
VertexShader = compile vs_1_0 VS();
PixelShader = compile ps_1_1 PS();
}
}

Then the C# code is just:

m_effect = Effect.FromFile(m_device, "../../effect.fx", null, null, ShaderFlags.None, null);

m_device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
m_device.BeginScene();
m_effect.Technique = "Monkey";
m_effect.SetValue("gTransform", m_device.Transform.World * m_device.Transform.View * m_device.Transform.Projection);
m_effect.Begin(FX.None);
m_effect.BeginPass(0);
// Draw stuff
m_effect.EndPass();
m_effect.End();
m_device.EndScene();
m_device.Present();

And yeah yeah, it’ll just render something in a slightly wimpier color than the color specified for the vertex (due to the 0.5 alpha). The idea’s there though - more can be added to the VS_OUTPUT and vertex structure and the appropriate calculations can be done in the appropriate shader. With hindsight, many calculations that are per-particle can be done at the vertex shader level. Oh well.

As for the stuff that’s actually drawn, well the call to DrawUserPrimitives now uses a custom vertex array (all 6 vertices!):

struct MyVertex
{
public Vector3 Position;
public Vector2 TuTv;
public int Color;
}
private MyVertex[] m_quad_verts = new MyVertex[6];

The only slight faff is setting up the VertexDeclaration (not the VertexFormat, I believe that’s for the fixed pipeline, not shaders). This is done in the renderer construction, around the same time as constructing effects, fonts and the like:

VertexElement[] decl = new VertexElement[] {
new VertexElement(0, 0, DeclarationType.Float3, DeclarationMethod.Default,
DeclarationUsage.Position, 0),
new VertexElement(0, 12, DeclarationType.Float2, DeclarationMethod.Default,
DeclarationUsage.TextureCoordinate, 0),
new VertexElement(0, 20, DeclarationType.Color, DeclarationMethod.Default,
DeclarationUsage.Color, 0),
VertexElement.VertexDeclarationEnd
};
m_device.VertexFormat = 0;
m_device.VertexDeclaration = new VertexDeclaration(m_device, decl);

Slightly scary looking, but it’s just setting up offsets into a datastructure. The important thing is that the VertexDeclaration matches the layout in memory as MyVertex.

Windows 9:51 am

I’ve been running a script at work for the last 2 days that goes through a huge filesystem on our network, parsing files and spitting the results into SQL statements. Trouble is, I didn’t think there’d be that many so I outputted them all to one directory. So far I’m up to 318351 files. Each around 5k of SQL. Windows doesn’t seem happy, I just managed to make more crap out, and dir stopped working a while back. Cygwin isn’t any better - I can’t even change into the directory. As for Explorer - well it won’t even let me access the drive.

If it ever finishes I need to push the SQL into MySQL. That’s if my PC doesn’t melt beforehand. Eep.

Graphics, Game writingSeptember 28, 2005 10:46 pm

The (small) game I’m currently working on needs some spit and polish. I’ve always wanted to write a simple particles engine. Looks like an obvious match to me.

My current tech is C#, Managed DirectX and some HLSL. The particles are alpha textured quads using DrawUserPrimitives. A tiny particle class manages some dumb Euler integration (I’ll crank this up if and when…) and everything, well, seems ok.

Currently I’ve got three sets of fixed particles launched up into the air and subjected to the whims of gravity. They start with random angles and magnitudes, so you get a rough fireworks display:

3 fireworks

Unfortunately I now need more information in my vertex structure so the particles can die off at different times - I’m already using color, texture and position. There’s a gap in my understanding between fuzzy-away from memory structures-managed-C# and hard-and-cold-HLSL. Time to tread into the unknown…

Blogging 9:40 pm

[waves]

I want somewhere away from my brain to store my geeky, techie thoughts. And somewhere to chronicle my journeys from them. Here will do nicely.

Now: time to do something and stop this structured procrastination.