<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.xna.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>XNA Framework</title><link>http://forums.xna.com/forums/56.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2007.1 (Build: 0.0)</generator><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/132051.aspx</link><pubDate>Mon, 26 Jan 2009 09:05:00 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:132051</guid><dc:creator>gamefool</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/132051.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=132051</wfw:commentRss><description>&lt;span style="background-color:#f4f7f5;"&gt;Doh! The following will handle that boundary case:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;border-bottom:#7f9db9 1px solid;font-family:courier new;background-color:white;"&gt;
&lt;table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px;" cellspacing="0" cellpadding="0"&gt;
    
    
        &lt;tr&gt;
            &lt;td&gt;&lt;span style="font-size:11px;"&gt;    &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;class&lt;/span&gt;&lt;span style="font-size:11px;"&gt; StringBuilderExtensions  &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;    {  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;        &lt;span style="color:#0000ff;"&gt;public&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="font-size:11px;"&gt; StringBuilder AppendWithoutGarbage(&lt;/span&gt;&lt;span style="color:#0000ff;"&gt;this&lt;/span&gt;&lt;span style="font-size:11px;"&gt; StringBuilder stringBuilder, &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-size:11px;"&gt; number)  &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;        {  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;            &lt;span style="color:#0000ff;"&gt;if&lt;/span&gt;&lt;span style="font-size:11px;"&gt; (number &amp;lt; 0)  &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;            {  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;                stringBuilder.Append(&amp;#39;-&amp;#39;);  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;            }  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt; &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;            &lt;span style="color:#0000ff;"&gt;int&lt;/span&gt;&lt;span style="font-size:11px;"&gt; index = stringBuilder.Length;  &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;            &lt;span style="color:#0000ff;"&gt;do&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;            {  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;                stringBuilder.Insert(index, _digits, (number % 10) + 9, 1);  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;                number /= 10;  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;            }  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;            &lt;span style="color:#0000ff;"&gt;while&lt;/span&gt;&lt;span style="font-size:11px;"&gt; (number != 0);  &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt; &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;            &lt;span style="color:#0000ff;"&gt;return&lt;/span&gt;&lt;span style="font-size:11px;"&gt; stringBuilder;  &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;        }  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt; &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;        &lt;span style="color:#0000ff;"&gt;private&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;static&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;readonly&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;char&lt;/span&gt;&lt;span style="font-size:11px;"&gt;[] _digits = &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;new&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;span style="color:#0000ff;"&gt;char&lt;/span&gt;&lt;span style="font-size:11px;"&gt;[]&lt;br /&gt;
                    {&lt;br /&gt;
                        &amp;#39;9&amp;#39;, &amp;#39;8&amp;#39;, &amp;#39;7&amp;#39;, &amp;#39;6&amp;#39;, &amp;#39;5&amp;#39;, &amp;#39;4&amp;#39;, &amp;#39;3&amp;#39;, &amp;#39;2&amp;#39;, &amp;#39;1&amp;#39;, &amp;#39;0&amp;#39;, &amp;#39;1&amp;#39;, &amp;#39;2&amp;#39;, &amp;#39;3&amp;#39;, &amp;#39;4&amp;#39;, &amp;#39;5&amp;#39;, &amp;#39;6&amp;#39;, &amp;#39;7&amp;#39;, &amp;#39;8&amp;#39;, &amp;#39;9&amp;#39;&lt;br /&gt;
                    };  &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;    } &lt;/td&gt;
        &lt;/tr&gt;
    
&lt;/table&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;/span&gt;</description></item><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/132030.aspx</link><pubDate>Mon, 26 Jan 2009 06:15:59 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:132030</guid><dc:creator>jwatte</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/132030.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=132030</wfw:commentRss><description>Your function fails if I try to append the number -2147483648 :-)&lt;br /&gt;
It&amp;#39;s the only legal &amp;quot;int&amp;quot; that it fails for, though.&lt;br /&gt;</description></item><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/132010.aspx</link><pubDate>Mon, 26 Jan 2009 05:33:51 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:132010</guid><dc:creator>gamefool</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/132010.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=132010</wfw:commentRss><description>&lt;p&gt;I decided to use CLR Profiler on my emulator and came across the same problem as described in this thread. I threw together a quick and dirty StringBuilder extension method that verifies it can avoid generating garbage: &lt;/p&gt;
&lt;div style="border-right:#7f9db9 1px solid;border-top:#7f9db9 1px solid;font-size:11px;overflow:auto;border-left:#7f9db9 1px solid;border-bottom:#7f9db9 1px solid;font-family:courier new;background-color:white;"&gt;
&lt;table style="border-top-width:0px;border-left-width:0px;margin:2px 0px;width:99%;border-bottom:#eee 0px solid;border-collapse:collapse;background-color:#fff;border-right-width:0px;" cellspacing="0" cellpadding="0"&gt;
    
    
        &lt;tr&gt;
            &lt;td&gt;&lt;span style="font-size:11px;"&gt;    &lt;/span&gt;&lt;span style="color:blue;"&gt;public&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;span style="color:blue;"&gt;static&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;span style="color:blue;"&gt;class&lt;/span&gt;&lt;span style="font-size:11px;"&gt; StringBuilderExtensions  &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;    {  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;        &lt;span style="color:blue;"&gt;public&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;span style="color:blue;"&gt;static&lt;/span&gt;&lt;span style="font-size:11px;"&gt; StringBuilder AppendWithoutGarbage(&lt;/span&gt;&lt;span style="color:blue;"&gt;this&lt;/span&gt;&lt;span style="font-size:11px;"&gt; StringBuilder stringBuilder, &lt;/span&gt;&lt;span style="color:blue;"&gt;int&lt;/span&gt;&lt;span style="font-size:11px;"&gt; number)  &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;        {  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;            &lt;span style="color:blue;"&gt;if&lt;/span&gt;&lt;span style="font-size:11px;"&gt; (number &amp;lt; 0)  &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;            {  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;                stringBuilder.Append(&amp;#39;-&amp;#39;);  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;                number = Math.Abs(number);  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;            }  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt; &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;            &lt;span style="color:blue;"&gt;int&lt;/span&gt;&lt;span style="font-size:11px;"&gt; index = stringBuilder.Length;  &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;            &lt;span style="color:blue;"&gt;do&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;            {  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;                stringBuilder.Insert(index, _digits, number % 10, 1);  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;                number /= 10;  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;            }  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;            &lt;span style="color:blue;"&gt;while&lt;/span&gt;&lt;span style="font-size:11px;"&gt; (number &amp;gt; 0);  &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt; &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;            &lt;span style="color:blue;"&gt;return&lt;/span&gt;&lt;span style="font-size:11px;"&gt; stringBuilder;  &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;        }  &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt; &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td style="background-color:#f7f7f7;"&gt;        &lt;span style="color:blue;"&gt;private&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;span style="color:blue;"&gt;static&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;span style="color:blue;"&gt;readonly&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;span style="color:blue;"&gt;char&lt;/span&gt;&lt;span style="font-size:11px;"&gt;[] _digits = &lt;/span&gt;&lt;span style="color:blue;"&gt;new&lt;/span&gt;&lt;span style="font-size:11px;"&gt; &lt;/span&gt;&lt;span style="color:blue;"&gt;char&lt;/span&gt;&lt;span style="font-size:11px;"&gt;[] { &amp;#39;0&amp;#39;, &amp;#39;1&amp;#39;, &amp;#39;2&amp;#39;, &amp;#39;3&amp;#39;, &amp;#39;4&amp;#39;, &amp;#39;5&amp;#39;, &amp;#39;6&amp;#39;, &amp;#39;7&amp;#39;, &amp;#39;8&amp;#39;, &amp;#39;9&amp;#39; };  &lt;/span&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr&gt;
            &lt;td&gt;    } &lt;/td&gt;
        &lt;/tr&gt;
    
&lt;/table&gt;
&lt;/div&gt;
&lt;p&gt;For some reason the StringBuilder.Insert(Int32, Char) overload calls ToString() internally, but this isn&amp;#39;t in the Compact Framework anyway, so I use StringBuilder.Insert(Int32, Char[], Int32, Int32). Obviously there&amp;#39;s a performance vs garbage tradeoff ... so consider this a starting point that eliminates the garbage with the least effort.&lt;/p&gt;</description></item><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/92452.aspx</link><pubDate>Wed, 24 Sep 2008 05:20:03 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:92452</guid><dc:creator>nop</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/92452.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=92452</wfw:commentRss><description>&lt;p&gt;&lt;BLOCKQUOTE&gt;&lt;div&gt;&lt;img src="http://forums.xna.com//Themes/default/images/icon-quote.gif"&gt; &lt;strong&gt;nop:&lt;/strong&gt;&lt;/div&gt;&lt;div&gt;Come to think of it I should probably clean up the code and release it or something...&lt;/div&gt;&lt;/BLOCKQUOTE&gt;&lt;/p&gt;
&lt;p&gt;
Well, it took me almost a month, but I finally found some time and untangled the font code from my hideous mess of a utility library and &lt;a href="http://code.google.com/p/cobalt-3d/"&gt;put it up on Google code&lt;/a&gt;. Sorry for the lack of sample project, etc, work is killing me right now and I haven&amp;#39;t had much time to get everything I want to release in a reasonably good state. You can look at the FrameStats component if you want to get an idea of where to start with it.
&lt;/p&gt;</description></item><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/88042.aspx</link><pubDate>Sat, 06 Sep 2008 16:18:24 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:88042</guid><dc:creator>The ZMan</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/88042.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=88042</wfw:commentRss><description>&lt;BLOCKQUOTE&gt;&lt;div&gt;&lt;img src="http://forums.xna.com//Themes/default/images/icon-quote.gif"&gt; &lt;strong&gt;Ego:&lt;/strong&gt;&lt;/div&gt;&lt;div&gt;
&lt;p&gt;You can get nasty and unexpected side effects by making strings mutable via pointer manipulation or reflection. Beyond the uncertainty in your own code caused by strings being mutable, string interning may cause you to get unexpected results, and mutable strings are unreliable as keys in a Dictionary&amp;lt;&amp;gt; or similar.&lt;/p&gt;
&lt;p&gt;&lt;/div&gt;&lt;/BLOCKQUOTE&gt;&lt;/p&gt;
&lt;p&gt;Absolutly, my intention was to hide the whole thing inside another class so the implementation was totally hidden and the mutable strings themselves were never exposed. Its not perfect but it would cut down on the major issues....&lt;/p&gt;</description></item><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/88027.aspx</link><pubDate>Sat, 06 Sep 2008 15:16:10 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:88027</guid><dc:creator>Ego</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/88027.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=88027</wfw:commentRss><description>&lt;p&gt;You can get nasty and unexpected side effects by making strings mutable via pointer manipulation or reflection. Beyond the uncertainty in your own code caused by strings being mutable, string interning may cause you to get unexpected results, and mutable strings are unreliable as keys in a Dictionary&amp;lt;&amp;gt; or similar.&lt;/p&gt;
&lt;p&gt;The additional of a StringBuilder overload to DrawString() etc. is great, but still not quite far enough if you&amp;#39;re doing a lot of text manipulation. You can&amp;#39;t use string.Format() or StringBuilder.AppendFormat() without generating garbage. You can&amp;#39;t append individual integers, floats etc. via StringBuilder.Append() or .Insert() without generating garbage. If you want allocation-free string formatting you have to write it yourself, but even appending individual characters to a StringBuilder isn&amp;#39;t guaranteed to happen without allocation. Moreover, that&amp;#39;s one function call per character, and if you&amp;#39;re dealing with large volumes of text at high frame rates, the whole thing can be rather unperformant.&lt;/p&gt;
&lt;p&gt;Much as it&amp;#39;s unpleasant and extreme, custom text output, formatting and word wrap based on custom memory buffers does avoid these problems. You don&amp;#39;t break string itself, and you can get back to strings at any point. However, I still wouldn&amp;#39;t advocate it except in extreme cases, as it&amp;#39;s something of a pain to create.&lt;/p&gt;</description></item><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/87983.aspx</link><pubDate>Sat, 06 Sep 2008 05:54:17 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:87983</guid><dc:creator>Brandon Bloom</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/87983.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=87983</wfw:commentRss><description>&lt;div&gt;As suggested earlier, you could use a dictionary of int-&amp;gt;string.&lt;/div&gt;
&lt;div&gt;This technique is called memoization: http://en.wikipedia.org/wiki/Memoization&lt;/div&gt;
&lt;div&gt;Dictionary healthMemos = new Dictionary();&lt;/div&gt;
&lt;div&gt;
&lt;font face="Lucida Console" size="2"&gt;
string GetHealthString(int health)&lt;br /&gt;
{&lt;br /&gt;
  string s;&lt;br /&gt;
  if (!healthMemos.TryGetValue(health, out s))&lt;br /&gt;
  {&lt;br /&gt;
    s = &amp;quot;Health: &amp;quot; + health;&lt;br /&gt;
    healthMemos[health] = s;&lt;br /&gt;
  }&lt;br /&gt;
  return s;&lt;br /&gt;
}&lt;br /&gt;
&lt;/font&gt;
&lt;/div&gt;</description></item><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/87965.aspx</link><pubDate>Sat, 06 Sep 2008 01:47:36 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:87965</guid><dc:creator>jwatte</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/87965.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=87965</wfw:commentRss><description>That&amp;#39;s a step forward, and perhaps enough. However, I wonder why there&amp;#39;s no &amp;quot;DrawString(char[ ], int length, ...)&amp;quot; as that would be the easiest fall-back for many possible cases. If it were me, that&amp;#39;s the method I would have implemented first, and then put the strings on top of that...&lt;br /&gt;</description></item><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/87926.aspx</link><pubDate>Fri, 05 Sep 2008 22:22:57 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:87926</guid><dc:creator>nop</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/87926.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=87926</wfw:commentRss><description>&lt;p&gt;&lt;BLOCKQUOTE&gt;&lt;div&gt;&lt;img src="http://forums.xna.com//Themes/default/images/icon-quote.gif"&gt; &lt;strong&gt;The ZMan:&lt;/strong&gt;&lt;/div&gt;&lt;div&gt;Do you use the standard font XNB files or did you have to write your own exporter too.&lt;/div&gt;&lt;/BLOCKQUOTE&gt;&lt;/p&gt;
&lt;p&gt;I rolled my own. Simple enough task really. Render character into bitmap. Trim away blank space. Pack characters into Alpha8 texture. I used interop to get at the kerning data, and stopped at calling GetCharABCWidthsFloat. Still not sure if I should go all the way and use GetKerningPairs. It hasn&amp;#39;t really seemed necessary yet.&lt;/p&gt;
&lt;p&gt;Most of the work was on the runtime side.  When I create a font I pick a set of point sizes to render it at, and at runtime I specify font size as a fraction of the screen height and it picks the closest matching set of glyphs based on screen resolution and the available font sizes. This has been pretty useful on the XBOX because I can do things like scalling my font sizes in code by some global amount and the layout/render just works without having to have a _pc and _xbox (the same as _pc, but bigger) set of fonts. The final glyph layout stuff is dead simple but the actual line layout (word wrap, center text in rectangle, clip/don&amp;#39;t clip, etc) is a loop of pure insanity, written with raw char pointers (since I wanted to be able to use it with either char[ ] or string data) and probably has a few bugs in it.&lt;/p&gt;</description></item><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/87923.aspx</link><pubDate>Fri, 05 Sep 2008 22:03:38 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:87923</guid><dc:creator>Shawn Hargreaves</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/87923.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=87923</wfw:commentRss><description>In 3.0, you can pass a StringBuilder directly to SpriteBatch.DrawString.
&lt;p&gt;You have to be careful to format into a StringBuilder without causing allocations, but this is possible if you tickle it just right, and easier than implementing your own system entirely from scratch.&lt;/p&gt;
&lt;br /&gt;</description></item><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/87913.aspx</link><pubDate>Fri, 05 Sep 2008 21:40:03 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:87913</guid><dc:creator>The ZMan</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/87913.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=87913</wfw:commentRss><description>&lt;BLOCKQUOTE&gt;&lt;div&gt;&lt;img src="http://forums.xna.com//Themes/default/images/icon-quote.gif"&gt; &lt;strong&gt;nop:&lt;/strong&gt;&lt;/div&gt;&lt;div&gt;
&lt;p&gt;&lt;BLOCKQUOTE&gt;&lt;div&gt;&lt;img src="http://forums.xna.com//Themes/default/images/icon-quote.gif"&gt; &lt;strong&gt;The ZMan:&lt;/strong&gt;&lt;/div&gt;&lt;div&gt;Strings are only immutable if you stay in safe code ;-)&lt;/div&gt;&lt;/BLOCKQUOTE&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Been there. Done that. Couldn&amp;#39;t figure out how to set the length... The approach works fairly well, though I ended up formatting into a char[ ], and then unsafely copying into a preallocated string once the final length was known. I was basically at the point of setting up a caching system for all the different-length strings I was using when I decided to just bite the bullet and write my own font renderer (and sneak in a few extra features as well).&lt;/p&gt;
&lt;p&gt;&lt;/div&gt;&lt;/BLOCKQUOTE&gt;&lt;/p&gt;
&lt;p&gt;Off the top of my head the 1st 2 (4?) bytes are the length... but note that only enough memory is there for the initial string. So you need to make a nice long string to &amp;#39;reserve&amp;#39; the memory. Then you can just fill it with any shorter string and reset the length. &lt;/p&gt;
&lt;p&gt;I should tidy up the struct approach - worked pretty well in my PoC... &lt;/p&gt;
&lt;p&gt;You are right about handling all the other stuff though. Do you use the standard font XNB files or did you have to write your own exporter too.&lt;/p&gt;</description></item><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/87903.aspx</link><pubDate>Fri, 05 Sep 2008 21:08:06 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:87903</guid><dc:creator>nop</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/87903.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=87903</wfw:commentRss><description>&lt;p&gt;&lt;BLOCKQUOTE&gt;&lt;div&gt;&lt;img src="http://forums.xna.com//Themes/default/images/icon-quote.gif"&gt; &lt;strong&gt;The ZMan:&lt;/strong&gt;&lt;/div&gt;&lt;div&gt;Strings are only immutable if you stay in safe code ;-)&lt;/div&gt;&lt;/BLOCKQUOTE&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Been there. Done that. Couldn&amp;#39;t figure out how to set the length... The approach works fairly well, though I ended up formatting into a char[ ], and then unsafely copying into a preallocated string once the final length was known. I was basically at the point of setting up a caching system for all the different-length strings I was using when I decided to just bite the bullet and write my own font renderer (and sneak in a few extra features as well).&lt;/p&gt;
&lt;p&gt;&lt;BLOCKQUOTE&gt;&lt;div&gt;&lt;img src="http://forums.xna.com//Themes/default/images/icon-quote.gif"&gt; &lt;strong&gt;The ZMan:&lt;/strong&gt;&lt;/div&gt;&lt;div&gt; Of course then you end up implementing your own
number formating code but thats much easier than writing your own font
rendering IMO&lt;/div&gt;&lt;/BLOCKQUOTE&gt;&lt;/p&gt;
&lt;p&gt;You end up doing both if you go the custom font route. Of course if you do the custom font rendering engine you can do things like rendering a font at multiple sizes and picking the closest one at runtime based on screen resolution and desired vertical height, or inserting formatting codes (just color changes in my case) into strings rather than breaking them up ahead of time and drawing them with multiple Measure/Draw calls (which can get ugly if you switch colors mid-word in oddly kerned fonts).&lt;/p&gt;</description></item><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/87888.aspx</link><pubDate>Fri, 05 Sep 2008 20:25:32 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:87888</guid><dc:creator>The ZMan</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/87888.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=87888</wfw:commentRss><description>&lt;p&gt;Strings are only immutable if you stay in safe code ;-) Its not too hard to work out how the string is laid out and modify it without creating garbage. Of course then you end up implementing your own number formating code but thats much easier than writing your own font rendering IMO&lt;/p&gt;
&lt;p&gt;Another idea I got from Frank&amp;#39;s talk at GameFest is to use a struct that overlays a byte[ with a string. Than you can modify the byte[ and pass the now modified string into the .Draw method. You don&amp;#39;t even need unsafe code to do that. I did a proof of concept but I didn&amp;#39;t do any serious coding.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/86568.aspx</link><pubDate>Sun, 31 Aug 2008 04:32:45 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:86568</guid><dc:creator>reedake2</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/86568.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=86568</wfw:commentRss><description>sigh, sometime I miss the days of and free() and delete() lol.</description></item><item><title>Re: Avoiding int.ToString() GC garbage</title><link>http://forums.xna.com/forums/thread/86441.aspx</link><pubDate>Sat, 30 Aug 2008 10:06:47 GMT</pubDate><guid isPermaLink="false">4aa5dbf6-357b-46b2-b5b2-1b660a6dc370:86441</guid><dc:creator>Ego</dc:creator><slash:comments>0</slash:comments><comments>http://forums.xna.com/forums/thread/86441.aspx</comments><wfw:commentRss>http://forums.xna.com/forums/commentrss.aspx?SectionID=56&amp;PostID=86441</wfw:commentRss><description>Very well put Jon. I&amp;#39;d advocate options 1 and 2 where possible, but performance issues lead me down the dark, winding road of option 3.
&lt;p&gt;The only way I&amp;#39;ve managed to avoid horrendous garbage issues when drawing large amounts of text (or small amounts of regularly-changing text; or a moderate amount of text which needs to word wrap in any sort of resizeable &amp;quot;window&amp;quot;) is to discard the string data type as a means of holding displayed text, and implement my own font rendering based on efficient text buffers rather along the lines of MemoryStream (though cutting out that particular middle man). On the face of it it might seems ludicrous and backward to do so, but the performance gain has been more than quite noticable. It&amp;#39;s a last resort, but it works.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;</description></item></channel></rss>