3

I have a problem with DrawString() in XNA. I use multiple SpriteBatches for several logical layers. For example: background, objects, menus and so on.

In my menu batch, I draw a menu (the big grey box in the background), the buttons (the smaller grey boxes on the menu) and the strings for the buttons.

The Problem: http://ompldr.org/vaGw4YQ/Unbenannt.png

But for some reason, the strings aren't drawn completely. Does anyone know why?

EDIT:

_menuLayer.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
        if (_menu != null)
        {
            _menuLayer.Draw(_menuBoard, new Vector2(graphics.PreferredBackBufferWidth / 2 - 160, graphics.PreferredBackBufferHeight / 2 - 240), Color.White);
        }
        _menuLayer.End();
        _buttonLayer.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
        if (_menu != null)
        {
            foreach (Button button in _menu.Buttons)
            {
                if (button.Pressed)
                {
                    _buttonLayer.Draw(_menuButtonPressed, button.Location, Color.White);
                    _buttonLayer.DrawString(_text, button.Text, button.GiveStringLocation(_text), Color.Black);
                }
                else
                {
                    _buttonLayer.Draw(_menuButton, button.Location, Color.White);
                    _buttonLayer.DrawString(_text, button.Text, button.GiveStringLocation(_text), Color.Black);
                }
            }
        }
        _buttonLayer.End();

<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
  <Asset Type="Graphics:FontDescription">

    <!--
    Modify this string to change the font that will be imported.
    -->
    <FontName>Arial</FontName>

    <!--
    Size is a float value, measured in points. Modify this value to change
    the size of the font.
    -->
    <Size>20</Size>

    <!--
    Spacing is a float value, measured in pixels. Modify this value to change
    the amount of spacing in between characters.
    -->
    <Spacing>0</Spacing>

    <!--
    UseKerning controls the layout of the font. If this value is true, kerning information
    will be used when placing characters.
    -->
    <UseKerning>true</UseKerning>

    <!--
    Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
    and "Bold, Italic", and are case sensitive.
    -->
    <Style>Bold</Style>

    <!--
    If you uncomment this line, the default character will be substituted if you draw
    or measure text that contains characters which were not included in the font.
    -->
    <!-- <DefaultCharacter>*</DefaultCharacter> -->

    <!--
    CharacterRegions control what letters are available in the font. Every
    character from Start to End will be built and made available for drawing. The
    default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
    character set. The characters are ordered according to the Unicode standard.
    See the documentation for more information.
    -->
    <CharacterRegions>
      <CharacterRegion>
        <Start>&#32;</Start>
        <End>&#126;</End>
      </CharacterRegion>
    </CharacterRegions>
  </Asset>
</XnaContent>
Marco Frost
  • 780
  • 3
  • 12
  • 25
  • And the code used to render the menu? – CodeCaster Feb 25 '13 at 23:06
  • Clipped or obscured is the most likely reason, possibly both. Unless you are doing something strange, the invisble ink (pen colour = background colour) is unlikely. No way to say from here though. – Tony Hopkinson Feb 25 '13 at 23:14
  • Hey, looks like you're using Unicode characters (Think `é` or `Á`). Spritefonts by default don't include these. Can you post your spritefont? – Steffan Donal Feb 25 '13 at 23:16
  • I've added the code. I hope, this gives you a better understanding of my problem... :) – Marco Frost Feb 25 '13 at 23:20
  • I meant the actual contents of the spritefont itself. – Steffan Donal Feb 25 '13 at 23:22
  • What is the full contents of the string? I.e., what are the values of button.Text for each of the buttons? – Katie Kilian Feb 25 '13 at 23:24
  • For example the first button.Text has the Value "Neues Spiel" and thats also the full content of the string. The SpriteFontContent is now also appended to the post here. – Marco Frost Feb 25 '13 at 23:25
  • Set the `BlendState` to `Opaque` and see if there are solid color squares everywhere a letter should be, then you'll know if those letters are being drawn. But I suspect your text contains non-ascii symbols, and you have to add those symbols to the spritefont settings file. I remember we had a guide for that somewhere on this site. – user1306322 Feb 25 '13 at 23:51
  • Thanks. But when I set it to opaque there are black things where the letters should be... – Marco Frost Feb 26 '13 at 00:08

3 Answers3

2

It looks like your font is only letting you use certain characters.

<CharacterRegions>
  <CharacterRegion>
    <Start>&#32;</Start>
    <End>&#126;</End>
  </CharacterRegion>
</CharacterRegions>

Make sure your characters are either within the ASCII Value of the Start and End tags, or change the Start and End Tags to include the characters you want.

http://www.asciitable.com/

naed21
  • 147
  • 3
  • 9
2

Try using SpriteSortMode.Immediate. Otherwise, I think the problem might be in the way you do things. It seems messy to leave rendering to a class outside your UI. You should be able to tell a button to draw, and it should draw itself. Or indeed just tell your UI to draw, and it draws all controls, which draws their child-controls, etc.

That way you can build your UI in a hierarchy::)

Structure your UI a bit; Have a base class like UIControl or something;

List<UIControl> Children { get; protected set; }


Update(MyInputSystem input, float dt)
{
    UpdateChildren(input, dt);
}

UpdateChildren(MyInputSystem input, float dt)
{
    foreach(var child in Children)
        child.Update(input, dt);
}

Draw(SpriteBatch spriteBatch)
{
    DrawChildren(spriteBatch);
}

DrawChildren(SpriteBatch spriteBatch)
{
    foreach(var child in Children)
        child.Draw(spriteBatch);
}

Then you have a Panel-class:

class Panel: UIControl {

Vector2 Position { get; set; }
Texture2D Texture { get; set; }

override Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Draw(Texture, Position, Color.White);
    base.Draw(spriteBatch);
}

}

Then you have a Label-class:

class Label: Panel{

SpriteFont Font { get; set; }
String Text { get; set; }
Vector2 TextOffset { get; set; }
Color TextColor { get; set; }

override Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Draw(Texture, Position, Color.White);
    spriteBatch.DrawString(Font, Text, Position + TextOffset,Text Color);
    DrawChildren(spriteBatch);
}

}

and a button-class:

class Button: Label { //Inherit label, so we dont need to create som much new code

public event EventHandler<EventArgs> Click;

Rectangle _Bounds
public Rectangle Bounds
{
    get
    {
        if (_Rectangle.Width == 0)
            _Rectangle = new Rectangle((int)Position.X,(int)Position.Y, Texture.Width, Texture.Height);

        return _Texture;
    }
}

override Update(MyInputSystem input, flaot dt)
{
    if (input.MouseClicked && Bounds.Contains(input.MousePosition))
        Click(this, new EventArgs());
}

}

You might have noticed that I dont have any SpriteBatch.Begin/End here? that is because:

class UI
{
List<UIControl> Controls;

Update(MyInputSystem input, flioat dt)
{
    foreach(var control in Controls)
       control.Update(input, dt);
}

Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Begin(/*Parameters*/);
    foreach(var control in Controls)
       control.Draw(spriteBatch);
    spriteBatch.End();
}
}

Catch my drift?

0

It may be an issue with the font that you are using. Make sure the font that you are using is a ".ttf" and try a few different ones. I highly suggest you use one of the Microsoft recommended fonts. Link: http://go.microsoft.com/fwlink/?LinkId=104778&clcid=0x409

7200rpm
  • 548
  • 1
  • 4
  • 14