Пример #1
0
 public void Dispose()
 {
     if (Texture != null)
     {
         Texture.Dispose();
         Texture = null;
     }
 }
Пример #2
0
        public void Draw(Texture2D texture, Vector2 position, Rectangle?sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth)
        {
#if MONOGAME || FNA
            _renderer.Draw(texture, position, sourceRectangle, color, rotation, origin, scale, effects, layerDepth);
#else
            _renderer.Draw(texture, position, sourceRectangle, color, rotation, origin, scale, effects, ImageOrientation.AsIs, layerDepth);
#endif
        }
Пример #3
0
        public static Texture2D CreateTexture2D(int width, int height)
        {
#if !STRIDE
            return(new Texture2D(MyraEnvironment.GraphicsDevice, width, height, false, SurfaceFormat.Color));
#else
            return(Texture2D.New2D(MyraEnvironment.GraphicsDevice, width, height, false, PixelFormat.R8G8B8A8_UNorm_SRgb, TextureFlags.ShaderResource));
#endif
        }
Пример #4
0
        public TextureWithOffset(Texture2D texture)
        {
            if (texture == null)
            {
                throw new ArgumentNullException("texture");
            }

            Texture = texture;
        }
Пример #5
0
        public Texture2DWrapper(Texture2D texture)
        {
            if (texture == null)
            {
                throw new ArgumentNullException(nameof(texture));
            }

            Texture = texture;
        }
Пример #6
0
        private static void DrawPolygonEdge(SpriteBatch spriteBatch, Texture2D texture, Vector2 point1, Vector2 point2,
                                            Color color, float thickness)
        {
            var length = Vector2.Distance(point1, point2);
            var angle  = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
            var scale  = new Vector2(length, thickness);

            spriteBatch.Draw(texture, point1, color, scale, angle);
        }
Пример #7
0
        public TextureRegion(Texture2D texture, Rectangle bounds)
        {
            if (texture == null)
            {
                throw new ArgumentNullException("texture");
            }

            _texture = texture;
            _bounds  = bounds;
        }
Пример #8
0
        public static Texture2D CreateTexture(GraphicsDevice device, int width, int height)
        {
#if MONOGAME || FNA
            var texture2d = new Texture2D(device, width, height);
#elif STRIDE
            var texture2d = Texture2D.New2D(device, width, height, false, PixelFormat.R8G8B8A8_UNorm_SRgb, TextureFlags.ShaderResource);
#endif

            return(texture2d);
        }
Пример #9
0
        public ITexture2D Create(int width, int height)
        {
#if MONOGAME || FNA
            var texture2d = new Texture2D(_device, width, height);
#elif STRIDE
            var texture2d = Texture2D.New2D(_device, width, height, false, PixelFormat.R8G8B8A8_UNorm, TextureFlags.ShaderResource);
#endif

            return(new Texture2DWrapper(texture2d));
        }
Пример #10
0
		public static void SetData<T>(Texture2D texture, T[] data) where T: struct
		{
#if !STRIDE
			texture.SetData(data);
#else
			var commandList = MyraEnvironment.Game.GraphicsContext.CommandList;

			texture.SetData(commandList, data);
#endif
		}
Пример #11
0
        public void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle?sourceRectangle, Color color)
        {
#if MONOGAME || FNA
            _renderer.Draw(texture, destinationRectangle, sourceRectangle, color * Opacity);
#elif STRIDE
            _renderer.Draw(texture, destinationRectangle, sourceRectangle, color * Opacity, 0, Vector2.Zero);
#else
            _renderer.Draw(texture, destinationRectangle, sourceRectangle, CrossEngineStuff.MultiplyColor(color, Opacity));
#endif
        }
Пример #12
0
        public TextureRegion(TextureRegion region, Rectangle bounds)
        {
            if (region == null)
            {
                throw new ArgumentNullException("region");
            }

            _texture = region.Texture;
            bounds.Offset(region.Bounds.Location);
            _bounds = bounds;
        }
Пример #13
0
 public FontAtlas(int w, int h, int count, Texture2D texture)
 {
     Width          = w;
     Height         = h;
     Texture        = texture;
     Nodes          = new FontAtlasNode[count];
     Nodes[0].X     = 0;
     Nodes[0].Y     = 0;
     Nodes[0].Width = w;
     NodesNumber++;
 }
Пример #14
0
        public static bool DisposeTextureBySpecs(Stride.Graphics.Texture tex, Vector3 dim, Stride.Graphics.PixelFormat pixelFormat, MultisampleCount samples)
        {
            if (tex == null || !TextureDimensionsEqual(tex, dim) || tex.Format != pixelFormat || tex.MultisampleCount != samples)
            {
                if (tex != null)
                {
                    tex.Dispose();
                }

                return(true);
            }
            return(false);
        }
