-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
minimal integration of direct2d and directwrite
- Loading branch information
1 parent
2547b90
commit f29d6ad
Showing
12 changed files
with
239 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# generate and compile exe files | ||
# 默认是编译 64 位的 dll,如果指定 32 位,则编译 32 位 dll | ||
$currentDirectory = Get-Location | ||
$cmakeListsPath = Join-Path -Path $currentDirectory -ChildPath "CMakeLists.txt" | ||
|
||
if (-not (Test-Path $cmakeListsPath)) | ||
{ | ||
Write-Host("No CMakeLists.txt in current directory, please check.") | ||
return | ||
} | ||
|
||
Write-Host "Start generating and compiling..." | ||
|
||
|
||
$arch = $args[0] | ||
|
||
if ($arch -eq "32") | ||
{ | ||
$buildFolderPath = ".\build32" | ||
|
||
if (-not (Test-Path $buildFolderPath)) | ||
{ | ||
New-Item -ItemType Directory -Path $buildFolderPath | Out-Null | ||
Write-Host "build32 folder created." | ||
} | ||
cmake -G "Visual Studio 17 2022" -A Win32 -S . -B ./build32/ | ||
|
||
if ($LASTEXITCODE -eq 0) | ||
{ | ||
cmake --build ./build32/ --config DEBUG | ||
} | ||
} elseif ($arch -eq "64") | ||
{ | ||
$buildFolderPath = ".\build64" | ||
|
||
if (-not (Test-Path $buildFolderPath)) | ||
{ | ||
New-Item -ItemType Directory -Path $buildFolderPath | Out-Null | ||
Write-Host "build64 folder created." | ||
} | ||
cmake -G "Visual Studio 17 2022" -A x64 -S . -B ./build64/ | ||
|
||
if ($LASTEXITCODE -eq 0) | ||
{ | ||
cmake --build ./build64/ --config DEBUG | ||
} | ||
} else | ||
{ # 默认是 64 位 | ||
$buildFolderPath = ".\build64" | ||
|
||
if (-not (Test-Path $buildFolderPath)) | ||
{ | ||
New-Item -ItemType Directory -Path $buildFolderPath | Out-Null | ||
Write-Host "build64 folder created." | ||
} | ||
cmake -G "Visual Studio 17 2022" -A x64 -S . -B ./build64/ | ||
|
||
if ($LASTEXITCODE -eq 0) | ||
{ | ||
cmake --build ./build64/ --config DEBUG | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include "D2DSource.h" | ||
#include "Define.h" | ||
|
||
void Direct2DSource::CreateGlobalD2DResources() | ||
{ | ||
|
||
if (!pD2DFactory) | ||
{ | ||
D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED, &pD2DFactory); | ||
} | ||
|
||
if (!pDWriteFactory) | ||
{ | ||
DWriteCreateFactory(DWRITE_FACTORY_TYPE_SHARED, __uuidof(IDWriteFactory), | ||
reinterpret_cast<IUnknown **>(&pDWriteFactory)); | ||
} | ||
|
||
if (!pTextFormat) | ||
{ | ||
pDWriteFactory->CreateTextFormat(SAMPLEIME_FONT_DEFAULT, nullptr, DWRITE_FONT_WEIGHT_NORMAL, | ||
DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, 16.0f, | ||
SAMPLEIME_LOCALE_DEFAULT, &pTextFormat); | ||
} | ||
} | ||
|
||
void Direct2DSource::ReleaseGlobalD2DResources() | ||
{ | ||
|
||
if (pD2DFactory) | ||
pD2DFactory->Release(); | ||
if (pDWriteFactory) | ||
pDWriteFactory->Release(); | ||
if (pTextFormat) | ||
pTextFormat->Release(); | ||
|
||
pD2DFactory = nullptr; | ||
pDWriteFactory = nullptr; | ||
pTextFormat = nullptr; | ||
} | ||
|
||
void Direct2DSource::CreateWindowD2DResources(HWND hwnd) | ||
{ | ||
if (!pRenderTarget) | ||
{ | ||
RECT rc; | ||
GetClientRect(hwnd, &rc); | ||
pD2DFactory->CreateHwndRenderTarget( | ||
D2D1::RenderTargetProperties(), | ||
D2D1::HwndRenderTargetProperties(hwnd, D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top)), | ||
&pRenderTarget); | ||
pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White), &pBrush); | ||
} | ||
} | ||
|
||
void Direct2DSource::ReleaseWindowD2DResources() | ||
{ | ||
if (pRenderTarget) | ||
pRenderTarget->Release(); | ||
if (pBrush) | ||
pBrush->Release(); | ||
|
||
pRenderTarget = nullptr; | ||
pBrush = nullptr; | ||
} | ||
|
||
void Direct2DSource::DrawWithDirect2D(HWND hwnd) | ||
{ | ||
if (!pRenderTarget || !pTextFormat) | ||
return; | ||
|
||
pRenderTarget->BeginDraw(); | ||
// RGB(25, 25, 25) | ||
pRenderTarget->Clear(D2D1::ColorF(25.0f / 255.0f, 25.0f / 255.0f, 25.0f / 255.0f)); | ||
|
||
// set brush color | ||
pRenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::White), &pBrush); | ||
// draw text | ||
pRenderTarget->DrawText(L"毛笔", 2, pTextFormat, D2D1::RectF(2, 2, 100, 100), pBrush); | ||
|
||
HRESULT hr = pRenderTarget->EndDraw(); | ||
if (hr == D2DERR_RECREATE_TARGET) | ||
{ | ||
ReleaseGlobalD2DResources(); | ||
ReleaseWindowD2DResources(); | ||
CreateGlobalD2DResources(); | ||
CreateWindowD2DResources(hwnd); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include <d2d1.h> | ||
#include <dwrite.h> | ||
|
||
#pragma comment(lib, "d2d1.lib") | ||
#pragma comment(lib, "dwrite.lib") | ||
|
||
struct Direct2DSource | ||
{ | ||
ID2D1Factory *pD2DFactory = nullptr; | ||
ID2D1HwndRenderTarget *pRenderTarget = nullptr; | ||
ID2D1SolidColorBrush *pBrush = nullptr; | ||
IDWriteFactory *pDWriteFactory = nullptr; | ||
IDWriteTextFormat *pTextFormat = nullptr; | ||
|
||
// | ||
// pD2DFactory, pDWriteFactory, pTextFormat | ||
// | ||
void CreateGlobalD2DResources(); | ||
void ReleaseGlobalD2DResources(); | ||
|
||
// | ||
// pRenderTarget, pBrush | ||
// | ||
void CreateWindowD2DResources(HWND hwnd); | ||
void ReleaseWindowD2DResources(); | ||
|
||
void DrawWithDirect2D(HWND hwnd); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters