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

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

  • 相关阅读:
    HTML大文件上传(博客迁移)
    微信小程序初探
    基于NodeJS微信公众号
    基于NodeJS的秘室聊天室
    CSS3 值得称赞新特性
    CAS FOR WINDOW ACTIVE DIRECTORY SSO单点登录
    IOS学习之-私人通讯录
    android 模拟2048
    使用ctypes在Python中调用C++动态库
    [Leetcode] Longest Palindromic Subsequence
  • 原文地址:https://www.cnblogs.com/rainbow70626/p/4553661.html
Copyright © 2020-2023  润新知