public LoadingScreen(SizeF size, SizeF maxSize)
        {
            loadWindow = new ImageWindow("Window/LoadWindow");
            loadWindow.Position = new PointF(0, 0);
            loadWindow.MaximumSize = maxSize;
            loadWindow.Colors = new ColorRect(ColorEx.Black);
            loadWindow.Size = size;

            Texture texture = TextureManager.Instance.Load("loadscreen.dds");
            TextureAtlas atlas = new TextureAtlas("_glue", texture);
            atlas.DefineImage("LoadImage1", new PointF(0, 0), new SizeF(1024, 768));

            loadWindow.VerticalFormat = VerticalImageFormat.Stretched;
            loadWindow.HorizontalFormat = HorizontalImageFormat.Stretched;
            loadWindow.SetImage(atlas.GetTextureInfo("LoadImage1"));
            loadWindow.Visible = true;
        }
Ejemplo n.º 2
0
        //protected System.Drawing.Font GetFontBySpacing(float lineSpacingPixel, FontStyle fontStyle) {
        //    // Compute size based on the regular font.  If we are using bold
        //    // and italic inline with the normal font, I don't want to switch
        //    // font sizes mid-line.
        //    int emHeight = fontFamily.GetEmHeight(FontStyle.Regular);
        //    int lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);

        //    // this is the largest emSize that will fit
        //    float emSize = lineSpacingPixel * ((float)emHeight / lineSpacing);
        //    return GetFont(emSize, fontFlags);
        //}

        protected void CreateFontGlyphSet(string chars, int size, FontStyle fontStyle)
        {
            glyphData = new GlyphData[chars.Length];

            // used for calculating position in the image for rendering the characters
            float x = 0, y = 0;

            StringFormat format = (StringFormat)StringFormat.GenericTypographic.Clone();

            format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces;

            int emHeight    = fontFamily.GetEmHeight(FontStyle.Regular);
            int lineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);
            int descent     = fontFamily.GetCellDescent(FontStyle.Regular);

            this.font = new System.Drawing.Font(fontFamily, size, fontStyle, GraphicsUnit.Pixel);
            log.InfoFormat("Code page for {0} = {1}", font, font.GdiCharSet);

            float lineSpacingPixel = font.Size * (float)lineSpacing / emHeight;
            float descentPixel     = font.Size * (float)descent / emHeight;

            this.cellDescent = descentPixel;
            this.ySpacing    = (float)Math.Ceiling(lineSpacingPixel);

            float height = ySpacing;

            int descentPixelCeil = (int)Math.Ceiling(descentPixel);

            int bitmapHeight = 512;
            int bitmapWidth  = 512;

            int charIndex = 0;
            int glyphSet  = 0;

            while (charIndex < chars.Length)
            {
                Bitmap bitmap = new Bitmap(bitmapWidth, bitmapHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                // get a handles to the graphics context of the bitmap
                Graphics g = Graphics.FromImage(bitmap);
                g.PageUnit = GraphicsUnit.Pixel;
                // Ideally, gdi would handle alpha correctly, but it doesn't seem to
                g.Clear(System.Drawing.Color.FromArgb(0, 1, 0, 0));
                // these fonts better look good!
                // g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                g.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.None;
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
                // g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
                // g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
                // g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
                // g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.None;
                // g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;
                // g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
                // g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;

                // Initialize up our x and y offset in this image
                x = 0;
                y = descentPixel;
                int firstChar = charIndex;

                Dictionary <char, Rect> glyphRects = new Dictionary <char, Rect>();
                // loop through each character in the glyph string and draw it to the bitmap
                while (charIndex < chars.Length)
                {
                    char c = chars[charIndex];

                    SizeF metrics = g.MeasureString(c.ToString(), font, 1024, format);
                    float width   = (float)Math.Ceiling(metrics.Width);

                    // are we gonna wrap?
                    if (x + width > bitmapWidth)
                    {
                        // increment the y coord and reset x to move to the beginning of next line
                        y += (height + InterGlyphPadSpace);
                        x  = 0;
                    }

                    if (y + height > bitmapHeight)
                    {
                        // need to break to a new image
                        break;
                    }

                    // draw the character
                    g.DrawString(c.ToString(), font, Brushes.White, x, y, format);

                    Rect rect = new Rect();

                    // calculate the texture coords for the character
                    // I think that these rectangles are not inclusive,
                    rect.Left   = x;
                    rect.Right  = rect.Left + width;
                    rect.Top    = y;
                    rect.Bottom = rect.Top + height;

                    // Stash the area that we drew into, so that we can
                    // use it later to construct the atlas.
                    glyphRects[c] = rect;

                    // increment X by the width of the current char
                    x += (width + InterGlyphPadSpace);
                    charIndex++;
                }
                // Ok, we are done with this texture
#if DEBUG_FONT_GLYPH
                {
                    // Save this for debugging
                    System.Drawing.Imaging.ImageFormat imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
                    string     debugFileName = string.Format("{0}_{1}_{2}.{3}", fontFamily.Name, size, glyphSet, imgformat.ToString().ToLower());
                    FileStream debugStream   = new FileStream(debugFileName, FileMode.OpenOrCreate);
                    bitmap.Save(debugStream, imgformat);
                    debugStream.Close();
                    log.InfoFormat("Saved font file: {0}: {1}x{2}", debugFileName, bitmap.Width, bitmap.Height);
                }
#endif
                // save the image to a memory stream
                Stream stream = new MemoryStream();
                // flip the image
                bitmap.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipY);
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

                // destroy the bitmap
                bitmap.Dispose();

                // Bitmap headers are 54 bytes.. skip them
                stream.Position = BitmapHeaderSize;

                Axiom.Media.Image image       = Axiom.Media.Image.FromRawStream(stream, bitmapWidth, bitmapHeight, Axiom.Media.PixelFormat.A8R8G8B8);
                string            imgName     = String.Format("_font_glyphs_{0}_{1}", this.name, glyphSet);
                Texture           fontTexture = TextureManager.Instance.LoadImage(imgName, image);

                // Construct and populate the atlas and glyph data structures
                TextureAtlas glyphAtlas = AtlasManager.Instance.CreateAtlas(imgName, fontTexture);
                glyphAtlasList.Add(glyphAtlas);
                for (int i = firstChar; i < charIndex; i++)
                {
                    Rect        rect        = glyphRects[chars[i]];
                    TextureInfo textureInfo = glyphAtlas.DefineImage(chars[i].ToString(), rect);
                    glyphData[i].TextureInfo       = textureInfo;
                    glyphData[i].HorizontalAdvance = rect.Width;
                    glyphData[i].Character         = chars[i];
                    glyphMap[chars[i]]             = i;
                }
                glyphSet++;
            }

