Sign in to follow this  

Easy question - Resizing a font?

This topic is 4708 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, it's me again. :) I'm using this to create my Direct3D font (just a plain old font, not a mesh font or anything 3D):
D3DXCreateFont( DXStuff->g_pd3dDevice,	// D3D device
	nHeight,			// Height
	nHeight*0.5f,			// Width (default 0)
	FW_BOLD,			// Weight
	1,				// MipLevels, 0 = autogen mipmaps
	FALSE,				// Italic
	DEFAULT_CHARSET,		// CharSet
	OUT_DEFAULT_PRECIS,		// OutputPrecision
	DEFAULT_QUALITY,		// Quality
	DEFAULT_PITCH | FF_DONTCARE,	// PitchAndFamily
	g_strFont,			// pFaceName
	&DXStuff->g_pFont)		// ppFont
It works fine, because most of my ingame text size should be as big as 'nHeight'. However, not ALL of the text should be that size... Is there any way to change the size of the text after I create the font object? I'd like to be able to use my one 'DXStuff->g_pFont' object to draw text in different sizes throughout my game, but I don't know how to resize the font without having to actually create a second font object that is hard-set to a different size. Thanks in advance for the help!

Share this post


Link to post
Share on other sites
Thanks for the reply DrunkenHyena, and sorry it took me so long to reply. Work has been keeping me from doing much coding, so I haven't been on the coding forums either.

Cool, I'll just create another font. I'm currently only using the one font, so I don't think I'll get much of a performance hit. Speaking of fonts...

	wsprintf(MyInfo, "%i", DXStuff->GameLevel);
SetRect( &DXStuff->rc,
(int)(54*DXStuff->DX_XMultiplier),
(int)(216*DXStuff->DX_YMultiplier),
0,
0 );
DXStuff->g_pFont->DrawText( NULL,
MyInfo,
-1,
&DXStuff->rc,
DT_NOCLIP, D3DXCOLOR( 1.0f, 1.0f, 1.0f, 1.0f ));


I'm using the above to draw some text onto the screen. It works fine, but I read somewhere that I could use a sprite to make the text draw even faster (first argument of 'DrawText' function)... As you can see, every render iteration is having to convert the integer 'DXStuff->GameLevel' into a string that 'DrawText' can use to put my game level data onto the screen. As for the sprite structure, well, I dunno how to use it so it's just set to NULL. Does using a sprite make that much of a difference? If so, how would I change the above code to utilize that sprite ability?
PS: I've never used sprites before, so you'll be teaching a dummy... Use small words, please! ;)

Thanks again for all the help guys, I really appreciate it! :)

Share this post


Link to post
Share on other sites
Alright, if the first parameter is set to null all it will do in the background is starting a sprite stage and ending a sprite stage (Thanks to DH for clarifying that before). DirectX already has a sprite object initialised in the background that handles the rendering of fonts, the preformace hit is made when the sprite interface is forcing a little amount of quads to the card at a time. This is what the font interface does wrong because every call to it forces the sprite interface to start and end which flushes it's buffers with small amounts of data.

What I did for a font class of mine is have it load a specific font style only, so for every different font style I have to have a new cFont. If I specify a new font size of that style to render, and if it doesn't find it loaded, it will initialise the font using D3DXCreateFont and dump that size into a map structure so it will always have the font size when it needs it. So if I initialize the font class with "verdana" and render size 10, it will notice that I haven't got a size 10, then it will create it. Next time it loops over it reads from the pre-initialised 10 size, and so on ...

Now like was said, if you flush the sprite interface with a little amount of sprites it causes overhead; maybe not a lot, but it all adds up. So what you will need to do is create a sprite interface for all of your sprites or fonts, you'll only need one of them so having more then one is pointless. What I did was have my graphics class set one up when DirectX was setting up.


// Member Variable
LPD3DXSPRITE m_sprite;
// Setup code
if(FAILED(D3DXCreateSprite(m_device, &m_sprite))){
Kill();
return false;
};


That's it!

So to render the font all you have to do is:


GraphicsClass->GetSpriteInterface()->Begin(flags);
// Draw all the fonts or sprites you need with the passed Sprite Interface
GraphicsClass->GetSpriteInterface()->End();


That should solve your problem. :)

Share this post


Link to post
Share on other sites
Cool! Thanks for the explanation, PumpkinPieman. I was able to squeeze 75 more frames per second out of my game by using a sprite for my text. I didn't know that text was actually drawn from sprite objects, so that makes this concept a little easier to understand.

Thanks again!

Share this post


Link to post
Share on other sites

This topic is 4708 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this