Managed Sprite and Font Problem

Started by
0 comments, last by Mrs Kensington 19 years, 5 months ago
I'm using October 2004 release of Managed DirectX 9 and am having trouble drawing sprites after drawing text. Here's some example code...

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace SpriteTest
{
	public class MainClass : Form
	{
		Device device = null;
		Microsoft.DirectX.Direct3D.Font font = null;
		Sprite sprite = null;
		Texture texture = null;
		
		bool drawingTextBeforeSprite = true;
		
		public MainClass()
		{
			this.ClientSize = new System.Drawing.Size(640, 480);
			this.Text = "Direct3D Project";
		}
		
		public bool InitializeGraphics()
		{
			try 
			{
				// Now let's setup the Direct3D stuff
				PresentParameters presentParams = new PresentParameters();
				presentParams.Windowed   = true;
				presentParams.SwapEffect = SwapEffect.Discard;
				
				// Create the device
				device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
				
				//create font
				System.Drawing.Font systemFont = new System.Drawing.Font(FontFamily.GenericSansSerif, 20.0f, Fontstyle.Bold);
				font = new Microsoft.DirectX.Direct3D.Font(device, systemFont);
				
				//create sprite
				FileInfo file = new FileInfo("texture.jpg");
				texture = new Texture(device, file.OpenRead(), Usage.None, Pool.Managed);
				sprite = new Sprite(device);
				
				return true;
			}
			catch (DirectXException) 
			{
				return false;
			}
		}
		
		protected virtual void Render()
		{
			if (device != null) {
				device.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
				device.BeginScene();
				
				sprite.Begin(SpriteFlags.AlphaBlend);
				
				if(drawingTextBeforeSprite)
					font.DrawText(sprite, "Hello World!", 0, 0, Color.White);
				
				sprite.Draw2D(texture, Rectangle.Empty, Rectangle.Empty, new Point(10, 10), Color.White);
				
				if(!drawingTextBeforeSprite)
					font.DrawText(sprite, "Hello World!", 0, 0, Color.White);
				
				sprite.End();
				
				device.EndScene();
				device.Present();
			}
		}
		
		public void Run()
		{
			while (Created) 
			{
				Render();
				Application.DoEvents();
			}
		}
		
		static void Main()
		{
			using (MainClass mainClass = new MainClass()) 
			{
				if (!mainClass.InitializeGraphics()) 
				{
					MessageBox.Show("Error while initializing Direct3D");
					return;
				}
				mainClass.Show();
				mainClass.Run();
			}
		}
	}
}

If drawingTextBeforeSprite is false i can see the texture with the font drawn on top. If i set drawingTextBeforeSprite to true I expect to see part of the text with the rest of the text obsured by my texture, however all i see is the Text. Does anyone know the cause of this problem? Thanks (in advance) for your help.
Advertisement
ok i worked out the problem... turns out I needed to use the method where you can specify a DrawTextFormat and pass in NoClip to stop it clipping data.

To me this seems to be a bug in Managed DirectX as (in my opinion) unless your specifying an area you want the text drawn (ie with a rectangle) it shouldn't clip anything...

anyway just documenting the problem here in case it confuses anyone else...

This topic is closed to new replies.

Advertisement