private void CreateSwapChain()
        {
            if (!Globals.ClosingEditor)
            {
                if (mChain != null)
                {
                    mChain.Dispose();
                }

                if (Graphics.GetGraphicsDevice() != null)
                {
                    if (pnlMapGrid.Width > 0 && pnlMapGrid.Height > 0)
                    {
                        if (pnlMapGrid.Width > 0 && pnlMapGrid.Height > 0)
                        {
                            mChain = new SwapChainRenderTarget(
                                Graphics.GetGraphicsDevice(), pnlMapGrid.Handle, pnlMapGrid.Width, pnlMapGrid.Height,
                                false, SurfaceFormat.Color, DepthFormat.Depth24, 0, RenderTargetUsage.DiscardContents,
                                PresentInterval.Immediate
                                );

                            Graphics.SetMapGridChain(mChain);
                        }
                    }
                }
            }
        }
        public void OnWindowSizeChanged(GraphicsDeviceControl control)
        {
            var clientSize = control.ClientSize;

            if (clientSize.Width <= 0 || clientSize.Height <= 0)
            {
                return;
            }

            _chain?.Dispose();

            var graphicsDevice = control.GraphicsDevice;

            if (graphicsDevice != null)
            {
                _chain = new SwapChainRenderTarget(graphicsDevice, control.Handle, clientSize.Width, clientSize.Height);

                graphicsDevice.PresentationParameters.BackBufferWidth  = clientSize.Width;
                graphicsDevice.PresentationParameters.BackBufferHeight = clientSize.Height;

                graphicsDevice.Viewport = new Viewport(0, 0, clientSize.Width, clientSize.Height, 0, 1);
            }

            if (_chain != null)
            {
                SwapChainUpdated?.Invoke(this, new SwapChainUpdatedEventArgs(_chain));
            }
        }
Example #3
0
        /// <summary>
        /// Initializes the GFX system, which contains basic MonoGame functionality.
        /// </summary>
        /// <param name="graphics">The graphics device service</param>
        /// <param name="swapChainRenderTarget">The swap chain render target</param>
        public void InitializeGFX(IGraphicsDeviceService graphics, SwapChainRenderTarget swapChainRenderTarget)
        {
            services = new GameServiceContainer();
            services.AddService <IGraphicsDeviceService>(graphics);

            this.graphics         = graphics.GraphicsDevice;
            SwapChainRenderTarget = swapChainRenderTarget;

            GetRenderTargetManager  = new RenderTargetManager(this);
            AntialisingRenderTarget = GetRenderTargetManager.CreateNewRenderTarget2D("MSAA", true);

            Content     = new ContentManager(services, "Content");
            spriteBatch = new SpriteBatch(graphics.GraphicsDevice);

            InternContent = new ResourceContentManager(services, Properties.Resources.ResourceManager);
            Font          = InternContent.Load <SpriteFont>("Font");
            FontHeight    = Font.MeasureString("A").Y;

            Pixel = new Texture2D(graphics.GraphicsDevice, 1, 1);
            Pixel.SetData(new[] { Color.White });

            Format = new System.Globalization.NumberFormatInfo();
            Format.CurrencyDecimalSeparator = ".";

            Cam             = new Camera2D();
            Cam.GetPosition = new Vector2(
                graphics.GraphicsDevice.Viewport.Width / 2, graphics.GraphicsDevice.Viewport.Height / 2);

            RenderTargetTimer          = new Timer();
            RenderTargetTimer.Interval = 500;
            RenderTargetTimer.Elapsed += (sender, e) => OnRenderTargetTimeOutEnd();
        }
Example #4
0
 private void DrawWindow_UpdateSwapChainRenderTarget(SwapChainRenderTarget obj)
 {
     if (_Editor != null)
     {
         _Editor.SwapChainRenderTarget = obj;
     }
 }
