zoukankan      html  css  js  c++  java
  • C#游戏开发中快速的游戏循环

    C#游戏开发中快速的游戏循环的实现。参考《精通C#游戏编程》一书。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace GameLoop
    {
        [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;
        }
    
        public class FastLoop
        {
            [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);
    
            PreciseTimer _timer = new PreciseTimer();
            public delegate void LoopCallback();
            LoopCallback _callback;
    
            public FastLoop(LoopCallback callback)
            {
                _callback = callback;
                Application.Idle += new EventHandler(OnApplicationEnterIdle);
            }
    
            void OnApplicationEnterIdle(object sender, EventArgs e)
            {
                while (IsAppStillIdle())
                {
                    _callback();
                }
            }
    
            private bool IsAppStillIdle()
            {
                Message msg;
                return !PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
            }
    
        }
    
    }

    ---恢复内容结束---

  • 相关阅读:
    Mac使用Homebrew进行软件包管理
    RNN模拟二进制加法
    虚拟机安装ubuntu18.04
    github合并分支到master
    Python配置虚拟环境
    Python的进程、线程、协程
    原码,反码,补码
    MySQL中的截位函数:RIGHT与LEFT
    MySQL查询和删除重复记录
    Mysql中的数据类型
  • 原文地址:https://www.cnblogs.com/rainbow70626/p/4553661.html
Copyright © 2011-2022 走看看