// ----------------------------------------------------------------------- // // Copyright 2013 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Direct2D1 { using System; using Perspex.Direct2D1.Media; using Perspex.Media; using Perspex.Platform; using Perspex.Rendering; using SharpDX; using SharpDX.Direct2D1; using Splat; using DwFactory = SharpDX.DirectWrite.Factory; public class Renderer : RendererBase { /// /// The render target. /// private RenderTarget renderTarget; /// /// Initializes a new instance of the class. /// /// The window handle. /// The width of the window. /// The height of the window. public Renderer(IntPtr hwnd, double width, double height) { this.Direct2DFactory = Locator.Current.GetService(); this.DirectWriteFactory = Locator.Current.GetService(); RenderTargetProperties renderTargetProperties = new RenderTargetProperties { }; HwndRenderTargetProperties hwndProperties = new HwndRenderTargetProperties { Hwnd = hwnd, PixelSize = new Size2((int)width, (int)height), PresentOptions = PresentOptions.Immediately, }; this.renderTarget = new WindowRenderTarget( this.Direct2DFactory, renderTargetProperties, hwndProperties); } /// /// Initializes a new instance of the class. /// /// The render target. public Renderer(RenderTarget renderTarget) { this.Direct2DFactory = Locator.Current.GetService(); this.DirectWriteFactory = Locator.Current.GetService(); this.renderTarget = renderTarget; } /// /// Gets the Direct2D factory. /// public Factory Direct2DFactory { get; private set; } /// /// Gets the DirectWrite factory. /// public DwFactory DirectWriteFactory { get; private set; } /// /// Resizes the renderer. /// /// The new width. /// The new height. public override void Resize(int width, int height) { WindowRenderTarget window = this.renderTarget as WindowRenderTarget; if (window == null) { throw new InvalidOperationException(string.Format( "A renderer with a target of type '{0}' cannot be resized.", this.renderTarget.GetType().Name)); } window.Resize(new Size2(width, height)); } /// /// Creates a drawing context for a rendering session. /// /// The platform handle. Unused. /// An . protected override IDrawingContext CreateDrawingContext(IPlatformHandle handle) { return new DrawingContext(this.renderTarget, this.DirectWriteFactory); } } }