Archive for 7月 25th, 2012

  1. 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!                |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    |                                          |
    +------------------------------------------+