Example #5
0
        /// <summary>
        /// Initializes the control.
        /// </summary>
        protected override void OnCreateControl()
        {
            // Don't initialize the graphics device if we are running in the designer.
            if (!DesignMode)
            {
                this.graphicsDeviceService = GraphicsDeviceService.AddRef(Handle,
                                                                          ClientSize.Width,
                                                                          ClientSize.Height);
                this.swapChainRT = new SwapChainRenderTarget(graphicsDeviceService.GraphicsDevice,
                                                             this.Handle,
                                                             ClientSize.Width,
                                                             ClientSize.Height)
                {
                    PresentInterval = PresentInterval.Immediate
                };

                // Register the service, so components like ContentManager can find it.
                services.AddService <IGraphicsDeviceService>(graphicsDeviceService);

                // Give derived classes a chance to initialize themselves.
                Initialize();
            }

            base.OnCreateControl();
        }
Example #6
0
        /// <summary>
        /// Initializes the GFX system, which contains basic MonoGame functionality.
        /// </summary>
        /// <param name="graphics">The graphics device service</param>
        /// <param name="swapChainRenderTarget">The swap chain render target</param>
        public void InitializeGFX_DX(
            IGraphicsDeviceService graphics,
            SwapChainRenderTarget swapChainRenderTarget)
        {
            InitializeGFX(graphics);

            SwapChainRenderTarget = swapChainRenderTarget;
        }
 private void RemoveRenderTarget()
 {
     lock (Lock ?? _dummyLock)
     {
         _renderTarget.SafeDispose();
         _renderTarget = null;
     }
 }
Example #8
0
    private void OnGraphicsServiceChanged(IGraphicsService oldGraphicsService, IGraphicsService newGraphicsService)
    {

      if (oldGraphicsService != null)
      {
        _renderTarget.SafeDispose();
        _renderTarget = null;
      }

    }
Example #9
0
 protected override void OnCreateControl()
 {
     if (!designMode)
     {
         _graphicsDeviceService = GraphicsDeviceService.AddRef(Handle, ClientSize.Width, ClientSize.Height);
         _chain = new SwapChainRenderTarget(_graphicsDeviceService.GraphicsDevice, Handle, ClientSize.Width,
                                            ClientSize.Height);
         Services.AddService <IGraphicsDeviceService>(_graphicsDeviceService);
         Initialize();
     }
     base.OnCreateControl();
 }
Example #10
0
        private void CreateGameWindow()
        {
            gameWindow          = GameWindow.Create(this, 300, 300);
            gameWindow.Position = (Vector2.One * 300).ToPoint();
            target = new SwapChainRenderTarget(GraphicsDevice, gameWindow.Handle, 300, 300);

            var f = System.Windows.Forms.Control.FromHandle(gameWindow.Handle).FindForm();

            f.Show();

            //target.Present();
        }
 #pragma warning disable 1591
 protected override void OnCreateControl()
 {
     if (!designMode)
     {
         _graphicsDeviceService = GraphicsDeviceService.AddRef(Handle, ClientSize.Width, ClientSize.Height, GraphicsProfile);
         _chain = new SwapChainRenderTarget(_graphicsDeviceService.GraphicsDevice, Handle, ClientSize.Width,
                                            ClientSize.Height);
         Services.AddService <IGraphicsDeviceService>(_graphicsDeviceService);
         Initialize();
         Microsoft.Xna.Framework.Input.Mouse.WindowHandle = Handle;
     }
     base.OnCreateControl();
 }
Example #12
0
        private void RefreshDXWindow()
        {
            if (_chain != null)
            {
                _chain.Dispose();
                _chain = new SwapChainRenderTarget(_graphicsDeviceService.GraphicsDevice, Handle, ClientSize.Width,
                                                   ClientSize.Height);

                GraphicsDevice.PresentationParameters.BackBufferWidth  = ClientSize.Width;
                GraphicsDevice.PresentationParameters.BackBufferHeight = ClientSize.Height;

                SwapChainRenderTargetRefreshed?.Invoke(_chain);
            }
        }
        public void Initialize(GraphicsDeviceControl control)
        {
            if (control.IsDesignMode)
            {
                return;
            }

            var clientSize = control.ClientSize;

            var width  = Math.Max(clientSize.Width, 1);
            var height = Math.Max(clientSize.Height, 1);

            _chain = new SwapChainRenderTarget(control.GraphicsDevice, control.Handle, width, height);
        }