Пример #15
0
        protected override Task LoadContent()
#endif
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            var fontSystems = new List <FontSystem>();

            // Simple
            var fontSystem = new FontSystem();

            LoadFontSystem(fontSystem);
            fontSystems.Add(fontSystem);

            // Blurry
            var settings = new FontSystemSettings
            {
                Effect       = FontSystemEffect.Blurry,
                EffectAmount = EffectAmount,
            };

            var blurryFontSystem = new FontSystem(settings);

            LoadFontSystem(blurryFontSystem);
            fontSystems.Add(blurryFontSystem);

            // Stroked
            settings.Effect = FontSystemEffect.Stroked;
            var strokedFontSystem = new FontSystem(settings);

            LoadFontSystem(strokedFontSystem);
            fontSystems.Add(strokedFontSystem);

            _fontSystems       = fontSystems.ToArray();
            _currentFontSystem = _fontSystems[0];

#if MONOGAME || FNA
            _white = new Texture2D(GraphicsDevice, 1, 1);
            _white.SetData(new[] { Color.White });
#elif STRIDE
            _white = Texture2D.New2D(GraphicsDevice, 1, 1, false, PixelFormat.R8G8B8A8_UNorm_SRgb, TextureFlags.ShaderResource);
            _white.SetData(GraphicsContext.CommandList, new[] { Color.White });
#endif

            GC.Collect();

#if STRIDE
            return(base.LoadContent());
#endif
        }
Пример #16
0
        public void Render(VoxelStorageContext storageContext, RenderDrawContext drawContext, RenderView view)
        {
            RenderView voxelizationView = view;
            Int2       ViewSize         = VoxelizationViewSizes[view];

            if (VoxelUtils.DisposeTextureBySpecs(MSAARenderTarget, new Vector3(ViewSize.X, ViewSize.Y, 1), PixelFormat.R8G8B8A8_UNorm, MultisampleCount))
            {
                MSAARenderTarget = Texture.New(storageContext.device, TextureDescription.New2D(ViewSize.X, ViewSize.Y, new MipMapCount(false), PixelFormat.R8G8B8A8_UNorm, TextureFlags.RenderTarget, 1, GraphicsResourceUsage.Default, MultisampleCount), null);
            }

            drawContext.CommandList.ResetTargets();
            if (MSAARenderTarget != null)
            {
                drawContext.CommandList.SetRenderTarget(null, MSAARenderTarget);
            }

            var renderSystem = drawContext.RenderContext.RenderSystem;

            drawContext.CommandList.SetViewport(new Viewport(0, 0, ViewSize.X, ViewSize.Y));

            renderSystem.Draw(drawContext, voxelizationView, renderSystem.RenderStages[voxelizationView.RenderStages[0].Index]);
        }
Пример #17
0
        public static void SetTextureData(Texture2D texture, Rectangle bounds, byte[] data)
        {
#if MONOGAME || FNA
            texture.SetData(0, bounds, data, 0, bounds.Width * bounds.Height * 4);
#elif STRIDE
            var    size = bounds.Width * bounds.Height * 4;
            byte[] temp;
            if (size == data.Length)
            {
                temp = data;
            }
            else
            {
                // Since Stride requres buffer size to match exactly, copy data in the temporary buffer
                temp = new byte[bounds.Width * bounds.Height * 4];
                Array.Copy(data, temp, temp.Length);
            }

            var context = MyraEnvironment.Game.GraphicsContext;
            texture.SetData(context.CommandList, temp, 0, 0, new ResourceRegion(bounds.Left, bounds.Top, 0, bounds.Right, bounds.Bottom, 1));
#endif
        }
Пример #18
0
        public void SetRenderTargetAndViewport(Texture depthStencilView, Texture renderTargetView, Texture secondRenderTarget)
        {
            depthStencilBuffer = depthStencilView;
            renderTargets[0]   = renderTargetView;
            renderTargets[1]   = secondRenderTarget;
            renderTargetCount  = renderTargetView != null ? 1 : 0;

            if (secondRenderTarget != null)
            {
                ++renderTargetCount;
            }

            CommonSetRenderTargetsAndViewport(depthStencilBuffer, renderTargetCount, renderTargets);
        }
Пример #19
0
 private static void Draw(this SpriteBatch spriteBatch, Texture2D texture,
                          Vector2 offset, Color color, Vector2 scale, float rotation = 0.0f)
 {
     spriteBatch.Draw(texture, offset, null, color,
                      rotation, Vector2.Zero, scale, SpriteEffects.None, 0.0f);
 }
Пример #20
0
 public TextureWithOffset(Texture2D texture, Point offset) : this(texture)
 {
     Offset = offset;
 }
