70 lines
1.9 KiB
C++
70 lines
1.9 KiB
C++
#include "DirectXTextPainter.h"
|
|
|
|
#include "DrawingContext.h"
|
|
#include "DrawingSurface.h"
|
|
|
|
#include "FontsManager.h"
|
|
#include "FontGlyph.h"
|
|
|
|
#include "DirectXShaderProgram.h"
|
|
#include "TextData.h"
|
|
|
|
#include "SceneText.h"
|
|
|
|
#include "StringUtils.h"
|
|
#include "File.h"
|
|
|
|
#include <windows.h>
|
|
|
|
#include <d2d1_3.h>
|
|
#include <d2d1_1.h>
|
|
#include <dwrite.h>
|
|
|
|
DirectXTextPainter::DirectXTextPainter()
|
|
{
|
|
|
|
}
|
|
|
|
void DirectXTextPainter::initialize(ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory)
|
|
{
|
|
initializeBrush(d2dContext);
|
|
}
|
|
|
|
void DirectXTextPainter::initializeBrush(ID2D1DeviceContext2* d2dContext)
|
|
{
|
|
d2dContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &mTextBrush);
|
|
}
|
|
|
|
void DirectXTextPainter::updateTextFormat(IDWriteFactory* directWriteFactory, unsigned fontSize)
|
|
{
|
|
directWriteFactory->CreateTextFormat(
|
|
L"Verdana",
|
|
NULL,
|
|
DWRITE_FONT_WEIGHT_NORMAL,
|
|
DWRITE_FONT_STYLE_NORMAL,
|
|
DWRITE_FONT_STRETCH_NORMAL,
|
|
fontSize,
|
|
L"en-us",
|
|
&mTextFormat
|
|
);
|
|
//mTextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
|
|
//mTextFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
|
|
}
|
|
|
|
void DirectXTextPainter::paint(SceneText* text, DrawingContext* context, ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory)
|
|
{
|
|
const auto location = text->getTransform().getLocation();
|
|
D2D1_RECT_F textRect = D2D1::RectF(location.getX(), location.getY(), location.getX() + 100, location.getY() + 100);
|
|
|
|
updateTextFormat(directWriteFactory, text->getTextData().mFont.getSize());
|
|
|
|
auto content = StringUtils::convert(text->getTextData().mContent);
|
|
|
|
d2dContext->BeginDraw();
|
|
d2dContext->SetTransform(D2D1::Matrix3x2F::Identity());
|
|
d2dContext->DrawText(content.c_str(), static_cast<UINT32>(content.size()), mTextFormat.Get(), &textRect, mTextBrush.Get());
|
|
d2dContext->EndDraw();
|
|
}
|
|
|
|
|
|
|