0 Replies - 1449 Views - Last Post: 24 November 2006 - 12:22 AM Rate Topic: -----

#1 Samuraijack  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 23-November 06

Manged Directx Problem.

Posted 24 November 2006 - 12:22 AM

Hey guys,
I am starting to learn Manged directx in C#,here is my problem i was doing the second tutorial in the samples and when i complied it it worked fine but the triangle didn't show,i compared my code by the tutorial code but there was no diffrence and as i am a total noob in directx i couldn't figure it out.
here is my code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Graphics
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		Device dev = null;
		VertexBuffer vb = null;
		VertexBuffer vertexBuffer = null;

		public Form1()
		{
			InitializeComponent();
			this.ClientSize = new Size(800,600);
			this.Text = "My First Directx Window";
		}

		public bool IntializeGraphics()
		{
			try
			{
				PresentParameters pParam = new PresentParameters();
				pParam.Windowed = true;
				pParam.SwapEffect = SwapEffect.Discard;

				dev = new Device(0,DeviceType.Hardware,this,CreateFlags.HardwareVertexProcessing,pParam);
				this.OnCreateDevice(dev,null);

				return true;
			}
			catch(DirectXException)
			{
				return false;
			}
		}

		public void OnCreateDevice(object sender,EventArgs e)
		{
			Device d = (Device)sender;

			vertexBuffer = new VertexBuffer(typeof(CustomVertex.TransformedColored),3,d,0,CustomVertex.TransformedColored.Format,Pool.Default);
			vertexBuffer.Created += new EventHandler(vertexBuffer_Created);
			vertexBuffer_Created(vertexBuffer,null);
		}

		public void vertexBuffer_Created(object sender,EventArgs e)
		{
			vb = (VertexBuffer)sender;
			GraphicsStream gs = vb.Lock(0,0,0);
			CustomVertex.TransformedColored[] verts = new CustomVertex.TransformedColored[3];

			verts[0].X = 150; verts[0].Y = 50; verts[0].Z = 0.5f; 
			verts[0].Color = System.Drawing.Color.Red.ToArgb();

			verts[1].X = 50; verts[1].Y = 250; verts[1].Z = 0.5f; 
			verts[1].Color = System.Drawing.Color.Blue.ToArgb();

			verts[2].X = 250; verts[2].Y = 250; verts[2].Z = 0.5f; 
			verts[2].Color = System.Drawing.Color.Violet.ToArgb();

			gs.Write(verts);
			vb.Unlock();
		}
	
		private void Render()
		{
			if (dev == null)
				return;
			
			dev.Clear(ClearFlags.Target,System.Drawing.Color.Black,1.0f,0);
			dev.BeginScene();

			dev.SetStreamSource(0,vertexBuffer,0);
			dev.VertexFormat = CustomVertex.TransformedColored.Format;
			dev.DrawPrimitives(PrimitiveType.TriangleList,0,1);

			dev.EndScene();
			dev.Present();
		}

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

		

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			using (Form1 frm = new Form1())
			{
				if(!frm.IntializeGraphics())
				{
					MessageBox.Show("This application will now close =)");
					return;
				}
			
				frm.Show();
				while(frm.Created)
				{
					frm.Render();
					Application.DoEvents();
				}
			}
//			Application.Run(new Form1());
		}
	}
}


and anther thing does any one here know if there is a video tutorial for Mdirectx.

thnx........ :)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1