#if DEBUG_FONT_GLYPH
            {
                Bitmap bitmap = new Bitmap(bitmapWidth, bitmapHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                // get a handles to the graphics context of the bitmap
                Graphics g = Graphics.FromImage(bitmap);
                g.PageUnit = GraphicsUnit.Pixel;
                // Ideally, gdi would handle alpha correctly, but it doesn't seem to
                g.Clear(System.Drawing.Color.FromArgb(0, 1, 0, 0));
                // these fonts better look good!
                g.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.None;
                g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

                string           test_text = "\u0031\u0644\u0032\u0644\u0627\u0645\u0020\u0639\u0644\u064a\u0028\u0643\u0029\u0645\u002e\u0020\u0643\u064a\u0641\u0020\u0627\u0644\u062d\u0627\u0644\u0039";
                CharacterRange[] ranges    = new CharacterRange[test_text.Length];
                for (int i = 0; i < test_text.Length; ++i)
                {
                    ranges[i] = new CharacterRange(i, 1);
                }

                // format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.DirectionRightToLeft;
                format.SetMeasurableCharacterRanges(ranges);
                System.Drawing.Region[] regions = g.MeasureCharacterRanges(test_text, font, new RectangleF(0, 0, 600, 400), format);
                foreach (System.Drawing.Region region in regions)
                {
                    log.InfoFormat("Region: {0}", region.GetBounds(g));
                }

                g.DrawString(test_text, font, Brushes.White, 100, 0, format);
                // save the image to a memory stream
                Stream stream = new MemoryStream();
                bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);

                // Save this for debugging
                System.Drawing.Imaging.ImageFormat imgformat = System.Drawing.Imaging.ImageFormat.Bmp;
                string     debugFileName = string.Format("{0}_{1}_{2}.{3}", fontFamily.Name, size, glyphSet, imgformat.ToString().ToLower());
                FileStream debugStream   = new FileStream(debugFileName, FileMode.OpenOrCreate);
                bitmap.Save(debugStream, imgformat);
                debugStream.Close();
                log.InfoFormat("Saved font file: {0}: {1}x{2}", debugFileName, bitmap.Width, bitmap.Height);

                // destroy the bitmap
                bitmap.Dispose();
                stream.Close();
            }
