Use DirectX with C++.Net

Started by
4 comments, last by AjitG 18 years, 7 months ago
I want to use DirectX with VC++.Net, but I can't put appropriate parameters to Device' constructor. Here is my code:
 public __gc class Form1 : public System::Windows::Forms::Form
	{	
	 static Device *device = NULL;

	 public:
		Form1(void)
		{
			InitializeComponent();
		}
		
		bool InitializeGraphics()
		{
			try
			{
			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 *dex )
			{
				return false;
			}
		}  
	}; 
Please check it for me, thanks [grin], thanks [grin], thanks [grin]
Advertisement
This isn't what you asked, but if you are going to use C++, why not use unmanaged code for the DirectX and managed code for the other stuff? I think you can do that.
This is the same problem that I have when I tried using managed DX with managed C++. I have a thread dedicated to this in the .NET forum on this site. The only thing I can really say is that somehow in C# (where the sdk tutorial code comes from), PresentParameters is automatically made into an array once it is created and so the call to create a new device won't generate a compiler error.

My guess is that maybe managed DX will be compatible with managed C++ once .net version 2 is released. However, that wouldn't really explain why Microsoft would say that managed DX supports / is supported by all managed languages in the sdk documentation if managed C++ is prone to problems. I even tried code to setup managed DirectDraw and I still get some error that managed C++ doesn't appear to be able to solve. For now, the best thing to do is either use VB.NET / C# with managed DX (assembly dlls and .net), or just C++ and unmanaged DX (headers, libs, and Win32).
Here's some code that I have tested to get Direct3D up and running; it seems that the older syntax of the VS.NET 2003 Toolkit is what was creating the problems with the PresentParameters object. Note that this code was compiled with the VC++ 2005 Express Edition, Beta 2, from the command line like this:

c:\cl /clr mdx9.cpp

Concerning the '^' that appears in the code, this is what the 2005 documentation says:
"The common language runtime maintains a separate heap on which it implements a precise, asynchronous, compacting garbage collection scheme. To work correctly, it must track all storage locations that can point into this heap at runtime. ^ provides a handle through which the garbage collector can track a reference to an object on the managed heap, thereby being able to update it whenever that object is moved.

Because regular C++ pointers (*) and references (&) cannot be tracked precisely, a handle-to object declarator is used.

Member selection through a handle (^) uses the pointer-to-member operator (->).
"

#using <mscorlib.dll>#using <System.dll>#using <System.Drawing.dll>#using <System.Windows.Forms.dll>#using <c:\managedDX9dll\Microsoft.DirectX.dll>#using <c:\managedDX9dll\Microsoft.DirectX.Direct3D.dll>using namespace System;using namespace System::Drawing;using namespace System::Windows::Forms;using namespace Microsoft::DirectX;using namespace Microsoft::DirectX::Direct3D;ref class MakeDev : public Form {	private:	Device			^device;			public:	MakeDev() {		this->Width = 800;		this->Height = 600;		this->Text = "Managed D3D in C++.net";	}		void inigfx() {		PresentParameters ^pp = gcnew PresentParameters();		pp->Windowed = true;		pp->SwapEffect = SwapEffect::Discard;		device = gcnew Device(0,DeviceType::Hardware,this,			CreateFlags::SoftwareVertexProcessing,pp);	}		void render() {		this->Show();		while(this->Created) {			device->Clear(ClearFlags::Target,Color::Blue,1.0f,0);			device->BeginScene();			device->EndScene();			device->Present();			Application::DoEvents();		}	}};int main() {	MakeDev ^md = gcnew MakeDev();	md->inigfx();	md->render();	return 0;}
I don't know much, but what I suggest is -
When you create a new project, there is an option of creating DirectX9.0 project.

It is like a wizard. Just follow the steps in the wizard, and your program will be generated ( this is similar to creating 'hello world' program using VC++ 6.0 )

Once you create this, you can see the code of how the Device is created.
I don't know much, but what I suggest is -
When you create a new project, there is an option of creating DirectX9.0 project.

It is like a wizard. Just follow the steps in the wizard, and your program will be generated ( this is similar to creating 'hello world' program using VC++ 6.0 )

Once you create this, you can see the code of how the Device is created.

This topic is closed to new replies.

Advertisement