/// <summary> /// Adds a new string to the text surface manager. This creates /// the associated text surface so that text can be rendered with /// a fast Blt rather than with a DrawText call. Note that caching /// could be done in a much more efficient manner and some text /// surfaces will have identical contents. /// </summary> /// <param name="key">The string to add.</param> internal void Add(string key) { if (string.IsNullOrEmpty(key)) { return; } var text = key; if (text.Length > 16) { text = text.Substring(0, 16); text += "..."; } // Set up the surface IGraphicsSurface ddSurface = GraphicsEngine.Current.CreateSurface(StandardFontRect.Right, StandardFontRect.Bottom); ddSurface.TransparencyKey = new ColorKey(DirectDrawSurface.MagentaColorKey); var rect = new Rectangle(); // Color in the back and add the text ddSurface.BltColorFill(ref rect, DirectDrawSurface.MagentaColorKey.low); ddSurface.SetForeColor(0); using (var font = new Font("Verdana", 6.75f, FontStyle.Regular)) { IntPtr dcHandle = ddSurface.GetDC(); using (var graphics = Graphics.FromHdc(dcHandle)) { graphics.DrawString(text, font, Brushes.Black, 1, 1); graphics.DrawString(text, font, Brushes.WhiteSmoke, 0, 0); } ddSurface.ReleaseDC(dcHandle); } sprites.Add(key, ddSurface); }