The implementation of a fast game loop in C # game development. Refer to the book "Mastering C # Game Programming".
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Runtime.InteropServices;namespacegameloop{[StructLayout (layoutkind.sequential)] Public structMessage { PublicIntPtr hWnd; PublicInt32 msg; PublicIntPtr WParam; PublicIntPtr LParam; Public UINTTime ; PublicSystem.Drawing.Point p; } Public classFastLoop {[System.Security.SuppressUnmanagedCodeSecurity] [DllImport ("User32.dll", CharSet =CharSet.Auto)] Public Static extern BOOLPeekMessage ( outMessage msg, IntPtr hWnd,UINTMessagefiltermin,UINTMessagefiltermax,UINTflags); Precisetimer _timer=NewPrecisetimer (); Public Delegate voidLoopcallback (); Loopcallback _callback; PublicFastLoop (Loopcallback callback) {_callback=callback; Application.idle+=NewEventHandler (Onapplicationenteridle); } voidOnapplicationenteridle (Objectsender, EventArgs e) { while(Isappstillidle ()) {_callback (); } } Private BOOLIsappstillidle () {Message msg; return! PeekMessage ( outMSG, IntPtr.Zero,0,0,0); } }}
---restore content ends---
A fast game loop in C # game development