(2019年5月6日)
■使用ソフト
・Visual Studio Community 2019
■言語
・C/C++
■Windows SDK バージョン
・10.0.17763.0
※Windows SDK バージョンの変更方法
■手順
1.基本的には以下の流れ参照
【C++】 DirectX11 - Direct2D 1.1の初期化(ComPtr使用) //これをベースにコードを変更する
【C++】 メッセージボックスの作成 //プロジェクトの作成からビルドまでの操作方法について
2.以下の4つのファイルを作成してビルドを行う。
(1)Main.h
(2)Main.cpp
(3)DirectX.h
(4)DirectX.cpp
(1)Main.h
#pragma once
#include <windows.h>
#pragma comment(lib,"winmm.lib")
//--------------------------------------------------------------------------------------
// Windowクラス:Window関係
//--------------------------------------------------------------------------------------
class Window
{
public:
HRESULT InitWindow(HINSTANCE hInstance, int nCmdShow);
void InitFps();
void CalculationFps();
void CalculationSleep();
void CalculationFrameTime();
static HWND GethWnd();
static int GetClientWidth();
static int GetClientHeight();
static double GetFps();
static double GetFrameTime();
private:
LARGE_INTEGER m_freq = { 0 };
LARGE_INTEGER m_starttime = { 0 };
LARGE_INTEGER m_nowtime = { 0 };
LARGE_INTEGER m_frametime_a = { 0 };
LARGE_INTEGER m_frametime_b = { 0 };
int m_iCount = 0;
static HWND g_hWnd;
static int g_iClientWidth;
static int g_iClientHeight;
static double g_dFps;
static double g_dFrameTime;
};
#include <windows.h>
#pragma comment(lib,"winmm.lib")
//--------------------------------------------------------------------------------------
// Windowクラス:Window関係
//--------------------------------------------------------------------------------------
class Window
{
public:
HRESULT InitWindow(HINSTANCE hInstance, int nCmdShow);
void InitFps();
void CalculationFps();
void CalculationSleep();
void CalculationFrameTime();
static HWND GethWnd();
static int GetClientWidth();
static int GetClientHeight();
static double GetFps();
static double GetFrameTime();
private:
LARGE_INTEGER m_freq = { 0 };
LARGE_INTEGER m_starttime = { 0 };
LARGE_INTEGER m_nowtime = { 0 };
LARGE_INTEGER m_frametime_a = { 0 };
LARGE_INTEGER m_frametime_b = { 0 };
int m_iCount = 0;
static HWND g_hWnd;
static int g_iClientWidth;
static int g_iClientHeight;
static double g_dFps;
static double g_dFrameTime;
};
(2)Main.cpp
#include "Main.h"
#include "DirectX.h"
//--------------------------------------------------------------------------------------
// 静的メンバ
//--------------------------------------------------------------------------------------
HWND Window::g_hWnd = nullptr;//ウィンドウハンドル
int Window::g_iClientWidth = 800;//クライアント領域の横幅
int Window::g_iClientHeight = 600;//クライアント領域の高さ
double Window::g_dFps = 0;//FPS
double Window::g_dFrameTime = 0;//1フレームあたりの時間
//--------------------------------------------------------------------------------------
// 前方宣言
//--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//--------------------------------------------------------------------------------------
// wWinMain()関数:エントリーポイント
//--------------------------------------------------------------------------------------
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
if (FAILED(CoInitialize(nullptr)))//COMの初期化
return 0;
Window win;
if (FAILED(win.InitWindow(hInstance, nCmdShow)))
return 0;
DirectX11 dx;
if (FAILED(dx.InitDevice()))
return 0;
win.InitFps();
// メインメッセージループ
MSG msg = { 0 };
while (WM_QUIT != msg.message)
{
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
win.CalculationFps();
win.CalculationFrameTime();
dx.Render();
win.CalculationSleep();
}
}
CoUninitialize();//COMの終了処理
return (int)msg.wParam;
}
//--------------------------------------------------------------------------------------
// WndProc()関数:ウィンドウプロシージャ
//--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT uiMessage, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (uiMessage)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uiMessage, wParam, lParam);
}
return 0;
}
//--------------------------------------------------------------------------------------
// Window::InitWindow()関数:ウィンドウの表示
//--------------------------------------------------------------------------------------
HRESULT Window::InitWindow(HINSTANCE hInstance, int nCmdShow)
{
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 = nullptr;
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = nullptr;
wcex.lpszClassName = L"WindowClass";
wcex.hIconSm = nullptr;
if (!RegisterClassEx(&wcex))
return E_FAIL;
RECT rc = { 0, 0, g_iClientWidth, g_iClientHeight };
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
g_hWnd = CreateWindow(L"WindowClass", L"1次元セル・オートマトン",//★---変更---
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, nullptr, nullptr, hInstance,
nullptr);
if (!g_hWnd)
return E_FAIL;
ShowWindow(g_hWnd, nCmdShow);
return S_OK;
}
//--------------------------------------------------------------------------------------
// Window::InitFps()関数:FPS計測の初期化
//--------------------------------------------------------------------------------------
void Window::InitFps()
{
QueryPerformanceFrequency(&m_freq);
QueryPerformanceCounter(&m_starttime);//現在の時間を取得(1フレーム目)
}
//--------------------------------------------------------------------------------------
// Window::CalculationFps()関数:FPS計測
//--------------------------------------------------------------------------------------
void Window::CalculationFps()
{
if (m_iCount == 60)//カウントが60の時の処理
{
QueryPerformanceCounter(&m_nowtime);//現在の時間を取得(60フレーム目)
//FPS = 1秒 / 1フレームの描画にかかる時間
// = 1000ms / ((現在の時間ms - 1フレーム目の時間ms) / 60フレーム)
g_dFps = 1000.0 / (static_cast<double>((m_nowtime.QuadPart - m_starttime.QuadPart) * 1000 / m_freq.QuadPart) / 60.0);
m_iCount = 0;//カウントを初期値に戻す
m_starttime = m_nowtime;//1フレーム目の時間を現在の時間にする
}
m_iCount++;//カウント+1
}
//--------------------------------------------------------------------------------------
// Window::CalculationSleep()関数:Sleepさせる時間の計算
//--------------------------------------------------------------------------------------
void Window::CalculationSleep()
{
QueryPerformanceCounter(&m_nowtime);//現在の時間を取得
//Sleepさせる時間ms = 1フレーム目から現在のフレームまでの描画にかかるべき時間ms - 1フレーム目から現在のフレームまで実際にかかった時間ms
// = (1000ms / 60)*フレーム数 - (現在の時間ms - 1フレーム目の時間ms)
DWORD dwSleepTime = static_cast<DWORD>((1000.0 / 60.0) * m_iCount - (m_nowtime.QuadPart - m_starttime.QuadPart) * 1000 / m_freq.QuadPart);
if (dwSleepTime > 0 && dwSleepTime < 18)//大きく変動がなければSleepTimeは1~17の間に納まる
{
timeBeginPeriod(1);
Sleep(dwSleepTime);
timeEndPeriod(1);
}
else//大きく変動があった場合
{
timeBeginPeriod(1);
Sleep(1);
timeEndPeriod(1);
}
}
//--------------------------------------------------------------------------------------
// Window::CalculationFrameTime()関数:1フレームあたりの時間の計測
//--------------------------------------------------------------------------------------
void Window::CalculationFrameTime()
{
static int iFlg;
if (iFlg == 0)
{
QueryPerformanceCounter(&m_frametime_a);
iFlg = 1;
}
QueryPerformanceCounter(&m_frametime_b);
g_dFrameTime = (m_frametime_b.QuadPart - m_frametime_a.QuadPart) * 1000.0 / m_freq.QuadPart;
m_frametime_a = m_frametime_b;
}
//--------------------------------------------------------------------------------------
// Window::GethWnd()関数:hWndの取得
//--------------------------------------------------------------------------------------
HWND Window::GethWnd()
{
return g_hWnd;
}
//--------------------------------------------------------------------------------------
// Window::GetClientWidth()関数:クライアント領域の横幅の取得
//--------------------------------------------------------------------------------------
int Window::GetClientWidth()
{
return g_iClientWidth;
}
//--------------------------------------------------------------------------------------
// Window::GetClientHeight()関数:クライアント領域の高さの取得
//--------------------------------------------------------------------------------------
int Window::GetClientHeight()
{
return g_iClientHeight;
}
//--------------------------------------------------------------------------------------
// Window::GetFps()関数:FPSの取得
//--------------------------------------------------------------------------------------
double Window::GetFps()
{
return g_dFps;
}
//--------------------------------------------------------------------------------------
// Window::GetFrameTime()関数:1フレームあたりの時間の取得
//--------------------------------------------------------------------------------------
double Window::GetFrameTime()
{
return g_dFrameTime;
}
#include "DirectX.h"
//--------------------------------------------------------------------------------------
// 静的メンバ
//--------------------------------------------------------------------------------------
HWND Window::g_hWnd = nullptr;//ウィンドウハンドル
int Window::g_iClientWidth = 800;//クライアント領域の横幅
int Window::g_iClientHeight = 600;//クライアント領域の高さ
double Window::g_dFps = 0;//FPS
double Window::g_dFrameTime = 0;//1フレームあたりの時間
//--------------------------------------------------------------------------------------
// 前方宣言
//--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//--------------------------------------------------------------------------------------
// wWinMain()関数:エントリーポイント
//--------------------------------------------------------------------------------------
int WINAPI wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow)
{
if (FAILED(CoInitialize(nullptr)))//COMの初期化
return 0;
Window win;
if (FAILED(win.InitWindow(hInstance, nCmdShow)))
return 0;
DirectX11 dx;
if (FAILED(dx.InitDevice()))
return 0;
win.InitFps();
// メインメッセージループ
MSG msg = { 0 };
while (WM_QUIT != msg.message)
{
if (PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
win.CalculationFps();
win.CalculationFrameTime();
dx.Render();
win.CalculationSleep();
}
}
CoUninitialize();//COMの終了処理
return (int)msg.wParam;
}
//--------------------------------------------------------------------------------------
// WndProc()関数:ウィンドウプロシージャ
//--------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT uiMessage, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
switch (uiMessage)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uiMessage, wParam, lParam);
}
return 0;
}
//--------------------------------------------------------------------------------------
// Window::InitWindow()関数:ウィンドウの表示
//--------------------------------------------------------------------------------------
HRESULT Window::InitWindow(HINSTANCE hInstance, int nCmdShow)
{
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 = nullptr;
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wcex.lpszMenuName = nullptr;
wcex.lpszClassName = L"WindowClass";
wcex.hIconSm = nullptr;
if (!RegisterClassEx(&wcex))
return E_FAIL;
RECT rc = { 0, 0, g_iClientWidth, g_iClientHeight };
AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, FALSE);
g_hWnd = CreateWindow(L"WindowClass", L"1次元セル・オートマトン",//★---変更---
WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, rc.right - rc.left, rc.bottom - rc.top, nullptr, nullptr, hInstance,
nullptr);
if (!g_hWnd)
return E_FAIL;
ShowWindow(g_hWnd, nCmdShow);
return S_OK;
}
//--------------------------------------------------------------------------------------
// Window::InitFps()関数:FPS計測の初期化
//--------------------------------------------------------------------------------------
void Window::InitFps()
{
QueryPerformanceFrequency(&m_freq);
QueryPerformanceCounter(&m_starttime);//現在の時間を取得(1フレーム目)
}
//--------------------------------------------------------------------------------------
// Window::CalculationFps()関数:FPS計測
//--------------------------------------------------------------------------------------
void Window::CalculationFps()
{
if (m_iCount == 60)//カウントが60の時の処理
{
QueryPerformanceCounter(&m_nowtime);//現在の時間を取得(60フレーム目)
//FPS = 1秒 / 1フレームの描画にかかる時間
// = 1000ms / ((現在の時間ms - 1フレーム目の時間ms) / 60フレーム)
g_dFps = 1000.0 / (static_cast<double>((m_nowtime.QuadPart - m_starttime.QuadPart) * 1000 / m_freq.QuadPart) / 60.0);
m_iCount = 0;//カウントを初期値に戻す
m_starttime = m_nowtime;//1フレーム目の時間を現在の時間にする
}
m_iCount++;//カウント+1
}
//--------------------------------------------------------------------------------------
// Window::CalculationSleep()関数:Sleepさせる時間の計算
//--------------------------------------------------------------------------------------
void Window::CalculationSleep()
{
QueryPerformanceCounter(&m_nowtime);//現在の時間を取得
//Sleepさせる時間ms = 1フレーム目から現在のフレームまでの描画にかかるべき時間ms - 1フレーム目から現在のフレームまで実際にかかった時間ms
// = (1000ms / 60)*フレーム数 - (現在の時間ms - 1フレーム目の時間ms)
DWORD dwSleepTime = static_cast<DWORD>((1000.0 / 60.0) * m_iCount - (m_nowtime.QuadPart - m_starttime.QuadPart) * 1000 / m_freq.QuadPart);
if (dwSleepTime > 0 && dwSleepTime < 18)//大きく変動がなければSleepTimeは1~17の間に納まる
{
timeBeginPeriod(1);
Sleep(dwSleepTime);
timeEndPeriod(1);
}
else//大きく変動があった場合
{
timeBeginPeriod(1);
Sleep(1);
timeEndPeriod(1);
}
}
//--------------------------------------------------------------------------------------
// Window::CalculationFrameTime()関数:1フレームあたりの時間の計測
//--------------------------------------------------------------------------------------
void Window::CalculationFrameTime()
{
static int iFlg;
if (iFlg == 0)
{
QueryPerformanceCounter(&m_frametime_a);
iFlg = 1;
}
QueryPerformanceCounter(&m_frametime_b);
g_dFrameTime = (m_frametime_b.QuadPart - m_frametime_a.QuadPart) * 1000.0 / m_freq.QuadPart;
m_frametime_a = m_frametime_b;
}
//--------------------------------------------------------------------------------------
// Window::GethWnd()関数:hWndの取得
//--------------------------------------------------------------------------------------
HWND Window::GethWnd()
{
return g_hWnd;
}
//--------------------------------------------------------------------------------------
// Window::GetClientWidth()関数:クライアント領域の横幅の取得
//--------------------------------------------------------------------------------------
int Window::GetClientWidth()
{
return g_iClientWidth;
}
//--------------------------------------------------------------------------------------
// Window::GetClientHeight()関数:クライアント領域の高さの取得
//--------------------------------------------------------------------------------------
int Window::GetClientHeight()
{
return g_iClientHeight;
}
//--------------------------------------------------------------------------------------
// Window::GetFps()関数:FPSの取得
//--------------------------------------------------------------------------------------
double Window::GetFps()
{
return g_dFps;
}
//--------------------------------------------------------------------------------------
// Window::GetFrameTime()関数:1フレームあたりの時間の取得
//--------------------------------------------------------------------------------------
double Window::GetFrameTime()
{
return g_dFrameTime;
}
(3)DirectX.h
#pragma once
#pragma comment(lib,"d3d11.lib")
#pragma comment(lib,"d2d1.lib")
#pragma comment(lib,"dwrite.lib")
#include <d3d11_1.h>
#include <directxcolors.h>
#include <d2d1_1.h>
#include <dwrite.h>
#include <wchar.h>
#include <wrl/client.h>
#include <random>
#include <vector>
//--------------------------------------------------------------------------------------
// DirectX11クラス:DirectX関係
//--------------------------------------------------------------------------------------
class DirectX11
{
public:
DirectX11();
~DirectX11();
HRESULT InitDevice();
void Render();
private:
//------------------------------------------------------------
// DirectX11とDirect2D 1.1の初期化
//------------------------------------------------------------
Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_D3DDeviceContext;
Microsoft::WRL::ComPtr<ID2D1DeviceContext> m_D2DDeviceContext;
Microsoft::WRL::ComPtr<IDXGISwapChain1> m_DXGISwapChain1;
Microsoft::WRL::ComPtr<ID2D1Bitmap1> m_D2DBitmap1;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> m_D3DRenderTargetView;
//------------------------------------------------------------
// DirectWriteの初期化
//------------------------------------------------------------
Microsoft::WRL::ComPtr<IDWriteTextFormat> m_DWriteTextFormat;
Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> m_D2DSolidBrush;
//------------------------------------------------------------
// ビットマップ作成
//------------------------------------------------------------
Microsoft::WRL::ComPtr<ID2D1Bitmap> m_D2DBitmap;
};
#pragma comment(lib,"d3d11.lib")
#pragma comment(lib,"d2d1.lib")
#pragma comment(lib,"dwrite.lib")
#include <d3d11_1.h>
#include <directxcolors.h>
#include <d2d1_1.h>
#include <dwrite.h>
#include <wchar.h>
#include <wrl/client.h>
#include <random>
#include <vector>
//--------------------------------------------------------------------------------------
// DirectX11クラス:DirectX関係
//--------------------------------------------------------------------------------------
class DirectX11
{
public:
DirectX11();
~DirectX11();
HRESULT InitDevice();
void Render();
private:
//------------------------------------------------------------
// DirectX11とDirect2D 1.1の初期化
//------------------------------------------------------------
Microsoft::WRL::ComPtr<ID3D11DeviceContext> m_D3DDeviceContext;
Microsoft::WRL::ComPtr<ID2D1DeviceContext> m_D2DDeviceContext;
Microsoft::WRL::ComPtr<IDXGISwapChain1> m_DXGISwapChain1;
Microsoft::WRL::ComPtr<ID2D1Bitmap1> m_D2DBitmap1;
Microsoft::WRL::ComPtr<ID3D11RenderTargetView> m_D3DRenderTargetView;
//------------------------------------------------------------
// DirectWriteの初期化
//------------------------------------------------------------
Microsoft::WRL::ComPtr<IDWriteTextFormat> m_DWriteTextFormat;
Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> m_D2DSolidBrush;
//------------------------------------------------------------
// ビットマップ作成
//------------------------------------------------------------
Microsoft::WRL::ComPtr<ID2D1Bitmap> m_D2DBitmap;
};
(4)DirectX.cpp
#include "Main.h"
#include "DirectX.h"
//--------------------------------------------------------------------------------------
// DirectX11::DirectX11()関数:コンストラクタ
//--------------------------------------------------------------------------------------
DirectX11::DirectX11()
{
}
//--------------------------------------------------------------------------------------
// DirectX11::~DirectX11関数:デストラクタ
//--------------------------------------------------------------------------------------
DirectX11::~DirectX11()
{
}
//--------------------------------------------------------------------------------------
// DirectX11::InitDevice():DirectX関係の初期化
//--------------------------------------------------------------------------------------
HRESULT DirectX11::InitDevice()
{
//------------------------------------------------------------
// DirectX11とDirect2D 1.1の初期化
//------------------------------------------------------------
HRESULT hr = S_OK;
UINT uiDeviceFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;//DirectX11上でDirect2Dを使用するために必要
#ifdef _DEBUG
uiDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
D3D_DRIVER_TYPE drivertypes[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE,
};
UINT uiDriverTypesNum = ARRAYSIZE(drivertypes);
D3D_FEATURE_LEVEL featurelevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
UINT uiFeatureLevelsNum = ARRAYSIZE(featurelevels);
Microsoft::WRL::ComPtr<ID3D11Device> D3DDevice;
D3D_DRIVER_TYPE drivertype = D3D_DRIVER_TYPE_NULL;
D3D_FEATURE_LEVEL featurelevel = D3D_FEATURE_LEVEL_11_0;
for (UINT uiDriverTypeIndex = 0; uiDriverTypeIndex < uiDriverTypesNum; uiDriverTypeIndex++)
{
drivertype = drivertypes[uiDriverTypeIndex];
hr = D3D11CreateDevice(nullptr, drivertype, nullptr, uiDeviceFlags, featurelevels, uiFeatureLevelsNum,
D3D11_SDK_VERSION, &D3DDevice, &featurelevel, &m_D3DDeviceContext);//&D3DDevice &m_D3DDeviceContext 初期化
if (hr == E_INVALIDARG)
{
hr = D3D11CreateDevice(nullptr, drivertype, nullptr, uiDeviceFlags, &featurelevels[1], uiFeatureLevelsNum - 1,
D3D11_SDK_VERSION, &D3DDevice, &featurelevel, &m_D3DDeviceContext);//&D3DDevice &m_D3DDeviceContext 初期化
}
if (SUCCEEDED(hr))
break;
}
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<IDXGIDevice2> DXGIDevice2;
hr = D3DDevice.As(&DXGIDevice2);//D3DDevice->QueryInterface()ではなくD3DDevice.As()、&DXGIDevice2 初期化
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ID2D1Factory1> D2DFactory1;
D2D1_FACTORY_OPTIONS factoryoptions = {};
#ifdef _DEBUG
factoryoptions.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
#endif
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, factoryoptions, D2DFactory1.GetAddressOf());//&D2DFactory1ではなくD2DFactory1.GetAddressOf()
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ID2D1Device> D2D1Device;
hr = D2DFactory1->CreateDevice(DXGIDevice2.Get(), &D2D1Device);//DXGIDevice2ではなくDXGIDevice2.Get()、&D2D1Device 初期化
if (FAILED(hr))
return hr;
hr = D2D1Device->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &m_D2DDeviceContext);//&m_D2DDeviceContext 初期化
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<IDXGIAdapter> DXGIAdapter;
hr = DXGIDevice2->GetAdapter(&DXGIAdapter);//&DXGIAdapter 初期化
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<IDXGIFactory2> DXGIFactory2;
hr = DXGIAdapter->GetParent(IID_PPV_ARGS(&DXGIFactory2));//&DXGIFactory2 初期化
if (FAILED(hr))
return hr;
DXGI_SWAP_CHAIN_DESC1 desc = {};
desc.Width = Window::GetClientWidth();
desc.Height = Window::GetClientHeight();
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
desc.Stereo = FALSE;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
desc.BufferCount = 2;
desc.Scaling = DXGI_SCALING_STRETCH;
desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
desc.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;
hr = DXGIFactory2->CreateSwapChainForHwnd(D3DDevice.Get(), Window::GethWnd(), &desc, nullptr, nullptr, &m_DXGISwapChain1);//D3DDeviceではなくD3DDevice.Get()、&m_DXGISwapChain1 初期化
if (FAILED(hr))
return hr;
(void)DXGIDevice2->SetMaximumFrameLatency(1);
DXGIFactory2->MakeWindowAssociation(Window::GethWnd(), DXGI_MWA_NO_ALT_ENTER);//Alt+Enter時フルスクリーンを無効
Microsoft::WRL::ComPtr<IDXGISurface2> DXGISurface2;
hr = m_DXGISwapChain1->GetBuffer(0, IID_PPV_ARGS(&DXGISurface2));//&DXGISurface2 初期化
if (FAILED(hr))
return hr;
hr = m_D2DDeviceContext->CreateBitmapFromDxgiSurface(DXGISurface2.Get(),//DXGISurface2ではなくDXGISurface2.Get()
D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE)), &m_D2DBitmap1);//&m_D2DBitmap1 初期化
if (FAILED(hr))
return hr;
m_D2DDeviceContext->SetTarget(m_D2DBitmap1.Get());//D2DBitmap1ではなくD2DBitmap1.Get()
Microsoft::WRL::ComPtr<ID3D11Texture2D> D3DTexture2D;
hr = m_DXGISwapChain1->GetBuffer(0, IID_PPV_ARGS(&D3DTexture2D));//&D3DTexture2D 初期化
if (FAILED(hr))
return hr;
hr = D3DDevice->CreateRenderTargetView(D3DTexture2D.Get(), nullptr, &m_D3DRenderTargetView);//D3DTexture2DではなくD3DTexture2D.Get()、&m_D3DRenderTargetView 初期化
if (FAILED(hr))
return hr;
m_D3DDeviceContext->OMSetRenderTargets(1, m_D3DRenderTargetView.GetAddressOf(), nullptr);//&m_D3DRenderTargetViewではなくm_D3DRenderTargetView.GetAddressOf()
D3D11_VIEWPORT vp;
vp.Width = (FLOAT)Window::GetClientWidth();
vp.Height = (FLOAT)Window::GetClientHeight();
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
m_D3DDeviceContext->RSSetViewports(1, &vp);
//------------------------------------------------------------
// DirectWriteの初期化
//------------------------------------------------------------
Microsoft::WRL::ComPtr<IDWriteFactory> DWriteFactory;
hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), &DWriteFactory);//&DWriteFactory 初期化
if (FAILED(hr))
return hr;
//関数CreateTextFormat()
//第1引数:フォント名(L"メイリオ", L"Arial", L"Meiryo UI"等)
//第2引数:フォントコレクション(nullptr)
//第3引数:フォントの太さ(DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_WEIGHT_BOLD等)
//第4引数:フォントスタイル(DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STYLE_OBLIQUE, DWRITE_FONT_STYLE_ITALIC)
//第5引数:フォントの幅(DWRITE_FONT_STRETCH_NORMAL,DWRITE_FONT_STRETCH_EXTRA_EXPANDED等)
//第6引数:フォントサイズ(20, 30等)
//第7引数:ロケール名(L"")
//第8引数:テキストフォーマット(&g_pTextFormat)
hr = DWriteFactory->CreateTextFormat(L"メイリオ", nullptr, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 20, L"", &m_DWriteTextFormat);//&m_DWriteTextFormat 初期化
if (FAILED(hr))
return hr;
//関数SetTextAlignment()
//第1引数:テキストの配置(DWRITE_TEXT_ALIGNMENT_LEADING:前, DWRITE_TEXT_ALIGNMENT_TRAILING:後, DWRITE_TEXT_ALIGNMENT_CENTER:中央)
hr = m_DWriteTextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
if (FAILED(hr))
return hr;
//関数CreateSolidColorBrush()
//第1引数:フォント色(D2D1::ColorF(D2D1::ColorF::Black):黒, D2D1::ColorF(D2D1::ColorF(0.0f, 0.2f, 0.9f, 1.0f)):RGBA指定)
hr = m_D2DDeviceContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &m_D2DSolidBrush);//&m_D2DSolidBrush 初期化
if (FAILED(hr))
return hr;
//--------------------------★変更↓--------------------------
//------------------------------------------------------------
// ビットマップ作成
//------------------------------------------------------------
const int iPixSize = 256;//縦横ピクセル数
hr = m_D2DDeviceContext->CreateBitmap(D2D1::SizeU(iPixSize, iPixSize), D2D1::BitmapProperties(D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)), &m_D2DBitmap);//&m_D2DBitmap 初期化
if (FAILED(hr))
return hr;
//--------------------------★変更↑--------------------------
return S_OK;
}
//--------------------------------------------------------------------------------------
// DirectX11::Render():DirectX関係の描画
//--------------------------------------------------------------------------------------
void DirectX11::Render()
{
m_D3DDeviceContext->ClearRenderTargetView(m_D3DRenderTargetView.Get(), DirectX::Colors::Aquamarine);//m_D3DRenderTargetViewではなくm_D3DRenderTargetView.Get()
//--------------------------★変更↓--------------------------
//------------------------------------------------------------
// 初期設定
//------------------------------------------------------------
static FLOAT fPosX = 50, fPosY = 50;//画像の左上の位置
const int iPixSize = 256;//縦横ピクセル数
//------------------------------------------------------------
// キー入力関係
//------------------------------------------------------------
//マウス入力
int iMouseL = 0, iMouseR = 0;
if (GetAsyncKeyState(VK_LBUTTON) & 0x8000) iMouseL = 1;
if (GetAsyncKeyState(VK_RBUTTON) & 0x8000) iMouseR = 1;
//マウス座標取得
POINT mousepoint;
static FLOAT fMouseX, fMouseY;
GetCursorPos(&mousepoint);//マウスのスクリーン座標取得
ScreenToClient(Window::GethWnd(), &mousepoint);//スクリーン座標をクライアント座標(アプリケーションの左上を(0, 0))に変換
fMouseX = static_cast<FLOAT>(mousepoint.x);
fMouseY = static_cast<FLOAT>(mousepoint.y);
//------------------------------------------------------------
// キー入力時の処理
//------------------------------------------------------------
static int iMouseClickFlg = 0;
static int iRule, iRuleDisplay;
if (iMouseL == 1 && iMouseClickFlg == 0)//マウス左ボタンが押されたらビットマップ書き替え
{
iMouseClickFlg = 1;
//1次元セル・オートマトン計算
iRuleDisplay = iRule;//Rule表示用変数に代入
//例:iRule=0のときiRuleArray=00000000(2進数)、iRule=1のときiRuleArray=00000001(2進数)
int iRuleArray[] = { (iRule >> 7) & 1, (iRule >> 6) & 1, (iRule >> 5) & 1, (iRule >> 4) & 1, (iRule >> 3) & 1, (iRule >> 2) & 1, (iRule >> 1) & 1, (iRule) & 1 };
iRule++;//Rule切り替え
if (iRule == 256) iRule = 0;//Rule=0~255
std::vector<std::vector<int> > vecData(iPixSize, std::vector<int>(iPixSize, 0));//vecData[iPixSize][iPixSize] = {0};
vecData[0][iPixSize / 2] = 1;//初期値
for (int y = 0; y < iPixSize - 1; y++)
{
for (int x = 0; x < iPixSize; x++)
{
int iLeft = (x - 1 + iPixSize) % iPixSize;
int iCenter = x;
int iRight = (x + 1) % iPixSize;
int iBitNum = vecData[y][iLeft] * 4 + vecData[y][iCenter] * 2 + vecData[y][iRight] * 1;
vecData[y + 1][x] = iRuleArray[7 - iBitNum];
}
}
//計算結果をビットマップに反映
std::vector<byte> vecBitmapData(iPixSize * iPixSize * 4, 0);//vecBitmapData[iPixSize * iPixSize * 4] = {0};
for (int y = 0; y < iPixSize; y++)
{
for (int x = 0; x < iPixSize; x++)
{
if (vecData[y][x] == 0)
{
vecBitmapData[(x * 4) + (y * iPixSize * 4)] = 255;//Blue
vecBitmapData[(x * 4 + 1) + (y * iPixSize * 4)] = 255;//Green
vecBitmapData[(x * 4 + 2) + (y * iPixSize * 4)] = 255;//Red
vecBitmapData[(x * 4 + 3) + (y * iPixSize * 4)] = 255;
}
else if (vecData[y][x] == 1)
{
vecBitmapData[(x * 4) + (y * iPixSize * 4)] = 0;//Blue
vecBitmapData[(x * 4 + 1) + (y * iPixSize * 4)] = 0;//Green
vecBitmapData[(x * 4 + 2) + (y * iPixSize * 4)] = 0;//Red
vecBitmapData[(x * 4 + 3) + (y * iPixSize * 4)] = 255;
}
}
}
m_D2DBitmap->CopyFromMemory(nullptr, &vecBitmapData.front(), iPixSize * 4);//ビットマップ書き替え
}
else if (iMouseL == 0)
{
iMouseClickFlg = 0;
}
//------------------------------------------------------------
// 文字操作
//------------------------------------------------------------
//FPS表示用
WCHAR wcText1[256] = { 0 };
swprintf(wcText1, 256, L"FPS=%lf", Window::GetFps());
//Rule表示用
WCHAR wcText2[256] = { 0 };
swprintf(wcText2, 256, L"Rule=%d", iRuleDisplay);
//------------------------------------------------------------
// 2D描画
//------------------------------------------------------------
m_D2DDeviceContext->BeginDraw();
//関数DrawBitmap()
//第1引数:レンダリングするビットマップ(m_D2DBitmapではなくm_D2DBitmap.Get())
//第2引数:ビットマップの位置の座標(D2D1::RectF(左, 上, 右, 下))
//第3引数:不透明度(0.0f~1.0f)
//第4引数:ビットマップが拡大縮小または回転される場合に使用する補間モード(D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR:ドット絵風[ギザギザ]
// D2D1_BITMAP_INTERPOLATION_MODE_LINEAR:写真風[なめらか])
//第5引数:トリミング(D2D1::RectF(左, 上, 右, 下), nullptr:イメージ全体の場合)
m_D2DDeviceContext->DrawBitmap(m_D2DBitmap.Get(), D2D1::RectF(fPosX, fPosY, fPosX + 512, fPosY + 512), 1.0f, D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR, D2D1::RectF(0, 0, iPixSize, iPixSize));
m_D2DDeviceContext->DrawText(wcText1, ARRAYSIZE(wcText1) - 1, m_DWriteTextFormat.Get(), D2D1::RectF(0, 0, 800, 20), m_D2DSolidBrush.Get(), D2D1_DRAW_TEXT_OPTIONS_NONE);//m_DWriteTextFormatではなくm_DWriteTextFormat.Get()
m_D2DDeviceContext->DrawText(wcText2, ARRAYSIZE(wcText2) - 1, m_DWriteTextFormat.Get(), D2D1::RectF(0, 20, 800, 40), m_D2DSolidBrush.Get(), D2D1_DRAW_TEXT_OPTIONS_NONE);//m_DWriteTextFormatではなくm_DWriteTextFormat.Get()
m_D2DDeviceContext->EndDraw();
//--------------------------★変更↑--------------------------
m_DXGISwapChain1->Present(0, 0);
}
#include "DirectX.h"
//--------------------------------------------------------------------------------------
// DirectX11::DirectX11()関数:コンストラクタ
//--------------------------------------------------------------------------------------
DirectX11::DirectX11()
{
}
//--------------------------------------------------------------------------------------
// DirectX11::~DirectX11関数:デストラクタ
//--------------------------------------------------------------------------------------
DirectX11::~DirectX11()
{
}
//--------------------------------------------------------------------------------------
// DirectX11::InitDevice():DirectX関係の初期化
//--------------------------------------------------------------------------------------
HRESULT DirectX11::InitDevice()
{
//------------------------------------------------------------
// DirectX11とDirect2D 1.1の初期化
//------------------------------------------------------------
HRESULT hr = S_OK;
UINT uiDeviceFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;//DirectX11上でDirect2Dを使用するために必要
#ifdef _DEBUG
uiDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
D3D_DRIVER_TYPE drivertypes[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE,
};
UINT uiDriverTypesNum = ARRAYSIZE(drivertypes);
D3D_FEATURE_LEVEL featurelevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
UINT uiFeatureLevelsNum = ARRAYSIZE(featurelevels);
Microsoft::WRL::ComPtr<ID3D11Device> D3DDevice;
D3D_DRIVER_TYPE drivertype = D3D_DRIVER_TYPE_NULL;
D3D_FEATURE_LEVEL featurelevel = D3D_FEATURE_LEVEL_11_0;
for (UINT uiDriverTypeIndex = 0; uiDriverTypeIndex < uiDriverTypesNum; uiDriverTypeIndex++)
{
drivertype = drivertypes[uiDriverTypeIndex];
hr = D3D11CreateDevice(nullptr, drivertype, nullptr, uiDeviceFlags, featurelevels, uiFeatureLevelsNum,
D3D11_SDK_VERSION, &D3DDevice, &featurelevel, &m_D3DDeviceContext);//&D3DDevice &m_D3DDeviceContext 初期化
if (hr == E_INVALIDARG)
{
hr = D3D11CreateDevice(nullptr, drivertype, nullptr, uiDeviceFlags, &featurelevels[1], uiFeatureLevelsNum - 1,
D3D11_SDK_VERSION, &D3DDevice, &featurelevel, &m_D3DDeviceContext);//&D3DDevice &m_D3DDeviceContext 初期化
}
if (SUCCEEDED(hr))
break;
}
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<IDXGIDevice2> DXGIDevice2;
hr = D3DDevice.As(&DXGIDevice2);//D3DDevice->QueryInterface()ではなくD3DDevice.As()、&DXGIDevice2 初期化
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ID2D1Factory1> D2DFactory1;
D2D1_FACTORY_OPTIONS factoryoptions = {};
#ifdef _DEBUG
factoryoptions.debugLevel = D2D1_DEBUG_LEVEL_INFORMATION;
#endif
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, factoryoptions, D2DFactory1.GetAddressOf());//&D2DFactory1ではなくD2DFactory1.GetAddressOf()
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ID2D1Device> D2D1Device;
hr = D2DFactory1->CreateDevice(DXGIDevice2.Get(), &D2D1Device);//DXGIDevice2ではなくDXGIDevice2.Get()、&D2D1Device 初期化
if (FAILED(hr))
return hr;
hr = D2D1Device->CreateDeviceContext(D2D1_DEVICE_CONTEXT_OPTIONS_NONE, &m_D2DDeviceContext);//&m_D2DDeviceContext 初期化
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<IDXGIAdapter> DXGIAdapter;
hr = DXGIDevice2->GetAdapter(&DXGIAdapter);//&DXGIAdapter 初期化
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<IDXGIFactory2> DXGIFactory2;
hr = DXGIAdapter->GetParent(IID_PPV_ARGS(&DXGIFactory2));//&DXGIFactory2 初期化
if (FAILED(hr))
return hr;
DXGI_SWAP_CHAIN_DESC1 desc = {};
desc.Width = Window::GetClientWidth();
desc.Height = Window::GetClientHeight();
desc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
desc.Stereo = FALSE;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
desc.BufferCount = 2;
desc.Scaling = DXGI_SCALING_STRETCH;
desc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
desc.AlphaMode = DXGI_ALPHA_MODE_UNSPECIFIED;
hr = DXGIFactory2->CreateSwapChainForHwnd(D3DDevice.Get(), Window::GethWnd(), &desc, nullptr, nullptr, &m_DXGISwapChain1);//D3DDeviceではなくD3DDevice.Get()、&m_DXGISwapChain1 初期化
if (FAILED(hr))
return hr;
(void)DXGIDevice2->SetMaximumFrameLatency(1);
DXGIFactory2->MakeWindowAssociation(Window::GethWnd(), DXGI_MWA_NO_ALT_ENTER);//Alt+Enter時フルスクリーンを無効
Microsoft::WRL::ComPtr<IDXGISurface2> DXGISurface2;
hr = m_DXGISwapChain1->GetBuffer(0, IID_PPV_ARGS(&DXGISurface2));//&DXGISurface2 初期化
if (FAILED(hr))
return hr;
hr = m_D2DDeviceContext->CreateBitmapFromDxgiSurface(DXGISurface2.Get(),//DXGISurface2ではなくDXGISurface2.Get()
D2D1::BitmapProperties1(D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE)), &m_D2DBitmap1);//&m_D2DBitmap1 初期化
if (FAILED(hr))
return hr;
m_D2DDeviceContext->SetTarget(m_D2DBitmap1.Get());//D2DBitmap1ではなくD2DBitmap1.Get()
Microsoft::WRL::ComPtr<ID3D11Texture2D> D3DTexture2D;
hr = m_DXGISwapChain1->GetBuffer(0, IID_PPV_ARGS(&D3DTexture2D));//&D3DTexture2D 初期化
if (FAILED(hr))
return hr;
hr = D3DDevice->CreateRenderTargetView(D3DTexture2D.Get(), nullptr, &m_D3DRenderTargetView);//D3DTexture2DではなくD3DTexture2D.Get()、&m_D3DRenderTargetView 初期化
if (FAILED(hr))
return hr;
m_D3DDeviceContext->OMSetRenderTargets(1, m_D3DRenderTargetView.GetAddressOf(), nullptr);//&m_D3DRenderTargetViewではなくm_D3DRenderTargetView.GetAddressOf()
D3D11_VIEWPORT vp;
vp.Width = (FLOAT)Window::GetClientWidth();
vp.Height = (FLOAT)Window::GetClientHeight();
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
vp.TopLeftX = 0;
vp.TopLeftY = 0;
m_D3DDeviceContext->RSSetViewports(1, &vp);
//------------------------------------------------------------
// DirectWriteの初期化
//------------------------------------------------------------
Microsoft::WRL::ComPtr<IDWriteFactory> DWriteFactory;
hr = DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), &DWriteFactory);//&DWriteFactory 初期化
if (FAILED(hr))
return hr;
//関数CreateTextFormat()
//第1引数:フォント名(L"メイリオ", L"Arial", L"Meiryo UI"等)
//第2引数:フォントコレクション(nullptr)
//第3引数:フォントの太さ(DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_WEIGHT_BOLD等)
//第4引数:フォントスタイル(DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STYLE_OBLIQUE, DWRITE_FONT_STYLE_ITALIC)
//第5引数:フォントの幅(DWRITE_FONT_STRETCH_NORMAL,DWRITE_FONT_STRETCH_EXTRA_EXPANDED等)
//第6引数:フォントサイズ(20, 30等)
//第7引数:ロケール名(L"")
//第8引数:テキストフォーマット(&g_pTextFormat)
hr = DWriteFactory->CreateTextFormat(L"メイリオ", nullptr, DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 20, L"", &m_DWriteTextFormat);//&m_DWriteTextFormat 初期化
if (FAILED(hr))
return hr;
//関数SetTextAlignment()
//第1引数:テキストの配置(DWRITE_TEXT_ALIGNMENT_LEADING:前, DWRITE_TEXT_ALIGNMENT_TRAILING:後, DWRITE_TEXT_ALIGNMENT_CENTER:中央)
hr = m_DWriteTextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_LEADING);
if (FAILED(hr))
return hr;
//関数CreateSolidColorBrush()
//第1引数:フォント色(D2D1::ColorF(D2D1::ColorF::Black):黒, D2D1::ColorF(D2D1::ColorF(0.0f, 0.2f, 0.9f, 1.0f)):RGBA指定)
hr = m_D2DDeviceContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &m_D2DSolidBrush);//&m_D2DSolidBrush 初期化
if (FAILED(hr))
return hr;
//--------------------------★変更↓--------------------------
//------------------------------------------------------------
// ビットマップ作成
//------------------------------------------------------------
const int iPixSize = 256;//縦横ピクセル数
hr = m_D2DDeviceContext->CreateBitmap(D2D1::SizeU(iPixSize, iPixSize), D2D1::BitmapProperties(D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED)), &m_D2DBitmap);//&m_D2DBitmap 初期化
if (FAILED(hr))
return hr;
//--------------------------★変更↑--------------------------
return S_OK;
}
//--------------------------------------------------------------------------------------
// DirectX11::Render():DirectX関係の描画
//--------------------------------------------------------------------------------------
void DirectX11::Render()
{
m_D3DDeviceContext->ClearRenderTargetView(m_D3DRenderTargetView.Get(), DirectX::Colors::Aquamarine);//m_D3DRenderTargetViewではなくm_D3DRenderTargetView.Get()
//--------------------------★変更↓--------------------------
//------------------------------------------------------------
// 初期設定
//------------------------------------------------------------
static FLOAT fPosX = 50, fPosY = 50;//画像の左上の位置
const int iPixSize = 256;//縦横ピクセル数
//------------------------------------------------------------
// キー入力関係
//------------------------------------------------------------
//マウス入力
int iMouseL = 0, iMouseR = 0;
if (GetAsyncKeyState(VK_LBUTTON) & 0x8000) iMouseL = 1;
if (GetAsyncKeyState(VK_RBUTTON) & 0x8000) iMouseR = 1;
//マウス座標取得
POINT mousepoint;
static FLOAT fMouseX, fMouseY;
GetCursorPos(&mousepoint);//マウスのスクリーン座標取得
ScreenToClient(Window::GethWnd(), &mousepoint);//スクリーン座標をクライアント座標(アプリケーションの左上を(0, 0))に変換
fMouseX = static_cast<FLOAT>(mousepoint.x);
fMouseY = static_cast<FLOAT>(mousepoint.y);
//------------------------------------------------------------
// キー入力時の処理
//------------------------------------------------------------
static int iMouseClickFlg = 0;
static int iRule, iRuleDisplay;
if (iMouseL == 1 && iMouseClickFlg == 0)//マウス左ボタンが押されたらビットマップ書き替え
{
iMouseClickFlg = 1;
//1次元セル・オートマトン計算
iRuleDisplay = iRule;//Rule表示用変数に代入
//例:iRule=0のときiRuleArray=00000000(2進数)、iRule=1のときiRuleArray=00000001(2進数)
int iRuleArray[] = { (iRule >> 7) & 1, (iRule >> 6) & 1, (iRule >> 5) & 1, (iRule >> 4) & 1, (iRule >> 3) & 1, (iRule >> 2) & 1, (iRule >> 1) & 1, (iRule) & 1 };
iRule++;//Rule切り替え
if (iRule == 256) iRule = 0;//Rule=0~255
std::vector<std::vector<int> > vecData(iPixSize, std::vector<int>(iPixSize, 0));//vecData[iPixSize][iPixSize] = {0};
vecData[0][iPixSize / 2] = 1;//初期値
for (int y = 0; y < iPixSize - 1; y++)
{
for (int x = 0; x < iPixSize; x++)
{
int iLeft = (x - 1 + iPixSize) % iPixSize;
int iCenter = x;
int iRight = (x + 1) % iPixSize;
int iBitNum = vecData[y][iLeft] * 4 + vecData[y][iCenter] * 2 + vecData[y][iRight] * 1;
vecData[y + 1][x] = iRuleArray[7 - iBitNum];
}
}
//計算結果をビットマップに反映
std::vector<byte> vecBitmapData(iPixSize * iPixSize * 4, 0);//vecBitmapData[iPixSize * iPixSize * 4] = {0};
for (int y = 0; y < iPixSize; y++)
{
for (int x = 0; x < iPixSize; x++)
{
if (vecData[y][x] == 0)
{
vecBitmapData[(x * 4) + (y * iPixSize * 4)] = 255;//Blue
vecBitmapData[(x * 4 + 1) + (y * iPixSize * 4)] = 255;//Green
vecBitmapData[(x * 4 + 2) + (y * iPixSize * 4)] = 255;//Red
vecBitmapData[(x * 4 + 3) + (y * iPixSize * 4)] = 255;
}
else if (vecData[y][x] == 1)
{
vecBitmapData[(x * 4) + (y * iPixSize * 4)] = 0;//Blue
vecBitmapData[(x * 4 + 1) + (y * iPixSize * 4)] = 0;//Green
vecBitmapData[(x * 4 + 2) + (y * iPixSize * 4)] = 0;//Red
vecBitmapData[(x * 4 + 3) + (y * iPixSize * 4)] = 255;
}
}
}
m_D2DBitmap->CopyFromMemory(nullptr, &vecBitmapData.front(), iPixSize * 4);//ビットマップ書き替え
}
else if (iMouseL == 0)
{
iMouseClickFlg = 0;
}
//------------------------------------------------------------
// 文字操作
//------------------------------------------------------------
//FPS表示用
WCHAR wcText1[256] = { 0 };
swprintf(wcText1, 256, L"FPS=%lf", Window::GetFps());
//Rule表示用
WCHAR wcText2[256] = { 0 };
swprintf(wcText2, 256, L"Rule=%d", iRuleDisplay);
//------------------------------------------------------------
// 2D描画
//------------------------------------------------------------
m_D2DDeviceContext->BeginDraw();
//関数DrawBitmap()
//第1引数:レンダリングするビットマップ(m_D2DBitmapではなくm_D2DBitmap.Get())
//第2引数:ビットマップの位置の座標(D2D1::RectF(左, 上, 右, 下))
//第3引数:不透明度(0.0f~1.0f)
//第4引数:ビットマップが拡大縮小または回転される場合に使用する補間モード(D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR:ドット絵風[ギザギザ]
// D2D1_BITMAP_INTERPOLATION_MODE_LINEAR:写真風[なめらか])
//第5引数:トリミング(D2D1::RectF(左, 上, 右, 下), nullptr:イメージ全体の場合)
m_D2DDeviceContext->DrawBitmap(m_D2DBitmap.Get(), D2D1::RectF(fPosX, fPosY, fPosX + 512, fPosY + 512), 1.0f, D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR, D2D1::RectF(0, 0, iPixSize, iPixSize));
m_D2DDeviceContext->DrawText(wcText1, ARRAYSIZE(wcText1) - 1, m_DWriteTextFormat.Get(), D2D1::RectF(0, 0, 800, 20), m_D2DSolidBrush.Get(), D2D1_DRAW_TEXT_OPTIONS_NONE);//m_DWriteTextFormatではなくm_DWriteTextFormat.Get()
m_D2DDeviceContext->DrawText(wcText2, ARRAYSIZE(wcText2) - 1, m_DWriteTextFormat.Get(), D2D1::RectF(0, 20, 800, 40), m_D2DSolidBrush.Get(), D2D1_DRAW_TEXT_OPTIONS_NONE);//m_DWriteTextFormatではなくm_DWriteTextFormat.Get()
m_D2DDeviceContext->EndDraw();
//--------------------------★変更↑--------------------------
m_DXGISwapChain1->Present(0, 0);
}
★部分を追加または変更
3.マウス左ボタンを押すごとにRuleが異なる1次元セル・オートマトン図形が表示される。
0 件のコメント:
コメントを投稿