Game Development Community

Bug in TX2D's use of XNA Framework

by Jason Cahill · in Torque X 2D · 07/13/2009 (4:11 am) · 3 replies

I noticed that my GUIButton controls do not always get the colors I specify in my XML and spent a while debugging it to find that XNA 3.0 has a bug in their constructor for Color(Vector4), which is used by GUIButtonStyle::OnLoaded.

The specific color that caused the problem was:
<TextColors>
        <ColorBase><X>255</X><Y>255</Y><Z>255</Z><W>255</W></ColorBase>
        <ColorHL><X>254</X><Y>194</Y><Z>43</Z><W>255</W></ColorHL>
        <ColorSEL><X>0</X><Y>0</Y><Z>0</Z><W>255</W></ColorSEL>
        <ColorNA><X>0</X><Y>0</Y><Z>0</Z><W>255</W></ColorNA>
      </TextColors>

I noticed that everything worked except ColorHL, which rendered as white. Stepping through the code proved that the bug is in the Color constructor. Here's the fix for those that care. It should be applied to TorqueCoreGUIGUIText (GUITextStyle) and TorqueCoreGUIButton (GUIButtonStyle):

public override void OnLoaded()
        {
            base.OnLoaded();

            for (int i = 0; i < _textColorAsVector4.Count; i++)
            {
                Vector4 vec = _textColorAsVector4[i];
                Color col = new Color();
                col.R = (Byte)vec.X;
                col.G = (Byte)vec.Y;
                col.B = (Byte)vec.Z;
                col.A = (Byte)vec.W;
                TextColor[(CustomColor)i] = col;
            }
        }

#1
07/13/2009 (8:22 am)
OK, I've been chatting with the XNA team about this and it appears this is a misuse of this API in TorqueX. The correct use of Color(Vector4) is to have each component of the color in the range of 0..1. So, the engine either needs to get fixed with my fix above, or a better fix would be:

TextColor[(CustomColor)i] = new Color(_textColorAsVector4[i] / 255.0f);

I have verified that this fix works correctly and have updated my engine sources.
#2
07/13/2009 (4:48 pm)
Nice work Jason! Added this thread to community links.

Brian
#3
09/13/2009 (4:42 am)
Fix added to the 3.1.4 release.

John K.