Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change fontsize during the game in XNA?

Tags:

xna

I need to use different fontsize of spritefont, Have to I create new spritefont for the each size?

like image 575
Okan Kocyigit Avatar asked Apr 22 '11 10:04

Okan Kocyigit


3 Answers

Basically yes.

There is an overload of SpriteBatch.DrawString that gives you an option to scale your text.

However the major downside to this is that your text will become pixelated as you scale it up. If you start at a higher resolution and scale down you will start to get artefacts as you get to smaller sizes.

So if you have a fixed number of sizes, you should create multiple versions of your sprite font at the different sizes you require.

If you want continuously scalable text with sharp edges, you could perhaps look into vector fonts. The Nuclex Framework has some code to do that.

like image 151
Andrew Russell Avatar answered Oct 24 '22 21:10

Andrew Russell


Suppose the SpriteFont you are using is named x.spritefont. Do the following to create new SpriteFont for each size.

  • Open the x.spritefont file from solution explorer.
  • Go to the tag and edit it to your desired font size.
  • To make multiple size font, duplicate the file and change the tags accordingly. Rename the files with size appended at last for easy remembering.

Now create multiple instances of SpriteFont and load them accordingly.

SpriteFont sf_s10;
SpriteFont sf_s14;

protected override void LoadContent()
{
  sf_s10 = Content.Load<SpriteFont>("x_10");
  sf_s14 = Content.Load<SpriteFont>("x_14");
  //OTHER LOADS 
}

to dynamically change fontSize, do the following:

SpriteFont current_font;

protected override void Update(GameTime gameTime)
{
  if(/*SOME_CONDITION_TO_DECREASE_SIZE*/)
    current_font=sf_s10;

  if(/*SOME_CONDITION_TO_INCREASE_SIZE*/)
    current_font=sf_s14;
}
like image 24
rahulroy9202 Avatar answered Oct 24 '22 20:10

rahulroy9202


You can also make your font at the largest size you need and scale down from there.

like image 23
nportelli Avatar answered Oct 24 '22 22:10

nportelli