Sign in to follow this  
TheNobleOne

Doing 2D with D3D and C#

Recommended Posts

I want to learn how to use D3D and beable to use it for 2D apps. However, I have no clue where to start. I can initialize a device however when I load a texture and then use Sprite.2D to render it does absolutly nothing. I am Specifically looking for a tutorial to teach me this but I can only find ones for doing 3D. Oh and btw here is my code that I have so far by just going off of the documentation provided by microsoft.
using System;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace D3D_Device_Creation
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		// Declarations
		Device device = null;  // the rendering device
		Sprite background = null;
		Texture bg = null;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			this.Height = 400;
			this.Width = 300;
			this.Text = "Create Devices Tutorial";

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(528, 397);
			this.Name = "Form1";
			this.Text = "Form1";

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Form1 form = new Form1();

			if(!form.InitialzeGraphics())
			{
				MessageBox.Show("Could not initialze Direct3D");
			}
			form.Show();

			form.InitializeTextures();
			while(form.Created)
			{
				form.Render();
				Application.DoEvents();
			}

			Application.Exit();
		}

		private void Render()
		{
			if(device == null)
			{
				return;
			}

			// clear the back buffer to a blue color
			device.Clear(ClearFlags.Target, System.Drawing.Color.Blue, 1.0f, 0);
			Rectangle size;
			size = new Rectangle(0,0,300,400);
			// begin the scene
			device.BeginScene();

			// Render code here
			background.Begin(SpriteFlags.None);
			background.Draw2D(bg, Rectangle.Empty, size, new Point(0, 0), Color.White);
			background.End();
			// End the scene
			device.EndScene();
			device.Present();
		}

		public bool InitialzeGraphics()
		{
			try
			{
				// set up d3d stuff
				PresentParameters presentParams = new PresentParameters();
				presentParams.Windowed = true;
				presentParams.SwapEffect = SwapEffect.Discard;

				device = new Device(0, DeviceType.Hardware, this,
					CreateFlags.SoftwareVertexProcessing, presentParams);
				return true;
			}
			catch(DirectXException)
			{
				return false;
			}
		}

		public void InitializeTextures()
		{
			try
			{
				background = new Sprite(device);
				
				bg = TextureLoader.FromFile(device, Application.StartupPath + "\\background.bmp");
			}
			catch(DirectXException)
			{
				MessageBox.Show("Background could not be loaded.");
			}
		}
	}
}

Share this post


Link to post
Share on other sites
Well how far did you get? Did it initialise properly (did the window change to the color you specified in the render loop)? If it didn't, you probably need to start there.

Once you get everything set up, I'd recommend you start out with Transformed, Lit Vertices (TL Vertices). These are, pretty much, the closest thing you can get to 3D without using sprites. IMO they're easier.

In site Linkies:
Beginners
Direct X

And, uh, I think the FAQ now has plenty of stuff there, if you want to check that first.

Best of luck to ya, D3D is awesome once you get through the rough beginning.

Share this post


Link to post
Share on other sites
yes the device initilizes fine and the screen desplays. however when i go to render my bitmap it does not render. So you are saying Transformed, Lit Verticese are easier to do 2d with then the sprites. So basically I can render a rectangle 2d rectangle for the background and then texture it with the background image.

Ok I looked up and found ID3DXSprite. However, I can't even find that object in Direct3D.

[Edited by - TheNobleOne on December 4, 2004 6:03:25 AM]

Share this post


Link to post
Share on other sites
There are some good examples in Tom's Book "Managed DirectX KickStart" that go through this, it has examples using sprites in both d3d and direct draw. The other option is to look at current projects and read and understand how they did it in code.

I found an older project call MCDX on sourceforge that has some really good code inside that may help.

Share this post


Link to post
Share on other sites
So you know: Microsoft.Direct3D.Sprite is the managed equivalent of I3DXSprite.

Have you set up your projection matrix? Take a look at Matrix.OrthoHL/RH to get a good projection matrix. I have not done 2d, but you may have to set up a view matrix as well.

Share this post


Link to post
Share on other sites
Two things, one which may help and one which may not:

First, I've built a 2D engine in D3D and C#. Therefore, I have some experience in the area and may be able to help you out. If you're interested, I'd rather do it over some chat program, just so save us the effort of a lot of posting. You can reach me on AIM: ArmySarj or ShavenMcJagger.

The second thing is if you're looking to just make a game, or learn how game engines work, I could give you a beta version of my engine and tutorials for it. I'm planning on having my first big release within a few weeks here. Again, if you're interested, contact me on one of the handles above,

--Vic--

Edited by Coder: Email address spam protection engaged [smile]. Take care Vic, never write your email address in public (Or is it being bombarded by spam already?)

[Edited by - Roof Top Pew Wee on December 6, 2004 7:08:50 PM]

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