Skip to main content
GameDev.net gamedev.net
🔒 Locked

Direct3D.Font Disposing Bug

Started by TimNyborg Apr 18, 2004 at 5:16 PM 11 replies 1k views
Original Post
TimNyborg
TimNyborg
Can anyone give me an idea why this might be happening? I have a basic DX9 program running which prints labels to the screen using the Direct3D.Font class. It works perfectly expect for one thing... After 625 Frames, it crashes, giving me the error ''Cannot access a disposed object named Microsoft.DirectX.Direct3D.Font"''. I''m haven''t disposed it, heck, I don''t ''have'' code to dispose it. What''s VERY odd is that it draws 8 labels per second, so it crashes after 625*8 or 5000 frames. My label printing function consists of: Public Sub DrawLabel(ByRef pLabel As clsMenuLabel) dxFont = New Direct3D.Font(D3DDevice, New Drawing.Font(FontFamily.GenericSansSerif, pLabel.FontSize, FontStyle.Regular, GraphicsUnit.Pixel)) dxFont.DrawText(pLabel.Text, New Rectangle(pLabel.X + pLabel.Parent.X, pLabel.Y + pLabel.Parent.Y, 800, 600), DrawTextFormat.Left Or DrawTextFormat.Top Or DrawTextFormat.WordBreak, pLabel.DrawColor) End Sub Note that the error doesn''t occur if I omit the dxFont = New Direct3d.font line... Does anyone have a clue how I screwed this up?
TimNyborg
TimNyborg
Whoops, that should read ''8 labels per frame'', not per second
RenderTarget
RenderTarget
One piece of advice I can give is to avoid creating a new font every frame. Create it during game init, and store it for use.

I like pie.
[sub]My spoon is too big.[/sub]
TimNyborg
TimNyborg
Then how do I change the properties of the font (namely the size)
TimNyborg
TimNyborg
Yes, I tried disposing of the fonts each frame.... ouch, HUGE demand on the system.

The idea of creating a whole set of font objects is possible, but not appealing, as it's not just sizes, but bolds, underline, varying font families, etc... that I want to use.

Is there no way of modifying the font in the D3DFont object? Is there another 'better' font system I could be using?

[edited by - TimNyborg on May 2, 2004 6:36:14 PM]
darookie
darookie
Implement your own custom font rendering system -
the Direct3D.Font class is buggy (the DirectX team told me that
it will be fixed in the next release, which they expect to be
released this summer).

To address your problem - it is not possible to use font
rendering the way you do it (least not reasonably fast).

You need to create a set of font objects for each font family
(sizing is possible by scaling the glyphs) and each style
(bold, italic, bold-italic). Underline and stroke an be
implemented by simply drawing lines, which is fast and saves
texture memory.

Custom font classes are faster than Direct3D.Font, since the
class creates a new texture for each label you draw. Take
a look at the DXUtil.Font class in the SDK. This class doesn''t
offer features like alignement, clipping and text-wrapping but
it illustrates how to create and use font textures.

If you really need clipping and alignment you can implement these
features easily - it really isn''t that hard to do.

Hope this helps,
Pat.

hplus0603
hplus0603
If you need that much custom text, use GDI (or something like FreeType) to render to an offscreen bitmap, and upload that as a texture. Else create the 20 or whatever different fonts that you need on program start-up, and re-use them between frames. Creating a new font each frame (and not even disposing it) is likely to run slow, and run you out of resources (which is probably the exception you''re seeing).
enum Bool { True, False, FileNotFound };
DrunkenHyena
DrunkenHyena
quote:
Original post by darookie
Custom font classes are faster than Direct3D.Font, since the
class creates a new texture for each label you draw.

No, it doesn''t. It used to, but since the Summer 2003 Update it caches glyphs but does not render each label as an individual texture. It''s actually pretty fast but it''s not meant to be used in the way the OP is using it.

Creating a font texture for every possible combination is not a trivial exercise. Just scaling the bitmap font will produce text that is not nearly as good as text rendered at the right size.

If all he''s doing is rendering labels and needs good quality output, GDI would be a much better choice.

Stay Casual,

Ken
Drunken Hyena
Stay Casual,KenDrunken Hyena</FON
TimNyborg
TimNyborg
It''s not just menus and labels, that''s only the menu set, I''m doing a lot of graphics stuff (and more in the future)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.