Example #14
0
 public virtual void SetWindowSize(int width, int height)
 {
     //if window size doesn't match, resize it and create new render target of proper size
     if (width > 0 && height > 0)
     {
         if (SwapChain == null || width != SwapChain.Bounds.Width || height != SwapChain.Bounds.Height)
         {
             Form.ClientSize = new System.Drawing.Size(width, height);
             SwapChain?.Dispose();
             SwapChain = new SwapChainRenderTarget(GraphicsDevice, Window.Handle, width, height);
             ResizeRecursive(new Rectangle(0, 0, width, height));
         }
     }
 }
Example #15
0
        /// <summary>
        /// Helper used by BeginDraw. This checks the graphics device status,
        /// making sure it is big enough for drawing the current control, and
        /// that the device is not lost. Returns an error string if the device
        /// could not be reset.
        /// </summary>
        string HandleDeviceReset()
        {
            bool deviceNeedsReset = false;
            var  clientSize       = this.ClientSize;

            switch (GraphicsDevice.GraphicsDeviceStatus)
            {
            case GraphicsDeviceStatus.Lost:
                // If the graphics device is lost, we cannot use it at all.
                return("Graphics device lost");

            case GraphicsDeviceStatus.NotReset:
                // If device is in the not-reset state, we should try to reset it.
                deviceNeedsReset = true;
                break;

            default:
                // If the device state is ok, check whether it is big enough.
                PresentationParameters pp = GraphicsDevice.PresentationParameters;

                deviceNeedsReset = (clientSize.Width != pp.BackBufferWidth) ||
                                   (clientSize.Height != pp.BackBufferHeight);
                break;
            }

            // Do we need to reset the device?
            if (deviceNeedsReset)
            {
                try
                {
                    this.swapChainRT.Dispose();
                    this.swapChainRT = new SwapChainRenderTarget(
                        this.graphicsDeviceService.GraphicsDevice,
                        this.Handle,
                        clientSize.Width,
                        clientSize.Height)
                    {
                        PresentInterval = PresentInterval.Immediate
                    };
                }
                catch (Exception e)
                {
                    return("Graphics device reset failed\n\n" + e);
                }
            }

            return(null);
        }
Example #16
0
        /// <summary>
        /// Disposes the control.
        /// </summary>
        protected override void Dispose(bool disposing)
        {
            if (this.swapChainRT != null && !this.swapChainRT.IsDisposed)
            {
                this.swapChainRT.Dispose();
                this.swapChainRT = null;
            }

            if (this.graphicsDeviceService != null)
            {
                this.graphicsDeviceService.Release(disposing);
                this.graphicsDeviceService = null;
            }

            base.Dispose(disposing);
        }
Example #17
0
        protected override void Dispose(bool disposing)
        {
            if (graphicsDeviceService != null)
            {
                graphicsDeviceService.Release(disposing);
                graphicsDeviceService = null;
            }

            if (_renderTarget != null)
            {
                _renderTarget.Dispose();
                _renderTarget = null;
            }

            base.Dispose(disposing);
        }
Example #18
0
        protected override void OnCreateControl()
        {
            // Don't initialize the graphics device if we are running in the designer.
            if (!DesignMode)
            {
                graphicsDeviceService = GraphicsDeviceService.AddRef(Handle,
                                                                     ClientSize.Width,
                                                                     ClientSize.Height);


                services.AddService <IGraphicsDeviceService>(graphicsDeviceService);
                _renderTarget = new SwapChainRenderTarget(GraphicsDevice, Handle, ClientSize.Width, ClientSize.Height);
                Initialize();
            }
            base.OnCreateControl();
        }
Example #19
0
        private void RefreshGLWindow()
        {
            if (_chain != null)
            {
                _chain.Dispose();
                _chain = new SwapChainRenderTarget_GL(_graphicsDeviceService.GraphicsDevice, ClientSize.Width,
                                                      ClientSize.Height);

                GraphicsDevice.PresentationParameters.BackBufferWidth  = ClientSize.Width;
                GraphicsDevice.PresentationParameters.BackBufferHeight = ClientSize.Height;
                GraphicsDevice.Viewport = new Viewport(0, 0, ClientSize.Width, ClientSize.Height);

                Sdl.Window.SetSize(_graphicsDeviceService.SDLPlatform.Window.Handle, ClientSize.Width, ClientSize.Height);

                SwapChainRenderTargetRefreshed?.Invoke(_chain);
            }
        }
