Archive for the ‘DirectX’ Category

  1. Hello, DirectX(MSIL) World!

    Posted on 7月 31st, 2012 by cx20

    Win32 DirectX(MSIL)

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

    ソースコード

     
    //  Microsoft (R) .NET Framework IL Disassembler.  Version 2.0.50727.42
    //  Copyright (c) Microsoft Corporation.  All rights reserved.
     
    // Metadata version: v2.0.50727
    .assembly extern System.Windows.Forms
    {
      .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .zV.4..
      .ver 2:0:0:0
    }
    .assembly extern Microsoft.DirectX.Direct3D
    {
      .publickeytoken = (31 BF 38 56 AD 36 4E 35 )                         // 1.8V.6N5
      .ver 1:0:2902:0
    }
    .assembly extern Microsoft.DirectX.Direct3DX
    {
      .publickeytoken = (31 BF 38 56 AD 36 4E 35 )                         // 1.8V.6N5
      .ver 1:0:2911:0
    }
    .assembly extern mscorlib
    {
      .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .zV.4..
      .ver 2:0:0:0
    }
    .assembly extern System.Drawing
    {
      .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )                         // .?_....:
      .ver 2:0:0:0
    }
    .assembly extern System.Windows.Forms as System.Windows.Forms_5
    {
      .publickeytoken = (B7 7A 5C 56 19 34 E0 89 )                         // .zV.4..
      .hash = (46 CF F1 2A B1 D2 B5 94 71 B8 95 F2 3D 65 06 C2   // F..*....q...=e..
               33 D3 CF 05 )                                     // 3...
      .ver 1:0:5000:0
    }
    .assembly extern System.Drawing as System.Drawing_6
    {
      .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )                         // .?_....:
      .hash = (01 F2 8E 4D 2E 2C 74 F0 83 0A 38 19 0C D6 04 59   // ...M.,t...8....Y
               7A 24 81 23 )                                     // z$.#
      .ver 1:0:5000:0
    }
    .assembly Hello
    {
      .custom instance void [mscorlib]System.Runtime.CompilerServices.CompilationRelaxationsAttribute::.ctor(int32) = ( 01 00 08 00 00 00 00 00 ) 
      .custom instance void [mscorlib]System.Runtime.CompilerServices.RuntimeCompatibilityAttribute::.ctor() = ( 01 00 01 00 54 02 16 57 72 61 70 4E 6F 6E 45 78   // ....T..WrapNonEx
                                                                                                                 63 65 70 74 69 6F 6E 54 68 72 6F 77 73 01 )       // ceptionThrows.
      .hash algorithm 0x00008004
      .ver 0:0:0:0
    }
    .module Hello.exe
    // MVID: {5CADCB46-A918-449C-8A1F-169311ED3D80}
    .imagebase 0x00400000
    .file alignment 0x00000200
    .stackreserve 0x00100000
    .subsystem 0x0002       // WINDOWS_GUI
    .corflags 0x00000003    //  ILONLY 32BITREQUIRED
    // Image base: 0x02FA0000
     
     
    // =============== CLASS MEMBERS DECLARATION ===================
     
    .class public auto ansi beforefieldinit HelloForm
           extends [System.Windows.Forms]System.Windows.Forms.Form
    {
      .field private class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device device_
      .field private class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters presentParam_
      .field private class [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.Font font_
      .method public hidebysig specialname rtspecialname 
              instance void  .ctor() cil managed
      {
        // コード サイズ       44 (0x2c)
        .maxstack  8
        IL_0000:  ldarg.0
        IL_0001:  call       instance void [System.Windows.Forms]System.Windows.Forms.Form::.ctor()
        IL_0006:  nop
        IL_0007:  nop
        IL_0008:  ldarg.0
        IL_0009:  ldc.i4     0x280
        IL_000e:  ldc.i4     0x1e0
        IL_0013:  newobj     instance void [System.Drawing]System.Drawing.Size::.ctor(int32,
                                                                                      int32)
        IL_0018:  call       instance void [System.Windows.Forms]System.Windows.Forms.Form::set_Size(valuetype [System.Drawing]System.Drawing.Size)
        IL_001d:  nop
        IL_001e:  ldarg.0
        IL_001f:  ldstr      "Hello, World!"
        IL_0024:  callvirt   instance void [System.Windows.Forms]System.Windows.Forms.Control::set_Text(string)
        IL_0029:  nop
        IL_002a:  nop
        IL_002b:  ret
      } // end of method HelloForm::.ctor
     
      .method public hidebysig instance bool 
              InitD3D() cil managed
      {
        // コード サイズ       84 (0x54)
        .maxstack  8
        .locals init (bool V_0,
                 class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters[] V_1)
        IL_0000:  nop
        IL_0001:  ldarg.0
        IL_0002:  newobj     instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters::.ctor()
        IL_0007:  stfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters HelloForm::presentParam_
        IL_000c:  ldarg.0
        IL_000d:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters HelloForm::presentParam_
        IL_0012:  ldc.i4.1
        IL_0013:  callvirt   instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters::set_Windowed(bool)
        IL_0018:  nop
        IL_0019:  ldarg.0
        IL_001a:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters HelloForm::presentParam_
        IL_001f:  ldc.i4.1
        IL_0020:  callvirt   instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters::set_SwapEffect(valuetype [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.SwapEffect)
        IL_0025:  nop
        IL_0026:  ldarg.0
        IL_0027:  ldc.i4.0
        IL_0028:  ldc.i4.1
        IL_0029:  ldarg.0
        IL_002a:  ldc.i4.s   64
        IL_002c:  ldc.i4.1
        IL_002d:  newarr     [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters
        IL_0032:  stloc.1
        IL_0033:  ldloc.1
        IL_0034:  ldc.i4.0
        IL_0035:  ldarg.0
        IL_0036:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters HelloForm::presentParam_
        IL_003b:  stelem.ref
        IL_003c:  ldloc.1
        IL_003d:  newobj     instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device::.ctor(int32,
                                                                                                                valuetype [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.DeviceType,
                                                                                                                class [System.Windows.Forms_5]System.Windows.Forms.Control,
                                                                                                                valuetype [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.CreateFlags,
                                                                                                                class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.PresentParameters[])
        IL_0042:  stfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device HelloForm::device_
        IL_0047:  ldarg.0
        IL_0048:  call       instance void HelloForm::InitFont()
        IL_004d:  nop
        IL_004e:  ldc.i4.1
        IL_004f:  stloc.0
        IL_0050:  br.s       IL_0052
     
        IL_0052:  ldloc.0
        IL_0053:  ret
      } // end of method HelloForm::InitD3D
     
      .method private hidebysig instance void 
              InitFont() cil managed
      {
        // コード サイズ       51 (0x33)
        .maxstack  4
        .locals init (valuetype [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.FontDescription V_0)
        IL_0000:  nop
        IL_0001:  ldloca.s   V_0
        IL_0003:  call       instance void [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.FontDescription::.ctor()
        IL_0008:  nop
        IL_0009:  ldloca.s   V_0
        IL_000b:  ldc.i4.s   16
        IL_000d:  call       instance void [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.FontDescription::set_Height(int32)
        IL_0012:  nop
        IL_0013:  ldloca.s   V_0
        IL_0015:  ldstr      bytearray (2D FF 33 FF 20 00 B4 30 B7 30 C3 30 AF 30 )       // -.3. ..0.0.0.0
        IL_001a:  call       instance void [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.FontDescription::set_FaceName(string)
        IL_001f:  nop
        IL_0020:  ldarg.0
        IL_0021:  ldarg.0
        IL_0022:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device HelloForm::device_
        IL_0027:  ldloc.0
        IL_0028:  newobj     instance void [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.Font::.ctor(class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device,
                                                                                                               valuetype [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.FontDescription)
        IL_002d:  stfld      class [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.Font HelloForm::font_
        IL_0032:  ret
      } // end of method HelloForm::InitFont
     
      .method public hidebysig instance void 
              Render() cil managed
      {
        // コード サイズ       111 (0x6f)
        .maxstack  6
        .locals init (bool V_0)
        IL_0000:  nop
        IL_0001:  ldarg.0
        IL_0002:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device HelloForm::device_
        IL_0007:  ldnull
        IL_0008:  call       bool [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device::op_Equality(class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device,
                                                                                                             class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device)
        IL_000d:  ldc.i4.0
        IL_000e:  ceq
        IL_0010:  stloc.0
        IL_0011:  ldloc.0
        IL_0012:  brtrue.s   IL_0017
     
        IL_0014:  nop
        IL_0015:  br.s       IL_006e
     
        IL_0017:  ldarg.0
        IL_0018:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device HelloForm::device_
        IL_001d:  ldc.i4.1
        IL_001e:  call       valuetype [System.Drawing]System.Drawing.Color [System.Drawing]System.Drawing.Color::get_Blue()
        IL_0023:  ldc.r4     1.
        IL_0028:  ldc.i4.0
        IL_0029:  callvirt   instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device::Clear(valuetype [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.ClearFlags,
                                                                                                                valuetype [System.Drawing_6]System.Drawing.Color,
                                                                                                                float32,
                                                                                                                int32)
        IL_002e:  nop
        IL_002f:  ldarg.0
        IL_0030:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device HelloForm::device_
        IL_0035:  callvirt   instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device::BeginScene()
        IL_003a:  nop
        IL_003b:  ldarg.0
        IL_003c:  ldfld      class [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.Font HelloForm::font_
        IL_0041:  ldnull
        IL_0042:  ldstr      "Hello, DirectX(MSIL) World!"
        IL_0047:  ldc.i4.s   10
        IL_0049:  ldc.i4.s   10
        IL_004b:  call       valuetype [System.Drawing]System.Drawing.Color [System.Drawing]System.Drawing.Color::get_White()
        IL_0050:  callvirt   instance int32 [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.Font::DrawText(class [Microsoft.DirectX.Direct3DX]Microsoft.DirectX.Direct3D.Sprite,
                                                                                                                   string,
                                                                                                                   int32,
                                                                                                                   int32,
                                                                                                                   valuetype [System.Drawing_6]System.Drawing.Color)
        IL_0055:  pop
        IL_0056:  ldarg.0
        IL_0057:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device HelloForm::device_
        IL_005c:  callvirt   instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device::EndScene()
        IL_0061:  nop
        IL_0062:  ldarg.0
        IL_0063:  ldfld      class [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device HelloForm::device_
        IL_0068:  callvirt   instance void [Microsoft.DirectX.Direct3D]Microsoft.DirectX.Direct3D.Device::Present()
        IL_006d:  nop
        IL_006e:  ret
      } // end of method HelloForm::Render
     
      .method public hidebysig static void  Main() cil managed
      {
        .entrypoint
        .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 ) 
        // コード サイズ       70 (0x46)
        .maxstack  2
        .locals init (class HelloForm V_0,
                 bool V_1)
        IL_0000:  nop
        IL_0001:  newobj     instance void HelloForm::.ctor()
        IL_0006:  stloc.0
        .try
        {
          IL_0007:  nop
          IL_0008:  ldloc.0
          IL_0009:  callvirt   instance bool HelloForm::InitD3D()
          IL_000e:  pop
          IL_000f:  ldloc.0
          IL_0010:  callvirt   instance void [System.Windows.Forms]System.Windows.Forms.Control::Show()
          IL_0015:  nop
          IL_0016:  br.s       IL_0027
     
          IL_0018:  nop
          IL_0019:  ldloc.0
          IL_001a:  callvirt   instance void HelloForm::Render()
          IL_001f:  nop
          IL_0020:  call       void [System.Windows.Forms]System.Windows.Forms.Application::DoEvents()
          IL_0025:  nop
          IL_0026:  nop
          IL_0027:  ldloc.0
          IL_0028:  callvirt   instance bool [System.Windows.Forms]System.Windows.Forms.Control::get_Created()
          IL_002d:  stloc.1
          IL_002e:  ldloc.1
          IL_002f:  brtrue.s   IL_0018
     
          IL_0031:  nop
          IL_0032:  leave.s    IL_0044
     
        }  // end .try
        finally
        {
          IL_0034:  ldloc.0
          IL_0035:  ldnull
          IL_0036:  ceq
          IL_0038:  stloc.1
          IL_0039:  ldloc.1
          IL_003a:  brtrue.s   IL_0043
     
          IL_003c:  ldloc.0
          IL_003d:  callvirt   instance void [mscorlib]System.IDisposable::Dispose()
          IL_0042:  nop
          IL_0043:  endfinally
        }  // end handler
        IL_0044:  nop
        IL_0045:  ret
      } // end of method HelloForm::Main
     
    } // end of class HelloForm
     
     
    // =============================================================
     
    // *********** 逆アセンブルが完了しました ***********************
    // 警告: Win32 リソース ファイル Hello.res を作成しました。

    上記コードは、以下の C# の実行ファイルを ildasm.exe で逆アセンブルしたものである。

    ソースコード

    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(MSIL) 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:¥> ilasm Hello.il

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, DirectX(MSIL) World!               |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  2. Hello, DirectX(C++/CLI) World!

    Posted on 7月 30th, 2012 by cx20

    Win32 DirectX(C++/CLI)

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

    ソースコード

    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    #using <Microsoft.DirectX.dll>
    #using <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;
     
    public ref class HelloForm : public Form
    {
    public:
        HelloForm()
        {
            this->Size = System::Drawing::Size( 640, 480 );
            this->Text = "Hello, World!";
        }
        bool InitD3D()
        {
            presentParam_ = gcnew PresentParameters();
            presentParam_->Windowed = true;
            presentParam_->SwapEffect = SwapEffect::Discard;
            device_ = gcnew Device(0, DeviceType::Hardware, this, CreateFlags::HardwareVertexProcessing, presentParam_);
     
            InitFont();
     
            return true;
        }
        void InitFont()
        {
            FontDescription fd;
     
            fd.Height = 16;
            fd.FaceName = "MS ゴシック";
     
            font_ = gcnew Microsoft::DirectX::Direct3D::Font(device_, fd);
        }
        void Render()
        {
            if (device_ == nullptr)
            {
                return;
            }
     
            device_->Clear(ClearFlags::Target, Color::Blue, 1.0f, 0);
            device_->BeginScene();
     
            font_->DrawText(nullptr, "Hello, DirectX(C++/CLI) World!", 10, 10, Color::White);
     
            device_->EndScene();
            device_->Present();
        }
    private:
        Device^ device_;
        PresentParameters^ presentParam_;
        Microsoft::DirectX::Direct3D::Font^ font_;
    };
     
    int main( array<System::String^>^ args )
    {
        HelloForm^ form = gcnew HelloForm();
     
        form->InitD3D();
        form->Show();
     
        while (form->Created)
        {
            form->Render();
            Application::DoEvents();
        }
     
        return 0;
    }

    コンパイル方法

    C:¥> cl Hello.cpp /clr ^
         /AI "C:WindowsMicrosoft.NETDirectX for Managed Code1.0.2911.0" ^
         /AI "C:WindowsMicrosoft.NETDirectX for Managed Code1.0.2902.0" ^
         /FU "Microsoft.DirectX.dll" ^
         /FU "Microsoft.DirectX.Direct3D.dll" ^
         /FU "Microsoft.DirectX.Direct3DX.dll" ^
         /link /SUBSYSTEM:WINDOWS /ENTRY:main

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, DirectX(C++/CLI) World!            |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  3. Hello, DirectX(VB.NET) World!

    Posted on 7月 29th, 2012 by cx20

    Win32 DirectX(VB.NET)

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

    ソースコード

    Imports System
    Imports System.Drawing
    Imports System.Windows.Forms
    Imports Microsoft.DirectX
    Imports Microsoft.DirectX.Direct3D
     
    Public Class HelloForm
        Inherits Form
     
        Private device_ As Device
        Private presentParam_ As PresentParameters
        Private font_ As Microsoft.DirectX.Direct3D.Font
     
        Public Sub New()
            MyBase.Size = New Size(640, 480)
            Me.Text = "Hello, World!"
        End Sub
     
        Public Function InitD3D() As Boolean
            Me.presentParam_ = New PresentParameters()
            Me.presentParam_.Windowed = True
            Me.presentParam_.SwapEffect = SwapEffect.Discard
            Me.device_ = New Device(0, DeviceType.Hardware, Me, CreateFlags.HardwareVertexProcessing, Me.presentParam_)
            Me.InitFont()
            Return True
        End Function
     
        Private Sub InitFont()
            Dim description As FontDescription = New FontDescription()
            description.Height = 16
            description.FaceName = "MS ゴシック"
            Me.font_ = New Microsoft.DirectX.Direct3D.Font(Me.device_, description)
        End Sub
     
        Public Sub Render()
            If Not Me.device_ Is Nothing Then
                Me.device_.Clear(ClearFlags.Target, Color.Blue, 1.0F, 0)
                Me.device_.BeginScene()
     
                Me.font_.DrawText(Nothing, "Hello, DirectX(VB.NET) World!", New Point(10, 10), Color.White)
                Me.device_.EndScene()
                Me.device_.Present()
            End If
        End Sub
     
        Shared Sub Main()
            Using helloForm As New HelloForm()
                helloForm.InitD3D()
                helloForm.Show()
                While helloForm.Created
                    helloForm.Render()
                    Application.DoEvents()
                End While
            End Using
        End Sub
    End Class

    コンパイル方法

    C:¥> vbc ^
        /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.vb

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, DirectX(VB.NET) World!             |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  4. 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!                 |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  5. Hello, DirectX(MASM) World!

    Posted on 7月 27th, 2012 by cx20

    Win32 DirectX(MASM)

    DirectX はマイクロソフトが Wnidows 用に開発したマルチメディア処理用 API である。
    以下は MSAM における DirectX アプリケーション の例となっている。

    ソースコード

    ; Listing generated by Microsoft (R) Optimizing Compiler Version 14.00.50727.762 
     
            TITLE   hello.c
            .686P
            .XMM
            include listing.inc
            .model  flat
     
    INCLUDELIB LIBCMT
    INCLUDELIB OLDNAMES
     
    PUBLIC  _g_pD3D
    PUBLIC  _g_pd3dDevice
    PUBLIC  _g_pd3dFont
    PUBLIC  _g_rect
    _BSS    SEGMENT
    _g_pD3D DD      01H DUP (?)
    _g_pd3dDevice DD 01H DUP (?)
    _g_pd3dFont DD  01H DUP (?)
    _g_rect DB      010H DUP (?)
    _BSS    ENDS
    _DATA   SEGMENT
    $SG94060 DB     082H, 'l', 082H, 'r ', 083H, 'S', 083H, 'V', 083H, 'b', 083H
            DB      'N', 00H
            ORG $+2
    $SG94064 DB     'Hello, DirectX(MASM) World!', 00H
    $SG94084 DB     'Hello, DirectX(MASM) World!', 00H
    $SG94116 DB     'helloWindow', 00H
    $SG94118 DB     'Hello, World!', 00H
    _DATA   ENDS
    PUBLIC  _InitD3D
    EXTRN   _Direct3DCreate9@4:PROC
    ; Function compile flags: /Odtp
    _TEXT   SEGMENT
    _d3dpp$ = -60                                           ; size = 56
    _hr$ = -4                                               ; size = 4
    _hWnd$ = 8                                              ; size = 4
    _InitD3D PROC
    ; File hello.c
    ; Line 17
            push    ebp
            mov     ebp, esp
            sub     esp, 60                                 ; 0000003cH
    ; Line 20
            push    32                                      ; 00000020H
            call    _Direct3DCreate9@4
            mov     DWORD PTR _g_pD3D, eax
    ; Line 21
            cmp     DWORD PTR _g_pD3D, 0
            jne     SHORT $LN2@InitD3D
    ; Line 23
            mov     eax, -2147467259                        ; 80004005H
            jmp     $LN3@InitD3D
    $LN2@InitD3D:
    ; Line 26
            mov     DWORD PTR _d3dpp$[ebp], 0
    ; Line 27
            mov     DWORD PTR _d3dpp$[ebp+4], 0
    ; Line 28
            mov     DWORD PTR _d3dpp$[ebp+8], 0
    ; Line 29
            mov     DWORD PTR _d3dpp$[ebp+12], 0
    ; Line 30
            mov     DWORD PTR _d3dpp$[ebp+16], 0
    ; Line 31
            mov     DWORD PTR _d3dpp$[ebp+20], 0
    ; Line 32
            mov     DWORD PTR _d3dpp$[ebp+24], 1
    ; Line 33
            mov     DWORD PTR _d3dpp$[ebp+28], 0
    ; Line 34
            mov     DWORD PTR _d3dpp$[ebp+32], 1
    ; Line 35
            mov     DWORD PTR _d3dpp$[ebp+36], 0
    ; Line 36
            mov     DWORD PTR _d3dpp$[ebp+40], 0
    ; Line 37
            mov     DWORD PTR _d3dpp$[ebp+44], 0
    ; Line 38
            mov     DWORD PTR _d3dpp$[ebp+48], 0
    ; Line 39
            mov     DWORD PTR _d3dpp$[ebp+52], 0
    ; Line 49
            push    OFFSET _g_pd3dDevice
            lea     eax, DWORD PTR _d3dpp$[ebp]
            push    eax
            push    32                                      ; 00000020H
            mov     ecx, DWORD PTR _hWnd$[ebp]
            push    ecx
            push    1
            push    0
            mov     edx, DWORD PTR _g_pD3D
            push    edx
            mov     eax, DWORD PTR _g_pD3D
            mov     ecx, DWORD PTR [eax]
            mov     edx, DWORD PTR [ecx+64]
            call    edx
            mov     DWORD PTR _hr$[ebp], eax
    ; Line 51
            cmp     DWORD PTR _hr$[ebp], 0
            jge     SHORT $LN1@InitD3D
    ; Line 53
            mov     eax, -2147467259                        ; 80004005H
            jmp     SHORT $LN3@InitD3D
    $LN1@InitD3D:
    ; Line 56
            xor     eax, eax
    $LN3@InitD3D:
    ; Line 57
            mov     esp, ebp
            pop     ebp
            ret     0
    _InitD3D ENDP
    _TEXT   ENDS
    PUBLIC  _Cleanup
    PUBLIC  __$ArrayPad$
    PUBLIC  _InitFont
    EXTRN   _D3DXCreateFontIndirectA@12:PROC
    EXTRN   __imp__lstrcpyA@8:PROC
    EXTRN   ___security_cookie:DWORD
    EXTRN   @__security_check_cookie@4:PROC
    ; Function compile flags: /Odtp
    _TEXT   SEGMENT
    _hr$ = -64                                              ; size = 4
    _lf$ = -60                                              ; size = 56
    __$ArrayPad$ = -4                                       ; size = 4
    _InitFont PROC
    ; Line 60
            push    ebp
            mov     ebp, esp
            sub     esp, 64                                 ; 00000040H
            mov     eax, DWORD PTR ___security_cookie
            xor     eax, ebp
            mov     DWORD PTR __$ArrayPad$[ebp], eax
    ; Line 64
            mov     DWORD PTR _lf$[ebp], 16                 ; 00000010H
    ; Line 65
            mov     DWORD PTR _lf$[ebp+4], 0
    ; Line 66
            mov     DWORD PTR _lf$[ebp+8], 0
    ; Line 67
            mov     DWORD PTR _lf$[ebp+12], 1
    ; Line 68
            mov     DWORD PTR _lf$[ebp+16], 0
    ; Line 69
            mov     BYTE PTR _lf$[ebp+20], 128              ; 00000080H
    ; Line 70
            mov     BYTE PTR _lf$[ebp+21], 7
    ; Line 71
            mov     BYTE PTR _lf$[ebp+22], 2
    ; Line 72
            mov     BYTE PTR _lf$[ebp+23], 49               ; 00000031H
    ; Line 73
            push    OFFSET $SG94060
            lea     eax, DWORD PTR _lf$[ebp+24]
            push    eax
            call    DWORD PTR __imp__lstrcpyA@8
    ; Line 75
            push    OFFSET _g_pd3dFont
            lea     ecx, DWORD PTR _lf$[ebp]
            push    ecx
            mov     edx, DWORD PTR _g_pd3dDevice
            push    edx
            call    _D3DXCreateFontIndirectA@12
            mov     DWORD PTR _hr$[ebp], eax
    ; Line 76
            cmp     DWORD PTR _hr$[ebp], 0
            jge     SHORT $LN2@InitFont
    ; Line 78
            call    _Cleanup
    ; Line 79
            mov     eax, DWORD PTR _hr$[ebp]
            jmp     SHORT $LN3@InitFont
    $LN2@InitFont:
    ; Line 90
            push    -1
            push    1056                                    ; 00000420H
            push    OFFSET _g_rect
            push    -1
            push    OFFSET $SG94064
            push    0
            mov     eax, DWORD PTR _g_pd3dFont
            push    eax
            mov     ecx, DWORD PTR _g_pd3dFont
            mov     edx, DWORD PTR [ecx]
            mov     eax, DWORD PTR [edx+56]
            call    eax
            mov     DWORD PTR _hr$[ebp], eax
    ; Line 92
            cmp     DWORD PTR _hr$[ebp], 0
            jge     SHORT $LN1@InitFont
    ; Line 94
            call    _Cleanup
    ; Line 95
            mov     eax, DWORD PTR _hr$[ebp]
            jmp     SHORT $LN3@InitFont
    $LN1@InitFont:
    ; Line 98
            mov     eax, DWORD PTR _hr$[ebp]
    $LN3@InitFont:
    ; Line 99
            mov     ecx, DWORD PTR __$ArrayPad$[ebp]
            xor     ecx, ebp
            call    @__security_check_cookie@4
            mov     esp, ebp
            pop     ebp
            ret     0
    _InitFont ENDP
    ; Function compile flags: /Odtp
    _Cleanup PROC
    ; Line 102
            push    ebp
            mov     ebp, esp
    ; Line 103
            cmp     DWORD PTR _g_pd3dFont, 0
            je      SHORT $LN3@Cleanup
    ; Line 105
            mov     eax, DWORD PTR _g_pd3dFont
            push    eax
            mov     ecx, DWORD PTR _g_pd3dFont
            mov     edx, DWORD PTR [ecx]
            mov     eax, DWORD PTR [edx+8]
            call    eax
    $LN3@Cleanup:
    ; Line 108
            cmp     DWORD PTR _g_pd3dDevice, 0
            je      SHORT $LN2@Cleanup
    ; Line 110
            mov     ecx, DWORD PTR _g_pd3dDevice
            push    ecx
            mov     edx, DWORD PTR _g_pd3dDevice
            mov     eax, DWORD PTR [edx]
            mov     ecx, DWORD PTR [eax+8]
            call    ecx
    $LN2@Cleanup:
    ; Line 113
            cmp     DWORD PTR _g_pD3D, 0
            je      SHORT $LN4@Cleanup
    ; Line 115
            mov     edx, DWORD PTR _g_pD3D
            push    edx
            mov     eax, DWORD PTR _g_pD3D
            mov     ecx, DWORD PTR [eax]
            mov     edx, DWORD PTR [ecx+8]
            call    edx
    $LN4@Cleanup:
    ; Line 117
            pop     ebp
            ret     0
    _Cleanup ENDP
    _TEXT   ENDS
    PUBLIC  __real@3f800000
    PUBLIC  _Render
    EXTRN   __fltused:DWORD
    ;       COMDAT __real@3f800000
    CONST   SEGMENT
    __real@3f800000 DD 03f800000r                   ; 1
    ; Function compile flags: /Odtp
    CONST   ENDS
    _TEXT   SEGMENT
    _Render PROC
    ; Line 120
            push    ebp
            mov     ebp, esp
    ; Line 121
            cmp     DWORD PTR _g_pd3dDevice, 0
            jne     SHORT $LN3@Render
    ; Line 123
            jmp     $LN4@Render
    $LN3@Render:
    ; Line 126
            cmp     DWORD PTR _g_pd3dFont, 0
            jne     SHORT $LN2@Render
    ; Line 128
            jmp     $LN4@Render
    $LN2@Render:
    ; Line 139
            push    0
            push    ecx
            fld1
            fstp    DWORD PTR [esp]
            push    -16776961                               ; ff0000ffH
            push    1
            push    0
            push    0
            mov     eax, DWORD PTR _g_pd3dDevice
            push    eax
            mov     ecx, DWORD PTR _g_pd3dDevice
            mov     edx, DWORD PTR [ecx]
            mov     eax, DWORD PTR [edx+172]
            call    eax
    ; Line 141
            mov     ecx, DWORD PTR _g_pd3dDevice
            push    ecx
            mov     edx, DWORD PTR _g_pd3dDevice
            mov     eax, DWORD PTR [edx]
            mov     ecx, DWORD PTR [eax+164]
            call    ecx
            test    eax, eax
            jl      SHORT $LN1@Render
    ; Line 151
            push    -1
            push    32                                      ; 00000020H
            push    OFFSET _g_rect
            push    -1
            push    OFFSET $SG94084
            push    0
            mov     edx, DWORD PTR _g_pd3dFont
            push    edx
            mov     eax, DWORD PTR _g_pd3dFont
            mov     ecx, DWORD PTR [eax]
            mov     edx, DWORD PTR [ecx+56]
            call    edx
    ; Line 153
            mov     eax, DWORD PTR _g_pd3dDevice
            push    eax
            mov     ecx, DWORD PTR _g_pd3dDevice
            mov     edx, DWORD PTR [ecx]
            mov     eax, DWORD PTR [edx+168]
            call    eax
    $LN1@Render:
    ; Line 156
            push    0
            push    0
            push    0
            push    0
            mov     ecx, DWORD PTR _g_pd3dDevice
            push    ecx
            mov     edx, DWORD PTR _g_pd3dDevice
            mov     eax, DWORD PTR [edx]
            mov     ecx, DWORD PTR [eax+68]
            call    ecx
    $LN4@Render:
    ; Line 157
            pop     ebp
            ret     0
    _Render ENDP
    _TEXT   ENDS
    PUBLIC  _WndProc@16
    EXTRN   __imp__DefWindowProcA@16:PROC
    EXTRN   __imp__ValidateRect@8:PROC
    EXTRN   __imp__PostQuitMessage@4:PROC
    ; Function compile flags: /Odtp
    _TEXT   SEGMENT
    tv64 = -4                                               ; size = 4
    _hWnd$ = 8                                              ; size = 4
    _message$ = 12                                          ; size = 4
    _wParam$ = 16                                           ; size = 4
    _lParam$ = 20                                           ; size = 4
    _WndProc@16 PROC
    ; Line 160
            push    ebp
            mov     ebp, esp
            push    ecx
    ; Line 161
            mov     eax, DWORD PTR _message$[ebp]
            mov     DWORD PTR tv64[ebp], eax
            cmp     DWORD PTR tv64[ebp], 2
            je      SHORT $LN2@WndProc
            cmp     DWORD PTR tv64[ebp], 15                 ; 0000000fH
            je      SHORT $LN1@WndProc
            jmp     SHORT $LN3@WndProc
    $LN2@WndProc:
    ; Line 164
            call    _Cleanup
    ; Line 165
            push    0
            call    DWORD PTR __imp__PostQuitMessage@4
    ; Line 166
            xor     eax, eax
            jmp     SHORT $LN5@WndProc
    $LN1@WndProc:
    ; Line 169
            call    _Render
    ; Line 170
            push    0
            mov     ecx, DWORD PTR _hWnd$[ebp]
            push    ecx
            call    DWORD PTR __imp__ValidateRect@8
    ; Line 171
            xor     eax, eax
            jmp     SHORT $LN5@WndProc
    $LN3@WndProc:
    ; Line 174
            mov     edx, DWORD PTR _lParam$[ebp]
            push    edx
            mov     eax, DWORD PTR _wParam$[ebp]
            push    eax
            mov     ecx, DWORD PTR _message$[ebp]
            push    ecx
            mov     edx, DWORD PTR _hWnd$[ebp]
            push    edx
            call    DWORD PTR __imp__DefWindowProcA@16
    $LN5@WndProc:
    ; Line 175
            mov     esp, ebp
            pop     ebp
            ret     16                                      ; 00000010H
    _WndProc@16 ENDP
    _TEXT   ENDS
    PUBLIC  _WinMain@16
    EXTRN   __imp__DispatchMessageA@4:PROC
    EXTRN   __imp__TranslateMessage@4:PROC
    EXTRN   __imp__GetMessageA@16:PROC
    EXTRN   __imp__UpdateWindow@4:PROC
    EXTRN   __imp__ShowWindow@8:PROC
    EXTRN   __imp__CreateWindowExA@48:PROC
    EXTRN   __imp__RegisterClassExA@4:PROC
    EXTRN   __imp__LoadCursorA@8:PROC
    EXTRN   __imp__LoadIconA@8:PROC
    ; Function compile flags: /Odtp
    _TEXT   SEGMENT
    _lpszClassName$ = -88                                   ; size = 4
    _msg$ = -84                                             ; size = 28
    _wcex$ = -56                                            ; size = 48
    _hWnd$ = -8                                             ; size = 4
    _lpszWindowName$ = -4                                   ; size = 4
    _hInstance$ = 8                                         ; size = 4
    _hPrevInstance$ = 12                                    ; size = 4
    _lpCmdLine$ = 16                                        ; size = 4
    _nCmdShow$ = 20                                         ; size = 4
    _WinMain@16 PROC
    ; Line 178
            push    ebp
            mov     ebp, esp
            sub     esp, 88                                 ; 00000058H
    ; Line 179
            mov     DWORD PTR _lpszClassName$[ebp], OFFSET $SG94116
    ; Line 180
            mov     DWORD PTR _lpszWindowName$[ebp], OFFSET $SG94118
    ; Line 185
            mov     DWORD PTR _wcex$[ebp], 48               ; 00000030H
    ; Line 186
            mov     DWORD PTR _wcex$[ebp+4], 3
    ; Line 187
            mov     DWORD PTR _wcex$[ebp+8], OFFSET _WndProc@16
    ; Line 188
            mov     DWORD PTR _wcex$[ebp+12], 0
    ; Line 189
            mov     DWORD PTR _wcex$[ebp+16], 0
    ; Line 190
            mov     eax, DWORD PTR _hInstance$[ebp]
            mov     DWORD PTR _wcex$[ebp+20], eax
    ; Line 191
            push    32512                                   ; 00007f00H
            mov     ecx, DWORD PTR _hInstance$[ebp]
            push    ecx
            call    DWORD PTR __imp__LoadIconA@8
            mov     DWORD PTR _wcex$[ebp+24], eax
    ; Line 192
            push    32512                                   ; 00007f00H
            push    0
            call    DWORD PTR __imp__LoadCursorA@8
            mov     DWORD PTR _wcex$[ebp+28], eax
    ; Line 193
            mov     DWORD PTR _wcex$[ebp+32], 6
    ; Line 194
            mov     DWORD PTR _wcex$[ebp+36], 0
    ; Line 195
            mov     edx, DWORD PTR _lpszClassName$[ebp]
            mov     DWORD PTR _wcex$[ebp+40], edx
    ; Line 196
            push    32512                                   ; 00007f00H
            mov     eax, DWORD PTR _hInstance$[ebp]
            push    eax
            call    DWORD PTR __imp__LoadIconA@8
            mov     DWORD PTR _wcex$[ebp+44], eax
    ; Line 198
            lea     ecx, DWORD PTR _wcex$[ebp]
            push    ecx
            call    DWORD PTR __imp__RegisterClassExA@4
    ; Line 205
            push    0
            mov     edx, DWORD PTR _hInstance$[ebp]
            push    edx
            push    0
            push    0
            push    480                                     ; 000001e0H
            push    640                                     ; 00000280H
            push    -2147483648                             ; 80000000H
            push    -2147483648                             ; 80000000H
            push    13565952                                ; 00cf0000H
            mov     eax, DWORD PTR _lpszWindowName$[ebp]
            push    eax
            mov     ecx, DWORD PTR _lpszClassName$[ebp]
            push    ecx
            push    0
            call    DWORD PTR __imp__CreateWindowExA@48
            mov     DWORD PTR _hWnd$[ebp], eax
    ; Line 207
            mov     edx, DWORD PTR _hWnd$[ebp]
            push    edx
            call    _InitD3D
            add     esp, 4
    ; Line 208
            call    _InitFont
    ; Line 210
            push    10                                      ; 0000000aH
            mov     eax, DWORD PTR _hWnd$[ebp]
            push    eax
            call    DWORD PTR __imp__ShowWindow@8
    ; Line 211
            mov     ecx, DWORD PTR _hWnd$[ebp]
            push    ecx
            call    DWORD PTR __imp__UpdateWindow@4
    $LN2@WinMain:
    ; Line 213
            push    0
            push    0
            push    0
            lea     edx, DWORD PTR _msg$[ebp]
            push    edx
            call    DWORD PTR __imp__GetMessageA@16
            test    eax, eax
            je      SHORT $LN1@WinMain
    ; Line 215
            lea     eax, DWORD PTR _msg$[ebp]
            push    eax
            call    DWORD PTR __imp__TranslateMessage@4
    ; Line 216
            lea     ecx, DWORD PTR _msg$[ebp]
            push    ecx
            call    DWORD PTR __imp__DispatchMessageA@4
    ; Line 217
            jmp     SHORT $LN2@WinMain
    $LN1@WinMain:
    ; Line 219
            xor     eax, eax
    ; Line 220
            mov     esp, ebp
            pop     ebp
            ret     16                                      ; 00000010H
    _WinMain@16 ENDP
    _TEXT   ENDS
    END

    上記コードは以下のC言語のソースを VC++ でアセンブリコード出力(cl /FA hello.c)したものに相当する。

    ソースコード

    #include <windows.h>
    #include <tchar.h>
    #include <d3d9.h>
    #include <d3dx9.h>
     
    LPDIRECT3D9         g_pD3D       = NULL;
    LPDIRECT3DDEVICE9   g_pd3dDevice = NULL;
    LPD3DXFONT          g_pd3dFont   = NULL;
    RECT                g_rect       = { 0, 0, 0, 0 };
     
    HRESULT InitD3D( HWND hWnd );
    HRESULT InitFont();
    VOID Cleanup();
    VOID Render();
     
    HRESULT InitD3D( HWND hWnd )
    {
        HRESULT hr;
        D3DPRESENT_PARAMETERS d3dpp;
        g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
        if( g_pD3D == NULL )
        {
            return E_FAIL;
        }
     
        d3dpp.BackBufferWidth             = 0;
        d3dpp.BackBufferHeight            = 0;
        d3dpp.BackBufferFormat            = D3DFMT_UNKNOWN;
        d3dpp.BackBufferCount             = 0;
        d3dpp.MultiSampleType             = D3DMULTISAMPLE_NONE;
        d3dpp.MultiSampleQuality          = 0;
        d3dpp.SwapEffect                  = D3DSWAPEFFECT_DISCARD;
        d3dpp.hDeviceWindow               = NULL;
        d3dpp.Windowed                    = TRUE;
        d3dpp.EnableAutoDepthStencil      = 0;
        d3dpp.AutoDepthStencilFormat      = D3DFMT_UNKNOWN;
        d3dpp.Flags                       = 0;
        d3dpp.FullScreen_RefreshRateInHz  = 0;
        d3dpp.PresentationInterval        = 0;
     
        hr = g_pD3D->lpVtbl->CreateDevice(
            g_pD3D,
            D3DADAPTER_DEFAULT,
            D3DDEVTYPE_HAL,
            hWnd,
            D3DCREATE_SOFTWARE_VERTEXPROCESSING,
            &d3dpp,
            &g_pd3dDevice
        );
     
        if( FAILED( hr ) )
        {
            return E_FAIL;
        }
     
        return S_OK;
    }
     
    HRESULT InitFont()
    {
        HRESULT hr;
     
        D3DXFONT_DESC lf;
        lf.Height          = 16;
        lf.Width           = 0;
        lf.Weight          = 0;
        lf.MipLevels       = 1;
        lf.Italic          = 0;
        lf.CharSet         = SHIFTJIS_CHARSET;
        lf.OutputPrecision = OUT_TT_ONLY_PRECIS;
        lf.Quality         = PROOF_QUALITY;
        lf.PitchAndFamily  = FIXED_PITCH | FF_MODERN;
        lstrcpy( lf.FaceName, _T("MS ゴシック") );
     
        hr = D3DXCreateFontIndirect( g_pd3dDevice, &lf, &g_pd3dFont );
        if ( FAILED( hr ) )
        {
            Cleanup();
            return hr;
        }
     
        hr = g_pd3dFont->lpVtbl->DrawText(
            g_pd3dFont,
            NULL, 
            _T("Hello, DirectX(MASM) World!"),
            -1,
            &g_rect,
            DT_CALCRECT | DT_LEFT | DT_SINGLELINE,
            0xffffffff
        );
     
        if ( FAILED( hr ) )
        {
            Cleanup();
            return hr;
        }
     
        return hr;
    }
     
    VOID Cleanup()
    {
        if ( g_pd3dFont != NULL )
        {
            g_pd3dFont->lpVtbl->Release( g_pd3dFont );
        }
     
        if( g_pd3dDevice != NULL )
        {
            g_pd3dDevice->lpVtbl->Release( g_pd3dDevice );
        }
     
        if( g_pD3D != NULL )
        {
            g_pD3D->lpVtbl->Release( g_pD3D );
        }
    }
     
    VOID Render()
    {
        if( g_pd3dDevice == NULL )
        {
            return;
        }
     
        if ( g_pd3dFont == NULL )
        {
            return;
        }
     
        g_pd3dDevice->lpVtbl->Clear(
            g_pd3dDevice,
            0, 
            NULL,
            D3DCLEAR_TARGET,
            D3DCOLOR_XRGB( 0, 0, 255 ),
            1.0f,
            0
        );
     
        if( SUCCEEDED( g_pd3dDevice->lpVtbl->BeginScene( g_pd3dDevice ) ) )
        {
            g_pd3dFont->lpVtbl->DrawText( 
                g_pd3dFont,
                NULL,
                _T("Hello, DirectX(MASM) World!"),
                -1,
                &g_rect,
                DT_LEFT | DT_SINGLELINE,
                0xffffffff
            );
     
            g_pd3dDevice->lpVtbl->EndScene( g_pd3dDevice );
        }
     
        g_pd3dDevice->lpVtbl->Present( g_pd3dDevice, NULL, NULL, NULL, NULL );
    }
     
    LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
    {
        switch( message )
        {
            case WM_DESTROY:
                Cleanup();
                PostQuitMessage( 0 );
                return 0;
     
            case WM_PAINT:
                Render();
                ValidateRect( hWnd, NULL );
                return 0;
        }
     
        return DefWindowProc( hWnd, message, wParam, lParam );
    }
     
    int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
    {
        LPCTSTR lpszClassName = _T("helloWindow");
        LPCTSTR lpszWindowName = _T("Hello, World!");
        MSG msg;
        HWND hWnd;
     
        WNDCLASSEX wcex;
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style          = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc    = WndProc;
        wcex.cbClsExtra     = 0;
        wcex.cbWndExtra     = 0;
        wcex.hInstance      = hInstance;
        wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
        wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName   = NULL;
        wcex.lpszClassName  = lpszClassName;
        wcex.hIconSm        = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
     
        RegisterClassEx(&wcex);
        hWnd = CreateWindow(
            lpszClassName,
            lpszWindowName,
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
            NULL, NULL, hInstance, NULL
        );
     
        InitD3D( hWnd );
        InitFont();
     
        ShowWindow( hWnd, SW_SHOWDEFAULT );
        UpdateWindow( hWnd );
     
        while( GetMessage( &msg, NULL, 0, 0 ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
     
        return 0;
    }

    コンパイル方法

    C:¥> SET INCLUDE=%DXSDK_DIR%INCLUDE;%INCLUDE%
    C:¥> SET LIB=%DXSDK_DIR%Libx86;%LIB%
    C:¥> ml hello.asm ^
             /link ^
             user32.lib ^
             dxguid.lib ^
             d3d9.lib ^
             d3dx9.lib ^
             /SUBSYSTEM:WINDOWS

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, DirectX(MASM) World!               |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  6. Hello, DirectX(MFC) World!

    Posted on 7月 26th, 2012 by cx20

    Win32 DirectX(MFC)

    DirectX はマイクロソフトが Wnidows 用に開発したマルチメディア処理用 API である。
    以下は VC++(MFC) における DirectX アプリケーション の例となっている。

    ソースコード

    #include <afxwin.h>
    #include <tchar.h>
    #include <tchar.h>
    #include <d3d9.h>
    #include <d3dx9.h>
     
    class CMainFrame : public CFrameWnd
    {
    public:
        CMainFrame();
        ~CMainFrame();
        BOOL PreCreateWindow(CREATESTRUCT& cs);
     
        HRESULT InitD3D();
        HRESULT InitFont();
        VOID Cleanup();
        VOID Render();
    protected:
        afx_msg void OnPaint();
        DECLARE_MESSAGE_MAP()
     
    private:
        LPDIRECT3D9         m_pD3D;
        LPDIRECT3DDEVICE9   m_pd3dDevice;
        LPD3DXFONT          m_pd3dFont;
        RECT                m_rect;
    };
     
    class CHelloApp : public CWinApp
    {
    public:
        BOOL InitInstance();
    };
     
    BOOL CHelloApp::InitInstance()
    {
        m_pMainWnd = new CMainFrame;
     
        m_pMainWnd->ShowWindow(m_nCmdShow);
        m_pMainWnd->UpdateWindow();
        return TRUE;
    }
     
    CHelloApp App;
     
    BEGIN_MESSAGE_MAP( CMainFrame, CFrameWnd )
        ON_WM_PAINT()
    END_MESSAGE_MAP()
     
    CMainFrame::CMainFrame()
    {
        m_pD3D       = NULL;
        m_pd3dDevice = NULL;
        m_pd3dFont   = NULL;
     
        memset( &m_rect, 0, sizeof(m_rect) );
     
        Create( NULL, _T("Hello, World!") );
     
        InitD3D();
        InitFont();
    }
     
    CMainFrame::~CMainFrame()
    {
        Cleanup();
    }
     
    BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
    {
        CFrameWnd::PreCreateWindow(cs);
        cs.cx = 640;
        cs.cy = 480;
     
        return TRUE;
    }
     
    void CMainFrame::OnPaint()
    {
        Render();
    }
     
    HRESULT CMainFrame::InitD3D()
    {
        HRESULT hr;
        m_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
        if( m_pD3D == NULL )
        {
            return E_FAIL;
        }
     
        D3DPRESENT_PARAMETERS d3dpp;
        d3dpp.BackBufferWidth             = 0;
        d3dpp.BackBufferHeight            = 0;
        d3dpp.BackBufferFormat            = D3DFMT_UNKNOWN;
        d3dpp.BackBufferCount             = 0;
        d3dpp.MultiSampleType             = D3DMULTISAMPLE_NONE;
        d3dpp.MultiSampleQuality          = 0;
        d3dpp.SwapEffect                  = D3DSWAPEFFECT_DISCARD;
        d3dpp.hDeviceWindow               = NULL;
        d3dpp.Windowed                    = TRUE;
        d3dpp.EnableAutoDepthStencil      = 0;
        d3dpp.AutoDepthStencilFormat      = D3DFMT_UNKNOWN;
        d3dpp.Flags                       = 0;
        d3dpp.FullScreen_RefreshRateInHz  = 0;
        d3dpp.PresentationInterval        = 0;
     
        hr = m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd,
                                          D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                          &d3dpp, &m_pd3dDevice );
        if( FAILED( hr ) )
        {
            return E_FAIL;
        }
     
        return S_OK;
    }
     
    HRESULT CMainFrame::InitFont()
    {
        HRESULT hr;
        D3DXFONT_DESC lf;
        lf.Height          = 16;
        lf.Width           = 0;
        lf.Weight          = 0;
        lf.MipLevels       = 1;
        lf.Italic          = 0;
        lf.CharSet         = SHIFTJIS_CHARSET;
        lf.OutputPrecision = OUT_TT_ONLY_PRECIS;
        lf.Quality         = PROOF_QUALITY;
        lf.PitchAndFamily  = FIXED_PITCH | FF_MODERN;
        lstrcpy( lf.FaceName, _T("MS ゴシック") );
     
        hr = D3DXCreateFontIndirect(m_pd3dDevice, &lf, &m_pd3dFont );
        if ( FAILED( hr ) )
        {
            Cleanup();
            return hr;
        }
     
        hr = m_pd3dFont->DrawText(
            NULL, 
            _T("Hello, DirectX(MFC) World!"),
            -1,
            &m_rect,
            DT_CALCRECT | DT_LEFT | DT_SINGLELINE,
            0xffffffff
        );
     
        if ( FAILED( hr ) )
        {
            Cleanup();
            return hr;
        }
     
        return hr;
    }
     
    VOID CMainFrame::Cleanup()
    {
        if ( m_pd3dFont != NULL )
        {
            m_pd3dFont->Release();
        }
     
        if( m_pd3dDevice != NULL )
        {
            m_pd3dDevice->Release();
        }
     
        if( m_pD3D != NULL )
        {
            m_pD3D->Release();
        }
    }
     
    VOID CMainFrame::Render()
    {
        if( m_pd3dDevice == NULL )
        {
            return;
        }
     
        if ( m_pd3dFont == NULL )
        {
            return;
        }
     
        m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );
     
        if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
        {
            m_pd3dFont->DrawText(
                NULL,
                _T("Hello, DirectX(MFC) World!"),
                -1,
                &m_rect,
                DT_LEFT | DT_SINGLELINE, 0xffffffff
            );
     
            m_pd3dDevice->EndScene();
        }
     
        m_pd3dDevice->Present( NULL, NULL, NULL, NULL );
    }

    コンパイル方法

    C:¥> SET INCLUDE=%DXSDK_DIR%INCLUDE;%INCLUDE%
    C:¥> SET LIB=%DXSDK_DIR%Libx86;%LIB%
    C:¥> cl hello.cpp ^
             /link ^
             user32.lib ^
             dxguid.lib ^
             d3d9.lib ^
             d3dx9.lib ^
             /SUBSYSTEM:WINDOWS

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, DirectX(MFC) World!                |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  7. Hello, DirectX(WTL) World!

    Posted on 7月 25th, 2012 by cx20

    Win32 DirectX(WTL)

    DirectX はマイクロソフトが Wnidows 用に開発したマルチメディア処理用 API である。
    以下は VC++(WTL) における DirectX アプリケーション の例となっている。

    ソースコード

    #include <atlbase.h>
    #include <atlapp.h>
    #include <atlcrack.h>
    #include <d3d9.h>
    #include <d3dx9.h>
     
    class CHelloWindow : public CWindowImpl<CHelloWindow>
    {
    public:
        CHelloWindow()
        {
            m_pD3D       = NULL;
            m_pd3dDevice = NULL;
            m_pd3dFont   = NULL;
     
            memset( &m_rect, 0, sizeof(m_rect) );
        }
     
        BEGIN_MSG_MAP( CHelloWindow )
            MSG_WM_PAINT   ( OnPaint   )
            MSG_WM_DESTROY ( OnDestroy )
        END_MSG_MAP()
     
        void OnPaint( HDC hDC )
        {
            Render();
        }
        void OnDestroy()
        {
            Cleanup();
            PostQuitMessage( 0 );
        }
     
        LRESULT OnPaint( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
        {
            Render();
            return 0;
        }
     
        LRESULT OnDestroy( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
        {
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
        }
     
        HRESULT InitD3D()
        {
            HRESULT hr;
            m_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
            if( m_pD3D == NULL )
            {
                return E_FAIL;
            }
     
            D3DPRESENT_PARAMETERS d3dpp;
            d3dpp.BackBufferWidth             = 0;
            d3dpp.BackBufferHeight            = 0;
            d3dpp.BackBufferFormat            = D3DFMT_UNKNOWN;
            d3dpp.BackBufferCount             = 0;
            d3dpp.MultiSampleType             = D3DMULTISAMPLE_NONE;
            d3dpp.MultiSampleQuality          = 0;
            d3dpp.SwapEffect                  = D3DSWAPEFFECT_DISCARD;
            d3dpp.hDeviceWindow               = NULL;
            d3dpp.Windowed                    = TRUE;
            d3dpp.EnableAutoDepthStencil      = 0;
            d3dpp.AutoDepthStencilFormat      = D3DFMT_UNKNOWN;
            d3dpp.Flags                       = 0;
            d3dpp.FullScreen_RefreshRateInHz  = 0;
            d3dpp.PresentationInterval        = 0;
     
            hr = m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd,
                                              D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                              &d3dpp, &m_pd3dDevice );
            if( FAILED( hr ) )
            {
                return E_FAIL;
            }
     
            return S_OK;
        }
     
        HRESULT InitFont()
        {
            HRESULT hr;
            D3DXFONT_DESC lf;
            lf.Height          = 16;
            lf.Width           = 0;
            lf.Weight          = 0;
            lf.MipLevels       = 1;
            lf.Italic          = 0;
            lf.CharSet         = SHIFTJIS_CHARSET;
            lf.OutputPrecision = OUT_TT_ONLY_PRECIS;
            lf.Quality         = PROOF_QUALITY;
            lf.PitchAndFamily  = FIXED_PITCH | FF_MODERN;
            lstrcpy( lf.FaceName, _T("MS ゴシック") );
     
            hr = D3DXCreateFontIndirect(m_pd3dDevice, &lf, &m_pd3dFont );
            if ( FAILED( hr ) )
            {
                Cleanup();
                return hr;
            }
     
            hr = m_pd3dFont->DrawText(
                NULL, 
                _T("Hello, DirectX(WTL) World!"),
                -1,
                &m_rect,
                DT_CALCRECT | DT_LEFT | DT_SINGLELINE,
                0xffffffff
            );
     
            if ( FAILED( hr ) )
            {
                Cleanup();
                return hr;
            }
     
            return hr;
        }
     
        VOID Cleanup()
        {
            if ( m_pd3dFont != NULL )
            {
                m_pd3dFont->Release();
            }
     
            if( m_pd3dDevice != NULL )
            {
                m_pd3dDevice->Release();
            }
     
            if( m_pD3D != NULL )
            {
                m_pD3D->Release();
            }
        }
     
        VOID Render()
        {
            if( m_pd3dDevice == NULL )
            {
                return;
            }
     
            if ( m_pd3dFont == NULL )
            {
                return;
            }
     
            m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );
     
            if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
            {
                m_pd3dFont->DrawText(
                    NULL,
                    _T("Hello, DirectX(WTL) World!"),
                    -1,
                    &m_rect,
                    DT_LEFT | DT_SINGLELINE, 0xffffffff
                );
     
                m_pd3dDevice->EndScene();
            }
     
            m_pd3dDevice->Present( NULL, NULL, NULL, NULL );
        }
    private:
        LPDIRECT3D9         m_pD3D;
        LPDIRECT3DDEVICE9   m_pd3dDevice;
        LPD3DXFONT          m_pd3dFont;
        RECT                m_rect;
    };
     
    CAppModule _Module;
     
    int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
    {
        _Module.Init(NULL, hInstance);
     
        CMessageLoop theLoop;
        _Module.AddMessageLoop(&theLoop);
     
        CHelloWindow wnd;
        wnd.Create( NULL, CWindow::rcDefault, _T("Hello, World!"), WS_OVERLAPPEDWINDOW | WS_VISIBLE );
        wnd.ResizeClient( 640, 480 );
        wnd.InitD3D();
        wnd.InitFont();
        int nRet = theLoop.Run();
     
        _Module.RemoveMessageLoop();
        _Module.Term();
     
        return nRet;
    }

    コンパイル方法

    C:¥> SET INCLUDE=<WTL>Include;%DXSDK_DIR%INCLUDE;%INCLUDE%
    C:¥> SET LIB=%DXSDK_DIR%Libx86;%LIB%
    C:¥> cl hello.cpp ^
             /link ^
             user32.lib ^
             dxguid.lib ^
             d3d9.lib ^
             d3dx9.lib ^
             /SUBSYSTEM:WINDOWS

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, DirectX(WTL) World!                |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  8. Hello, DirectX(ATL) World!

    Posted on 7月 24th, 2012 by cx20

    Win32 DirectX(ATL)

    DirectX はマイクロソフトが Wnidows 用に開発したマルチメディア処理用 API である。
    以下は VC++(ATL) における DirectX アプリケーション の例となっている。

    ソースコード

    #include <atlbase.h>
    #include <atlwin.h> 
    #include <d3d9.h>
    #include <d3dx9.h>
     
    class CHelloWindow : public CWindowImpl<CHelloWindow>
    {
    public:
        CHelloWindow()
        {
            m_pD3D       = NULL;
            m_pd3dDevice = NULL;
            m_pd3dFont   = NULL;
     
            memset( &m_rect, 0, sizeof(m_rect) );
        }
     
        BEGIN_MSG_MAP( CHelloWindow )
            MESSAGE_HANDLER( WM_PAINT,   OnPaint   )
            MESSAGE_HANDLER( WM_DESTROY, OnDestroy )
        END_MSG_MAP()
     
        LRESULT OnPaint( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
        {
            Render();
            return 0;
        }
     
        LRESULT OnDestroy( UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled )
        {
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
        }
     
        HRESULT InitD3D()
        {
            HRESULT hr;
            m_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
            if( m_pD3D == NULL )
            {
                return E_FAIL;
            }
     
            D3DPRESENT_PARAMETERS d3dpp;
            d3dpp.BackBufferWidth             = 0;
            d3dpp.BackBufferHeight            = 0;
            d3dpp.BackBufferFormat            = D3DFMT_UNKNOWN;
            d3dpp.BackBufferCount             = 0;
            d3dpp.MultiSampleType             = D3DMULTISAMPLE_NONE;
            d3dpp.MultiSampleQuality          = 0;
            d3dpp.SwapEffect                  = D3DSWAPEFFECT_DISCARD;
            d3dpp.hDeviceWindow               = NULL;
            d3dpp.Windowed                    = TRUE;
            d3dpp.EnableAutoDepthStencil      = 0;
            d3dpp.AutoDepthStencilFormat      = D3DFMT_UNKNOWN;
            d3dpp.Flags                       = 0;
            d3dpp.FullScreen_RefreshRateInHz  = 0;
            d3dpp.PresentationInterval        = 0;
     
            hr = m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd,
                                              D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                              &d3dpp, &m_pd3dDevice );
            if( FAILED( hr ) )
            {
                return E_FAIL;
            }
     
            return S_OK;
        }
     
        HRESULT InitFont()
        {
            HRESULT hr;
            D3DXFONT_DESC lf;
            lf.Height          = 16;
            lf.Width           = 0;
            lf.Weight          = 0;
            lf.MipLevels       = 1;
            lf.Italic          = 0;
            lf.CharSet         = SHIFTJIS_CHARSET;
            lf.OutputPrecision = OUT_TT_ONLY_PRECIS;
            lf.Quality         = PROOF_QUALITY;
            lf.PitchAndFamily  = FIXED_PITCH | FF_MODERN;
            lstrcpy( lf.FaceName, _T("MS ゴシック") );
     
            hr = D3DXCreateFontIndirect(m_pd3dDevice, &lf, &m_pd3dFont );
            if ( FAILED( hr ) )
            {
                Cleanup();
                return hr;
            }
     
            hr = m_pd3dFont->DrawText(
                NULL, 
                _T("Hello, DirectX(ATL) World!"),
                -1,
                &m_rect,
                DT_CALCRECT | DT_LEFT | DT_SINGLELINE,
                0xffffffff
            );
     
            if ( FAILED( hr ) )
            {
                Cleanup();
                return hr;
            }
     
            return hr;
        }
     
        VOID Cleanup()
        {
            if ( m_pd3dFont != NULL )
            {
                m_pd3dFont->Release();
            }
     
            if( m_pd3dDevice != NULL )
            {
                m_pd3dDevice->Release();
            }
     
            if( m_pD3D != NULL )
            {
                m_pD3D->Release();
            }
        }
     
        VOID Render()
        {
            if( m_pd3dDevice == NULL )
            {
                return;
            }
     
            if ( m_pd3dFont == NULL )
            {
                return;
            }
     
            m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );
     
            if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
            {
                m_pd3dFont->DrawText(
                    NULL,
                    _T("Hello, DirectX(ATL) World!"),
                    -1,
                    &m_rect,
                    DT_LEFT | DT_SINGLELINE, 0xffffffff
                );
     
                m_pd3dDevice->EndScene();
            }
     
            m_pd3dDevice->Present( NULL, NULL, NULL, NULL );
        }
     
    private:
        LPDIRECT3D9         m_pD3D;
        LPDIRECT3DDEVICE9   m_pd3dDevice;
        LPD3DXFONT          m_pd3dFont;
        RECT                m_rect;
    };
     
    CComModule _Module;
     
    int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
    {
        _Module.Init(NULL, hInstance);
     
        CHelloWindow wnd;
        wnd.Create( NULL, CWindow::rcDefault, _T("Hello, World!"), WS_OVERLAPPEDWINDOW | WS_VISIBLE );
        wnd.ResizeClient( 640, 480 );
        wnd.InitD3D();
        wnd.InitFont();
        MSG msg;
        while( GetMessage( &msg, NULL, 0, 0 ) ){
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
     
        _Module.Term();
     
        return (int)msg.wParam;
    }

    コンパイル方法

    C:¥> SET INCLUDE=%DXSDK_DIR%INCLUDE;%INCLUDE%
    C:¥> SET LIB=%DXSDK_DIR%Libx86;%LIB%
    C:¥> cl hello.cpp ^
             /link ^
             user32.lib ^
             dxguid.lib ^
             d3d9.lib ^
             d3dx9.lib ^
             /SUBSYSTEM:WINDOWS

    実行結果

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

    Posted on 7月 23rd, 2012 by cx20

    Win32 DirectX(C++)

    DirectX はマイクロソフトが Wnidows 用に開発したマルチメディア処理用 API である。
    以下は C++ における DirectX アプリケーション の例となっている。

    ソースコード

    #include <windows.h>
    #include <tchar.h>
    #include <d3d9.h>
    #include <d3dx9.h>
     
    LPDIRECT3D9         g_pD3D       = NULL;
    LPDIRECT3DDEVICE9   g_pd3dDevice = NULL;
    LPD3DXFONT          g_pd3dFont   = NULL;
    RECT                g_rect       = { 0, 0, 0, 0 };
     
    HRESULT InitD3D( HWND hWnd );
    HRESULT InitFont();
    VOID Cleanup();
    VOID Render();
     
    HRESULT InitD3D( HWND hWnd )
    {
        HRESULT hr;
        g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
        if( g_pD3D == NULL )
        {
            return E_FAIL;
        }
     
        D3DPRESENT_PARAMETERS d3dpp;
        d3dpp.BackBufferWidth             = 0;
        d3dpp.BackBufferHeight            = 0;
        d3dpp.BackBufferFormat            = D3DFMT_UNKNOWN;
        d3dpp.BackBufferCount             = 0;
        d3dpp.MultiSampleType             = D3DMULTISAMPLE_NONE;
        d3dpp.MultiSampleQuality          = 0;
        d3dpp.SwapEffect                  = D3DSWAPEFFECT_DISCARD;
        d3dpp.hDeviceWindow               = NULL;
        d3dpp.Windowed                    = TRUE;
        d3dpp.EnableAutoDepthStencil      = 0;
        d3dpp.AutoDepthStencilFormat      = D3DFMT_UNKNOWN;
        d3dpp.Flags                       = 0;
        d3dpp.FullScreen_RefreshRateInHz  = 0;
        d3dpp.PresentationInterval        = 0;
     
        hr = g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                          D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                          &d3dpp, &g_pd3dDevice );
        if( FAILED( hr ) )
        {
            return E_FAIL;
        }
     
        return S_OK;
    }
     
    HRESULT InitFont()
    {
        HRESULT hr;
        D3DXFONT_DESC lf;
        lf.Height          = 16;
        lf.Width           = 0;
        lf.Weight          = 0;
        lf.MipLevels       = 1;
        lf.Italic          = 0;
        lf.CharSet         = SHIFTJIS_CHARSET;
        lf.OutputPrecision = OUT_TT_ONLY_PRECIS;
        lf.Quality         = PROOF_QUALITY;
        lf.PitchAndFamily  = FIXED_PITCH | FF_MODERN;
        lstrcpy( lf.FaceName, _T("MS ゴシック") );
     
        hr = D3DXCreateFontIndirect(g_pd3dDevice, &lf, &g_pd3dFont );
        if ( FAILED( hr ) )
        {
            Cleanup();
            return hr;
        }
     
        hr = g_pd3dFont->DrawText(
            NULL, 
            _T("Hello, DirectX(C++) World!"),
            -1,
            &g_rect,
            DT_CALCRECT | DT_LEFT | DT_SINGLELINE,
            0xffffffff
        );
     
        if ( FAILED( hr ) )
        {
            Cleanup();
            return hr;
        }
     
        return hr;
    }
     
    VOID Cleanup()
    {
        if ( g_pd3dFont != NULL )
        {
            g_pd3dFont->Release();
        }
     
        if( g_pd3dDevice != NULL )
        {
            g_pd3dDevice->Release();
        }
     
        if( g_pD3D != NULL )
        {
            g_pD3D->Release();
        }
    }
     
    VOID Render()
    {
        if( g_pd3dDevice == NULL )
        {
            return;
        }
     
        if ( g_pd3dFont == NULL )
        {
            return;
        }
     
        g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );
     
        if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
        {
            g_pd3dFont->DrawText(
                NULL,
                _T("Hello, DirectX(C++) World!"),
                -1,
                &g_rect,
                DT_LEFT | DT_SINGLELINE, 0xffffffff
            );
     
            g_pd3dDevice->EndScene();
        }
     
        g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
    }
     
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch( message )
        {
            case WM_DESTROY:
                Cleanup();
                PostQuitMessage( 0 );
                return 0;
     
            case WM_PAINT:
                Render();
                ValidateRect( hWnd, NULL );
                return 0;
        }
     
        return DefWindowProc( hWnd, message, wParam, lParam );
    }
     
     
    int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
    {
        LPCTSTR lpszClassName = _T("helloWindow");
        LPCTSTR lpszWindowName = _T("Hello, World!");
     
        WNDCLASSEX wcex;
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style          = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc    = WndProc;
        wcex.cbClsExtra     = 0;
        wcex.cbWndExtra     = 0;
        wcex.hInstance      = hInstance;
        wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
        wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName   = NULL;
        wcex.lpszClassName  = lpszClassName;
        wcex.hIconSm        = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
     
        RegisterClassEx(&wcex);
        HWND hWnd = CreateWindow(
            lpszClassName,
            lpszWindowName,
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
            NULL, NULL, hInstance, NULL
            );
     
        InitD3D( hWnd );
        InitFont();
     
        ShowWindow( hWnd, SW_SHOWDEFAULT );
        UpdateWindow( hWnd );
     
        MSG msg;
        while( GetMessage( &msg, NULL, 0, 0 ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
     
        return 0;
    }

    コンパイル方法

    C:¥> SET INCLUDE=%DXSDK_DIR%INCLUDE;%INCLUDE%
    C:¥> SET LIB=%DXSDK_DIR%Libx86;%LIB%
    C:¥> cl hello.cpp ^
             /link ^
             user32.lib ^
             dxguid.lib ^
             d3d9.lib ^
             d3dx9.lib ^
             /SUBSYSTEM:WINDOWS

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, DirectX(C++) World!                |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+
  10. Hello, DirectX(C言語) World!

    Posted on 7月 22nd, 2012 by cx20

    Win32 DirectX(C言語)

    DirectX はマイクロソフトが Wnidows 用に開発したマルチメディア処理用 API である。
    以下は C++ における DirectX アプリケーション の例となっている。

    ソースコード

    #include <windows.h>
    #include <tchar.h>
    #include <d3d9.h>
    #include <d3dx9.h>
     
    LPDIRECT3D9         g_pD3D       = NULL;
    LPDIRECT3DDEVICE9   g_pd3dDevice = NULL;
    LPD3DXFONT          g_pd3dFont   = NULL;
    RECT                g_rect       = { 0, 0, 0, 0 };
     
    HRESULT InitD3D( HWND hWnd );
    HRESULT InitFont();
    VOID Cleanup();
    VOID Render();
     
    HRESULT InitD3D( HWND hWnd )
    {
        HRESULT hr;
        D3DPRESENT_PARAMETERS d3dpp;
        g_pD3D = Direct3DCreate9( D3D_SDK_VERSION );
        if( g_pD3D == NULL )
        {
            return E_FAIL;
        }
     
        d3dpp.BackBufferWidth             = 0;
        d3dpp.BackBufferHeight            = 0;
        d3dpp.BackBufferFormat            = D3DFMT_UNKNOWN;
        d3dpp.BackBufferCount             = 0;
        d3dpp.MultiSampleType             = D3DMULTISAMPLE_NONE;
        d3dpp.MultiSampleQuality          = 0;
        d3dpp.SwapEffect                  = D3DSWAPEFFECT_DISCARD;
        d3dpp.hDeviceWindow               = NULL;
        d3dpp.Windowed                    = TRUE;
        d3dpp.EnableAutoDepthStencil      = 0;
        d3dpp.AutoDepthStencilFormat      = D3DFMT_UNKNOWN;
        d3dpp.Flags                       = 0;
        d3dpp.FullScreen_RefreshRateInHz  = 0;
        d3dpp.PresentationInterval        = 0;
     
        hr = g_pD3D->lpVtbl->CreateDevice(
            g_pD3D,
            D3DADAPTER_DEFAULT,
            D3DDEVTYPE_HAL,
            hWnd,
            D3DCREATE_SOFTWARE_VERTEXPROCESSING,
            &d3dpp,
            &g_pd3dDevice
        );
     
        if( FAILED( hr ) )
        {
            return E_FAIL;
        }
     
        return S_OK;
    }
     
    HRESULT InitFont()
    {
        HRESULT hr;
     
        D3DXFONT_DESC lf;
        lf.Height          = 16;
        lf.Width           = 0;
        lf.Weight          = 0;
        lf.MipLevels       = 1;
        lf.Italic          = 0;
        lf.CharSet         = SHIFTJIS_CHARSET;
        lf.OutputPrecision = OUT_TT_ONLY_PRECIS;
        lf.Quality         = PROOF_QUALITY;
        lf.PitchAndFamily  = FIXED_PITCH | FF_MODERN;
        lstrcpy( lf.FaceName, _T("MS ゴシック") );
     
        hr = D3DXCreateFontIndirect( g_pd3dDevice, &lf, &g_pd3dFont );
        if ( FAILED( hr ) )
        {
            Cleanup();
            return hr;
        }
     
        hr = g_pd3dFont->lpVtbl->DrawText(
            g_pd3dFont,
            NULL, 
            _T("Hello, DirectX World!"),
            -1,
            &g_rect,
            DT_CALCRECT | DT_LEFT | DT_SINGLELINE,
            0xffffffff
        );
     
        if ( FAILED( hr ) )
        {
            Cleanup();
            return hr;
        }
     
        return hr;
    }
     
    VOID Cleanup()
    {
        if ( g_pd3dFont != NULL )
        {
            g_pd3dFont->lpVtbl->Release( g_pd3dFont );
        }
     
        if( g_pd3dDevice != NULL )
        {
            g_pd3dDevice->lpVtbl->Release( g_pd3dDevice );
        }
     
        if( g_pD3D != NULL )
        {
            g_pD3D->lpVtbl->Release( g_pD3D );
        }
    }
     
    VOID Render()
    {
        if( g_pd3dDevice == NULL )
        {
            return;
        }
     
        if ( g_pd3dFont == NULL )
        {
            return;
        }
     
        g_pd3dDevice->lpVtbl->Clear(
            g_pd3dDevice,
            0, 
            NULL,
            D3DCLEAR_TARGET,
            D3DCOLOR_XRGB( 0, 0, 255 ),
            1.0f,
            0
        );
     
        if( SUCCEEDED( g_pd3dDevice->lpVtbl->BeginScene( g_pd3dDevice ) ) )
        {
            g_pd3dFont->lpVtbl->DrawText( 
                g_pd3dFont,
                NULL,
                _T("Hello, DirectX World!"),
                -1,
                &g_rect,
                DT_LEFT | DT_SINGLELINE,
                0xffffffff
            );
     
            g_pd3dDevice->lpVtbl->EndScene( g_pd3dDevice );
        }
     
        g_pd3dDevice->lpVtbl->Present( g_pd3dDevice, NULL, NULL, NULL, NULL );
    }
     
    LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
    {
        switch( message )
        {
            case WM_DESTROY:
                Cleanup();
                PostQuitMessage( 0 );
                return 0;
     
            case WM_PAINT:
                Render();
                ValidateRect( hWnd, NULL );
                return 0;
        }
     
        return DefWindowProc( hWnd, message, wParam, lParam );
    }
     
    int APIENTRY _tWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow )
    {
        LPCTSTR lpszClassName = _T("helloWindow");
        LPCTSTR lpszWindowName = _T("Hello, World!");
        MSG msg;
        HWND hWnd;
     
        WNDCLASSEX wcex;
        wcex.cbSize = sizeof(WNDCLASSEX);
        wcex.style          = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc    = WndProc;
        wcex.cbClsExtra     = 0;
        wcex.cbWndExtra     = 0;
        wcex.hInstance      = hInstance;
        wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
        wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW+1);
        wcex.lpszMenuName   = NULL;
        wcex.lpszClassName  = lpszClassName;
        wcex.hIconSm        = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
     
        RegisterClassEx(&wcex);
        hWnd = CreateWindow(
            lpszClassName,
            lpszWindowName,
            WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
            NULL, NULL, hInstance, NULL
        );
     
        InitD3D( hWnd );
        InitFont();
     
        ShowWindow( hWnd, SW_SHOWDEFAULT );
        UpdateWindow( hWnd );
     
        while( GetMessage( &msg, NULL, 0, 0 ) )
        {
            TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
     
        return 0;
    }

    コンパイル方法

    C:¥> SET INCLUDE=%DXSDK_DIR%INCLUDE;%INCLUDE%
    C:¥> SET LIB=%DXSDK_DIR%Libx86;%LIB%
    C:¥> cl hello.c ^
             /link ^
             user32.lib ^
             dxguid.lib ^
             d3d9.lib ^
             d3dx9.lib ^
             /SUBSYSTEM:WINDOWS

    実行結果

    +------------------------------------------+
    |Hello, World!                    [_][~][X]|
    +------------------------------------------+
    |Hello, DirectX World!                     |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+