Direct 3D で Form を表示する

Command Line の環境で Direct 3D で Form を生成して表示します。
Command Line から簡単に実行できる Direct 3D の Program を作成します。 (●^o^●)

前田稔(Maeda Minoru)の超初心者のプログラム入門

プロジェクトの設定

  1. C++/CLI で Direct 3DGraphics の Form を生成します。 (^_^;
    今回はウインドウを Blue でクリアするだけです。
    VC++ のプロジェクトで作成すると非常に多くのファイルが自動的に作成されるのですが、今回作成するファイルは dxform.cpp の一本だけです。
    DXForm.cpp を DX.BAT ファイルと同じフォルダー(C:\Data\Cpp\DirectX\) に格納して下さい。
    コンパイルと実行の方法は Command Line から DirectX を動かす を参照して下さい。
    ファイル名 説明
    DXForm.cpp Direct 3D Form
  2. #using で実行に必要な DLL(Dynamic Link Library) をリンクします。
    using namespace は名前空間の設定で、修飾しなくても Class を参照できるようにしています。
        #using <System.dll>
        #using <System.Windows.Forms.dll>
        #using <System.Drawing.dll>
        #using <Microsoft.DirectX.dll>
        #using <Microsoft.DirectX.Direct3D.dll>
        #using <Microsoft.DirectX.Direct3DX.dll>
    
        using namespace System;
        using namespace System::Windows::Forms;
        using namespace System::Drawing;
        using namespace Microsoft::DirectX;
        using namespace Microsoft::DirectX::Direct3D;
        
  3. CreateDevice は Direct 3D のフォーム(Window) を生成するクラスです。
    ^device は Direct 3D のデバイスで ^ はポインタを意味します。
    device = nullptr; で初期値として null を設定します。
    this->ClientSize に Form の大きさを設定します。
    this->Text = "D3D CreateDevice"; は caption の設定です。
        ref class CreateDevice : public System::Windows::Forms::Form
        {
            Microsoft::DirectX::Direct3D::Device  ^device;
    
          public:
            // Constructor
            CreateDevice()
            {
                device = nullptr;
                // Window のサイズを設定
                this->ClientSize = System::Drawing::Size(400,300);
                // caption の設定
                this->Text = "D3D CreateDevice";
            }
        
  4. Direct 3D の初期化を行う InitializeGraphics() メソッドです。
    ^presentParams にパラメータを設定して gcnew Device() でデバイスを生成します。
        bool InitializeGraphics()
        {
            PresentParameters ^presentParams = gcnew PresentParameters();
            presentParams->Windowed=true;
            presentParams->SwapEffect = SwapEffect::Discard;
            device = gcnew Device(0, DeviceType::Hardware, this, CreateFlags::SoftwareVertexProcessing, presentParams);
            if (device!=nullptr)    return true;
            MessageBox::Show("Create Direct3D Device Error.");
            return false; 
        }
        
  5. ウインドウを描画する Render() メソッドです。
    今回はウインドウを Blue でクリアするだけです。
        void Render()
        {
            if (device==nullptr)    return;
    
            //Clear the backbuffer to a blue color 
            device->Clear(ClearFlags::Target, System::Drawing::Color::Blue, 1.0f, 0);
            //Begin the scene
            device->BeginScene();
    
            // Rendering of scene objects can happen here
    
            //End the scene
            device->EndScene();
            device->Present();
        }
        
  6. OnPaint を override するメソッドです。
    ウインドウを描画するメソッド Render() を呼び出します。
        // Window の描画 OnPaint() を override
        virtual void OnPaint(PaintEventArgs ^e) override
        {
            this->Render();    // Render on painting
        }
        
  7. OnKeyPress を override するメソッドです。
    Escape キーが押されるとプログラムを終了します。
    
        // OnKeyPress() を override
        virtual void OnKeyPress(System::Windows::Forms::KeyPressEventArgs ^e) override
        {
            if ((int)e->KeyChar == (int)System::Windows::Forms::Keys::Escape)
                this->Close();  // Esc was pressed
        }
        
  8. main() メソッドでは gcnew CreateDevice() で Form を生成します。
    メッセージループから frm->Render(); で描画関数を呼び出していることに注目して下さい。 ヽ(^^ )
    ゲームプログラムでは、たとえ画面が変わらなくても毎回描画するのが基本です。
        int main()
        {
            CreateDevice ^frm;
            frm = gcnew CreateDevice();
            if (frm)
            {
                if (!frm->InitializeGraphics())
                {
                    MessageBox::Show("Could not initialize Direct3D.");
                    return 0;
                }
                frm->Show();
    
                // メッセージループ
                while(frm->Created)
                {
                    frm->Render();
                    Application::DoEvents();
                }
            }
            return 0;
        }
        
  9. Command Prompt から実行すると、少し置いて空のウインドウが表示されます。

超初心者のプログラム入門(C/C++)