現在すべての処理をWinMain.cppに書いている。
このまま作っていくとWinMain.cppがグチャグチャになり、どこに何が書いてあるか分からなくなる。
そこで、DirectXに関する部分は別ファイルに引っ越ししよう。
この時に、複数回使うものであればクラスにする。
1回しか使わないものであればnamespaceで処理を分ける。
今から分けたいDirect3Dの初期化処理は1回しかやらないものなので、namespaceで分けることにする。
まず、こうして……
#pragma once
namespace Direct3D
{
//初期化
void Initialize(int winW, int winH, HWND hWnd);
//描画開始
void BeginDraw();
//描画終了
void EndDraw();
//解放
void Release();
};
WinMain.cppからDirect3Dに関わる部分を持ってくる(コピーじゃなくて切り取り)
#pragma once
//インクルード
#include <d3d11.h>
//リンカ
#pragma comment(lib, "d3d11.lib")
namespace Direct3D
{
//初期化
void Initialize(int winW, int winH, HWND hWnd);
//描画開始
void BeginDraw();
//描画終了
void EndDraw();
//解放
void Release();
};
まずこうして……
#include "Direct3D.h"
//変数
namespace Direct3D
{
}
//初期化
void Direct3D::Initialize(int winW, int winH, HWND hWnd)
{
}
//描画開始
void Direct3D::BeginDraw()
{
}
//描画終了
void Direct3D::EndDraw()
{
}
//解放処理
void Direct3D::Release()
{
}
Main.cppから該当する部分を持ってくる。
#include "Direct3D.h"
//変数
namespace Direct3D
{
ID3D11Device* pDevice = nullptr; //デバイス
ID3D11DeviceContext* pContext = nullptr; //デバイスコンテキスト
IDXGISwapChain* pSwapChain = nullptr; //スワップチェイン
ID3D11RenderTargetView* pRenderTargetView = nullptr; //レンダーターゲットビュー
}
//初期化
void Direct3D::Initialize(int winW, int winH, HWND hWnd)
{
///////////////////////////いろいろ準備するための設定///////////////////////////////
//いろいろな設定項目をまとめた構造体
DXGI_SWAP_CHAIN_DESC scDesc;
//とりあえず全部0
ZeroMemory(&scDesc, sizeof(scDesc));
:
:
:
以下省略(書かなくても分かるね?)
移動した処理を呼び出せばいい。
//インクルード
#include <Windows.h>
#include "Direct3D.h"
:
:
:
//エントリーポイント
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
:
:
:
//ウィンドウを表示
ShowWindow(hWnd, nCmdShow);
//Direct3D初期化
Direct3D::Initialize(winW, winH, hWnd);
//メッセージループ(何か起きるのを待つ)
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while (msg.message != WM_QUIT)
{
//メッセージあり
if (PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
//メッセージなし
else
{
//ゲームの処理
Direct3D::BeginDraw();
//描画処理
Direct3D::EndDraw();
}
}
Direct3D::Release();
return 0;
}
問題なく実行できることを確認しておこう。