Avatar of ironwill96

asked on 

Fuzzy and Blurry Text using Sprites in Directx9 C#


I am using sprites to try to render text because I need to be able to rotate them etc.  I moved this out of the other post about fuzzy text because it was confusing the original poster's issues and mine are not necessarily directly related.

Here is the code where I actually render the sprites.

[source lang="c#"]
Microsoft.DirectX.Direct3D.Font font = new Microsoft.DirectX.Direct3D.Font(device, l.LayerFont.Height, 0, FontWeight.Normal, 0, false, CharacterSet.Default, Precision.Default, FontQuality.AntiAliased, PitchAndFamily.DefaultPitch, l.LayerFont.FontFamily.Name);                      

Microsoft.DirectX.Direct3D.Sprite mySprite = new Sprite(device);

mySprite.Transform = Matrix.Transformation2D(new Microsoft.DirectX.Vector2(0, 0), 0.0f, new Microsoft.DirectX.Vector2(1, 1), new Microsoft.DirectX.Vector2((float)l.BmpObject.Width/2, (float)l.BmpObject.Height/2), l.RotationAngle, new Microsoft.DirectX.Vector2(l.XPosition - .5f, l.YPosition - .5f));

font.DrawText(mySprite, l.LayerText, new Rectangle(0, 0, l.BmpObject.Width, l.BmpObject.Height), DrawTextFormat.Center, Color.Black);


And here is the code where I setup my device:

[source lang="c#"]
public void InitializeGraphics()
            PresentParameters pp = new PresentParameters();
            pp.Windowed = true;
            pp.SwapEffect = SwapEffect.Discard;
            pp.EnableAutoDepthStencil = true;
            pp.AutoDepthStencilFormat = DepthFormat.D16;
            pp.DeviceWindowHandle = this.Handle;
            device = new Device(0, DeviceType.Hardware, this,
            CreateFlags.HardwareVertexProcessing, pp);
            device.DeviceReset += new EventHandler(this.OnDeviceReset);
            OnDeviceReset(device, null);
            initialized = true;

private void CameraPositioning()
            if(this.mapMain != null)            
                device.Transform.Projection = Matrix.OrthoLH(this.mapMain.getDXWidth(), this.mapMain.getDXHeight(), 1f, 550f);                
                device.Transform.View = Matrix.LookAtLH(new Microsoft.DirectX.Vector3(0, 0, 450), new Microsoft.DirectX.Vector3(0, 0, 0), new Microsoft.DirectX.Vector3(0, -1, 0));              
                device.RenderState.Lighting = false;
                device.RenderState.CullMode = Cull.None;


The camera positioning method you can see uses getDXWidth and getDXHeight to grab the size of the custom control that we are using to render DirectX to (we are not rendering to a form).

The "l" object is a custom layer object I have for my drawings that stores a lot of meta-data.  As you can see it has a rotation angle float built in as well as its x and y positions as they are translated.  It also has a bitmap object that represents the size of the rectangle needed to draw the sprite that i'm using to grab the width and height.  My font is pretty fuzzy as you can see in the pictures below and gets worse as soon as I rotate it.  It does seem like i'm getting a texel alignment issue of some sort, but I can't figure out where I would pass the -.5 offset to the X and Y as this article suggests to do: http://msdn.microsoft.com/en-us/library/bb219690.aspx

I tried putting it in my translation matrix but that obviously didn't make any difference and the font.DrawText only takes ints for the rectangle the sprite is put on.

Here are two pictures to illustrate the issue:

Test Image before rotation (note still slightly blurry):
<img src="http://www.greatpcs.net/test1.jpg" />  

Test image after some rotation (note how much more blurry it is):
<img src="http://www.greatpcs.net/test1rotate.jpg" />  

Game ProgrammingC#

Avatar of undefined
Last Comment

8/22/2022 - Mon