アットウィキロゴ

DirectXの初期化

まず、プロジェクトにDirectX SDKのインクルードパスを通します。

インクルードディレクトリ:$(DXSDK_DIR)Include
ライブラリディレクトリ:$(DXSDK_DIR)Lib\x86

main.cppを次のようにします。

main.cpp


/***********************************************************
    DirectXの初期化
**********************************************************/

#pragma comment( lib, "d3d9.lib" )
#include <d3d9.h>

#include <cstdlib>
#include <iostream>
#include <string>

#include "lib\Window.h"

#define SAFE_DELETE(o) {if(o)delete o;o=nullptr;}
#define SAFE_RELEASE(o) {if(o)o->Release();o=nullptr;}

/* フレームワーク用 */
Window* window = nullptr;           // ウィンドウ

IDirect3D9* direct3d = nullptr;     // DirectX9オブジェクト
IDirect3DDevice9* device = nullptr; // DirectX9描画デバイス

// エントリーポイント
void main()
{
    // メモリリークの検出を有効化
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

    try {

        // ウィンドウの作成
        if( !(window = Window::Create( TEXT("DirectXSample"), 640, 480 )) )
            throw std::runtime_error("ウィンドウの作成に失敗しました。");

        // DirectX9オブジェクトの生成
        if( !(direct3d = Direct3DCreate9( D3D_SDK_VERSION ) ))
            throw std::runtime_error("DirectX9オブジェクトの生成に失敗しました。");

        // DirectX9描画デバイスの生成
        IDirect3DDevice9* device = nullptr;
        D3DPRESENT_PARAMETERS pp = {0};
        pp.Windowed = TRUE;
        pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
        HRESULT hr = direct3d->CreateDevice(
            D3DADAPTER_DEFAULT,
            D3DDEVTYPE_HAL,
            window->GetHwnd(),  // HWND
            D3DCREATE_HARDWARE_VERTEXPROCESSING,
            &pp, &device
            );
        if( FAILED(hr) )
            throw std::runtime_error("DirectX9描画デバイスの生成に失敗しました。");

        // メインループ
        while( window->ProcessMessage() )
        {
            device->Clear( 0, nullptr, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0x22, 0x22, 0x22 ), 1.0f, 0 );
            if( SUCCEEDED( device->BeginScene() ) ){

                ; // TODO:1フレームの処理

                device->EndScene();
            }
            device->Present( nullptr, nullptr, nullptr, nullptr );
        }

    }
    // エラー処理
    catch( std::exception e ){
        MessageBoxA( nullptr, e.what(), "error", MB_OK );
    }

    // DirectXの解放
    SAFE_RELEASE(device);
    SAFE_RELEASE(direct3d);

    // ウィンドウの解放
    SAFE_DELETE(window);

    // アプリケーション終了
    exit(EXIT_SUCCESS);
}

最終更新:2013年11月18日 13:06