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

参考

ClrD3D_21.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;
 
ref class MainForm : Form
{
private:
	Vector3 vecTexPos;
	Device^ dev;
	PresentParameters^ pp;
	Texture^ tex;
	Mesh^ mesh;
	Sprite^ spr;
	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";
		vecTexPos = Vector3(100, 100, 0.5f);
		KeyDown += gcnew KeyEventHandler(this, &MainForm::OnKeyDown);
	}
 
	bool DXInitialize()
	{
		try {
			pp = gcnew PresentParameters;
			pp->Windowed = true;
			pp->SwapEffect = SwapEffect::Discard;
			pp->AutoDepthStencilFormat = DepthFormat::D16;
			pp->EnableAutoDepthStencil = true;
 
			dev = gcnew Device(0, DeviceType::Hardware, this,
				CreateFlags::HardwareVertexProcessing, pp);
			CreateTexture();
			CreateMesh();
			spr = gcnew Sprite(dev);
			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(5.0f, 5.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, 5.0f, 15.0f);
		// ライトの設定
		dev->Lights[0]->Direction = Vector3::Normalize(Vector3(-1, -2, -3));
		dev->Lights[0]->Type = LightType::Directional;
		dev->Lights[0]->Diffuse = Color::FromArgb(0xff,0xff,0xff);
		dev->Lights[0]->Ambient = Color::FromArgb(0x28,0x28,0x28);
		dev->Lights[0]->Enabled = true;
		dev->Lights[0]->Update();
 
		dev->Clear(ClearFlags::Target | ClearFlags::ZBuffer, 0x7f7fff, 1.0f, 0);
		dev->BeginScene();
 
		Material mat;
		mat.AmbientColor = ColorValue(1.0f, 1.0f, 1.0f);
		mat.DiffuseColor = ColorValue(1.0f, 1.0f, 1.0f);
		dev->Material = mat;
		dev->Transform->World = Matrix::RotationY(Environment::TickCount / 4000.0f);
		mesh->DrawSubset(0);
 
		spr->Begin(SpriteFlags::None);
		spr->Draw(tex, Vector3(0,0,0), vecTexPos, Color::Transparent.ToArgb());
		spr->End();
		font->DrawText(nullptr, "fps=" + nFps + " z=" + vecTexPos.Z, 0, 0, Color::White);
		dev->EndScene();
		try {
			dev->Present();
		}
		catch (DeviceLostException^) {
			ResetDevice();
		}
	}
private:
	void CreateTexture()
	{
		String^ srcFile = "C:\\tmp\\hoge.bmp";	// ペイントで64x64くらいのを作る
		tex = TextureLoader::FromFile(dev, srcFile);
	}
 
	void CreateMesh()
	{
		mesh = Mesh::Box(dev, 2, 2, 2);
	}
 
	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;
			}
		}
	}
 
	void OnKeyDown(Object^ sender, KeyEventArgs^ e)
	{
		switch (e->KeyCode) {
		case Keys::Up:
			if (e->Shift) {
				vecTexPos.Z = Math::Min(vecTexPos.Z + 0.05f, 1.0f);
			} else {
				vecTexPos.Y -= 5;
			}
			break;
		case Keys::Down:
			if (e->Shift) {
				vecTexPos.Z = Math::Max(vecTexPos.Z - 0.05f, 0.0f);
			} else {
				vecTexPos.Y += 5;
			}
			break;
		case Keys::Left:
			vecTexPos.X -= 5;
			break;
		case Keys::Right:
			vecTexPos.X += 5;
			break;
		case Keys::Escape:
			Close();
			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月04日 09:27