Doing 2D with D3D and C#

Started by
5 comments, last by Roof Top Pew Wee 19 years, 4 months ago
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.");
			}
		}
	}
}
My JournalComputer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter. (Eric Raymond)"C makes it easy to shoot yourself in the foot. C++ makes itharder, but when you do, it blows away your whole leg."-- Bjarne Stroustrup
Advertisement
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.
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]
My JournalComputer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter. (Eric Raymond)"C makes it easy to shoot yourself in the foot. C++ makes itharder, but when you do, it blows away your whole leg."-- Bjarne Stroustrup
Can someone help me out to learn how to use I3DXSprite in C# :S
My JournalComputer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter. (Eric Raymond)"C makes it easy to shoot yourself in the foot. C++ makes itharder, but when you do, it blows away your whole leg."-- Bjarne Stroustrup
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.
Mykre - BlogVirtual Realm :- XNA News and Resources from Down Under** For those Interested in an Australian XNA User Group Contact me though my site.
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.
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]

This topic is closed to new replies.

Advertisement