Пример #21
0
 /// <summary>
 /// Create an instance of <see cref="Sprite"/> from the provided <see cref="Texture"/>.
 /// A unique Id is set as name and the <see cref="Region"/> is initialized to the size of the whole texture.
 /// </summary>
 /// <param name="texture">The texture to use as texture</param>
 public Sprite(Texture texture)
     : this(Guid.NewGuid().ToString(), texture)
 {
 }
Пример #22
0
 public void Draw(Texture2D texture, Vector2 position, Rectangle?sourceRectangle, Color color)
 {
     _renderer.Draw(texture, position, sourceRectangle, color);
 }
Пример #23
0
 public void Draw(Texture2D texture, Vector2 position, Color color)
 {
     _renderer.Draw(texture, position, color * Opacity);
 }
Пример #24
0
 public void Draw(Texture2D texture, Rectangle destinationRectangle, Color color)
 {
     _renderer.Draw(texture, destinationRectangle, color * Opacity);
 }
Пример #25
0
 private void Draw(Texture2D texture, Vector2 offset, Color color, Vector2 scale, float rotation = 0.0f)
 {
     Draw(texture, offset, null, color, rotation, Vector2.Zero, scale, SpriteEffects.None, 0.0f);
 }
Пример #26
0
        public NinePatchRegion(Texture2D texture, Rectangle bounds, Thickness info) : base(texture, bounds)
        {
            _info = info;

            var centerWidth  = bounds.Width - info.Left - info.Right;
            var centerHeight = bounds.Height - info.Top - info.Bottom;

            var y = bounds.Y;

            if (info.Top > 0)
            {
                if (info.Left > 0)
                {
                    _topLeft = new TextureRegion(texture,
                                                 new Rectangle(bounds.X,
                                                               y,
                                                               info.Left,
                                                               info.Top));
                }

                if (centerWidth > 0)
                {
                    _topCenter = new TextureRegion(texture,
                                                   new Rectangle(bounds.X + info.Left,
                                                                 y,
                                                                 centerWidth,
                                                                 info.Top));
                }

                if (info.Right > 0)
                {
                    _topRight = new TextureRegion(texture,
                                                  new Rectangle(bounds.X + info.Left + centerWidth,
                                                                y,
                                                                info.Right,
                                                                info.Top));
                }
            }

            y += info.Top;
            if (centerHeight > 0)
            {
                if (info.Left > 0)
                {
                    _centerLeft = new TextureRegion(texture,
                                                    new Rectangle(bounds.X,
                                                                  y,
                                                                  info.Left,
                                                                  centerHeight));
                }

                if (centerWidth > 0)
                {
                    _center = new TextureRegion(texture,
                                                new Rectangle(bounds.X + info.Left,
                                                              y,
                                                              centerWidth,
                                                              centerHeight));
                }

                if (info.Right > 0)
                {
                    _centerRight = new TextureRegion(texture,
                                                     new Rectangle(bounds.X + info.Left + centerWidth,
                                                                   y,
                                                                   info.Right,
                                                                   centerHeight));
                }
            }

            y += centerHeight;
            if (info.Bottom > 0)
            {
                if (info.Left > 0)
                {
                    _bottomLeft = new TextureRegion(texture,
                                                    new Rectangle(bounds.X,
                                                                  y,
                                                                  info.Left,
                                                                  info.Bottom));
                }

                if (centerWidth > 0)
                {
                    _bottomCenter = new TextureRegion(texture,
                                                      new Rectangle(bounds.X + info.Left,
                                                                    y,
                                                                    centerWidth,
                                                                    info.Bottom));
                }

                if (info.Right > 0)
                {
                    _bottomRight = new TextureRegion(texture,
                                                     new Rectangle(bounds.X + info.Left + centerWidth,
                                                                   y,
                                                                   info.Right,
                                                                   info.Bottom));
                }
            }
        }
Пример #27
0
 /// <summary>
 /// Covers the whole texture
 /// </summary>
 /// <param name="texture"></param>
 public TextureRegion(Texture2D texture) : this(texture, new Rectangle(0, 0, texture.Width, texture.Height))
 {
 }
Пример #28
0
        public void RenderGlyph(ITexture2DManager textureManager, DynamicFontGlyph glyph, IFontSource fontSource, int blurAmount, int strokeAmount, bool premultiplyAlpha, int kernelWidth, int kernelHeight)
