• Advertisement
Sign in to follow this  

Rendering text - C#, SlimDX, DirectX11 - how?

This topic is 1961 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

With my pacman game taking shape, I need to start drawing text to screen for things like showing the score, high score tables etc.

Initial research is not encouraging:


Essentially it seems quite difficult to do something as simple as writing text to the screen. Aaron's method seems the only one out there which is practical at the moment.

Does anyone else have any advice they could offer? I thought it would be worth asking before getting stuck into a chosen strategy.


Share this post

Link to post
Share on other sites
I've never used Aaron's method, so I'm not sure what the advantages / downsides of it are, but according to his blog post I'd say it works fine.

Personally, I create a texture containing all characters for the font I want to use ahead of time.
You can do this with a program such as [url="http://code.google.com/p/fontbuilder/"]FontBuilder[/url] or you can write something yourself.

I then render text using hardware instancing.
I queue up the information for all characters that need to be drawn: where they need to be drawn, where they are on the texture, what color I want them in etc...
Then I build an instance buffer from that information, and render the entire text using a single draw call.

That being said, if you feel that Aaron's method is practical and it provides everything you need, go with it.
No need to make things more complicated if you have a solution that fits your needs. :)

Share this post

Link to post
Share on other sites
FontBuilder - not used this before. Very interesting.

Can you describe this method of rendering text using hardware instancing in more detail - or even point me in the direction of some on-line resources?

I was thinking of just creating a set of bitmaps or Jpegs, but with one for each character, rather than having them all present in one bitmap (which I think is what you are describing). I would then just create a sprite for each letter, and write a route to convert a character string into a set of sprites. This would be realitvely easy to do in terms of technical skill required.

However, what I think you are proposing is a bit smarter than that. It sounds fascinating though.

For now I have imported Aaron's code into my game framework, to figure out how it works. I estimated that the same amount of time would be required to import Aaron's code as to display a sprite for each character. It's a bit of an assumption of course, and I might live to eat my words.

Have you written your own custom sprite class? I discussed this with Nico Shertler a bit - and since I wanted to learn how sprites were displayed I decided to make my own class rather than use his sptite text renderer.

I totally agree on your statement about making things too complicated. I was on the verge of abandoning importing Aaron's code, but at the last minute thought it was worth another few hours just in case I could nail it. I'm getting quite close - but again I am really interested in the method you mention. There are always a few ways to tackle a problem. [img]http://public.gamedev.net//public/style_emoticons/default/cool.png[/img]

Share this post

Link to post
Share on other sites
Yes, I have written my own sprite renderer that handles both sprite sheets and fonts.
It handles both because if you handle fonts this way, it basically works the same way as sprite sheets.
Each font glyph is a sprite and they are all stored within the same texture, which is your sprite sheet.

You need to know where on the texture each glyph is located.
Tools such as FontBuilder provide this information for you in an additional file.
You need to parse this information and create some way to map single characters to glyph location within your code. (C# dictionary, c++ map...)

Then create a simple function that draws text.
Just split the string you want to draw into individual characters, look them up in your map, and render a screen quad using this data.

Here's some old hlsl code I had lying around:

[source lang="cpp"]Texture2D SpriteSheet : register(t0);
SamplerState SpriteSampler : register(s0);

cbuffer CBText : register (b0)
float2 TextureSize : packoffset(c0.x);
float2 ViewportSize : packoffset(c0.z);

struct VSInput
// Vertex Data
float2 Position : POSITION0;
// Instance Data
float4 Color : COLOR0;
float4 Source : SRCRECT;
float4 Destination : DSTRECT;

struct PSInput
float4 Position : SV_Position;
float2 TexCoord : TEXCOORD;
float4 Color : COLOR;

PSInput VSText(VSInput input)
PSInput output = (PSInput)0;

float4 SourceRect = float4(input.Source.xy / TextureSize, input.Source.zw / TextureSize);
float4 DestinationRect = float4(input.Destination.xy / ViewportSize, input.Destination.zw / ViewportSize);

float2 OutPos = input.Position * DestinationRect.zw + DestinationRect.xy;
OutPos = OutPos * 2.0 - 1.0;
OutPos.y = -OutPos.y;

output.Position = float4(OutPos, 0, 1);
output.TexCoord = input.Position * SourceRect.zw + SourceRect.xy;
output.Color = input.Color;

return output;

float4 PSText(PSInput input) : SV_TARGET
float4 Color = SpriteSheet.Sample(SpriteSampler, input.TexCoord);
Color = Color * input.Color;
Color.rgb *= Color.a;
return Color;

You render a simple fullscreen quad with these shaders. (from (0, 0) to (1, 1))
Source, Destination, TextureSize and ViewportSize are provided in pixels.
If you've never used hardware instancing before you might want to create an implementation without it first.
Just put the Color/Source/Destination instance data I have into the constant buffer for now, and make 1 draw call per character.
Remember to add some distance between characters so they're not all rendered in the same spot.

To be honest, doing this without hardware instancing is probably fine, unless you render a lot of text.
I'd only look into it if you think that it is affecting your performance, or if you want to learn a very efficient way of doing it.
Once you have this running, changing it to use hardware instancing is pretty straight forward and reduces your draw calls to 1.


Edit: Don't use a texture for each font glyph. That's a really bad idea. :D
Think about it, for each character you draw you would not only issue a draw call, but make a state change as well (change texture register).
This isn't horribly expensive, but the overhead of doing so quickly builds up if you're rendering a lot of characters. Edited by Hyunkel

Share this post

Link to post
Share on other sites
Nice one Hyu - and always a pleasure to speak to someone else who's worked with SlimDX. It's about as easy as finding a badger in clear daylight.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement