Client LuaCsForBarotrauma
GraphicsQuad.cs
1 using Microsoft.Xna.Framework;
2 using Microsoft.Xna.Framework.Graphics;
3 using System;
4 using System.Collections.Generic;
5 using System.Text;
6 
7 namespace Barotrauma
8 {
9  static class GraphicsQuad
10  {
11  private static VertexBuffer vertexBuffer = null;
12  private static IndexBuffer indexBuffer = null;
13  private static BasicEffect basicEffect = null;
14  private static GraphicsDevice graphicsDevice = null;
15 
16  public static void Init(GraphicsDevice graphics)
17  {
18  if (graphicsDevice != null) { return; }
19 
20  graphicsDevice = graphics;
21 
22  vertexBuffer = new VertexBuffer(graphics, VertexPositionTexture.VertexDeclaration, 4, BufferUsage.WriteOnly);
23  indexBuffer = new IndexBuffer(graphics, IndexElementSize.SixteenBits, 4, BufferUsage.WriteOnly);
24 
25  InitVertexData();
26  indexBuffer.SetData(new ushort[] { 0, 1, 2, 3 });
27 
28  basicEffect = new BasicEffect(graphics) { TextureEnabled = true };
29 
30  GameMain.Instance.ResolutionChanged += () =>
31  {
32  InitVertexData();
33  };
34  }
35 
36  private static void InitVertexData()
37  {
38  Vector2 halfPixelOffset = Vector2.Zero;
39 #if LINUX || OSX
40  halfPixelOffset = new Vector2(0.5f / GameMain.GraphicsWidth, 0.5f / GameMain.GraphicsHeight);
41 #endif
42 
43  VertexPositionTexture[] vertices =
44  {
45  new VertexPositionTexture(new Vector3(-1f, -1f, 1f), new Vector2(0f, 1f) + halfPixelOffset),
46  new VertexPositionTexture(new Vector3(-1f, 1f, 1f), new Vector2(0f, 0f) + halfPixelOffset),
47  new VertexPositionTexture(new Vector3(1f, -1f, 1f), new Vector2(1f, 1f) + halfPixelOffset),
48  new VertexPositionTexture(new Vector3(1f, 1f, 1f), new Vector2(1f, 0f) + halfPixelOffset)
49  };
50 
51  vertexBuffer.SetData(vertices);
52  }
53 
54  public static void UseBasicEffect(Texture2D texture)
55  {
56  basicEffect.Texture = texture;
57  basicEffect.CurrentTechnique.Passes[0].Apply();
58  }
59 
60  public static void Render()
61  {
62  graphicsDevice.SetVertexBuffer(vertexBuffer);
63  graphicsDevice.Indices = indexBuffer;
64  graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleStrip, 0, 0, 2);
65  }
66  }
67 }