#endif
        }
        protected void CreateImage(int width, int height)
        {
            string imgName = String.Format("_font_string_{0}", this.name);
            Axiom.Core.Texture fontTexture = null;
            if (Axiom.Core.TextureManager.Instance.HasResource(imgName))
            {
                fontTexture = Axiom.Core.TextureManager.Instance.GetByName(imgName);
                chunkAtlas = AtlasManager.Instance.GetTextureAtlas(imgName);
            }
            else
            {
                fontTexture = Axiom.Core.TextureManager.Instance.LoadImage(imgName, null);
                chunkAtlas = AtlasManager.Instance.CreateAtlas(imgName, fontTexture);
            }

            System.Diagnostics.Debug.Assert(bitmap == null);
            if (dynamic)
                bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            else
                bitmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            CopyBitmapToTexture(bitmap, fontTexture);
            textDirty = true;
        }
Ejemplo n.º 4
0
 public static bool GetImage(string texture, out TextureAtlas imageset, out TextureInfo image)
 {
     imageset = null;
     image = null;
     bool createdImageset = false;
     string imageName = null;
     string imagesetName = null;
     string[] vals = null;
     if (texture != null) {
         char[] delims = { '/', '\\' };
         vals = texture.Split(delims);
     }
     if (vals != null && vals.Length >= 3) {
         imagesetName = vals[1];
         imageName = vals[2];
         if (!AtlasManager.Instance.ContainsKey(imagesetName)) {
             // Load the additional plugin imageset
             string imagesetFile =
                 AssetManager.Instance.ResolveResourceData("Imageset", imagesetName + ".xml");
             if (imagesetFile == null) {
                 log.WarnFormat("Invalid imageset: {0}", imagesetName);
                 return false;
             }
             imageset = AtlasManager.Instance.CreateAtlas(imagesetFile);
             createdImageset = true;
         }
         imageset = AtlasManager.Instance.GetTextureAtlas(imagesetName);
         if (imageset.ContainsKey(imageName))
             image = imageset.GetTextureInfo(imageName);
     }
     return createdImageset;
 }
 public void DestroyAtlas(TextureAtlas atlas)
 {
     foreach (KeyValuePair<string, TextureAtlas> kvp in atlases) {
         if (kvp.Value == atlas) {
             // invalidates my iteration
             atlases.Remove(kvp.Key);
             return;
         }
     }
 }
 // FIXME - actually load an atlas file
 public TextureAtlas CreateAtlas(string atlasFile)
 {
     TextureAtlas rv = new TextureAtlas(atlasFile);
     atlases[rv.Name] = rv;
     return rv;
 }
 /// <summary>
 ///   Construct a TextureInfo object where we are passed the uv array.
 /// </summary>
 /// <param name="atlas"></param>
 /// <param name="uvArray"></param>
 public TextureInfo(TextureAtlas atlas, string name, PointF[] uvArray)
 {
     this.atlas = atlas;
     this.size = new SizeF(0, 0); // meaningless in this case
     this.name = name;
     this.uvArray = uvArray;
 }
 public TextureInfo(TextureAtlas atlas, string name, PointF point, SizeF size)
 {
     this.atlas = atlas;
     this.size = size;
     this.name = name;
     #if TEXEL_OFFSET
     uvArray[0] = new Point((point.x + .5f) / atlas.texture.Width, (point.y + .5f) / atlas.texture.Height);
     uvArray[1] = new Point((point.x + size.width - .5f) / atlas.texture.Width, (point.y + .5f) / atlas.texture.Height);
     uvArray[2] = new Point((point.x + .5f) / atlas.texture.Width, (point.y + size.height -.5f) / atlas.texture.Height);
     uvArray[3] = new Point((point.x + size.width -.5f) / atlas.texture.Width, (point.y + size.height -.5f) / atlas.texture.Height);
     #else
     uvArray[0] = new PointF(point.X / atlas.texture.Width, point.Y / atlas.texture.Height);
     uvArray[1] = new PointF((point.X + size.Width) / atlas.texture.Width, point.Y / atlas.texture.Height);
     uvArray[2] = new PointF(point.X / atlas.texture.Width, (point.Y + size.Height) / atlas.texture.Height);
     uvArray[3] = new PointF((point.X + size.Width) / atlas.texture.Width, (point.Y + size.Height) / atlas.texture.Height);
     #endif
 }
 public TextureInfo(TextureAtlas atlas, string name, RectangleF rect)
 {
     this.atlas = atlas;
     this.size = new SizeF(rect.Width, rect.Height);
     this.name = name;
     uvArray[0] = new PointF(rect.Left / atlas.texture.Width, rect.Top / atlas.texture.Height);
     uvArray[1] = new PointF(rect.Right / atlas.texture.Width, rect.Top / atlas.texture.Height);
     uvArray[2] = new PointF(rect.Left / atlas.texture.Width, rect.Bottom / atlas.texture.Height);
     uvArray[3] = new PointF(rect.Right / atlas.texture.Width, rect.Bottom / atlas.texture.Height);
 }