Guest User

Untitled

a guest
Nov 18th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. public static RawColorBGRA lightBlue = new RawColorBGRA
  2. {
  3. R = 0x00,
  4. G = 0x99,
  5. B = 0xcc,
  6. A = 0x33
  7. };
  8.  
  9. using System;
  10. using SharpDX;
  11. using SharpDX.Direct3D9;
  12. using SharpDX.Mathematics.Interop;
  13.  
  14. namespace DirectXProject
  15. {
  16. public class DX
  17. {
  18. public Device device;
  19. public RawRectangle fontDimension;
  20.  
  21. VertexBuffer vertices;
  22. VertexDeclaration vertexDecl;
  23. VertexElement[] vertexElems;
  24.  
  25. struct Vertex
  26. {
  27. public Vector4 Position;
  28. public ColorBGRA Color;
  29. }
  30.  
  31. public DX(IntPtr handle, int width, int height)
  32. {
  33. device = new Device(
  34. new Direct3D(),
  35. 0,
  36. DeviceType.Hardware,
  37. handle,
  38. CreateFlags.HardwareVertexProcessing,
  39. new PresentParameters(width, height));
  40. }
  41.  
  42. public void RunLoop()
  43. {
  44. lock (this)
  45. {
  46. device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, DXColor.wheat, 1.0f, 0);
  47. device.BeginScene();
  48.  
  49. DrawFillRectangle(vertices, 20, 20, 40, 40, DXColor.lightBlue);
  50.  
  51. device.EndScene();
  52. device.Present();
  53. }
  54. }
  55.  
  56. public void DrawFillRectangle(VertexBuffer vertices, float x1, float y1, float x2, float y2, ColorBGRA color)
  57. {
  58. vertices = new VertexBuffer(device, 4 * 20, Usage.WriteOnly, VertexFormat.None, Pool.Managed);
  59.  
  60. vertices.Lock(0, 0, LockFlags.None).WriteRange(new[] {
  61. new Vertex() { Color = color, Position = new Vector4(x1, y1, 0.5f, 1.0f) },
  62. new Vertex() { Color = color, Position = new Vector4(x2, y1, 0.5f, 1.0f) },
  63. new Vertex() { Color = color, Position = new Vector4(x2, y2, 0.5f, 1.0f) },
  64. new Vertex() { Color = color, Position = new Vector4(x1, y2, 0.5f, 1.0f) }
  65. });
  66.  
  67. vertices.Unlock();
  68.  
  69.  
  70. vertexElems = new[] {
  71. new VertexElement(0, 0, DeclarationType.Float4, DeclarationMethod.Default, DeclarationUsage.PositionTransformed, 0),
  72. new VertexElement(0, 16, DeclarationType.Color, DeclarationMethod.Default, DeclarationUsage.Color, 0),
  73. VertexElement.VertexDeclarationEnd
  74. };
  75.  
  76. vertexDecl = new VertexDeclaration(device, vertexElems);
  77.  
  78. device.SetStreamSource(0, vertices, 0, 20);
  79. device.VertexDeclaration = vertexDecl;
  80. device.DrawPrimitives(PrimitiveType.TriangleFan, 0, 2);
  81. }
  82. }
  83. }
Add Comment
Please, Sign In to add comment