Tom Miller's Blog

The Ramblings of Miller. These postings are provided "AS IS" with no warranties, and confer no rights.

My last post on render loops (hopefully)..

My last post on render loops (hopefully)..

  • Comments 29

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.

  • Tom Miller finally states the last word on the subject of game loops in Managed DirectX code.  Now...
  • So Tom how does this way of looping compare (fps and memory wise) to the way Rick Hoskinson recently started discussing on his blog. http://blogs.msdn.com/rickhos/archive/2005/04/04/405327.aspx

    -Richard
    Richard.Parsons (at) gmail.com
  • Why on earth is this page using SSL?
  • Tom, in your Message struct, what is WindowMessage? Would that be System.Windows.Forms.Message?
  • How does this stand up to supporting rendering to Controls and not just a Form, I wonder..?
  • Added link to 'the saga of the MDX render loop' http://www.thezbuffer.com/articles/185.aspx
  • Hi Tom,

    Can't help but notice that your new approach, if i'm not mistaken, is quite similar how GLUT handles the rendering. In GLUT, you need to hook up a callback to the idle event for rendering. Your latest approach is the cleanest so far. KUDOS Tom!

    BTW, I have your book "Beginning 3D Game Programming" and it ROCKS!

    PS: Sorry about the OpenGL / GLUT post. Since I moved to using C#, I'm now a Managed DirectX fan :P
  • This is bloody brilliant, why didn't I think of this? I have perhaps the most complicated render loop in the history of the universe right now (mind you, it handles switching between windowed/fullscreen, task switching, res changes flawlessly).
  • public WindowMessage msg;

    Where does "WindowMessage" come from? I can't seem to find it in any of the namespaces!
  • I've been tracking the posts regarding render loops, but early on I moved the scene preperation and rendering into a seperate thread. The thread renders to a window or a full screen, at synchronized frame rate or at maximum, with varying "sleep" times. This has worked much better than all proposed "main thread" render loop proposals so far.

  • i was going to try it out, but where did you get that WindowMessage enum/struct/class/whatever from? the one inside the Message struct declaration.

    thanks []

  • also, why the use of SuppressUnmanagedCodeSecurity? theres no need for that (i think).

    and where is that CharSet.Auto from? the only one i know is CharacterSet, and it has no .Auto

    also, could you post a complete working example please? something really simple like device.Clear(...); device.Present();

    i managed to compile it by removing the CharSet=CharSet.Auto and chaging the public WindowMessage msg; Message struct member to public int msg; (since i dont know where that WindowMessage is), but i dont know if it is as good as yours.

    again thanks :)
  • Is there anywhere a newbie can go to view VB.NET solutions to the issues that started this issue applies to VB.NET Managed DirectX coding too?

    Everything about Managed DirectX samples and blog chats is infuriatingly C# centric. Am I simply choosing the wrong language if I wish to learn to use Managed DirectX? The utter lack of VB.NET samples even in the DirectX SDK itself, not to mention no talk of how this render loop issue here is implemented in VB.NET, suggests VB.NET is indeed a poor language choice.

    Any pointers to a good VB site covering this issue would be very welcome.
  • I hate to sound stupid but what #using' do you need with that.

    I copied the code in to a blank project and I get compile errors.

    Thanks

    Mort.
  • Wassup with all that C# Code?
    Forgot about vb have we?
Page 1 of 2 (29 items) 12