Advertisement
kkc0923

Untitled

Nov 24th, 2012
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.42 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Windows.UI.Xaml.Media;
  7. using Windows.UI.Xaml.Media.Imaging;
  8. using SharpDX;
  9. using SharpDX.Direct3D;
  10. using SharpDX.DirectWrite;
  11.  
  12. using AlphaMode = SharpDX.Direct2D1.AlphaMode;
  13. using Color = SharpDX.Color;
  14. using FontStyle = SharpDX.DirectWrite.FontStyle;
  15. using ImageBrush = Windows.UI.Xaml.Media.ImageBrush;
  16. using Rectangle = Windows.UI.Xaml.Shapes.Rectangle;
  17.  
  18. namespace NovelReader
  19. {
  20.     public class DirectXCanvas
  21.     {
  22.         SharpDX.Direct3D11.Device1 d3dDevice;
  23.         SharpDX.Direct2D1.Device d2dDevice;
  24.         SharpDX.Direct2D1.DeviceContext d2dContext;
  25.         SharpDX.Direct2D1.Factory1 d2dFactory;
  26.         SharpDX.DirectWrite.Factory dwirteFactory;
  27.         SurfaceImageSource surfaceImageSource;
  28.         ImageBrush brush;
  29.         SharpDX.Direct2D1.Brush colorBrush;
  30.         Rectangle rect;
  31.         TextFormat textFormat;
  32.  
  33.         public DirectXCanvas()
  34.         {
  35.         }
  36.  
  37.         public void Initialize(Rectangle rect)
  38.         {
  39.             this.rect = rect;
  40.  
  41.             // Create Factories
  42.             this.d2dFactory = new SharpDX.Direct2D1.Factory1(SharpDX.Direct2D1.FactoryType.SingleThreaded, SharpDX.Direct2D1.DebugLevel.None);
  43.             this.dwirteFactory = new Factory(FactoryType.Shared);
  44.  
  45.             // Create D3D Device
  46.             var deviceCreationFlags = SharpDX.Direct3D11.DeviceCreationFlags.VideoSupport | SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
  47.  
  48.             try
  49.             {
  50.                 using(var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, deviceCreationFlags))
  51.                 {
  52.                     this.d3dDevice = defaultDevice.QueryInterface<SharpDX.Direct3D11.Device1>();
  53.                 }
  54.             }
  55.             catch (Exception e)
  56.             {
  57.                 deviceCreationFlags = SharpDX.Direct3D11.DeviceCreationFlags.BgraSupport;
  58.  
  59.                 using (var defaultDevice = new SharpDX.Direct3D11.Device(DriverType.Hardware, deviceCreationFlags))
  60.                 {
  61.                     this.d3dDevice = defaultDevice.QueryInterface<SharpDX.Direct3D11.Device1>();
  62.                 }
  63.             }
  64.  
  65.             // Create Direct2D Device
  66.             using (var dxgiDevice = d3dDevice.QueryInterface<SharpDX.DXGI.Device>())
  67.             {
  68.                 d2dDevice = new SharpDX.Direct2D1.Device(d2dFactory, dxgiDevice);
  69.             }
  70.  
  71.             // Create Direct2D context
  72.             this.d2dContext = new SharpDX.Direct2D1.DeviceContext(this.d2dDevice, SharpDX.Direct2D1.DeviceContextOptions.None);
  73.  
  74.             // Create SurfaceImageSource
  75.             this.surfaceImageSource = new SurfaceImageSource((int)rect.ActualWidth, (int)rect.ActualHeight);
  76.  
  77.             this.brush = new ImageBrush();
  78.             this.brush.ImageSource = this.surfaceImageSource;
  79.             rect.Fill = this.brush;
  80.  
  81.             using (SharpDX.DXGI.ISurfaceImageSourceNative surfaceImageSourceNative = ComObject.As<SharpDX.DXGI.ISurfaceImageSourceNative>(this.surfaceImageSource))
  82.             {
  83.                 surfaceImageSourceNative.Device = d3dDevice.QueryInterface<SharpDX.DXGI.Device>();
  84.             }
  85.  
  86.             // Create brush and text format
  87.             this.colorBrush = new SharpDX.Direct2D1.SolidColorBrush(this.d2dContext, Color.White);
  88.             this.textFormat = new TextFormat(this.dwirteFactory, "Calibri", 20);
  89.         }
  90.  
  91.         public void Render()
  92.         {
  93.             DrawingPoint drawingPoint;
  94.             SharpDX.Rectangle drawRect = new SharpDX.Rectangle(0, 0, (int)this.rect.ActualWidth, (int)this.rect.ActualHeight);
  95.  
  96.             using (SharpDX.DXGI.ISurfaceImageSourceNative surfaceImageSourceNative = ComObject.As<SharpDX.DXGI.ISurfaceImageSourceNative>(this.surfaceImageSource))
  97.             using (SharpDX.DXGI.Surface surface = surfaceImageSourceNative.BeginDraw(drawRect, out drawingPoint))
  98.             {
  99.                 d2dContext.BeginDraw();
  100.                 d2dContext.TextAntialiasMode = SharpDX.Direct2D1.TextAntialiasMode.Cleartype;
  101.                 d2dContext.Clear(Color.White);
  102.                 d2dContext.Transform = Matrix3x2.Identity;
  103.                 d2dContext.DrawText("Test Text", this.textFormat, new RectangleF(0, 0, 100, 100), this.colorBrush);
  104.                 d2dContext.EndDraw();
  105.  
  106.                 surfaceImageSourceNative.EndDraw();
  107.             }
  108.            
  109.         }
  110.     }
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement