Example #1
0
        private void SetupInstance(RendererOptions options)
        {
            _rendererOptions = options;

            if (options.Hwnd == IntPtr.Zero)
            {
                throw new ArgumentNullException(nameof(options.Hwnd));
            }

            if (User32.IsWindow(options.Hwnd) == 0)
            {
                throw new ArgumentException("The window does not exist (hwnd = 0x" + options.Hwnd.ToString("X") + ")");
            }

            RECT bounds = new RECT();

            if (HelperMethods.GetRealWindowRect(options.Hwnd, out bounds) == 0)
            {
                throw new Exception("Failed to get the size of the given window (hwnd = 0x" + options.Hwnd.ToString("X") + ")");
            }

            this.Width  = bounds.Right - bounds.Left;
            this.Height = bounds.Bottom - bounds.Top;

            this.VSync      = options.VSync;
            this.MeasureFPS = options.MeasureFps;

            _deviceProperties = new HwndRenderTargetProperties()
            {
                Hwnd           = options.Hwnd,
                PixelSize      = new Size2(this.Width, this.Height),
                PresentOptions = options.VSync ? PresentOptions.None : PresentOptions.Immediately
            };

            var renderProperties = new RenderTargetProperties(
                RenderTargetType.Default,
                new PixelFormat(Format.R8G8B8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied),
                96.0f, 96.0f, // we use 96.0f because it's the default value. This will scale every drawing by 1.0f (it obviously does not scale anything). Our drawing will be dpi aware!
                RenderTargetUsage.None,
                FeatureLevel.Level_DEFAULT);

            _factory     = new Factory();
            _fontFactory = new FontFactory();

            try
            {
                _device = new WindowRenderTarget(_factory, renderProperties, _deviceProperties);
            }
            catch (SharpDXException) // D2DERR_UNSUPPORTED_PIXEL_FORMAT
            {
                renderProperties.PixelFormat = new PixelFormat(Format.B8G8R8A8_UNorm, SharpDX.Direct2D1.AlphaMode.Premultiplied);
                _device = new WindowRenderTarget(_factory, renderProperties, _deviceProperties);
            }

            _device.AntialiasMode = AntialiasMode.Aliased; // AntialiasMode.PerPrimitive fails rendering some objects
            // other than in the documentation: Cleartype is much faster for me than GrayScale
            _device.TextAntialiasMode = options.AntiAliasing ? SharpDX.Direct2D1.TextAntialiasMode.Cleartype : SharpDX.Direct2D1.TextAntialiasMode.Aliased;

            _sharedBrush = new SolidColorBrush(_device, default(RawColor4));
        }
Example #2
0
        public Direct2DRenderer(IntPtr hwnd, bool vsync, bool measureFps)
        {
            var options = new RendererOptions()
            {
                Hwnd         = hwnd,
                VSync        = vsync,
                MeasureFps   = measureFps,
                AntiAliasing = false
            };

            SetupInstance(options);
        }
Example #3
0
        public Direct2DRenderer(IntPtr hwnd)
        {
            var options = new RendererOptions()
            {
                Hwnd         = hwnd,
                VSync        = false,
                MeasureFps   = false,
                AntiAliasing = false
            };

            SetupInstance(options);
        }
Example #4
0
 public Direct2DRenderer(RendererOptions options)
 {
     SetupInstance(options);
 }