Example #20
0
 private void CreateSwapChain()
 {
     if (_chain != null)
     {
         _chain.Dispose();
     }
     if (_graphicsDevice != null)
     {
         if (this.Width > 0 && this.Height > 0)
         {
             _chain = new SwapChainRenderTarget(_graphicsDevice,
                                                this.Handle,
                                                this.Width, this.Height, false, SurfaceFormat.Color,
                                                DepthFormat.Depth24, 0, RenderTargetUsage.DiscardContents, PresentInterval.Immediate);
         }
     }
 }
        /// <inheritdoc/>
        bool IPresentationTarget.BeginRender(RenderContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }

            var graphicsDevice = GraphicsService.GraphicsDevice;

            if (_renderTarget == null ||
                _renderTarget.GraphicsDevice != graphicsDevice ||
                _requiresResize)
            {
                if (_renderTarget == null)
                {
                    _renderTarget = new SwapChainRenderTarget(
                        graphicsDevice,
                        this,
                        _width,
                        _height,
                        _compositionScaleX,
                        _compositionScaleY,
                        false,
                        SurfaceFormat.Color,
                        DepthFormat.Depth24Stencil8,
                        0,
                        RenderTargetUsage.PreserveContents,
                        PresentInterval.Default);
                }
                else
                {
                    _renderTarget.Resize(_width, _height, _compositionScaleX, _compositionScaleY);
                }

                _requiresResize = false;
            }

            graphicsDevice.SetRenderTarget(_renderTarget);
            context.RenderTarget = _renderTarget;
            context.Viewport     = new Viewport(0, 0, _renderTarget.Width, _renderTarget.Height);
            _rendering           = true;
            return(true);
        }
        protected override void OnClientSizeChanged(EventArgs e)
        {
            base.OnClientSizeChanged(e);

            if (_chain != null)
            {
                if (ClientSize.Width > 0 && ClientSize.Height > 0)
                {
                    _chain.Dispose();
                    _chain = new SwapChainRenderTarget(_graphicsDeviceService.GraphicsDevice, Handle, ClientSize.Width,
                                                       ClientSize.Height);

                    GraphicsDevice.PresentationParameters.BackBufferWidth  = ClientSize.Width;
                    GraphicsDevice.PresentationParameters.BackBufferHeight = ClientSize.Height;

                    SwapChainRenderTargetRefreshed?.Invoke(_chain);
                }
            }
        }
#pragma warning disable 1591
        protected override void OnCreateControl()
        {
            if (!designMode && ClientSize.Width > 0 && ClientSize.Height > 0)
            {
                _graphicsDeviceService = GraphicsDeviceService.AddRef(Handle, ClientSize.Width, ClientSize.Height, GraphicsProfile);
                Services.AddService <IGraphicsDeviceService>(_graphicsDeviceService);
#if DX
                _chain = new SwapChainRenderTarget(_graphicsDeviceService.GraphicsDevice, Handle, ClientSize.Width, ClientSize.Height);

                Microsoft.Xna.Framework.Input.Mouse.WindowHandle = Handle;
#elif GL
                _chain = new SwapChainRenderTarget_GL(_graphicsDeviceService.GraphicsDevice, ClientSize.Width, ClientSize.Height);

                _Intervall.Enabled = true;
                _Intervall.Start();
                _Intervall.Tick += (sender, e) => { PresentDirty(); };
#endif
                AutomaticInvalidation = true;
                Initialize();
            }
            base.OnCreateControl();
        }
Example #24
0
        string HandleDeviceReset()
        {
            bool deviceNeedsReset = false;

            switch (GraphicsDevice.GraphicsDeviceStatus)
            {
            case GraphicsDeviceStatus.Lost:
                return("Graphics device lost");

            case GraphicsDeviceStatus.NotReset:
                deviceNeedsReset = true;
                break;

            default:
                PresentationParameters pp = GraphicsDevice.PresentationParameters;

                deviceNeedsReset = (ClientSize.Width > pp.BackBufferWidth) ||
                                   (ClientSize.Height > pp.BackBufferHeight);
                break;
            }

            if (deviceNeedsReset)
            {
                try
                {
                    graphicsDeviceService.ResetDevice(_renderTarget.Width,
                                                      _renderTarget.Height);

                    _renderTarget.Dispose();
                    _renderTarget = new SwapChainRenderTarget(GraphicsDevice, Handle, ClientSize.Width, ClientSize.Height);
                }
                catch (Exception e)
                {
                    return("Graphics device reset failed\n\n" + e);
                }
            }

            return(null);
        }
