Sign in to follow this  
Kevin31

[SlimDX] Drawing text in DX 10

Recommended Posts

Hi, I'm having trouble drawing a text in DX 10 using SlimDX. I've initialized the device and swapchain, created the font and drawn it, but nothing is shown at all, except a blue screen (thats the color I cleared to). Created Font with this Desc:
DX10.FontDescription fontDesc = new SlimDX.Direct3D10.FontDescription {
	Height = 12,
	FaceName = "Times New Roman",
	IsItalic = false,
	Width = 0,
	MipLevels = 1,
	CharacterSet = SlimDX.Direct3D10.FontCharacterSet.Default,
	Precision = SlimDX.Direct3D10.FontPrecision.Default,
	Quality = SlimDX.Direct3D10.FontQuality.Antialiased,
	PitchAndFamily = SlimDX.Direct3D10.FontPitchAndFamily.Default | DX10.FontPitchAndFamily.DontCare
};

And drawn with this method:
public void DrawText(Font font, Vector2 pos, string text, Color4 color) {
	uint col = (uint)color.Alpha << 24 | (uint)color.Blue << 16 | (uint)color.Green << 8 | (uint)color.Red;
	font.font.Draw(null, text, new System.Drawing.Rectangle((int)pos.X, (int)pos.Y, 0, 0), SlimDX.Direct3D10.FontDrawFlags.NoClip,
		col);
	
}

This's how I initialize the device(/swapchain/etc.):
		public void Create(GameWindow wnd) { 
			//1. Create Device + Swapchain:
			DXGI.SwapChainDescription desc = new DXGI.SwapChainDescription()
			{
				BufferCount = 1,
				ModeDescription = new DXGI.ModeDescription(wnd.form.ClientSize.Width,
					wnd.form.ClientSize.Height, new Rational(60, 1), DXGI.Format.R8G8B8A8_UNorm),
				IsWindowed = true,
				OutputHandle = wnd.Handle,
				SampleDescription = new DXGI.SampleDescription(1, 0),
				SwapEffect = DXGI.SwapEffect.Discard,
				Usage = DXGI.Usage.RenderTargetOutput
			};

			DX10.DeviceCreationFlags flags = SlimDX.Direct3D10.DeviceCreationFlags.None;
#if DEBUG
			flags = DX10.DeviceCreationFlags.Debug;
#endif
			if (DX10.Device.CreateWithSwapChain(null, SlimDX.Direct3D10.DriverType.Hardware, flags, desc,
				out device, out swapChain).IsFailure) {
				MessageBox.Show("Failed to create Direct3D 10 Device!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}

			//2. Create RenderTargetView:
			DX10.Texture2D backBuffer = DX10.Texture2D.FromSwapChain<DX10.Texture2D>(swapChain, 0);
			renderTargetView = new DX10.RenderTargetView(device, backBuffer);
			backBuffer.Dispose();

			//3. Create Depth Buffer:
			DX10.Texture2DDescription depthDesc = new DX10.Texture2DDescription { 
				Width = wnd.form.ClientSize.Width, 
				Height = wnd.form.ClientSize.Height,
				MipLevels = 1,
				ArraySize = 1,
				Format = DXGI.Format.D24_UNorm_S8_UInt,
				SampleDescription = new SlimDX.DXGI.SampleDescription(1, 0),
				Usage = DX10.ResourceUsage.Default,
				BindFlags = DX10.BindFlags.DepthStencil,
				CpuAccessFlags = DX10.CpuAccessFlags.None,
				OptionFlags = DX10.ResourceOptionFlags.None
			};

			DX10.Texture2D depthStencilBuffer = new SlimDX.Direct3D10.Texture2D(device, depthDesc);
			depthStencilView = new SlimDX.Direct3D10.DepthStencilView(device, depthStencilBuffer);
			depthStencilBuffer.Dispose();
			
			//Bind views to output merger stage:
			device.OutputMerger.SetTargets(depthStencilView, renderTargetView);

			MainViewport = new Viewport();
			MainViewport.Width = wnd.form.ClientSize.Width;
			MainViewport.Height = wnd.form.ClientSize.Height;

			device.Rasterizer.SetViewports(MainViewport.viewport);
		}

I don't get any DX errors in the Output Window, except a info:
Quote:
D3D10: INFO: ID3D10Device::IASetPrimitiveTopology: Topology value D3D10_PRIMITIVE_TOPOLOGY_UNDEFINED is OK to set during IASetPrimitiveTopology; but will not be valid for any Draw routine. [ STATE_SETTING INFO #237: DEVICE_IASETPRIMITIVETOPOLOGY_TOPOLOGY_UNDEFINED ]
I don't have a clue how to fix it. I hope you can help me... Thanks in advance! regards, Kevin

Share this post


Link to post
Share on other sites
Hi Kevin,
I'll answer here to your PM.
I'll be frank, using the Sprite/Font class from D3DX is plain madness. They have big problems, and low documentation. Sometimes they work, sometimes not. (and the reasons are not that obvious)
For example the DX9 one resets the states, the DX10 one not. etc...
I've passed a really bad 7-8 days trying to make them work correctly and fastly.

About your code :
The call to draw the text is correct, you probably have to look in Pix if some precedent call is making the states fizzle out.

I suggest to take a look at this two articles I wrote about rewriting the two classes by yourself. (i've done it with SlimDX, its pretty easy, and I got some increase in speed and expandibility. (other than simplicity XD)) :

http://feal87.wordpress.com/2009/06/01/remake-of-id3dx10font-id3dxfont/
http://feal87.wordpress.com/2009/05/31/scrapping-out-the-id3dx10sprite-id3dxsprite-interface/

Its useless to post them as they are highly binded to my engine structure and would not fit in your. Follow the ideas written and make your own implementation, its a fun and rewardfull experience.

(My actual implementation is a little different from the one I talked about in those posts, but I think its better for you to start with that that's more simple :D)

Share this post


Link to post
Share on other sites
Quote:
Original post by feal87
Hi Kevin,
I'll answer here to your PM.
I'll be frank, using the Sprite/Font class from D3DX is plain madness. They have big problems, and low documentation. Sometimes they work, sometimes not. (and the reasons are not that obvious)
For example the DX9 one resets the states, the DX10 one not. etc...
I've passed a really bad 7-8 days trying to make them work correctly and fastly.

About your code :
The call to draw the text is correct, you probably have to look in Pix if some precedent call is making the states fizzle out.

I suggest to take a look at this two articles I wrote about rewriting the two classes by yourself. (i've done it with SlimDX, its pretty easy, and I got some increase in speed and expandibility. (other than simplicity XD)) :

http://feal87.wordpress.com/2009/06/01/remake-of-id3dx10font-id3dxfont/
http://feal87.wordpress.com/2009/05/31/scrapping-out-the-id3dx10sprite-id3dxsprite-interface/

Its useless to post them as they are highly binded to my engine structure and would not fit in your. Follow the ideas written and make your own implementation, its a fun and rewardfull experience.

(My actual implementation is a little different from the one I talked about in those posts, but I think its better for you to start with that that's more simple :D)


I already looked in PIX at the DX calls, and compared it to a working DX/C++ application, which did nothing more than draw a text. But I haven't found any difference between my code and the c++ application, well probably I#ve overlooked something.

Thanks for the links, I think now I'll implement my own Sprite/Font classes...

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