Sign in to follow this  
dejangex

Direct3D.Font.DrawText Problem (C# 05)

Recommended Posts

dejangex    130
Hi everyone, I'm working on a project that uses DirectX and C#, and untill now I had no problems. I have encountered some very strange behavior with Direct3D.Font.DrawText method. Internally I use the same sprite object for drawing sprites and for text. Excluding other parameters, you must pass XY coordinates or a rectangle that represents the position of the text. So if I pass the new Rectangle(0,0,100,100) the text would be positioned on the top-left of the screen inside the rectangle. Well, it isnt. Its positioned relative to the last sprite drawn with the sprite object. Here is the text drawing code:
			public static void DrawText(string text, string font, Rectangle position, int color)
			{
				if (true == m_FontCollection.ContainsKey(font))
				{
					m_FontCollection[font].DrawText(m_sprSprite, text, position, Direct3D.DrawTextFormat.WordBreak, color);
				}
				else
				{
					Log.Write("DrawText: Font '" + font + "' was not loaded.");
					Engine.Exit(true);
				}
			}


Here the sprite drawin routine:
			public static void Draw2D(Sprite s)
			{
				try
				{
					m_sprSprite.Draw2D(s.GetTexture(), s.GetSrcRect(), s.GetSize(), s.GetCenter(), s.GetRotation(), s.GetPosition(), s.GetColor());
				}
				catch (Exception e)
				{
					Log.Write(e);
					throw e;
				}
			}


So what happens is that when I draw just the text, it is positioned just fine, but if I draw a sprite and then some text, the text is positioned relative to the last sprite drawn position. Can't figure this one out... [Edited by - dejangex on March 2, 2008 4:29:28 AM]

Share this post


Link to post
Share on other sites
dejangex    130
Strange that nobody knows... Only way I found to fix this is to use 2 separate Direct3D.Sprite objects: one for drawing text and the other for drawing sprites. It's probably a DirectX bug.

Share this post


Link to post
Share on other sites
Mike.Popoloski    3258
Unfortunately, with MDX being deprecated by Microsoft, problems like this won't go away (assuming there IS in fact a bug in the Sprite object). Depending on how far along you are in your project, I would recommend checking out SlimDX, which is an open source library that replaces MDX.

Share this post


Link to post
Share on other sites
keshtath    146
Theres another thread where it sounds like someone was having a similar problem:

http://www.gamedev.net/community/forums/topic.asp?topic_id=485264

Looks like calling SetTransform before every render call could fix the problem for you.

If your going to do C# and DirectX I would suggest using XNA which has replaced MDX, added alot of new features and appears to be supported by the community on a pretty grand scale. I've found more information, tutorials, and support for XNA than MDX by a factor of 10. Add XBOX 360 and upcoming Zune support and it becomes a no brainer to go with XNA. :)

If you're curious I have a ton of links on xna.

Share this post


Link to post
Share on other sites
dejangex    130
Im working on a 2D game, so I dont need any fancy features. I got all the video code going, so its ok now. XNA is primarily intended for the XBOX and im really down with using third party stuff.

Great to see that there are people who would help though. Thank you all.

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