XNA DrawString() draws only a partial string

余生长醉 提交于 2020-01-14 19:15:19

问题


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>

回答1:


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?




回答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/




回答3:


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



来源:https://stackoverflow.com/questions/15078203/xna-drawstring-draws-only-a-partial-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!