SHARE
TWEET

InDrawFunctions.cs

a guest Nov 15th, 2016 17 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using MonoGame.Extended.BitmapFonts;
  6.  
  7. namespace TheMarket.Share
  8. {
  9.     class InDrawFunctions
  10.     {
  11.         private static Vector2 defaultvec2 = default(Vector2);
  12.         private static SpriteFont defaultsf = default(SpriteFont);
  13.         private static BitmapFont defaultbmf = default(BitmapFont);
  14.         private static Color defaultcolor = default(Color);
  15.         private static BasicEffect be;
  16.         private static VertexBuffer solidTriangles;
  17.         private static VertexBuffer hollowTriangles;
  18.         private static VertexBuffer lines;
  19.         private static List<Poly> solidTrianglesList = new List<Poly>();
  20.         private static List<Poly> linesList = new List<Poly>();
  21.  
  22.         public static void Initialize(GraphicsDevice gd)
  23.         {
  24.             be = new BasicEffect(gd);
  25.             //right bottom
  26.             //be.Projection=Matrix.CreateOrthographic(TheMarket.PreferredCurrentScreenSize.X, TheMarket.PreferredCurrentScreenSize.Y, 0,1000)+Matrix.CreateTranslation(350,350) * Matrix.CreateScale(Manager.TranslationVectorToVirtual.X, Manager.TranslationVectorToVirtual.Y, 1f);
  27.             //be.Projection = Matrix.CreateOrthographicOffCenter(0, TheMarket.PreferredCurrentScreenSize.X, TheMarket.PreferredCurrentScreenSize.Y, 0, 0, 1000); //Correct One!
  28.             be.Projection = Matrix.CreateOrthographicOffCenter(0, Manager.Game.GraphicsDevice.Viewport.Width,
  29.                 Manager.Game.GraphicsDevice.Viewport.Height, 0, 0, 1);// * Matrix.CreateTranslation(-0.5f, -0.5f, 0);
  30.             //be.Projection = Matrix.CreateOrthographicOffCenter(0, gd.Adapter.CurrentDisplayMode.Width, gd.Adapter.CurrentDisplayMode.Height, 0, 0, 1000);
  31.             be.View = Matrix.Identity;//* Matrix.CreateScale(Manager.TranslationVectorToReal.X, Manager.TranslationVectorToReal.Y, 1f);
  32.             be.World = Matrix.Identity ;
  33.             be.VertexColorEnabled = true;
  34.         }
  35.  
  36.         public static void Begin()
  37.         {
  38.             solidTrianglesList=new List<Poly>();
  39.             linesList = new List<Poly>();
  40.         }
  41.  
  42.         public static void End()
  43.         {
  44.            
  45.             if (solidTrianglesList.Count > 0)
  46.             {
  47.                 VertexPositionColor[] vpcs = new VertexPositionColor[solidTrianglesList.Sum(m => m.Vertices.Length)];
  48.  
  49.                 for (int i = 0; i < solidTrianglesList.Count; i++)
  50.                 {
  51.                     for (int j = 0; j < solidTrianglesList[i].Vertices.Length; j++)
  52.                     {
  53.                         vpcs[i* solidTrianglesList[i].Vertices.Length + j] = solidTrianglesList[i].Vertices[j];
  54.                     }
  55.                 }
  56.                 solidTriangles = new VertexBuffer(Manager.Game.GraphicsDevice, typeof(VertexPositionColor), vpcs.Length,
  57.                     BufferUsage.WriteOnly);
  58.                 solidTriangles.SetData(vpcs);
  59.  
  60.                 var rasterizerState = new RasterizerState();
  61.                 rasterizerState.CullMode = CullMode.None;
  62.                 Manager.Game.GraphicsDevice.RasterizerState = rasterizerState;
  63.  
  64.                 Manager.Game.GraphicsDevice.SetVertexBuffer(solidTriangles);
  65.                 foreach (var pass in be.CurrentTechnique.Passes)
  66.                 {
  67.                     pass.Apply();
  68.                     Manager.Game.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleList, 0, vpcs.Length/3);
  69.                 }
  70.             }
  71.  
  72.             if (linesList.Count > 0)
  73.             {
  74.                 VertexPositionColor[] vpcs3 = new VertexPositionColor[linesList.Sum(m => m.Vertices.Length)];
  75.                 int offset = 0;
  76.                 for (int i = 0; i < linesList.Count; i++)
  77.                 {
  78.                     for (int j = 0; j < linesList[i].Vertices.Length; j++)
  79.                     {
  80.                         var index = offset + j;
  81.                         vpcs3[index] = linesList[i].Vertices[j];
  82.                     }
  83.                     offset += linesList[i].Vertices.Length;
  84.                 }
  85.                 lines = new VertexBuffer(Manager.Game.GraphicsDevice, typeof(VertexPositionColor), vpcs3.Length,
  86.                     BufferUsage.WriteOnly);
  87.                 lines.SetData(vpcs3);
  88.                
  89.  
  90.                 var rasterizerState = new RasterizerState();
  91.                 rasterizerState.CullMode = CullMode.None;
  92.                 Manager.Game.GraphicsDevice.RasterizerState = rasterizerState;
  93.  
  94.                 Manager.Game.GraphicsDevice.SetVertexBuffer(lines);
  95.                 foreach (var pass in be.CurrentTechnique.Passes)
  96.                 {
  97.                     pass.Apply();
  98.                     Manager.Game.GraphicsDevice.DrawPrimitives(PrimitiveType.LineList, 0, vpcs3.Length / 2);
  99.                 }
  100.             }
  101.         }
  102.  
  103.         public static void Set<T>(SpriteBatch spb, int x, int y, T obj, int minLength = 0, Color col = default(Color), SpriteFont sprfnt = default(SpriteFont), Vector2 origin = default(Vector2), Vector2 scale = default(Vector2), float rotation = 0f)
  104.         {
  105.             if (col == defaultcolor)
  106.                 col = Color.Black;
  107.             if (sprfnt == defaultsf)
  108.                 sprfnt = Manager.Fonts[0];
  109.             if (origin == defaultvec2)
  110.                 origin = Vector2.Zero;
  111.             if (scale == defaultvec2)
  112.                 scale = Vector2.One;
  113. #if BOXES
  114.             DrawBox(spb, new Rectangle(new Point(x, y), sprfnt.MeasureString(obj.ToString()).ToPoint()), Color.FromNonPremultiplied(0,0,0,127)); //Debug the Boundings
  115. #endif
  116.             spb.DrawString(sprfnt, obj.ToString(), new Vector2(x, y), col, 0f, origin, scale, SpriteEffects.None, rotation);
  117.         }
  118.  
  119.         public static void SetBF<T>(SpriteBatch spb, int x, int y, T obj, int minLength = 0, Color col = default(Color), BitmapFont sprfnt = default(BitmapFont), Vector2 origin = default(Vector2), Vector2 scale = default(Vector2), float rotation = 0f)
  120.         {
  121.             if (col == defaultcolor)
  122.                 col = Color.Black;
  123.             if (sprfnt == defaultbmf)
  124.                 sprfnt = Manager.BitmapFonts[0];
  125.             if (origin == defaultvec2)
  126.                 origin = Vector2.Zero;
  127.             if (scale == defaultvec2)
  128.                 scale = Vector2.One;
  129.  
  130.             if (sprfnt != null)
  131.             {
  132. #if BOXES
  133.                 DrawBox(spb, new Rectangle(new Point(x, y), new Point(sprfnt.MeasureString(obj.ToString()).Width, sprfnt.MeasureString(obj.ToString()).Height)), Color.FromNonPremultiplied(0, 0, 0, 127),true);
  134.                 DrawBox(spb, new Rectangle(new Point(x, y), new Point(sprfnt.MeasureString(obj.ToString()).Width, sprfnt.MeasureString(obj.ToString()).Height)), Color.FromNonPremultiplied(255, 0, 0, 255),false); //Debug the Boundings
  135. #endif
  136.                 spb.DrawString(sprfnt, obj.ToString(), new Vector2(x, y), col,rotation,origin,scale,SpriteEffects.None,0f);
  137.             }
  138.         }
  139.  
  140.         public static void Set<T>(SpriteBatch spb, Vector2 pos, T obj, int minLength = 0, Color col = default(Color), SpriteFont sprfnt = default(SpriteFont), Vector2 origin = default(Vector2), Vector2 scale = default(Vector2), float rotation = 0f)
  141.         {
  142.             Set<T>(spb, (int)pos.X, (int)pos.Y, obj, minLength, col, sprfnt, origin, scale, rotation);
  143.         }
  144.  
  145.         public static void SetBF<T>(SpriteBatch spb, Vector2 pos, T obj, int minLength = 0, Color col = default(Color), BitmapFont bmfnt = default(BitmapFont), Vector2 origin = default(Vector2), Vector2 scale = default(Vector2), float rotation = 0f)
  146.         {
  147.             SetBF<T>(spb, (int)pos.X, (int)pos.Y, obj, minLength, col, bmfnt, origin, scale, rotation);
  148.         }
  149.  
  150.         public static void DrawBox(SpriteBatch spb, Rectangle rect, Color col, bool filled = false)
  151.         {
  152.             if (filled)
  153.             {
  154.                 solidTrianglesList.Add(new Poly
  155.                 {
  156.                     Vertices = new VertexPositionColor[]
  157.                     {
  158.                         new VertexPositionColor(new Vector3(rect.X, rect.Y, 0), col),
  159.                         new VertexPositionColor(new Vector3(rect.X + rect.Width, rect.Y, 0), col),
  160.                         new VertexPositionColor(new Vector3(rect.X + rect.Width, rect.Y + rect.Height, 0), col),
  161.                         new VertexPositionColor(new Vector3(rect.X, rect.Y, 0), col),
  162.                         new VertexPositionColor(new Vector3(rect.X, rect.Y + rect.Height, 0), col),
  163.                         new VertexPositionColor(new Vector3(rect.X + rect.Width, rect.Y + rect.Height, 0), col),
  164.                     }
  165.                 });
  166.             }
  167.             else
  168.             {
  169.                 linesList.Add(new Poly
  170.                 {
  171.                     Vertices = new VertexPositionColor[]
  172.                     {
  173.                         new VertexPositionColor(new Vector3(rect.X, rect.Y, 0), col),
  174.                         new VertexPositionColor(new Vector3(rect.X+rect.Width, rect.Y, 0), col),
  175.                         new VertexPositionColor(new Vector3(rect.X, rect.Y, 0), col),
  176.                         new VertexPositionColor(new Vector3(rect.X, rect.Y+rect.Height, 0), col),
  177.  
  178.                         new VertexPositionColor(new Vector3(rect.X, rect.Y+rect.Height, 0), col),
  179.                         new VertexPositionColor(new Vector3(rect.X+rect.Width, rect.Y+rect.Height, 0), col),
  180.                         new VertexPositionColor(new Vector3(rect.X+rect.Width, rect.Y, 0), col),
  181.                         new VertexPositionColor(new Vector3(rect.X+rect.Width, rect.Y+rect.Height, 0), col)
  182.                     }
  183.                 });
  184.             }
  185.         }
  186.  
  187.         public static void DrawLine(SpriteBatch spb, Vector2 a,Vector2 b,Color col)
  188.         {
  189.             linesList.Add(new Poly
  190.             {
  191.                 Vertices = new VertexPositionColor[]
  192.                 {
  193.                     new VertexPositionColor(new Vector3(a,0), col),
  194.                     new VertexPositionColor(new Vector3(b,0), col),
  195.                 }
  196.             });
  197.         }
  198.  
  199.         private class Poly
  200.         {
  201.             public VertexPositionColor[] Vertices;
  202.         }
  203.     }
  204. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top