Example #25
0
        private void frmAnimation_Load(object sender, EventArgs e)
        {
            //Animation Sound
            cmbSound.Items.Clear();
            cmbSound.Items.Add(Strings.General.none);
            cmbSound.Items.AddRange(GameContentManager.SmartSortedSoundNames);

            //Lower Animation Graphic
            cmbLowerGraphic.Items.Clear();
            cmbLowerGraphic.Items.Add(Strings.General.none);
            cmbLowerGraphic.Items.AddRange(
                GameContentManager.GetSmartSortedTextureNames(GameContentManager.TextureType.Animation)
                );

            //Upper Animation Graphic
            cmbUpperGraphic.Items.Clear();
            cmbUpperGraphic.Items.Add(Strings.General.none);
            cmbUpperGraphic.Items.AddRange(
                GameContentManager.GetSmartSortedTextureNames(GameContentManager.TextureType.Animation)
                );

            mLowerWindow = new SwapChainRenderTarget(
                Core.Graphics.GetGraphicsDevice(), picLowerAnimation.Handle, picLowerAnimation.Width,
                picLowerAnimation.Height
                );

            mUpperWindow = new SwapChainRenderTarget(
                Core.Graphics.GetGraphicsDevice(), picUpperAnimation.Handle, picUpperAnimation.Width,
                picUpperAnimation.Height
                );

            mLowerDarkness = Core.Graphics.CreateRenderTexture(picLowerAnimation.Width, picLowerAnimation.Height);
            mUpperDarkness = Core.Graphics.CreateRenderTexture(picUpperAnimation.Width, picUpperAnimation.Height);

            InitLocalization();
            UpdateEditor();
        }
Example #26
0
 internal DrawService(IGraphicsDeviceService graphics, SwapChainRenderTarget swapChainRenderTarget)
 {
     // Initialize GFX-System
     InitializeGFX_DX(graphics, swapChainRenderTarget);
 }
Example #27
0
    /// <inheritdoc/>
    bool IPresentationTarget.BeginRender(RenderContext context)
    {
      if (context == null)
        throw new ArgumentNullException("context");

      var graphicsDevice = GraphicsService.GraphicsDevice;
      int width = ClientSize.Width;
      int height = ClientSize.Height;


      if (_renderTarget == null
          || _renderTarget.GraphicsDevice != graphicsDevice
          || _renderTarget.Width != width
          || _renderTarget.Height != height)
      {
        if (_renderTarget == null)
          _renderTarget = new SwapChainRenderTarget(graphicsDevice, Handle, width, height);
        else
          _renderTarget.Resize(width, height);
      }

      graphicsDevice.SetRenderTarget(_renderTarget);
      context.RenderTarget = _renderTarget;
#else
      bool deviceNeedsReset;
      switch (graphicsDevice.GraphicsDeviceStatus)
      {
        case GraphicsDeviceStatus.Lost:
          return false;

        case GraphicsDeviceStatus.NotReset:
          deviceNeedsReset = true;
          break;

        default:
          var presentationParameters = graphicsDevice.PresentationParameters;
          deviceNeedsReset = (width > presentationParameters.BackBufferWidth)
                             || (height > presentationParameters.BackBufferHeight);
          break;
      }

      if (deviceNeedsReset)
      {
        try
        {
          var presentationParameters = graphicsDevice.PresentationParameters;
          presentationParameters.BackBufferWidth = Math.Max(width, presentationParameters.BackBufferWidth);
          presentationParameters.BackBufferHeight = Math.Max(height, presentationParameters.BackBufferHeight);
          graphicsDevice.Reset(presentationParameters);
        }
        catch (Exception)
        {
          return false;
        }
      }


      context.Viewport = new Viewport(0, 0, width, height);
      _rendering = true;
      return true;
    }
Example #28
0
        public SwapChainUpdatedEventArgs([NotNull] SwapChainRenderTarget swapChainRenderTarget)
        {
            Guard.ArgumentNotNull(swapChainRenderTarget, nameof(swapChainRenderTarget));

            SwapChain = swapChainRenderTarget;
        }