Advertisement
Guest User

Untitled

a guest
Oct 24th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows.Forms;
  7. using System.Runtime.InteropServices;
  8.  
  9. namespace FastGameLoopSandbox
  10. {
  11.     public class FastLoop
  12.     {
  13.         PreciseTimer _timer = new PreciseTimer();
  14.         public delegate void LoopCallback(double deltaTime);
  15.         LoopCallback _callback;
  16.  
  17.         [StructLayout(LayoutKind.Sequential)]
  18.         public struct Message
  19.         {
  20.             public IntPtr hWnd;
  21.             public Int32 msg;
  22.             public IntPtr wParam;
  23.             public IntPtr lParam;
  24.             public uint time;
  25.             public System.Drawing.Point p;
  26.         }
  27.  
  28.         [System.Security.SuppressUnmanagedCodeSecurity]
  29.         [DllImport("User32.dll", CharSet = CharSet.Auto)]
  30.         public static extern bool PeekMessage(
  31.             out Message msg,
  32.             IntPtr hWnd,
  33.             uint messageFilterMin,
  34.             uint messageFilterMax,
  35.             uint flags);
  36.  
  37.         public FastLoop(LoopCallback callback)
  38.         {
  39.             _callback = callback;
  40.             Application.Idle += new EventHandler(OnApplicationEnterIdle);
  41.         }
  42.  
  43.         void OnApplicationEnterIdle(object sender, EventArgs e)
  44.         {
  45.             while (IsAppStillIdle())
  46.             {
  47.                 _callback(_timer.GetElapsedTime());
  48.             }
  49.         }
  50.  
  51.         private bool IsAppStillIdle()
  52.         {
  53.             Message msg;
  54.             return(PeekMessage(out msg, IntPtr.Zero, 0, 0, 0));
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement