Ejemplo n.º 1
0
    private static void DrawTextUsingTexture(FontManagerDrawText draw, int fontSize)
    {
      bool textureCached = false;
      int cacheSlot = 0;
      FontTexture drawingTexture = new FontTexture();
      foreach (FontTexture cachedTexture in _listFontTextures)
      {
        if (cachedTexture.text == draw.text && cachedTexture.size == fontSize)
        {
          textureCached = true;
          drawingTexture = cachedTexture;
          break;
        }
        cacheSlot++;
      }

      Size size = new Size(0, 0);
      if (textureCached)
      {
        //keep commonly used textures at the top of the pile
        _listFontTextures.RemoveAt(cacheSlot);
        _listFontTextures.Add(drawingTexture);
      }
      else // texture needs to be cached
      {
        Texture texture = null;
        float textwidth = 0, textheight = 0;

        MeasureText(draw.text, ref textwidth, ref textheight, fontSize);
        size.Width = (int)textwidth;
        size.Height = (int)textheight;

        try
        {
          // The MemoryStream must be kept open for the lifetime of the bitmap
          using (MemoryStream imageStream = new MemoryStream())
          {
            using (Bitmap bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb))
            {
              using (Graphics g = Graphics.FromImage(bitmap))
              {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                g.TextRenderingHint = TextRenderingHint.AntiAlias;
                g.TextContrast = 0;
                g.DrawString(draw.text, CachedSystemFont(fontSize), Brushes.White, new Point((int)0, (int)0),
                             StringFormat.GenericTypographic);

                bitmap.Save(imageStream, ImageFormat.Bmp);

                imageStream.Position = 0;
                Format format = Format.Dxt3;
                if (GUIGraphicsContext.GetTexturePoolType() == Pool.Default)
                {
                  format = Format.Unknown;
                }

                ImageInformation info = new ImageInformation();
                try
                {
                  texture = TextureLoader.FromStream(GUIGraphicsContext.DX9Device,
                                                     imageStream, (int)imageStream.Length,
                                                     0, 0,
                                                     1,
                                                     0,
                                                     format,
                                                     GUIGraphicsContext.GetTexturePoolType(),
                                                     Filter.None,
                                                     Filter.None,
                                                     0,
                                                     ref info);
                }
                catch (OutOfVideoMemoryException oovme)
                {
                  Log.Error("GUIFontManager: OutOfVideoMemory in DrawTextUsingTexture - {0}", oovme.Message);
                }
                catch (OutOfMemoryException oome)
                {
                  Log.Error("GUIFontManager: OutOfMemory in DrawTextUsingTexture - {0}", oome.Message);
                }
              }
            }
          }
        }
        catch (Exception ex)
        {
          Log.Error("GUIFontManager: Error in DrawTextUsingTexture - {0}", ex.Message);
        }

        MeasureText(draw.text, ref textwidth, ref textheight, fontSize);
        size.Width = (int)textwidth;
        size.Height = (int)textheight;

        FontTexture newTexture = new FontTexture();
        newTexture.text = draw.text;
        newTexture.texture = texture;
        newTexture.size = fontSize;

        if (_listFontTextures.Count >= _maxCachedTextures)
        {
          //need to clear this and not rely on the finalizer
          FontTexture disposableFont = _listFontTextures[0];
          _listFontTextures.RemoveAt(0);
          disposableFont.Dispose();
        }
        _listFontTextures.Add(newTexture);
        drawingTexture = newTexture;
      }

      _d3dxSprite.Draw(drawingTexture.texture, new Rectangle(0, 0, size.Width, size.Height),
                       Vector3.Empty,
                       new Vector3((int)draw.xpos, (int)draw.ypos, 0), (int)draw.color);
    }
        private static void DrawTextUsingTexture(FontManagerDrawText draw, int fontSize)
        {
            bool textureCached  = false;
            int  cacheSlot      = 0;
            var  drawingTexture = new FontTexture();

            foreach (FontTexture cachedTexture in ListFontTextures)
            {
                if (cachedTexture.text == draw.Text && cachedTexture.size == fontSize)
                {
                    textureCached  = true;
                    drawingTexture = cachedTexture;
                    break;
                }
                cacheSlot++;
            }

            var size = new Size(0, 0);

            if (textureCached)
            {
                //keep commonly used textures at the top of the pile
                ListFontTextures.RemoveAt(cacheSlot);
                ListFontTextures.Add(drawingTexture);
            }
            else // texture needs to be cached
            {
                Texture texture = null;
                float   textwidth = 0, textheight = 0;

                MeasureText(draw.Text, ref textwidth, ref textheight, fontSize);
                size.Width  = (int)textwidth;
                size.Height = (int)textheight;

                try
                {
                    // The MemoryStream must be kept open for the lifetime of the bitmap
                    using (var imageStream = new MemoryStream())
                    {
                        using (var bitmap = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb))
                        {
                            using (Graphics g = Graphics.FromImage(bitmap))
                            {
                                g.SmoothingMode     = SmoothingMode.AntiAlias;
                                g.TextRenderingHint = TextRenderingHint.AntiAlias;
                                g.TextContrast      = 0;
                                g.DrawString(draw.Text, CachedSystemFont(fontSize), Brushes.White, new Point(0, 0),
                                             StringFormat.GenericTypographic);

                                bitmap.Save(imageStream, ImageFormat.Bmp);

                                imageStream.Position = 0;
                                var format = Format.Dxt3;
                                if (GUIGraphicsContext.GetTexturePoolType() == Pool.Default)
                                {
                                    format = Format.Unknown;
                                }

                                var info = new ImageInformation();
                                try
                                {
                                    texture = TextureLoader.FromStream(GUIGraphicsContext.DX9Device,
                                                                       imageStream, (int)imageStream.Length,
                                                                       0, 0,
                                                                       1,
                                                                       0,
                                                                       format,
                                                                       GUIGraphicsContext.GetTexturePoolType(),
                                                                       Filter.None,
                                                                       Filter.None,
                                                                       0,
                                                                       ref info);
                                }
                                catch (OutOfVideoMemoryException oovme)
                                {
                                    Log.Error("GUIFontManager: OutOfVideoMemory in DrawTextUsingTexture - {0}", oovme.Message);
                                }
                                catch (OutOfMemoryException oome)
                                {
                                    Log.Error("GUIFontManager: OutOfMemory in DrawTextUsingTexture - {0}", oome.Message);
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.Error("GUIFontManager: Error in DrawTextUsingTexture - {0}", ex.Message);
                }

                MeasureText(draw.Text, ref textwidth, ref textheight, fontSize);
                size.Width  = (int)textwidth;
                size.Height = (int)textheight;

                var newTexture = new FontTexture {
                    text = draw.Text, texture = texture, size = fontSize
                };

                if (ListFontTextures.Count >= MaxCachedTextures)
                {
                    //need to clear this and not rely on the finalizer
                    FontTexture disposableFont = ListFontTextures[0];
                    ListFontTextures.RemoveAt(0);
                    disposableFont.Dispose();
                }
                ListFontTextures.Add(newTexture);
                drawingTexture = newTexture;
            }

            _sprite.Draw(drawingTexture.texture, new Rectangle(0, 0, size.Width, size.Height),
                         Vector3.Empty,
                         new Vector3((int)draw.Xpos, (int)draw.Ypos, 0), draw.Color);
        }