Example #1
0
        public static ImageData FromTexture2D(Texture2D texture2D, SystemManagers managers, Color[] colorBuffer)
        {
            ImageData imageData = null;

            switch (texture2D.Format)
            {
                case SurfaceFormat.Color:
                    {
                        if (colorBuffer == null)
                        {
                            colorBuffer = new Color[texture2D.Width * texture2D.Height];
                        }

                        lock (colorBuffer)
                        {

                            texture2D.GetData<Color>(colorBuffer, 0, texture2D.Width * texture2D.Height);

                            imageData = new ImageData(
                                texture2D.Width, texture2D.Height, colorBuffer, managers);
                        }
                    }
                    break;
                case SurfaceFormat.Dxt3:

                    Byte[] byteData = new byte[texture2D.Width * texture2D.Height];
                    texture2D.GetData<byte>(byteData);

                    imageData = new ImageData(texture2D.Width, texture2D.Height, byteData, managers);

                    break;

                default:
                    throw new NotImplementedException("The format " + texture2D.Format + " isn't supported.");

                    //break;
            }
            return imageData;
        }
Example #2
0
 public void CopyTo(ImageData destination, int xOffset, int yOffset)
 {
     for (int x = 0; x < width; x++)
     {
         for (int y = 0; y < height; y++)
         {
             destination.mData[(y + yOffset) * destination.width + (x + xOffset)] = this.mData[y * width + x];
         }
     }
 }
Example #3
0
        private Texture2D RenderToTexture2DUsingImageData(IEnumerable lines, HorizontalAlignment horizontalAlignment, SystemManagers managers)
        {
            ImageData[] imageDatas = new ImageData[this.mTextures.Length];

            for (int i = 0; i < imageDatas.Length; i++)
            {
                // Only use the existing buffer on one-page fonts
                var bufferToUse = mColorBuffer;
                if (i > 0)
                {
                    bufferToUse = null;
                }
                imageDatas[i] = ImageData.FromTexture2D(this.mTextures[i], managers, bufferToUse);
            }

            Point point = new Point();

            int maxWidthSoFar = 0;
            int requiredWidth = 0;
            int requiredHeight = 0;

            List<int> widths = new List<int>();

            foreach (string line in lines)
            {
                requiredHeight += LineHeightInPixels;
                requiredWidth = 0;

                requiredWidth = MeasureString(line);
                widths.Add(requiredWidth);
                maxWidthSoFar = System.Math.Max(requiredWidth, maxWidthSoFar);
            }

            const int MaxWidthAndHeight = 2048; // change this later?
            maxWidthSoFar = System.Math.Min(maxWidthSoFar, MaxWidthAndHeight);
            requiredHeight = System.Math.Min(requiredHeight, MaxWidthAndHeight);



            ImageData imageData = null;

            if (maxWidthSoFar != 0)
            {
                imageData = new ImageData(maxWidthSoFar, requiredHeight, managers);

                int lineNumber = 0;

                foreach (string line in lines)
                {
                    point.X = 0;

                    if (horizontalAlignment == HorizontalAlignment.Right)
                    {
                        point.X = maxWidthSoFar - widths[lineNumber];
                    }
                    else if (horizontalAlignment == HorizontalAlignment.Center)
                    {
                        point.X = (maxWidthSoFar - widths[lineNumber]) / 2;
                    }

                    foreach (char c in line)
                    {

                        BitmapCharacterInfo characterInfo = GetCharacterInfo(c);

                        int sourceLeft = characterInfo.GetPixelLeft(Texture);
                        int sourceTop = characterInfo.GetPixelTop(Texture);
                        int sourceWidth = characterInfo.GetPixelRight(Texture) - sourceLeft;
                        int sourceHeight = characterInfo.GetPixelBottom(Texture) - sourceTop;

                        int distanceFromTop = characterInfo.GetPixelDistanceFromTop(LineHeightInPixels);

                        // There could be some offset for this character
                        int xOffset = characterInfo.GetPixelXOffset(LineHeightInPixels);
                        point.X += xOffset;

                        point.Y = lineNumber * LineHeightInPixels + distanceFromTop;

                        Microsoft.Xna.Framework.Rectangle sourceRectangle = new Microsoft.Xna.Framework.Rectangle(
                            sourceLeft, sourceTop, sourceWidth, sourceHeight);

                        int pageIndex = characterInfo.PageNumber;

                        imageData.Blit(imageDatas[pageIndex], sourceRectangle, point);

                        point.X -= xOffset;
                        point.X += characterInfo.GetXAdvanceInPixels(LineHeightInPixels);

                    }
                    point.X = 0;
                    lineNumber++;
                }
            }


            if (imageData != null)
            {
                // We don't want
                // to generate mipmaps
                // because text is usually
                // rendered pixel-perfect.

                const bool generateMipmaps = false;


                return imageData.ToTexture2D(generateMipmaps);
            }
            else
            {
                return null;
            }
        }
Example #4
0
        public void Blit(ImageData source, Rectangle sourceRectangle, Point destination)
        {
            for (int y = 0; y < sourceRectangle.Height; y++)
            {
                for (int x = 0; x < sourceRectangle.Width; x++)
                {
                    int sourceX = x + sourceRectangle.X;
                    int sourceY = y + sourceRectangle.Y;

                    int destinationX = x + destination.X;
                    int destinationY = y + destination.Y;

                    if (destinationX > -1 && destinationY > -1 && destinationX < this.Width && destinationY < this.Height)
                    {
                        //this.SetPixel(destinationX, destinationY, source.GetPixelColor(sourceX, sourceY));
                        this.AddPixelRegular(destinationX, destinationY, source.GetPixelColor(sourceX, sourceY));
                    }
                }
            }
        }
        private void CreateMaxAlphaTexture()
        {
            if (maxAlphaImageData == null)
            {
                maxAlphaImageData = new ImageData(mCurrentTexture.Width, mCurrentTexture.Height, mManagers);
                maxAlphaImageData.CopyFrom(mCurrentTexture);

                MaximizeAlpha();

                maxAlphaTexture = maxAlphaImageData.ToTexture2D(generateMipmaps: false);
            }
            else
            {
                bool showingBiggerTexture = mCurrentTexture.Width > maxAlphaImageData.Width || mCurrentTexture.Height > maxAlphaImageData.Height;
                if (showingBiggerTexture)
                {
                    maxAlphaImageData = new ImageData(mCurrentTexture.Width, mCurrentTexture.Height, mManagers);
                }

                maxAlphaImageData.CopyFrom(mCurrentTexture);

                MaximizeAlpha();

                if (showingBiggerTexture)
                {
                    if (maxAlphaTexture != null)
                    {
                        maxAlphaTexture.Dispose();
                    }
                    maxAlphaTexture = maxAlphaImageData.ToTexture2D(generateMipmaps: false);
                }
                else
                {
                    maxAlphaImageData.ToTexture2D(maxAlphaTexture);
                }

            }
        }