My last post on render loops (hopefully)..

The most common topic on my blog returns again.  This time it will be brief as all I’m going to to do now is show you the render loop the June’05 SDK will be using.  A coworker in another group came up with this markedly simple, yet deceptively effective loop for that groups projects.  I liked it so much, i’m sharing it with everyone else. =)

The basic loop (slightly modified from his original version and the version in the new SDK for ease of reading):

public void MainLoop()
{
// Hook the application’s idle event
System.Windows.Forms.Application.Idle += new EventHandler(OnApplicationIdle);
System.Windows.Forms.Application.Run(myForm);
}

private void OnApplicationIdle(object sender, EventArgs e)
{
while (AppStillIdle)
{
// Render a frame during idle time (no messages are waiting)
UpdateEnvironment();
Render3DEnvironment();
}
}

private bool AppStillIdle
{
get
{
NativeMethods.Message msg;
return !NativeMethods.PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
}
}
And the declarations for those two native methods members:

[StructLayout(LayoutKind.Sequential)]
public struct Message
{
public IntPtr hWnd;
public WindowMessage msg;
public IntPtr wParam;
public IntPtr lParam;
public uint time;
public System.Drawing.Point p;
}

[System.Security.SuppressUnmanagedCodeSecurity] // We won’t use this maliciously
[DllImport(“User32.dll”, CharSet=CharSet.Auto)]
public static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
——

Simple, elegant, effective.  No extra allocations, no extra collections, it just works..  The Idle event fires when there’s no messages in the queue, and then the handler keeps looping continuously until a message does appear, in which case it stops..  Once all the messages are handled, the idle event is fired again, and the process starts over.

Win a prize ($50,000) for writing Managed DX apps..

Since I’m feeling quite lazy, I’m just going to copy the basic post from the Zbuffer site..

So you think you are a good Managed DirectX programmer eh ? Well if you have some SQL skills (or a friend who does) then you could be ready to enter the Connected Systems Developer Competition. There are 15 categories to enter and number 10 is:

  • Best use of Managed DirectX, Visual Studio 2005 and SQL Server Express

I know it seems strange to put DirectX and SQL server together, but the competition *is* all about connected systems. Closing date is September 15th 2005 so you have plenty of time. Lots of other prizes too – check out the official rules