Wednesday, February 03, 2010

else....where??

http://thedailywtf.com/Articles/Else-where.aspx

「我手下曾經有一個非常強的專家,他留下一句名言:給我足夠的NAND Gate(譯註 : 這是一種邏輯閘,相當於先取and再取not...剩下的請查閱這裡),我將統治全世界」,Rob B,在他的一本書裡面寫到:「其實換個繞口令一點的說法,你用上一整片的NAND Gates,你可以做出其他邏輯閘能作到的任何事情。當然啦,因為有其他邏輯閘能作這些事情,你實在不該浪費一狗票NAND gates來作這種無聊的事情。恩...這樣的確是辦得到的...寫code的時候偶爾也會有類似的思維」

「前陣子我們從我們的外包商那裡看到了一份實在是爛到爆炸的C++ code。這東西平均每行出錯率實在是真他媽的高,而且他的寫碼風格讓我們越看越想咒他老母。不過,這種What the fuck的感覺,到我們看到這組code的時候達到了最高點。」


while(true)
{
if(mainType == 7)
{
subType = 4;
break;
}

if(mainType == 9)
{
subType = 6;
break;
}

if(mainType == 11)
{
subType = 9;
break;
}

break;
}


「來來來我們看看這堆鳥東西....」Rob繼續寫到:「我花了些時間在想,他幹麼寫這樣?這個while loop到底是幹麼的?只是拿來搭配break跳過下面那些if的嗎?我還真沒看過那麼蠢的,完全不是拿來作條件判斷的break...好啦,這是我第一次看到。」

Bob補充:「我幾乎可以確定這傢伙完全不知道什麼叫做else if....他好像只知道if while break,所以就把他們拼在一起了...用這種超白爛的方法做出這個邏輯。」

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);
}