Jump to content
  • Advertisement
Sign in to follow this  
capn_midnight

[.net] MDX and the sample framework

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

I'm fiddling around with the MDX Sample Framework, trying get just some simple 2D stuff going. I have some code that I have adapted from the Text3D demo that comes with the SDK. I'm just trying to draw a 10x10 red square at the point (100,100) just to see if I could get it working, but all I get is a blank screen. I apparently suck worse than an Oreck.
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using Microsoft.Samples.DirectX.UtilityToolkit;

namespace MyForm
{
	public class MyForm : IDeviceCreation, IFrameworkCallback
	{
		Framework framework;
		Sprite sp;
		Texture t;
		FirstPersonCamera camera=new FirstPersonCamera();
		public MyForm(Framework f)
		{
			framework = f;
		}
		#region framework event handlers
		public void OnDestroy(object source, EventArgs args)
		{
			if(sp!=null)
			{
				sp.Dispose();
				sp=null;
			}
		}
		public void OnDeviceCreated(object source, DeviceEventArgs args)
		{
			//make a small, red square as a test sprite
			sp = new Sprite(args.Device);
			Bitmap b = new Bitmap(10, 10);
			for(int y=0;y<10;++y)
			{
				for(int x=0;x<10;++x)
				{
					b.SetPixel(x,y,Color.Red);
				}
			}
			t = new Texture(framework.Device, b, Usage.None, Pool.Managed);			
		}
		public void OnDeviceLost(object source, EventArgs args)
		{
			if(sp!=null)
				sp.OnLostDevice();
		}
		public void OnDeviceReset(object source, DeviceEventArgs args)
		{
			sp.OnResetDevice();
			// Set the transform matrices
			Vector3 eyeLocation = new Vector3(0.0f,-5.0f,-10.0f);
			Vector3 lookAtPoint = new Vector3(0.2f, 0.0f,  0.0f);
			args.Device.Transform.World = Matrix.Identity;
			camera.SetViewParameters(eyeLocation, lookAtPoint);
			camera.SetProjectionParameters((float)Math.PI / 4, 1.0f, 1.0f, 1000.0f);
		}
		#endregion
		#region IDeviceCreation Members

		//using the method from the example code
		public bool IsDeviceAcceptable(Caps caps, Format adapterFormat, Format backBufferFormat, bool isWindowed)
		{
			// Skip back buffer formats that don't support alpha blending
			if (!Manager.CheckDeviceFormat(caps.AdapterOrdinal, caps.DeviceType, adapterFormat, 
				Usage.QueryPostPixelShaderBlending, ResourceType.Textures, backBufferFormat))
				return false;

			return true;
		}

		//use the method from the example code
		public void ModifyDeviceSettings(DeviceSettings settings, Caps caps)
		{
			// If device doesn't support HW T&L or doesn't support 1.1 vertex shaders in HW 
			// then switch to SWVP.
			if ( (!caps.DeviceCaps.SupportsHardwareTransformAndLight) ||
				(caps.VertexShaderVersion < new Version(1,1)) )
			{
				settings.BehaviorFlags = CreateFlags.SoftwareVertexProcessing;
			}
			else
			{
				settings.BehaviorFlags = CreateFlags.HardwareVertexProcessing;
			}

			// This application is designed to work on a pure device by not using 
			// any get methods, so create a pure device if supported and using HWVP.
			if ( (caps.DeviceCaps.SupportsPureDevice) && 
				((settings.BehaviorFlags & CreateFlags.HardwareVertexProcessing) != 0 ) )
				settings.BehaviorFlags |= CreateFlags.PureDevice;

			// For the first device created if its a REF device, optionally display a warning dialog box
			if (settings.DeviceType == DeviceType.Reference)
			{
				Utility.DisplaySwitchingToRefWarning(framework, "Text3D");
			}
		}

		#endregion
		#region IFrameworkCallback Members

		public void OnFrameMove(Device device, double totalTime, float elapsedTime)
		{
			//empty, nothing yet
		}

		public void OnFrameRender(Device device, double totalTime, float elapsedTime)
		{
			device.Transform.View = camera.ViewMatrix;
			device.Transform.Projection = camera.ProjectionMatrix;
			device.BeginScene();
			device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);
			sp.Begin(SpriteFlags.Billboard | SpriteFlags.AlphaBlend);
			sp.Draw2D(t,Point.Empty, 0, new Point(100,100), Color.White);
			sp.End();
			device.EndScene();
		}

