Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Windows.UI.Xaml.Media;
- using Windows.UI.Xaml.Media.Imaging;
- using SharpDX;
- using SharpDX.Direct3D;
- using SharpDX.DirectWrite;
- using AlphaMode = SharpDX.Direct2D1.AlphaMode;
- using Color = SharpDX.Color;
- using FontStyle = SharpDX.DirectWrite.FontStyle;
- using ImageBrush = Windows.UI.Xaml.Media.ImageBrush;
- using Rectangle = Windows.UI.Xaml.Shapes.Rectangle;
- namespace NovelReader
- {
- public class DirectXCanvas
- {
- SharpDX.Direct3D11.Device1 d3dDevice;
- SharpDX.Direct2D1.Device d2dDevice;
- SharpDX.Direct2D1.DeviceContext d2dContext;
- SharpDX.Direct2D1.Factory1 d2dFactory;
- SharpDX.DirectWrite.Factory dwirteFactory;
- SurfaceImageSource surfaceImageSource;
- ImageBrush brush;
- SharpDX.Direct2D1.Brush colorBrush;
- Rectangle rect;
- TextFormat textFormat;
- public DirectXCanvas()
- {
- }
- public void Initialize(Rectangle rect)
- {
- this.rect = rect;
- // Create Factories
- this.d2dFactory = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, SharpDX.Direct2D1.DebugLevel.None);
- this.dwirteFactory = new Factory(FactoryType.Shared);
- // Create D3D Device
- var deviceCreationFlags = SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport | SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
- try
- {
- using(var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, deviceCreationFlags))
- {
- this.d3dDevice = defaultDevice.QueryInterface<SharpDX.Direct3D11.Device1>();
- }
- }
- catch (Exception e)
- {
- deviceCreationFlags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
- using (var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, deviceCreationFlags))
- {
- this.d3dDevice = defaultDevice.QueryInterface<SharpDX.Direct3D11.Device1>();
- }
- }
- // Create Direct2D Device
- using (var dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device>())
- {
- d2dDevice = new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice);
- }
- // Create Direct2D context
- this.d2dContext = new SharpDX.Direct2D1.DeviceContext(this.d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);
- // Create SurfaceImageSource
- this.surfaceImageSource = new SurfaceImageSource((int)rect.ActualWidth, (int)rect.ActualHeight);
- this.brush = new ImageBrush();
- this.brush.ImageSource = this.surfaceImageSource;
- rect.Fill = this.brush;
- using (SharpDX.DXGI.ISurfaceImageSourceNative surfaceImageSourceNative = ComObject.As<SharpDX.DXGI.ISurfaceImageSourceNative>(this.surfaceImageSource))
- {
- surfaceImageSourceNative.Device = d3dDevice.QueryInterface<SharpDX.DXGI.Device>();
- }
- // Create brush and text format
- this.colorBrush = new SharpDX.Direct2D1.SolidColorBrush(this.d2dContext, Color.White);
- this.textFormat = new TextFormat(this.dwirteFactory, "Calibri", 20);
- }
- public void Render()
- {
- DrawingPoint drawingPoint;
- SharpDX.Rectangle drawRect = new SharpDX.Rectangle(0, 0, (int)this.rect.ActualWidth, (int)this.rect.ActualHeight);
- using (SharpDX.DXGI.ISurfaceImageSourceNative surfaceImageSourceNative = ComObject.As<SharpDX.DXGI.ISurfaceImageSourceNative>(this.surfaceImageSource))
- using (SharpDX.DXGI.Surface surface = surfaceImageSourceNative.BeginDraw(drawRect, out drawingPoint))
- {
- d2dContext.BeginDraw();
- d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Cleartype;
- d2dContext.Clear(Color.White);
- d2dContext.Transform = Matrix3x2.Identity;
- d2dContext.DrawText("Test Text", this.textFormat, new RectangleF(0, 0, 100, 100), this.colorBrush);
- d2dContext.EndDraw();
- surfaceImageSourceNative.EndDraw();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement