Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- namespace FastGameLoopSandbox
- {
- public class FastLoop
- {
- PreciseTimer _timer = new PreciseTimer();
- public delegate void LoopCallback(double deltaTime);
- LoopCallback _callback;
- [StructLayout(LayoutKind.Sequential)]
- public struct Message
- {
- public IntPtr hWnd;
- public Int32 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);
- public FastLoop(LoopCallback callback)
- {
- _callback = callback;
- Application.Idle += new EventHandler(OnApplicationEnterIdle);
- }
- void OnApplicationEnterIdle(object sender, EventArgs e)
- {
- while (IsAppStillIdle())
- {
- _callback(_timer.GetElapsedTime());
- }
- }
- private bool IsAppStillIdle()
- {
- Message msg;
- return(PeekMessage(out msg, IntPtr.Zero, 0, 0, 0));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement