Inheritance: GraphicsResource
Exemple #1
0
		public Graphics( BitmapFont font )
		{
			batcher = new Batcher( Core.graphicsDevice );
			bitmapFont = font;

			// the bottom/right pixel is white on the default font so we'll use that for the pixelTexture
			var fontTex = bitmapFont.defaultCharacterRegion.subtexture.texture2D;
			pixelTexture = new Subtexture( fontTex, fontTex.Width - 1, fontTex.Height - 1, 1, 1 );
		}
Exemple #2
0
		public void unload()
		{
			if( pixelTexture != null )
				pixelTexture.texture2D.Dispose();
			pixelTexture = null;

			batcher.Dispose();
			batcher = null;
		}
Exemple #3
0
 public static void DrawLineAngle(this Batcher batcher, Vector2 start, float radians, float length, Color color,
                                  float thickness)
 {
     batcher.Draw(Graphics.Instance.PixelTexture, start, Graphics.Instance.PixelTexture.SourceRect, color,
                  radians, new Vector2(0f, 0.5f), new Vector2(length, thickness), SpriteEffects.None, 0);
 }
Exemple #4
0
		void drawLine( Batcher batcher, Vector2 start, Vector2 end, Color color, float thickness = 2f )
		{
			var delta = end - start;
			var angle = (float)Math.Atan2( delta.Y, delta.X );
			batcher.draw( Graphics.instance.pixelTexture, start + entity.transform.position + localOffset, Graphics.instance.pixelTexture.sourceRect, color, angle, new Vector2( 0, 0.5f ), new Vector2( delta.Length(), thickness ), SpriteEffects.None, layerDepth );
		}
 /// <summary>
 /// Submit a text string of sprites for drawing in the current batch.
 /// </summary>
 /// <param name="batcher">Batcher.</param>
 /// <param name="font">Font.</param>
 /// <param name="text">Text.</param>
 /// <param name="position">Position.</param>
 /// <param name="color">Color.</param>
 public static void drawString(this Batcher batcher, IFont font, string text, Vector2 position, Color color)
 {
     batcher.drawString(font, text, position, color, 0.0f, Vector2.Zero, new Vector2(1.0f), SpriteEffects.None, 0.0f);
 }
Exemple #6
0
 public static void drawLine(this Batcher batcher, float x1, float y1, float x2, float y2, Color color)
 {
     drawLine(batcher, new Vector2(x1, y1), new Vector2(x2, y2), color);
 }
Exemple #7
0
 public static void drawHollowRect(this Batcher batcher, RectangleF rect, Color color, float thickness = 1)
 {
     drawHollowRect(batcher, rect.x, rect.y, rect.width, rect.height, color, thickness);
 }
Exemple #8
0
 public static void drawHollowRect(this Batcher batcher, Vector2 position, float width, float height, Color color, float thickness = 1)
 {
     drawHollowRect(batcher, position.X, position.Y, width, height, color, thickness);
 }
Exemple #9
0
 public static void drawCircle(this Batcher batcher, float x, float y, float radius, Color color, int resolution)
 {
     drawCircle(batcher, new Vector2(x, y), radius, color, resolution);
 }
Exemple #10
0
        public void Render(Batcher batcher, Vector2 offset)
        {
            var origin = new Vector2(0, _image.Height / 2f);

            batcher.Draw(_image, offset + Position, null, _color, 0, origin, _scale, SpriteEffects.None, 0);
        }
Exemple #11
0
 public static void DrawString(this Batcher batcher, NezSpriteFont spriteFont, StringBuilder text, Vector2 position, Color color,
                               float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth)
 {
     batcher.DrawString(spriteFont, text, position, color, rotation, origin, new Vector2(scale), effects, layerDepth);
 }
Exemple #12
0
 public static void DrawLine(this Batcher batcher, float x1, float y1, float x2, float y2, Color color)
 {
     DrawLine(batcher, new System.Numerics.Vector2(x1, y1), new System.Numerics.Vector2(x2, y2), color);
 }
Exemple #13
0
 public static void DrawPixel(this Batcher batcher, float x, float y, Color color, int size = 1)
 {
     DrawPixel(batcher, new System.Numerics.Vector2(x, y), color, size);
 }
Exemple #14
0
 public static void DrawLine(this Batcher batcher, System.Numerics.Vector2 start, System.Numerics.Vector2 end, Color color)
 {
     DrawLineAngle(batcher, start, Mathf.AngleBetweenVectors(start, end), System.Numerics.Vector2.Distance(start, end), color);
 }
