Freigeben über


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.

Comments

  • Anonymous
    May 05, 2005
    Tom Miller finally states the last word on the subject of game loops in Managed DirectX code.  Now...

  • Anonymous
    May 06, 2005
    The comment has been removed

  • Anonymous
    May 06, 2005
    Why on earth is this page using SSL?

  • Anonymous
    May 07, 2005
    Tom, in your Message struct, what is WindowMessage? Would that be System.Windows.Forms.Message?

  • Anonymous
    May 07, 2005
    How does this stand up to supporting rendering to Controls and not just a Form, I wonder..?

  • Anonymous
    May 08, 2005
    Added link to 'the saga of the MDX render loop' http://www.thezbuffer.com/articles/185.aspx

  • Anonymous
    May 08, 2005
    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

  • Anonymous
    May 08, 2005
    The comment has been removed

  • Anonymous
    May 08, 2005
    public WindowMessage msg;

    Where does "WindowMessage" come from? I can't seem to find it in any of the namespaces!

  • Anonymous
    May 09, 2005
    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.

  • Anonymous
    May 12, 2005

    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 []

  • Anonymous
    May 12, 2005

    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 :)

  • Anonymous
    May 16, 2005
    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.

  • Anonymous
    May 16, 2005
    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.

  • Anonymous
    May 22, 2005
    Wassup with all that C# Code?
    Forgot about vb have we?

  • Anonymous
    May 26, 2005
    The PeekMessage loop has been in use for ages by game programmers who code in unmanaged DirectX and even OpenGL. What surprises me is that it took such a long before someone was able to figure it out for managed dx!

  • Anonymous
    May 30, 2005
    I put the using directive System.Runtime.InteropServices and that solved all of the errors except for one that says:

    The type or namespace name 'WindowMessage' could not be found (are you missing a using directive or an assembly reference?)

    I've looked in the MSDN and all over the web, and I cannot find the namespace it is in. Thank you for your help.

  • Anonymous
    May 31, 2005
    I can only guess this works - since I cannot get it to run. Reason for this being that I - as an average programmer - simply have no idea what namespaces / references to include.

    What type is WindowMessage? IntPtr? (I tried looking for WindowMessage in MSDN to no avail).

    System.Drawing.NativeMethods seems to be an internal class. Consequently I get compilation errors when trying to compile the code. What am I missing?

    Might be I am the only one that plays around with MDX without a complete understanding and knowledge of the .net framework, at least to my very limited mind a complete code sample would be extremely helpful.

    Don't get me wrong, I am not critizing here. It's just that I have very limited ressources (both time and brain ;) )

  • Anonymous
    June 02, 2005
    I have made a sample framework app using the loop you have suggested here. It does work better than the original SDK loop. It is located <a href="http://photongl.blogspot.com">here</a>.

  • Anonymous
    June 04, 2005
    I'm wondering how much of a performance difference will be if I take out the AppStillIdle while loop and just call the update and render methods within OnApplicationIdle.

    Tom, do you have any figures on this?

  • Anonymous
    June 08, 2005
    That looks great. I am new to DirectX, but I remember back when doing OpenGL that the main render loop was the most difficult part- especially trying to balance always redrawing without overworking the CPU.

    Could you provide the source for the C# version of the WindowMessage struct as well? I am having difficulty getting this to compile since I assume that structure is defined else where in your code. (Is it defined somewhere in the managed source for the April MDX SDK update?)

    Thanks,
    -Chris

  • Anonymous
    June 11, 2005
    Simply put: Yuck. That's not simple nor elegant. :/

  • Anonymous
    June 21, 2005
    Hrm. The Message struct is already defined by the .NET libraries as:

    System.Windows.Forms.Message

  • Anonymous
    February 08, 2006
    I have checked in a new initial version of the Client UI which is based on the OnApplicationIdle style...

  • Anonymous
    May 02, 2006
    The comment has been removed

  • Anonymous
    November 29, 2006
    PingBack from http://www.zaknafein.hjcrusaders.com/?p=15

  • Anonymous
    November 29, 2006
    PingBack from http://www.zaknafein.hjcrusaders.com/?p=14

  • Anonymous
    May 29, 2009
    PingBack from http://paidsurveyshub.info/story.php?title=tom-miller-s-blog-my-last-post-on-render-loops-hopefully

  • Anonymous
    June 12, 2009
    PingBack from http://insomniacuresite.info/story.php?id=8983