protected unsafe void AddTex(Char chr) { if (this.CharTiles.ContainsKey(chr)) { return; } String text = chr.ToString(); this.sizef = this._tempGr.MeasureString(text, Font, PointF.Empty, StringFormat.GenericTypographic); if (this.sizef.Width <= 0f) { this.sizef.Width = this.sizef.Height / 2f; } if (this.bitmap == null || (Int32)Math.Ceiling(this.sizef.Width) != this.bitmap.Width || (Int32)Math.Ceiling(this.sizef.Height) != this.bitmap.Height) { this.bitmap = new Bitmap((Int32)Math.Ceiling(this.sizef.Width), (Int32)Math.Ceiling(this.sizef.Height), PixelFormat.Format32bppArgb); this.gr = System.Drawing.Graphics.FromImage(this.bitmap); } else { this.gr.Clear(System.Drawing.Color.Empty); } this.gr.TextRenderingHint = this.textRenderingHint; this.gr.DrawString(text, Font, this._brush, 0f, 0f, StringFormat.GenericTypographic); if (this.bitmap.Height > this.currentMaxHeight) { this.currentMaxHeight = this.bitmap.Height; } if (this.currentLeft + this.bitmap.Width + 1 > this.currentTex2d.Width) { this.currentTop += this.currentMaxHeight + 1; this.currentLeft = 0; } if (this.currentTop + this.currentMaxHeight > this.currentTex2d.Height) { NewTex(); } CharTile charTile = new CharTile(this.currentTex2d, new Rectangle(this.currentLeft, this.currentTop, this.bitmap.Width, this.bitmap.Height)); this.CharTiles.Add(chr, charTile); Int32[] array = new Int32[this.bitmap.Width * this.bitmap.Height]; BitmapData bitmapData = this.bitmap.LockBits(new System.Drawing.Rectangle(0, 0, this.bitmap.Width, this.bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); Int32 * ptr = (Int32 *)((void *)bitmapData.Scan0); for (Int32 i = 0; i < array.Length; i++) { if (*ptr != 0) { array[i] = *ptr; } ptr++; } this.bitmap.UnlockBits(bitmapData); this.gds.GraphicsDevice.Textures[0] = null; this.currentTex2d.SetData <Int32>(0, new Rectangle?(charTile.rect), array, 0, array.Length); this.currentLeft += charTile.rect.Width + 1; }
/// <summary> /// 绘制字符数组 (不带Begin End) /// </summary> /// <param name="sb"> SpriteBatch</param> /// <param name="str"> 字符数组</param> /// <param name="position">位置</param> /// <param name="maxBound">绘制的最大范围限定</param> /// <param name="scale"> 缩放</param> /// <param name="color"> 颜色</param> /// <returns>绘制到的范围</returns> public Vector2 Draw(SpriteBatch sb, Char[] str, Vector2 position, Vector2 maxBound, Vector2 scale, Color color) { if (maxBound.X == 0f) { maxBound.X = Single.MaxValue; } else { maxBound.X += position.X; } if (maxBound.Y == 0f) { maxBound.Y = Single.MaxValue; } else { maxBound.Y += position.Y; } Vector2 vector = position; Single num = 0f; Single num2 = 0f; foreach (Char c in str) { AddTex(c); CharTile charTile = this.CharTiles[c]; if (c == '\r' || vector.X + charTile.rect.Width * scale.X > maxBound.X) { if (vector.X > num2) { num2 = vector.X; } vector.X = position.X; vector.Y += num * scale.Y + this.Spacing.Y * scale.X; num = 0f; } else if (c != '\n') { if (charTile.rect.Height > num) { num = charTile.rect.Height; if (vector.Y + num * scale.Y > maxBound.Y) { break; } } if (sb != null) { sb.Draw(charTile.tex, vector, new Rectangle?(charTile.rect), color, 0f, Vector2.Zero, scale, 0, 0f); } vector.X += charTile.rect.Width * scale.X + this.Spacing.X * scale.X; } } if (vector.X > num2) { num2 = vector.X; } vector.X = num2 - this.Spacing.X * scale.X; vector.Y += num * scale.Y; return(vector - position); }