Exemple #15
0
 public static void DrawCircle(this Batcher batcher, float x, float y, float radius, Color color,
                               int thickness = 1, int resolution = 12)
 {
     DrawCircle(batcher, new Vector2(x, y), radius, color, thickness, resolution);
 }
Exemple #16
0
 public static void DrawRect(this Batcher batcher, Rectangle rect, Color color)
 {
     batcher.Draw(Graphics.Instance.PixelTexture, rect, Graphics.Instance.PixelTexture.SourceRect, color);
 }
Exemple #17
0
 public static void drawHollowRect(this Batcher batcher, Rectangle rect, Color color)
 {
     drawHollowRect(batcher, rect.X, rect.Y, rect.Width, rect.Height, color);
 }
Exemple #18
0
 public static void drawRect(this Batcher batcher, Rectangle rect, Color color)
 {
     batcher.draw(Graphics.instance.pixelTexture, rect, Graphics.instance.pixelTexture.sourceRect, color);
 }
Exemple #19
0
 public static void drawHollowRect(this Batcher batcher, RectangleF rect, Color color)
 {
     drawHollowRect(batcher, rect.x, rect.y, rect.width, rect.height, color);
 }
Exemple #20
0
 public static void drawHollowRect(this Batcher batcher, Rectangle rect, Color color, float thickness = 1)
 {
     drawHollowRect(batcher, rect.X, rect.Y, rect.Width, rect.Height, color, thickness);
 }
Exemple #21
0
 /// <summary>
 /// Submit a text string of sprites for drawing in the current batch.
 /// </summary>
 /// <param name="batcher">Batcher.</param>
 /// <param name="font">Font.</param>
 /// <param name="text">Text.</param>
 /// <param name="position">Position.</param>
 /// <param name="color">Color.</param>
 public static void DrawString(this Batcher batcher, IFont font, string text, System.Numerics.Vector2 position, Color color)
 {
     batcher.DrawString(font, text, position, color, 0.0f, System.Numerics.Vector2.Zero, new System.Numerics.Vector2(1.0f), SpriteEffects.None,
                        0.0f);
 }
Exemple #22
0
 public static void drawPixel(this Batcher batcher, float x, float y, Color color, int size = 1)
 {
     drawPixel(batcher, new Vector2(x, y), color, size);
 }
Exemple #23
0
		void IFont.drawInto( Batcher batcher, StringBuilder text, Vector2 position, Color color,
			float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float depth )
		{
			var source = new FontCharacterSource( text );
			drawInto( batcher, ref source, position, color, rotation, origin, scale, effect, depth );
		}
