Ejemplo n.º 1
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();
            }
        }