#endif
        {
            var pad = Math.Max(DynamicFontGlyph.PadFromBlur(blurAmount), DynamicFontGlyph.PadFromBlur(strokeAmount));

            // Render glyph to byte buffer
            var bufferSize = glyph.Bounds.Width * glyph.Bounds.Height;
            var buffer     = _byteBuffer;

            if ((buffer == null) || (buffer.Length < bufferSize))
            {
                buffer      = new byte[bufferSize];
                _byteBuffer = buffer;
            }
            Array.Clear(buffer, 0, bufferSize);

            var colorBuffer = _colorBuffer;

            if ((colorBuffer == null) || (colorBuffer.Length < bufferSize * 4))
            {
                colorBuffer  = new byte[bufferSize * 4];
                _colorBuffer = colorBuffer;
            }

            fontSource.RasterizeGlyphBitmap(glyph.Id,
                                            glyph.Size,
                                            buffer,
                                            pad + pad * glyph.Bounds.Width,
                                            glyph.Bounds.Width - pad * 2,
                                            glyph.Bounds.Height - pad * 2,
                                            glyph.Bounds.Width);

            if (strokeAmount > 0)
            {
                var width  = glyph.Bounds.Width;
                var top    = width * strokeAmount;
                var bottom = (glyph.Bounds.Height - strokeAmount) * glyph.Bounds.Width;
                var right  = glyph.Bounds.Width - strokeAmount;
                var left   = strokeAmount;

                byte d;
                for (var i = 0; i < bufferSize; ++i)
                {
                    var ci    = i * 4;
                    var col   = buffer[i];
                    var black = 0;
                    if (col == 255)
                    {
                        colorBuffer[ci] = colorBuffer[ci + 1] = colorBuffer[ci + 2] = colorBuffer[ci + 3] = 255;
                        continue;
                    }

                    if (i >= top)
                    {
                        black = buffer[i - top];
                    }
                    if (i < bottom)
                    {
                        d     = buffer[i + top];
                        black = ((255 - d) * black + 255 * d) / 255;
                    }
                    if (i % width >= left)
                    {
                        d     = buffer[i - strokeAmount];
                        black = ((255 - d) * black + 255 * d) / 255;
                    }
                    if (i % width < right)
                    {
                        d     = buffer[i + strokeAmount];
                        black = ((255 - d) * black + 255 * d) / 255;
                    }

                    if (black == 0)
                    {
                        if (col == 0)
                        {
                            colorBuffer[ci] = colorBuffer[ci + 1] = colorBuffer[ci + 2] = colorBuffer[ci + 3] = 0;                             //black transparency to suit stroke
                            continue;
                        }

                        if (premultiplyAlpha)
                        {
                            colorBuffer[ci] = colorBuffer[ci + 1] = colorBuffer[ci + 2] = colorBuffer[ci + 3] = col;
                        }
                        else
                        {
                            colorBuffer[ci]     = colorBuffer[ci + 1] = colorBuffer[ci + 2] = 255;
                            colorBuffer[ci + 3] = col;
                        }
                    }
                    else
                    {
                        if (col == 0)
                        {
                            colorBuffer[ci]     = colorBuffer[ci + 1] = colorBuffer[ci + 2] = 0;
                            colorBuffer[ci + 3] = (byte)black;
                            continue;
                        }

                        if (premultiplyAlpha)
                        {
                            var alpha = ((255 - col) * black + 255 * col) / 255;
                            colorBuffer[ci]     = colorBuffer[ci + 1] = colorBuffer[ci + 2] = (byte)((alpha * col) / 255);
                            colorBuffer[ci + 3] = (byte)alpha;
                        }
                        else
                        {
                            colorBuffer[ci]     = colorBuffer[ci + 1] = colorBuffer[ci + 2] = col;
                            colorBuffer[ci + 3] = (byte)(((255 - col) * black + 255 * col) / 255);
                        }
                    }
                }
            }
            else
            {
                if (blurAmount > 0)
                {
                    fixed(byte *bdst = &buffer[0])
                    {
                        Blur(bdst, glyph.Bounds.Width, glyph.Bounds.Height, glyph.Bounds.Width, blurAmount);
                    }
                }

                for (var i = 0; i < bufferSize; ++i)
                {
                    var ci = i * 4;
                    var c  = buffer[i];

                    if (premultiplyAlpha)
                    {
                        colorBuffer[ci] = colorBuffer[ci + 1] = colorBuffer[ci + 2] = colorBuffer[ci + 3] = c;
                    }
                    else
                    {
                        colorBuffer[ci]     = colorBuffer[ci + 1] = colorBuffer[ci + 2] = 255;
                        colorBuffer[ci + 3] = c;
                    }
                }
            }

#if MONOGAME || FNA || STRIDE
            // Write to texture
            if (Texture == null)
            {
                Texture = Texture2DManager.CreateTexture(graphicsDevice, Width, Height);
            }

            Texture2DManager.SetTextureData(Texture, glyph.Bounds, colorBuffer);
#else
            // Write to texture
            if (Texture == null)
            {
                Texture = textureManager.CreateTexture(Width, Height);
            }

            textureManager.SetTextureData(Texture, glyph.Bounds, colorBuffer);
#endif
        }