Monday, April 30, 2007

GamePad Dead Zone functionality

The last couple of nights I've been working on the dead zone functionality for the GamePad class in MonoXna, which was an interesting excursion off into formula's and equations I haven't been exposed to for many years.

I read an post by Shawn Hargreaves, which gave me a good start on understanding the MSXNA implementation.  I've now implemented the basic functionality of the two algorithms, IndependentAxis and Circular.

I used Shawn's formula for the IndependentAxis deadzone calculation and here is a plot of this:

Independent Axis - Plot X Independent Axis - Plot XY
IndependentAxisPlotX4  IndependentAxisPlotXY1 

I ended up using a variation of Shawn's formula for the IndependentAxis and using a bit of trig, ended up with the following for the Circular implementation:

/// <summary>
/// As per http://blogs.msdn.com/shawnhar/archive/2007/03/28/gamepads-suck.aspx
/// Both <paramref name="x"/> and <paramref name="y"/> parameters assumed to be between -1.0 and 1.0
/// </summary>
static Vector2 CircularAxisDeadZone(float x, float y)
{
    float dist = (float)Math.Max(Math.Sqrt(x * x + y * y), EPSILON);
    float deadZone = Math.Max(dist - DEADZONE, 0)/(MAX_DIST - DEADZONE)/dist;
    return new Vector2(x*deadZone, y*deadZone);
}

The MSDN documentation states that the Circular algorithm uses the combined position of both joystick axes to make the deadzone calculation.  So,

  1. We calculate the distance from the centre, and if zero, returning EPSILON, so we never divide by zero.
  2. If we're in the DEADZONE (=0.2f), we'll clamp to 0, otherwise we'll create a weighting factor for the deadzone calculation
  3. return a Vector2 with the new x,y values

Here is a plot of our IndependentAxis calculation:

Circular - Plot XY
CircularPlotXY1 

Monday, April 23, 2007

#64 - Commodore 64 emulator on Mono

With the recent success of running Pong on Mono.Xna, on the mono runtime, I decided to try my port of #64.  I was pleasantly surprised to see it run without recompilation or any issues on the mono runtime.

My first observation was the not insignificant increase in CPU usage over running on the Microsoft JIT. 

I haven't had a chance to dig into things yet, and it may not be for a while, some areas I will look at are:

  • switch statements
    • Like many emulators, #64 has a number of large switch statements to emulate the various CPUs (6502, 6510, 6581, 6569, etc), and these are perhaps not as well optimized as the MS JIT.
  • inlining
    • Whilst I inlined some code, the other possibility is that the mono JIT may not be as aggressive when inlining code.
  • floating point
    • The sound emulation does use a significant amount of floating point code.
  • memory allocation
    • I am pretty sure the memory allocation is minimal, and suspect the mono memory allocator is just a pointer being incremented, much like the MS implementation 

It will be an interesting journey into the inners of the code generation and optimizations, since I'm not familiar with all the optimization techniques.  I'll blog about my experiences when the time comes.

Thursday, April 19, 2007

Running Pong under Mono.Xna on mono

It is official; we've successfully run Pong under Mono.Xna!  It is running with either the Microsoft or mono runtimes!

Last night, I completed most of the core functionality within the Microsoft.Xna.Framework.Game.dll assembly, and wrote a number of unit tests to exercise the Game logic.

Looks and runs identical to it's counterpart running under the Microsoft implementation of Xna.

PongFirstRun3 

My next step is to run it under the mono runtime on my windows machine and then follow with running it on my MacBook Pro.  This is completed.  Now for my Macbook Pro.

I will submit patches after I complete the additional classes I stubbed to get this basically working.

Tuesday, April 17, 2007

Mono.Xna - The beginning

Why?

Given my interest in game programming, graphics and generally making computers do interesting things other than running enterprise software, I've starting contributing to the mono.xna team.

These series of posts, tagged with XNA, will chronicle my findings, exploration and implementation of my areas of the XNA framework.

Hardware and Development

I own a Macbook Pro and a Windows laptop, so I can develop and test on both these platforms.  My primary development environment will be VS 2005, since I cannot live without Resharper 2.5, and think any serious software engineer should at least look at this tool.

And it begins...

After I obtained the current code, I found the best place to start was implementing the classes in Microsoft.Xna.Framework.Game.dll, since this assembly was largely unimplemented, and is really one of the core entry points for a consumer of this framework.

Based on previous conversations in the discussion group, Tao and SDL.NET are the cross-platform APIs of choice.  This was great to see, given I've previously used and contributed to SDL.NET for my C# implementation of the Frodo C64 emulator, and found it easy to use.

Wednesday, April 04, 2007

Command Prompt : Set the title for easier ALT-TAB

If you are like me, and run several command prompts at any given time, rooted at different folders, it turns into a game of chance, when 'ALT-TAB'ing. "Visual Studio 2005 Command Prompt" is a little too generic.

I'm in the habit of using the title command to use something more meaningful: