Ejemplo n.º 1
0
        public void Dispose()
        {
            if (resourceOwner && renderTargetView != null)
            {
                renderTargetView.Dispose();
            }

            renderTargetView = null;
        }
Ejemplo n.º 2
0
        internal RenderTarget(SlimDX.Direct3D11.RenderTargetView renderTargetView)
        {
            #if ASSERT
            if (renderTargetView == null)
            {
                throw new ArgumentNullException("renderTargetView");
            }
            #endif

            this.renderTargetView = renderTargetView;
            resourceOwner = false;
            uniqueId = new UniqueId<RenderTarget>();
        }
Ejemplo n.º 3
0
        internal RenderTarget(SlimDX.Direct3D11.Device device, SlimDX.Direct3D11.Resource resource)
        {
            #if ASSERT
            if (device == null)
            {
                throw new ArgumentNullException("device");
            }

            if (resource == null)
            {
                throw new ArgumentNullException("resource");
            }
            #endif

            renderTargetView = new SlimDX.Direct3D11.RenderTargetView(device, resource);
            resourceOwner = true;
            uniqueId = new UniqueId<RenderTarget>();
        }
Ejemplo n.º 4
0
        public Device()
        {
            // Default background color
            backgroundColor = new SlimDX.Color4(0.5f, 0.5f, 1.0f);

            form = new SlimDX.Windows.RenderForm()
            {
                Text = "Game",
                WindowState = System.Windows.Forms.FormWindowState.Maximized
            };

            swapChainDescription = new SlimDX.DXGI.SwapChainDescription()
            {
                BufferCount = 1,
                Usage = SlimDX.DXGI.Usage.RenderTargetOutput,
                OutputHandle = form.Handle,
                IsWindowed = true,
                ModeDescription = new SlimDX.DXGI.ModeDescription(form.ClientSize.Width, form.ClientSize.Height, new SlimDX.Rational(60, 1), SlimDX.DXGI.Format.R8G8B8A8_UNorm),
                SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0),
                Flags = SlimDX.DXGI.SwapChainFlags.AllowModeSwitch,
                SwapEffect = SlimDX.DXGI.SwapEffect.Discard
            };

            var result = SlimDX.Direct3D11.Device.CreateWithSwapChain(SlimDX.Direct3D11.DriverType.Hardware, SlimDX.Direct3D11.DeviceCreationFlags.SingleThreaded, swapChainDescription, out device, out swapChain);

            if (result.IsFailure)
            {
                throw new ApplicationException("Failed to initialize DirectX 11.");
            }

            deviceContext = device.ImmediateContext;

            using (var resource = SlimDX.Direct3D11.Resource.FromSwapChain<SlimDX.Direct3D11.Texture2D>(swapChain, 0))
            {
                screenRenderTargetView = new SlimDX.Direct3D11.RenderTargetView(device, resource);
            }

            using (var factory = swapChain.GetParent<SlimDX.DXGI.Factory>())
            {
                result = factory.SetWindowAssociation(form.Handle, SlimDX.DXGI.WindowAssociationFlags.IgnoreAltEnter);

                if (result.IsFailure)
                {
                    throw new ApplicationException("Failed to associate window with DirectX device.");
                }
            }

            viewport = new SlimDX.Direct3D11.Viewport(0.0f, 0.0f, Convert.ToSingle(form.ClientSize.Width), Convert.ToSingle(form.ClientSize.Height));

            deviceContext.Rasterizer.SetViewports(viewport);

            form.KeyDown += (sender, e) =>
            {
                if (e.Alt && e.KeyCode == System.Windows.Forms.Keys.Enter)
                {
                    swapChain.IsFullScreen = !swapChain.IsFullScreen;
                }
            };

            form.UserResized += (sender, e) =>
            {
                var resizeResult = swapChain.ResizeBuffers(2, 0, 0, SlimDX.DXGI.Format.R8G8B8A8_UNorm, SlimDX.DXGI.SwapChainFlags.AllowModeSwitch);

                if (resizeResult.IsSuccess)
                {
                    using (var resource = SlimDX.Direct3D11.Resource.FromSwapChain<SlimDX.Direct3D11.Texture2D>(swapChain, 0))
                    {
                        screenRenderTargetView.Dispose();
                        screenRenderTargetView = new SlimDX.Direct3D11.RenderTargetView(device, resource);
                    }

                    viewport = new SlimDX.Direct3D11.Viewport(0.0f, 0.0f, Convert.ToSingle(form.ClientSize.Width), Convert.ToSingle(form.ClientSize.Height));

                    deviceContext.Rasterizer.SetViewports(viewport);
                }
            };
        }
Ejemplo n.º 5
0
        public void Dispose()
        {
            if (screenRenderTargetView != null)
            {
                screenRenderTargetView.Dispose();
                screenRenderTargetView = null;
            }

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

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

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

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