• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

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

4 posts in this topic

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

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  
Followers 0