0

这是我的 program.cs 文件(这都是在 VS 2010 上用 C# 完成的)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;

    namespace GameLoop
    {
     static class Program
        {
            static FastLoop _fastLoop = new FastLoop(GameLoop);
            /// <summary>
            /// The main entry point for the application.
         /// </summary>
            [STAThread]
         static void Main()
         {
               Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
         }
         static void GameLoop(double elapsedTime)
          {
                //GameCode goes here
             //Get Input
             //Process
            //Render
                System.Console.WriteLine("loop");
         }
      }
    }

这是我的 FastLoop.cs 文件(这都是在 C# 中的 VS 2010 上完成的)

    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(double elapsedTime);
            LoopCallback _callback;

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

            }
        }
    }

这是我的 PerciseTimer.cs 文件(这都是在 C# 中的 VS 2010 上完成的)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;

    namespace GameLoop
    {
        public class PreciseTimer
        {
            [System.Security.SuppressUnmanagedCodeSecurity]
            [DllImport("kernal32")]
            private static extern bool QueryPerformanceFrequency(ref long PerformanceFrequency);

             [System.Security.SuppressUnmanagedCodeSecurity]
            [DllImport("kernal32")]
        private static extern bool QueryPerformanceCounter(ref long PerformanceCount);

            long _ticksPerSecond = 0;
            long _previousElapsedTime = 0;

            public PreciseTimer()
            {
                QueryPerformanceFrequency(ref _ticksPerSecond);
                GetElapsedTime();//get rid if first rubbish result
            }
            public double GetElapsedTime()
            {
                long time = 0;
                QueryPerformanceCounter (ref time);
                double elapsedTime = (double)(time -_previousElapsedTime)/(double)_ticksPerSecond;

                _previousElapsedTime = time;
                return elapsedTime;
        }
    }
    }

这是我的 form1.cs 文件(这都是在 C# 中的 VS 2010 上完成的)

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace GameLoop
    {
        public partial class Form1 : Form
        {
            bool _fullscreen = true;
            public Form1()
            {
                InitializeComponent();
                simpleOpenGlControl1.InitializeContexts();
                if (_fullscreen)
                {
                    FormBorderStyle = FormBorderStyle.None;
                    WindowState = FormWindowState.Maximized;

                }
            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }

            private void simpleOpenGlControl1_Load(object sender, EventArgs e)
            {

            }
        }
    }
4

1 回答 1

0

发生 TypeInitializationException 是因为由于构造函数中发生的另一个异常或由于静态初始化的字段而无法初始化您的类型。

  • 通常 TypeInitializationException 只是另一个实际导致问题的异常的包装器,因此检查内部异常通常会向您显示问题。

  • 如果这不起作用,请尝试从构造函数中注释(或移动代码)以消除这种可能性。

  • 将任何静态字段的初始化移动到函数中并显式调用它们。

于 2012-08-27T15:31:13.550 回答