		#endregion
		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static int Main() 
		{
			using(Framework framework = new Framework())
			{
				MyForm form = new MyForm(framework);
				framework.Disposing += new EventHandler(form.OnDestroy);
				framework.DeviceCreated += new DeviceEventHandler(form.OnDeviceCreated);
				framework.DeviceLost += new EventHandler(form.OnDeviceLost);
				framework.DeviceReset += new DeviceEventHandler(form.OnDeviceReset);

				framework.SetCallbackInterface(form);
				framework.SetDeviceCreationInterface(form);
				framework.SetCursorSettings(true, true);
				framework.Initialize(true, true, true);
				framework.CreateDevice(0, true, Framework.DefaultSizeWidth, Framework.DefaultSizeHeight,form);

				framework.MainLoop();			
				return framework.ExitCode;
			}
		}
	}
}

Share this post


Link to post
Share on other sites
Advertisement
Is any of your MDX stuff worked?
And in the your code where's your lights?
Don't you need some lights in there somewhere if you are using cameras?

Share this post


Link to post
Share on other sites
I'm afraid I've never used a Sprite; I would draw a quad of transformed vertices instead.

However, you could try specifying a rectangle instead of just a point.

Hope that helps,
Andrew

Share this post


Link to post
Share on other sites
A couple of things that may or may not address your problem.

Don't use SpriteFlags.Billboard with Draw2D() billboarding only makes sense with sprites in 3D space and requires extra configuration (See: Sprite.SetWorldViewLH/RH()).

Sprite is not effected by the World/View/Projection matricies unless you set SpriteFalgs.ObjectSpace or call SetWorldViewLH/RH() so when you draw sprites using Draw() or Draw2D() you are drawing in screen space.

I don't know if the framework calls it for you, but you may need to Call device.Present() after you call end scene.

You are using two methods that lots of people have trouble with.

1. Creating a texture from a bitmap. Try using TextureLoader.FromFile (or FromWhatever) instead. If you want to paint on a texture with GDI you can get a graphics context directly from its primary surface (GetSurfaceLevel(0).GetGrpahics()).

2. Sprite.Draw2D() this quirky method translates before it rotates creating some interesting, but generally wrong effects. It may be a better choice to set the transform on your sprite to a matrix generated by something like Matrix.AffineTransform2D() and call Draw().

hope something here helps.

Share this post


Link to post
Share on other sites
turnpast, thanks for the suggestions, we'll see how they turn out.

My intention with this is to just do some simple 2D, sprite based rendering, think Super Mario Bros., etc. The project is meant to be very small, so I thought the Sample Framework would be a good choice in that regard. Right now, instead of copying an image file over that I could use, I wanted something generated on the fly just for this testing code.

The documentation on the Sample Framework is very sparse. There is documentation on the methods and the parameters, but no real documentation on usage. Of course, the framework is very prelim stuff, the docs are a little behind the actually framework.

I originally thought to include a call to device.Present(), but the sample code did not include this call, so I thought that it might be redundant. It's in there now, and nothing has changed.

I'll kill the Bitmap now. Just loading a small red square from a file.

Since making the original post, I turned lighting off.

I don't need Draw2D to do any rotations what-so-ever, so it's rotation quirks (of which I am familiar, angrily so) should not be a problem.

Okay, rebuilding...

no dice.

Relevant bits of code that changed

public void OnDeviceCreated(object source, DeviceEventArgs args)
{
//turn off lighting
args.Device.SetRenderState(RenderStates.Lighting, false);
sp = new Sprite(args.Device);
t = Texture.FromStream(args.Device, new System.IO.FileStream("test.bmp", System.IO.FileMode.Open), Usage.None, Pool.Managed);

// This caused an exception
//t = new Texture(args.Device, 10, 10, 1, Usage.None, Format.X8B8G8R8, Pool.Managed); //Tried both Format.[A|X]8R8G8B8
//Graphics g = t.GetSurfaceLevel(0).GetGraphics();
//g.DrawRectangle(new Pen(new SolidBrush(Color.Red),1.0f),0,0,10,10);
}
public void OnDeviceReset(object source, DeviceEventArgs args)
{
sp.OnResetDevice();
}
public void OnFrameRender(Device device, double totalTime, float elapsedTime)
{
device.BeginScene();
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);

sp.Begin(SpriteFlags.AlphaBlend);
sp.Draw(t,new Vector3(5.0f,5.0f,5.0f),new Vector3(0.2f,0.0f,0.0f),Color.White.ToArgb());
sp.End();
device.EndScene();
device.Present();
}



All I can think of is that it is some kind of lighting issue. I've got everything so far except lighting, but the sample project didn't use any lights.

oh... works now. Bollocks, when I got rid of the camera, I forgot to put the sprite into a viewable area. My new OnFrameRender method

public void OnFrameRender(Device device, double totalTime, float elapsedTime)
{
device.BeginScene();
device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black, 1.0f, 0);

sp.Begin(SpriteFlags.AlphaBlend);
sp.Draw2D(t, new Rectangle(0,0,10,10), new Rectangle(0,0,10,10), new Point(100,100), Color.White);
sp.End();
device.EndScene();
device.Present();
}


Thanks everyone for your help. turnpast, I would rate you up, but apparently I already have.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!