Tuesday, February 02, 2010

Classic WTF : What the hell enum!?

One day, while I was roaming through Internet, I bump into a article while I was viewing a frequently viewed forum =3

in HeaderDefine.h(This is a header file that will be included in almost every files)


/*----Be sure you have READ this!----
...
...(a long term of manual of this header)
...

I have made this way to make sure
everyone will read this manual.
By Genius Jason
----------------------------------------------/*
enum DECLARE
{
I_HAVE_READ_THE_MANUAL_AND_KNOW_WHAT_I_AM_DOING_THIS = 38106,
I_HAVE_READ_ABOVE_AND_AGGREED_THE_TERM_OF_USE = 39100
};


You might be confused what the hell it is for, ya, it's a trick. While using some function or callback, you have to follow his way or a assert will be invoked :

KaimCore.Init(I_HAVE_READ_ABOVE_AND_.....
KamiCore.CleanupDump(I_HAVE_READ_THE_MANUAL_AND...



"Fuck you Jason you damn moron!"

Game loop in C#

Game update loop is a problem in C#. Of course, first thought of us must be attach the Idle event of Application, like this.


Application.Idle += new EventHandler(OnAppIdle);
Application.Run(mMainFrame);

........
........

static void OnAppIdle(object sender, EventArgs e)
{
//Do some game loop update here
mMainFrame.DoUpdate();

}



It seems reasonable, we register a listener to Idle event, so it must be run OnAppIdle while application idle, right?

Partially right.

When the application keep going, it will only get message while "When application need to be updated". It means, therefore, if you leave mouse out of this application form(so no MouseMove event sent), it will totally stop, so it will be updated only when getting some message : such like OnMouseMove....etc.

Solution is from Rich, and he found it in sample of MDX applications. Don't ask me how to do it, just need to know use this way to keep the game loop.

We might discuss this in later post =3


static class Program
{
///
/// The main entry point for the application.
///

///
static SceneEditorMain mMainFrame;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

mMainFrame = new SceneEditorMain();
Application.Idle += new EventHandler(OnAppIdle);
Application.Run(mMainFrame);
}

static void OnAppIdle(object sender, EventArgs e)
{
while (AppStillIdle)
{
mMainFrame.DoUpdate();
}
}


private static bool AppStillIdle
{
get
{
NativeMethods.Message msg;
return !NativeMethods.PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
}
}


}

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

[System.Security.SuppressUnmanagedCodeSecurity]
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
}