Esempio n. 1
0
        //constructor
        public GameEngine()
        {
            SetThreadExecutionState(ExecutionState.ES_CONTINUOUS | ExecutionState.ES_DISPLAY_REQUIRED | ExecutionState.ES_SYSTEM_REQUIRED);

            updateTimer.Elapsed   += onUpdateTimer;
            updateTimer.AutoReset  = true;
            inputTimer.Elapsed    += onInputTimer;
            inputTimer.AutoReset   = true;
            physicsTimer.Elapsed  += onPhysicsTimer;
            physicsTimer.AutoReset = true;
            drawTimer.Elapsed     += onDrawTimer;
            drawTimer.AutoReset    = true;

            int processors = Environment.ProcessorCount;

            if (processors >= 4)
            {
                drawTimer.ProcessorNumber    = processors - 3;
                updateTimer.ProcessorNumber  = processors - 2;
                inputTimer.ProcessorNumber   = processors - 1;
                physicsTimer.ProcessorNumber = processors;
            }
            else if (processors >= 2)
            {
                drawTimer.ProcessorNumber    = 1;
                updateTimer.ProcessorNumber  = 1;
                inputTimer.ProcessorNumber   = 2;
                physicsTimer.ProcessorNumber = 2;
            }

            updateTimer.Start();
            inputTimer.Start();
            physicsTimer.Start();
            drawTimer.Start();
        }
Esempio n. 2
0
        public void Test_StartStop()
        {
            int ms         = 200;
            int iterations = 5;

            using (PreciseTimer timer = new PreciseTimer(new TimerCallback(IncrementCount), TimeSpan.FromMilliseconds(ms), PreciseTimerCallbackMode.Async))
            {
                timer.Start();
                Thread.Sleep(ms * iterations);
                timer.Stop();
                Trace.WriteLine("Timer stopped");

                Assert.AreEqual(iterations, _Count);
            }
        }
Esempio n. 3
0
 //public
 public void Start()
 {
     if (timer != null)
     {
         startTime = MathUtil.TicksToMilliseconds();
         timer.Start();
     }
     else if (preciseTimer != null)
     {
         preciseTimer.Start();
     }
     else
     {
         new Thread(delegate() {
             onExecute(0.0d);
         }).Start();
     }
 }
Esempio n. 4
0
        public void Test_Precision()
        {
            int ms         = 1;
            int iterations = 100;

            using (PreciseTimer timer = new PreciseTimer(new TimerCallback(AddTicks), TimeSpan.FromMilliseconds(ms), PreciseTimerCallbackMode.Synchronized))
            {
                timer.Start();
                Thread.Sleep(ms * iterations);
                timer.Stop();
                Trace.WriteLine("Timer stopped");

                int  length = _Ticks.Count;
                long difference;
                for (int i = 1; i < length; i++)
                {
                    difference = _Ticks[i] - _Ticks[i - 1];
                    Trace.WriteLine(difference);

                    Assert.GreaterOrEqual(difference, ms * 10000);
                    Assert.LessOrEqual(difference, ms * 10000 + 120); // we should have at least a 120 nano second precision
                }
            }
        }
Esempio n. 5
0
        //constructor
        /// <summary>
        /// A physical window that displays on the screen. This is where all DisplayObjects will live when drawn to the screen. You may have multiple windows.
        /// </summary>
        /// <param name="width">The window's width.</param>
        /// <param name="height">The window's height.</param>
        /// <param name="title">The window's title.</param>
        /// <param name="style">Any style flags to apply to the window.</param>
        /// <param name="vSync">(optional) Whether or not this window syncs its rendering to the refresh rate of the monitor.</param>
        /// <param name="synchronous">(optional) Whether or not the window's update() and draw() calls will be handled by the main GameEngine (ture) or itself (false).</param>
        /// <param name="antiAliasing">(optional) The amount of AntiAliasing to use. More = slower but lexx pixelated.</param>
        public Window(double width, double height, string title, WindowStyle style, bool vSync = true, bool synchronous = true, uint antiAliasing = 16)
        {
            if (double.IsNaN(width))
            {
                throw new ArgumentNullException("width");
            }
            if (double.IsInfinity(width) || width < 1.0d)
            {
                throw new InvalidOperationException("width cannot be less than 1 or infinity.");
            }
            if (double.IsNaN(height))
            {
                throw new ArgumentNullException("height");
            }
            if (double.IsInfinity(height) || height < 1.0d)
            {
                throw new InvalidOperationException("height cannot be less than 1 or infinity.");
            }

            window = new RenderWindow(new VideoMode((uint)width, (uint)height), title, (Styles)style, new ContextSettings(24, 8, antiAliasing));

            styles               = style;
            _fullscreen          = ((style & WindowStyle.Fullscreen) != WindowStyle.None) ? true : false;
            _title               = title;
            previousVsync        = _vSync = vSync;
            previousSynchronous  = _synchronous = synchronous;
            previousAntialiasing = _antiAliasing = antiAliasing;

            _icon = TextureUtil.FromBitmap(new System.Drawing.Bitmap(1, 1));
            window.SetIcon(_icon.Size.X, _icon.Size.Y, TextureUtil.ToBytes(_icon));

            window.Closed  += onClosed;
            window.Resized += onResize;

            window.GainedFocus         += onFocused;
            window.KeyPressed          += onKeyDown;
            window.KeyReleased         += onKeyUp;
            window.MouseMoved          += onMouseMove;
            window.MouseWheelScrolled  += onMouseWheel;
            window.MouseButtonPressed  += onMouseDown;
            window.MouseButtonReleased += onMouseUp;

            window.SetVerticalSyncEnabled(vSync);
            IntRect vp = window.GetViewport(window.GetView());

            viewport.Width  = (double)vp.Width;
            viewport.Height = (double)vp.Height;
            _quadTree       = new QuadTree <DisplayObject>(new PreciseRectangle(0.0d, 0.0d, viewport.Width, viewport.Height));

            int processors = Environment.ProcessorCount;

            if (processors >= 4)
            {
                drawTimer.ProcessorNumber   = processors - 3;
                updateTimer.ProcessorNumber = processors - 2;
            }
            else if (processors >= 2)
            {
                drawTimer.ProcessorNumber   = 1;
                updateTimer.ProcessorNumber = 1;
            }

            updateTimer.Elapsed  += onUpdateTimer;
            updateTimer.AutoReset = true;
            drawTimer.Elapsed    += onDrawTimer;
            drawTimer.AutoReset   = true;

            window.SetActive(false);

            _physicsWorld = physicsEngine.CreateWorld();
            inputEngine.AddWindow(this);
            gameEngine.AddWindow(this);
            Start.AddWindow(this);

            if (!synchronous)
            {
                updateTimer.Start();
                drawTimer.Start();
            }
        }