Exemple #24
0
        public void DrawInto(Batcher batcher, ref FontCharacterSource text, Vector2 position, Color color,
                             float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float depth)
        {
            var flipAdjustment = Vector2.Zero;

            var flippedVert = (effect & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically;
            var flippedHorz = (effect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally;

            if (flippedVert || flippedHorz)
            {
                Vector2 size;
                MeasureString(ref text, out size);

                if (flippedHorz)
                {
                    origin.X        *= -1;
                    flipAdjustment.X = -size.X;
                }

                if (flippedVert)
                {
                    origin.Y        *= -1;
                    flipAdjustment.Y = _font.LineSpacing - size.Y;
                }
            }

            // TODO: This looks excessive... i suspect we could do most of this with simple vector math and avoid this much matrix work.
            var requiresTransformation = flippedHorz || flippedVert || rotation != 0f || scale != Vector2.One;

            if (requiresTransformation)
            {
                Matrix2D temp;
                Matrix2D.CreateTranslation(-origin.X, -origin.Y, out _transformationMatrix);
                Matrix2D.CreateScale((flippedHorz ? -scale.X : scale.X), (flippedVert ? -scale.Y : scale.Y), out temp);
                Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix2D.CreateTranslation(flipAdjustment.X, flipAdjustment.Y, out temp);
                Matrix2D.Multiply(ref temp, ref _transformationMatrix, out _transformationMatrix);
                Matrix2D.CreateRotation(rotation, out temp);
                Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix2D.CreateTranslation(position.X, position.Y, out temp);
                Matrix2D.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
            }

            // Get the default glyph here once.
            SpriteFont.Glyph?defaultGlyph = null;
            if (_font.DefaultCharacter.HasValue)
            {
                defaultGlyph = _glyphs[_font.DefaultCharacter.Value];
            }

            var currentGlyph     = SpriteFont.Glyph.Empty;
            var offset           = requiresTransformation ? Vector2.Zero : position - origin;
            var firstGlyphOfLine = true;

            for (var i = 0; i < text.Length; ++i)
            {
                var c = text[i];

                if (c == '\r')
                {
                    continue;
                }

                if (c == '\n')
                {
                    offset.X         = requiresTransformation ? 0f : position.X - origin.X;
                    offset.Y        += _font.LineSpacing;
                    firstGlyphOfLine = true;
                    continue;
                }

                if (!_glyphs.TryGetValue(c, out currentGlyph))
                {
                    if (!defaultGlyph.HasValue)
                    {
                        throw new ArgumentException("Errors.TextContainsUnresolvableCharacters", "text");
                    }

                    currentGlyph = defaultGlyph.Value;
                }

                // The first character on a line might have a negative left side bearing.
                // In this scenario, SpriteBatch/SpriteFont normally offset the text to the right,
                // so that text does not hang off the left side of its rectangle.
                if (firstGlyphOfLine)
                {
                    offset.X        += Math.Max(currentGlyph.LeftSideBearing, 0);
                    firstGlyphOfLine = false;
                }
                else
                {
                    offset.X += _font.Spacing + currentGlyph.LeftSideBearing;
                }

                var p = offset;

                if (flippedHorz)
                {
                    p.X += currentGlyph.BoundsInTexture.Width;
                }
                p.X += currentGlyph.Cropping.X;

                if (flippedVert)
                {
                    p.Y += currentGlyph.BoundsInTexture.Height - _font.LineSpacing;
                }
                p.Y += currentGlyph.Cropping.Y;

                // transform our point if we need to
                if (requiresTransformation)
                {
                    Vector2Ext.Transform(ref p, ref _transformationMatrix, out p);
                }

                var destRect = RectangleExt.FromFloats(p.X, p.Y,
                                                       currentGlyph.BoundsInTexture.Width * scale.X,
                                                       currentGlyph.BoundsInTexture.Height * scale.Y);

                batcher.Draw(_font.Texture, destRect, currentGlyph.BoundsInTexture, color, rotation, Vector2.Zero,
                             effect, depth);

                offset.X += currentGlyph.Width + currentGlyph.RightSideBearing;
            }
        }
Exemple #25
0
		public void drawInto( Batcher batcher, ref FontCharacterSource text, Vector2 position, Color color,
		                        float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float depth )
		{
			var flipAdjustment = Vector2.Zero;

			var flippedVert = ( effect & SpriteEffects.FlipVertically ) == SpriteEffects.FlipVertically;
			var flippedHorz = ( effect & SpriteEffects.FlipHorizontally ) == SpriteEffects.FlipHorizontally;

			if( flippedVert || flippedHorz )
			{
				Vector2 size;
				measureString( ref text, out size );

				if( flippedHorz )
				{
					origin.X *= -1;
					flipAdjustment.X = -size.X;
				}

				if( flippedVert )
				{
					origin.Y *= -1;
					flipAdjustment.Y = _font.LineSpacing - size.Y;
				}
			}

			// TODO: This looks excessive... i suspect we could do most of this with simple vector math and avoid this much matrix work.
			var requiresTransformation = flippedHorz || flippedVert || rotation != 0f || scale != Vector2.One;
			if( requiresTransformation )
			{
				Matrix2D temp;
				Matrix2D.createTranslation( -origin.X, -origin.Y, out _transformationMatrix );
				Matrix2D.createScale( ( flippedHorz ? -scale.X : scale.X ), ( flippedVert ? -scale.Y : scale.Y ), out temp );
				Matrix2D.multiply( ref _transformationMatrix, ref temp, out _transformationMatrix );
				Matrix2D.createTranslation( flipAdjustment.X, flipAdjustment.Y, out temp );
				Matrix2D.multiply( ref temp, ref _transformationMatrix, out _transformationMatrix );
				Matrix2D.createRotation( rotation, out temp );
				Matrix2D.multiply( ref _transformationMatrix, ref temp, out _transformationMatrix );
				Matrix2D.createTranslation( position.X, position.Y, out temp );
				Matrix2D.multiply( ref _transformationMatrix, ref temp, out _transformationMatrix );
			}

			// Get the default glyph here once.
			SpriteFont.Glyph? defaultGlyph = null;
			if( _font.DefaultCharacter.HasValue )
				defaultGlyph = _glyphs[_font.DefaultCharacter.Value];

			var currentGlyph = SpriteFont.Glyph.Empty;
			var offset = requiresTransformation ? Vector2.Zero : position - origin;
			var firstGlyphOfLine = true;

			for( var i = 0; i < text.Length; ++i )
			{
				var c = text[i];

				if( c == '\r' )
					continue;

				if( c == '\n' )
				{
					offset.X = requiresTransformation ? 0f : position.X - origin.X;
					offset.Y += _font.LineSpacing;
					firstGlyphOfLine = true;
					continue;
				}

				if( !_glyphs.TryGetValue( c, out currentGlyph ) )
				{
					if( !defaultGlyph.HasValue )
						throw new ArgumentException( "Errors.TextContainsUnresolvableCharacters", "text" );

					currentGlyph = defaultGlyph.Value;
				}

				// The first character on a line might have a negative left side bearing.
				// In this scenario, SpriteBatch/SpriteFont normally offset the text to the right,
				// so that text does not hang off the left side of its rectangle.
				if( firstGlyphOfLine )
				{
					offset.X += Math.Max( currentGlyph.LeftSideBearing, 0 );
					firstGlyphOfLine = false;
				}
				else
				{
					offset.X += _font.Spacing + currentGlyph.LeftSideBearing;
				}

				var p = offset;

				if( flippedHorz )
					p.X += currentGlyph.BoundsInTexture.Width;
				p.X += currentGlyph.Cropping.X;

				if( flippedVert )
					p.Y += currentGlyph.BoundsInTexture.Height - _font.LineSpacing;
				p.Y += currentGlyph.Cropping.Y;

				// transform our point if we need to
				if( requiresTransformation )
					Vector2Ext.transform( ref p, ref _transformationMatrix, out p );

				var destRect = RectangleExt.fromFloats( p.X, p.Y, 
					               currentGlyph.BoundsInTexture.Width * scale.X,
					               currentGlyph.BoundsInTexture.Height * scale.Y );

				batcher.draw( _font.Texture, destRect, currentGlyph.BoundsInTexture, color, rotation, Vector2.Zero, effect, depth );

				offset.X += currentGlyph.Width + currentGlyph.RightSideBearing;
			}
		}
 /// <summary>
 /// Submit a text string of sprites for drawing in the current batch.
 /// </summary>
 /// <param name="batcher">Batcher.</param>
 /// <param name="font">Font.</param>
 /// <param name="text">Text.</param>
 /// <param name="position">Position.</param>
 /// <param name="color">Color.</param>
 /// <param name="rotation">Rotation.</param>
 /// <param name="origin">Origin.</param>
 /// <param name="scale">Scale.</param>
 /// <param name="effects">Effects.</param>
 /// <param name="layerDepth">Layer depth.</param>
 public static void drawString(this Batcher batcher, IFont font, string text, Vector2 position, Color color, float rotation,
                               Vector2 origin, float scale, SpriteEffects effects, float layerDepth)
 {
     batcher.drawString(font, text, position, color, rotation, origin, new Vector2(scale), effects, layerDepth);
 }
Exemple #27
0
        /// <summary>
        /// Submit a text string of sprites for drawing in the current batch.
        /// </summary>
        /// <param name="spriteFont">A font.</param>
        /// <param name="text">The text which will be drawn.</param>
        /// <param name="position">The drawing location on screen.</param>
        /// <param name="color">A color mask.</param>
        public static void DrawString(this Batcher batcher, BitmapFont bitmapFont, StringBuilder text, Vector2 position, Color color)
        {
            var source = new FontCharacterSource(text);

            bitmapFont.DrawInto(batcher, ref source, position, color, 0, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
        }
Exemple #28
0
 /// <summary>
 /// called before the Scene is rendered. This allows a transition to render to a RenderTarget if needed and avoids issues with MonoGame
 /// clearing the framebuffer when a RenderTarget is used.
 /// </summary>
 public virtual void PreRender(Batcher batcher)
 {
 }
Exemple #29
0
 public static void DrawString(this Batcher batcher, NezSpriteFont spriteFont, StringBuilder text,
                               Vector2 position, Color color)
 {
     batcher.DrawString(spriteFont, text, position, color, 0.0f, Vector2.Zero, new Vector2(1.0f),
                        SpriteEffects.None, 0.0f);
 }
Exemple #30
0
 public static void DrawLineAngle(this Batcher batcher, float startX, float startY, float radians, float length,
                                  Color color)
 {
     DrawLineAngle(batcher, new Vector2(startX, startY), radians, length, color);
 }
Exemple #31
0
        public override void Render(Batcher batcher, Camera camera)
        {
            // TODO: make culling smarter and only render the lines that are actually on the screen rather than all or nothing
            var width  = _points.GetLength(0);
            var height = _points.GetLength(1);

            for (var y = 1; y < height; y++)
            {
                for (var x = 1; x < width; x++)
                {
                    var left = new Vector2();
                    var up   = new Vector2();
                    var p    = ProjectToVector2(_points[x, y].Position);

                    if (x > 1)
                    {
                        float thickness;
                        Color gridColor;
                        if (y % GridMajorPeriodY == 1)
                        {
                            thickness = GridMajorThickness;
                            gridColor = GridMajorColor;
                        }
                        else
                        {
                            thickness = GridMinorThickness;
                            gridColor = GridMinorColor;
                        }


                        // use Catmull-Rom interpolation to help smooth bends in the grid
                        left = ProjectToVector2(_points[x - 1, y].Position);
                        var clampedX = Math.Min(x + 1, width - 1);
                        var mid      = Vector2.CatmullRom(ProjectToVector2(_points[x - 2, y].Position), left, p,
                                                          ProjectToVector2(_points[clampedX, y].Position), 0.5f);

                        // If the grid is very straight here, draw a single straight line. Otherwise, draw lines to our new interpolated midpoint
                        if (Vector2.DistanceSquared(mid, (left + p) / 2) > 1)
                        {
                            DrawLine(batcher, left, mid, gridColor, thickness);
                            DrawLine(batcher, mid, p, gridColor, thickness);
                        }
                        else
                        {
                            DrawLine(batcher, left, p, gridColor, thickness);
                        }
                    }

                    if (y > 1)
                    {
                        float thickness;
                        Color gridColor;
                        if (x % GridMajorPeriodX == 1)
                        {
                            thickness = GridMajorThickness;
                            gridColor = GridMajorColor;
                        }
                        else
                        {
                            thickness = GridMinorThickness;
                            gridColor = GridMinorColor;
                        }

                        up = ProjectToVector2(_points[x, y - 1].Position);
                        var clampedY = Math.Min(y + 1, height - 1);
                        var mid      = Vector2.CatmullRom(ProjectToVector2(_points[x, y - 2].Position), up, p,
                                                          ProjectToVector2(_points[x, clampedY].Position), 0.5f);

                        if (Vector2.DistanceSquared(mid, (up + p) / 2) > 1)
                        {
                            DrawLine(batcher, up, mid, gridColor, thickness);
                            DrawLine(batcher, mid, p, gridColor, thickness);
                        }
                        else
                        {
                            DrawLine(batcher, up, p, gridColor, thickness);
                        }
                    }

                    // Add interpolated lines halfway between our point masses. This makes the grid look
                    // denser without the cost of simulating more springs and point masses.
                    if (x > 1 && y > 1)
                    {
                        var upLeft = ProjectToVector2(_points[x - 1, y - 1].Position);
                        DrawLine(batcher, 0.5f * (upLeft + up), 0.5f * (left + p), GridMinorColor,
                                 GridMinorThickness);                        // vertical line
                        DrawLine(batcher, 0.5f * (upLeft + left), 0.5f * (up + p), GridMinorColor,
                                 GridMinorThickness);                        // horizontal line
                    }
                }
            }
        }
Exemple #32
0
 public static void DrawRect(this Batcher batcher, Vector2 position, float width, float height, Color color)
 {
     DrawRect(batcher, position.X, position.Y, width, height, color);
 }
Exemple #33
0
 public static void drawLineAngle(this Batcher batcher, Vector2 start, float radians, float length, Color color)
 {
     batcher.draw(Graphics.instance.pixelTexture, start, Graphics.instance.pixelTexture.sourceRect, color, radians, Vector2.Zero, new Vector2(length, 1), SpriteEffects.None, 0);
 }
Exemple #34
0
 public static void DrawLine(this Batcher batcher, Vector2 start, Vector2 end, Color color, float thickness)
 {
     DrawLineAngle(batcher, start, Mathf.AngleBetweenVectors(start, end), Vector2.Distance(start, end), color,
                   thickness);
 }
Exemple #35
0
 public static void drawLine(this Batcher batcher, Vector2 start, Vector2 end, Color color)
 {
     drawLineAngle(batcher, start, Mathf.angleBetweenVectors(start, end), Vector2.Distance(start, end), color);
 }