Archive for 7月 28th, 2012

  1. Hello, DirectX(C#) World!

    Posted on 7月 28th, 2012 by cx20

    Win32 DirectX(C#)

    DirectX はマイクロソフトが Wnidows 用に開発したマルチメディア処理用 API である。
    .NET Framework 用の DirectX ライブラリとしては、DirectX SDK より入手可能な Managed DirectX がある。
    以下は C# における Managed DirectX アプリケーション の例となっている。

    ソースコード

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    using Microsoft.DirectX;
    using Microsoft.DirectX.Direct3D;
     
    public class HelloForm : Form
    {
        private Device device_;
        private PresentParameters presentParam_;
        private Microsoft.DirectX.Direct3D.Font font_;
     
        public HelloForm()
        {
            this.Size = new Size(640, 480);
            this.Text = "Hello, World!";
        }
     
        public bool InitD3D()
        {
            presentParam_ = new PresentParameters();
     
            presentParam_.Windowed = true;
            presentParam_.SwapEffect = SwapEffect.Discard;
     
            device_ = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParam_);
     
            InitFont();
     
            return true;
        }
     
        private void InitFont()
        {
            FontDescription fd = new FontDescription();
     
            fd.Height = 16;
            fd.FaceName = "MS ゴシック";
     
            font_ = new Microsoft.DirectX.Direct3D.Font(device_, fd);
        }
     
        public void Render()
        {
            if (device_ == null)
            {
                return;
            }
     
            device_.Clear(ClearFlags.Target, Color.Blue, 1.0f, 0);
            device_.BeginScene();
     
            font_.DrawText(null, "Hello, DirectX(C#) World!", 10, 10, Color.White);
     
            device_.EndScene();
            device_.Present();
        }
     
        [STAThread]
        public static void Main()
        {
            using (HelloForm form = new HelloForm())
            {
                form.InitD3D();
                form.Show();
     
                while (form.Created)
                {
                    form.Render();
                    Application.DoEvents();
                }
            }
        }
    }

    コンパイル方法

    C:¥> csc ^
        /target:winexe ^
        /platform:x86 ^
        /r:"C:WindowsMicrosoft.NETDirectX for Managed Code1.0.2902.0Microsoft.DirectX.dll" ^
        /r:"C:WindowsMicrosoft.NETDirectX for Managed Code1.0.2902.0Microsoft.DirectX.Direct3D.dll" ^
        /r:"C:WindowsMicrosoft.NETDirectX for Managed Code1.0.2911.0Microsoft.DirectX.Direct3DX.dll" ^
        Hello.cs

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, DirectX(C#) World!                 |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+