開発環境 Microsoft Visual C++ 2010 Express (SP1)
実行環境 Microsoft Windows XP Home Edition (SP3)
プロジェクトの種類 空の CLR プロジェクト
プロジェクト名 ClrD3D

参考

ClrD3D.cpp
/*
プロジェクトへの参照の追加(参照タブ)
C:\WINDOWS\Microsoft.NET\DirectX for Managed Code\1.0.2902.0
Microsoft.DirectX.dll
Microsoft.DirectX.Direct3D.dll
Microsoft.DirectX.Direct3DX.dll
*/
#pragma comment(linker, "/subsystem:windows /entry:main")
 
// アセンブリ
#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
 
// 名前空間
using namespace System;
using namespace System::Drawing;
using namespace System::Threading;
using namespace System::Windows::Forms;
using namespace Microsoft::DirectX;
using namespace Microsoft::DirectX::Direct3D;
 
#define RGB(r,g,b) (((r)<<16) | ((g)<<8) | (b))
 
ref class MainForm : Form
{
private:
	Device^ dev;
	PresentParameters^ pp;
	VertexBuffer^ vb;
	Direct3D::Font^ font;
	int nSec;
	int nFps;
	int nFrame;
public:
	MainForm()
	{
		MinimumSize = Drawing::Size(160, 120);
		MaximizeBox = false;
		ClientSize = Drawing::Size(640, 480);
		Text = "ClrD3D";
	}
 
	bool DXInitialize()
	{
		try {
			pp = gcnew PresentParameters;
			pp->Windowed = true;
			pp->SwapEffect = SwapEffect::Discard;
			dev = gcnew Device(0, DeviceType::Hardware, this,
				CreateFlags::HardwareVertexProcessing, pp);
			CreateVertex();
			CreateFont();
			return true;
		}
		catch (Exception^) {
			return false;
		}
	}
 
	void Render()
	{
		if (dev == nullptr) return;
		if (WindowState == FormWindowState::Minimized) return;
		nFrame++;
		if (nSec != DateTime::Now.Second) {
			nSec = DateTime::Now.Second;
			nFps = nFrame;
			nFrame = 0;
		}
		// カメラの設定
		dev->Transform->View = Matrix::LookAtLH(
			Vector3(0.0f, 0.0f, -5.0f),
			Vector3(0.0f, 0.0f,  0.0f),
			Vector3(0.0f, 1.0f,  0.0f));
		dev->Transform->Projection = Matrix::PerspectiveFovLH((float)Math::PI / 4,
			(float)ClientSize.Width / ClientSize.Height, 3.0f, 15.0f);
		dev->RenderState->Lighting = false;
 
//		dev->Transform->World = Matrix::Scaling(2.0f, 0.5f, 1.0f);	// 拡大・縮小
//		dev->Transform->World = Matrix::Translation(2, -2, 5);	// 平行移動
//		dev->Transform->World = Matrix::RotationX(Environment::TickCount / 300.0f);
		dev->Transform->World =
			Matrix::Scaling(2, 1, 1) *
			Matrix::RotationY(Environment::TickCount / 300.0f) *
			Matrix::RotationX(Environment::TickCount / 400.0f) *
			Matrix::Translation(0, 0, 1);
		dev->RenderState->CullMode = Cull::None;
		dev->Clear(ClearFlags::Target, Color::Black, 1.0f, 0);
		dev->BeginScene();
 
		dev->SetStreamSource(0, vb, 0);
		dev->VertexFormat = CustomVertex::PositionColored::Format;
		dev->DrawPrimitives(PrimitiveType::TriangleList, 0, 1);
		font->DrawText(nullptr, "fps=" + nFps, 0, 0, Color::White);
 
		dev->EndScene();
		try {
			dev->Present();
		}
		catch (DeviceLostException^) {
			ResetDevice();
		}
	}
private:
	void CreateVertex()
	{
		vb = gcnew VertexBuffer(CustomVertex::PositionColored::typeid, 3, dev,
			(Usage)0, CustomVertex::PositionColored::Format, Pool::Managed);
		GraphicsStream^ gs = vb->Lock(0, 0, (LockFlags)0);
 
		// 頂点データ
		array<CustomVertex::PositionColored>^ v =
			gcnew array<CustomVertex::PositionColored>(3);
		v[0].Position = Vector3(0.0f, 2.0f, 0.0f);
		v[0].Color = Color::LightPink.ToArgb();
		v[1].Position = Vector3(1.0f, 0.0f, 0.0f);
		v[1].Color = Color::Aqua.ToArgb();
		v[2].Position = Vector3(-1.0f, 0.0f, 0.0f);
		v[2].Color = Color::Brown.ToArgb();
 
		gs->Write(v);
		vb->Unlock();
	}
 
	void CreateFont()
	{
		FontDescription fd;
		fd.Height = 20;
		fd.FaceName = "MS ゴシック";
		font = gcnew Direct3D::Font(dev, fd);
	}
 
	void ResetDevice()
	{
		int hResult;
		if (!dev->CheckCooperativeLevel(hResult)) {
			switch (hResult) {
			case ResultCode::DeviceLost:
				Thread::Sleep(10);
				break;
			case ResultCode::DeviceNotReset:
				dev->Reset(pp);
				break;
			}
		}
	}
};
 
int main()
{
	MainForm^ form = gcnew MainForm;
	if (!form->DXInitialize()) {
		MessageBox::Show("Direct3Dの初期化に失敗しました。", "ClrD3D");
		return 0;
	}
	form->Show();
	while (form->Created) {
		form->Render();
		Application::DoEvents();
	}
	return 0;
}
 
最終更新:2012年11月02日 20:02