Ejemplo n.º 1
0
 public SpriteDrawInfo(SpriteTexture texture, SamplerState textureSampler)
 {
     Texture        = texture;
     TextureSampler = textureSampler;
     Vertices       = new List <VertexSprite2>();
     Indices        = new List <ushort>();
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Draw Sprite
        /// </summary>
        /// <param name="spriteTexture">The texture that will be used</param>
        /// <param name="destRect">The destination rectangle = drawing translation offset + scaling</param>
        /// <param name="color">Color Modifiers</param>
        /// <param name="textureArrayIndex"> </param>
        public void Draw(SpriteTexture spriteTexture, ref Rectangle destRect, ref ByteColor color, int textureArrayIndex = 0, int drawGroupId = 0)
        {
            Vector2 position = new Vector2(destRect.X, destRect.Y);
            Vector2 size     = new Vector2(destRect.Width, destRect.Height);

            Draw(spriteTexture, ref position, ref size, ref color, textureArrayIndex, drawGroupId);
        }
Ejemplo n.º 3
0
        public void Draw(SpriteTexture spriteTexture, ref Rectangle destRect, ref Rectangle srcRect, ref ByteColor color, float textureRotation, SamplerState sampler, int textureArrayIndex = 0, bool sourceRectInTextCoord = true, int drawGroupId = 0)
        {
            Vector2 position = new Vector2(destRect.Left, destRect.Top);
            Vector2 size     = new Vector2(destRect.Width, destRect.Height);

            var src = new RectangleF(srcRect.Left, srcRect.Top, srcRect.Width, srcRect.Height);

            _spriteBuffer.AddSprite(spriteTexture, sampler, ref position, ref size, ref src, sourceRectInTextCoord, textureArrayIndex, ref color, drawGroupId, textureRotation, float.NaN);
        }
Ejemplo n.º 4
0
        public void DrawWithWrapping(SpriteTexture spriteTexture, ref Rectangle destRect, ref Rectangle srcRect, ref ByteColor color, int textureArrayIndex = 0, bool sourceRectInTextCoord = true, int drawGroupId = 0)
        {
            Vector2 position = new Vector2(destRect.Left, destRect.Top);
            Vector2 size     = new Vector2(destRect.Width, destRect.Height);

            var src = new RectangleF(srcRect.Left, srcRect.Top, srcRect.Width, srcRect.Height);

            _spriteBuffer.AddWrappingSprite(spriteTexture, _spriteSamplerWrap, ref position, ref size, ref src, textureArrayIndex, ref color, drawGroupId);
        }
Ejemplo n.º 5
0
        public void AddSprite(SpriteTexture texture, SamplerState sampler, ref Vector2 position, ref RectangleF sourceRect, bool sourceRectInTextCoord, int textureArrayIndex, ref ByteColor color, int drawGroupId, float spriteDepth = float.NaN)
        {
            if (float.IsNaN(spriteDepth))
            {
                spriteDepth = AutoDepth;
                if (_enableDepthSprite)
                {
                    AutoDepth -= 0.0001f;
                }
            }

            GetSpriteDrawInfo(texture, sampler, drawGroupId).AddSprite(ref position, ref sourceRect, sourceRectInTextCoord, textureArrayIndex, ref color, spriteDepth);
        }
Ejemplo n.º 6
0
        public void AddWrappingSprite(SpriteTexture texture, SamplerState sampler, ref Vector2 position, ref Vector2 size, int textureArrayIndex, ref ByteColor color, int drawGroupId, float spriteDepth = float.NaN)
        {
            if (float.IsNaN(spriteDepth))
            {
                spriteDepth = AutoDepth;
                if (_enableDepthSprite)
                {
                    AutoDepth -= 0.0001f;
                }
            }

            GetSpriteDrawInfo(texture, sampler, drawGroupId).AddWrappingSprite(ref position, ref size, new Vector2(texture.Width, texture.Height), textureArrayIndex, ref color, spriteDepth);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Get the Buffer group where the sprite will be added, these compononents will be draw at the same time
        /// </summary>
        /// <param name="texture"></param>
        /// <param name="sampler"></param>
        /// <returns></returns>
        public SpriteDrawInfo GetSpriteDrawInfo(SpriteTexture texture, SamplerState sampler, int drawGroupId, float textureRotation = 0)
        {
            SpriteDrawInfo spriteDrawInfo;
            int            textureHashCode = SpriteDrawInfo.ComputeHashCode(texture, sampler, drawGroupId, textureRotation);

            if (_spritesByTexture.TryGetValue(textureHashCode, out spriteDrawInfo) == false)
            {
                //The sprite group for this texture is not existing => Create it !
                if (textureRotation != 0f)
                {
                    Matrix textureRotationMatrix = Matrix.Translation(-0.5f, -0.5f, 0) * Matrix.RotationYawPitchRoll(0f, 0f, textureRotation) * Matrix.Translation(0.5f, 0.5f, 0);
                    spriteDrawInfo = new SpriteDrawInfo(texture, sampler, textureRotationMatrix);
                }
                else
                {
                    spriteDrawInfo = new SpriteDrawInfo(texture, sampler);
                }
                _spritesByTexture.Add(textureHashCode, spriteDrawInfo);
            }

            return(spriteDrawInfo);
        }
Ejemplo n.º 8
0
 public void Draw(SpriteTexture spriteTexture, ref Rectangle destRect, ref Rectangle srcRect, ref ByteColor color, int textureArrayIndex = 0, bool sourceRectInTextCoord = true, int drawGroupId = 0)
 {
     Draw(spriteTexture, ref destRect, ref srcRect, ref color, 0.0f, _spriteSamplerClamp, textureArrayIndex, sourceRectInTextCoord, drawGroupId);
 }
Ejemplo n.º 9
0
 public void DrawWithWrapping(SpriteTexture spriteTexture, ref Vector2 position, ref Vector2 size, ref ByteColor color, int textureArrayIndex = 0, int drawGroupId = 0)
 {
     _spriteBuffer.AddWrappingSprite(spriteTexture, _spriteSamplerWrap, ref position, ref size, textureArrayIndex, ref color, drawGroupId);
 }
Ejemplo n.º 10
0
 public void Draw(SpriteTexture spriteTexture, Vector2 position, Vector2 size, ByteColor color, int textureArrayIndex = 0, int drawGroupId = 0)
 {
     _spriteBuffer.AddSprite(spriteTexture, _spriteSamplerClamp, ref position, ref size, textureArrayIndex, ref color, drawGroupId);
 }
Ejemplo n.º 11
0
 public void Draw(SpriteTexture spriteTexture, Rectangle destRect, ByteColor color, int textureArrayIndex = 0, int drawGroupId = 0)
 {
     Draw(spriteTexture, ref destRect, ref color, textureArrayIndex, drawGroupId);
 }
Ejemplo n.º 12
0
 public static int ComputeHashCode(SpriteTexture texture, SamplerState sampler, int groupId, float rotation = 0)
 {
     return(texture.GetHashCode() ^ sampler.GetHashCode() ^ groupId.GetHashCode() ^ rotation.GetHashCode());
 }
Ejemplo n.º 13
0
        private void Initialize(bool antiAliased, SharpDX.Direct3D11.Device device, bool withKerning)
        {
            TextRenderingHint hint = antiAliased ? TextRenderingHint.AntiAliasGridFit : TextRenderingHint.SystemDefault;

            Bitmap measuringBitmap = new Bitmap(1, 1, PixelFormat.Format32bppArgb);

            _fontGraphics = ToDispose(Graphics.FromImage(measuringBitmap));

            _fontGraphics.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            _fontGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            _fontGraphics.TextRenderingHint = hint;
            _withKerning = withKerning;

            if (withKerning)
            {
                _kernings = new Dictionary <int, int>();

                //Get Kerning Information.
                var result = GetFontKernings();
                if (result != null)
                {
                    //Dictionary<short, int> dico2;
                    //int amount;
                    foreach (var value in result)
                    {
                        if (value.KernAmount != 0)
                        {
                            int kernHash = GetKerningHash((char)value.First, (char)value.Second);
                            _kernings.Add(kernHash, value.KernAmount);
                        }
                    }
                }
            }

            HeightInPoints = _font.SizeInPoints;
            _charHeight    = _font.Height;

            var ascent  = _font.FontFamily.GetCellAscent(FontStyle);
            var descent = _font.FontFamily.GetCellDescent(FontStyle);
            var spacing = _font.FontFamily.GetLineSpacing(FontStyle);

            var ascentPixel  = _font.Size * ascent / _font.FontFamily.GetEmHeight(FontStyle);
            var descentPixel = _font.Size * descent / _font.FontFamily.GetEmHeight(FontStyle);
            var spacingPixel = _font.Size * spacing / _font.FontFamily.GetEmHeight(FontStyle);


            HeightInPixel = ascentPixel + descentPixel;

            //Get the Qt of characters (= 128 first ascii + custom selected range)
            int unicodeCharToBuffer = 128 + LanguageUnicodeRanges.Current.Settings.Languages[1].GetUnicodesKeys().Count;

            char[] allChars = new char[unicodeCharToBuffer + 1];
            //Create the first 128
            int nbrChar;

            for (nbrChar = 0; nbrChar < 128; ++nbrChar)
            {
                allChars[nbrChar] = (char)(nbrChar + StartChar);
            }

            foreach (int unicodeIndex in LanguageUnicodeRanges.Current.Settings.Languages[1].GetUnicodesKeys())
            {
                allChars[nbrChar] = (char)(unicodeIndex);
                nbrChar++;
            }

            allChars[unicodeCharToBuffer] = (char)0;

            string allCharsString = new string(allChars);

            SizeF sizeRect = _fontGraphics.MeasureString(allCharsString, _font, unicodeCharToBuffer);

            int texHeight = (int)(sizeRect.Height + (sizeRect.Height * 0.1));

            // Create a temporary Bitmap and Graphics for drawing the characters one by one
            int      tempSize     = (int)(_size * 2);
            Bitmap   drawBitmap   = new Bitmap(tempSize, tempSize, PixelFormat.Format32bppArgb);
            Graphics drawGraphics = Graphics.FromImage(drawBitmap);

            drawGraphics.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            drawGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            drawGraphics.TextRenderingHint = hint;

            // Create a temporary Bitmap + Graphics for creating a full character set
            Bitmap   textBitmap   = new Bitmap(TexWidth, texHeight, PixelFormat.Format32bppArgb);
            Graphics textGraphics = Graphics.FromImage(textBitmap);

            textGraphics.Clear(System.Drawing.Color.FromArgb(0, 255, 255, 255));
            textGraphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;

            // Solid brush for text rendering
            SolidBrush brush = new SolidBrush(System.Drawing.Color.FromArgb(255, 255, 255, 255));

            // Draw all of the characters, and copy them to the full character set
            char[] charString = new char[2];
            charString[1] = (char)0;
            int currentX = 0;
            int currentY = 0;

            for (int i = 0; i < unicodeCharToBuffer; ++i)
            {
                charString[0] = allCharsString[i];

                // Draw the character
                drawGraphics.Clear(System.Drawing.Color.FromArgb(0, 255, 255, 255));
                drawGraphics.DrawString(new string(charString[0], 1), _font, brush, new PointF(0, 0));

                var fastBitmap = new LockBitmap(drawBitmap);
                fastBitmap.LockBits(readOnly: true);

                // Figure out the amount of blank space before the character
                int minX = 0;
                for (int x = 0; x < tempSize; ++x)
                {
                    for (int y = 0; y < tempSize; ++y)
                    {
                        if (fastBitmap.GetPixel(x, y).A > 0)
                        {
                            minX = Math.Max(0, x - 1);
                            x    = tempSize;
                            break;
                        }
                    }
                }

                // Figure out the amount of blank space after the character
                int maxX = tempSize - 1;
                for (int x = tempSize - 1; x >= 0; --x)
                {
                    for (int y = 0; y < tempSize; ++y)
                    {
                        if (fastBitmap.GetPixel(x, y).A > 0)
                        {
                            maxX = Math.Min(tempSize - 1, x + 1);
                            x    = -1;
                            break;
                        }
                    }
                }

                fastBitmap.UnlockBits();

                int charWidth = maxX - minX;

                // Figure out if we need to move to the next row
                if (currentX + charWidth >= TexWidth)
                {
                    currentX  = 0;
                    currentY += (int)(_charHeight) + 1;
                }

                CharDescriptors[allCharsString[i]] = new RectangleF(currentX, currentY, charWidth, _charHeight);

                // Copy the character over
                int height = (int)(_charHeight + 1);
                textGraphics.DrawImage(drawBitmap, currentX, currentY, new Rectangle(minX, 0, minX + charWidth, 0 + height), GraphicsUnit.Pixel);

                currentX += charWidth + 1;
            }

            // Figure out the width of a space character
            charString[0] = ' ';
            charString[1] = (char)0;

            sizeRect    = drawGraphics.MeasureString(new string(charString[0], 1), _font, NumChars);
            _spaceWidth = sizeRect.Width;

            // Lock the bitmap for direct memory access
            BitmapData bmData = textBitmap.LockBits(new Rectangle(0, 0, 0 + TexWidth, 0 + texHeight), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            // Create a D3D texture, initalized with the bitmap data
            Texture2DDescription texDesc = new Texture2DDescription();

            texDesc.Width             = TexWidth;
            texDesc.Height            = texHeight;
            texDesc.MipLevels         = 1;
            texDesc.ArraySize         = 1;
            texDesc.Format            = Format.R8G8B8A8_UNorm;
            texDesc.SampleDescription = new SampleDescription(1, 0);
            texDesc.Usage             = ResourceUsage.Immutable;
            texDesc.BindFlags         = BindFlags.ShaderResource;
            texDesc.CpuAccessFlags    = CpuAccessFlags.None;
            texDesc.OptionFlags       = ResourceOptionFlags.None;

            DataRectangle data = new DataRectangle(new DataStream(bmData.Scan0, 4 * TexWidth * texHeight, true, false).DataPointer, TexWidth * 4);

            Texture2D texture = new Texture2D(device, texDesc, data);

            textBitmap.UnlockBits(bmData);

            ShaderResourceViewDescription srDesc = new ShaderResourceViewDescription();

            srDesc.Format         = Format.R8G8B8A8_UNorm;
            srDesc.Dimension      = ShaderResourceViewDimension.Texture2DArray;
            srDesc.Texture2DArray = new ShaderResourceViewDescription.Texture2DArrayResource()
            {
                MostDetailedMip = 0, MipLevels = 1, FirstArraySlice = 0, ArraySize = 1
            };

            _srView = ToDispose(new ShaderResourceView(device, texture, srDesc));

            SpriteTexture = ToDispose(new SpriteTexture(texture, _srView, new Vector2(0, 0)));
            texture.Dispose();

            measuringBitmap.Dispose();
            textGraphics.Dispose();
            drawGraphics.Dispose();
            drawBitmap.Dispose();
        }