Simple dx examples working.

This commit is contained in:
jmsgrogan 2023-01-05 16:57:46 +00:00
parent e0cad34d55
commit 850bd6906f
4 changed files with 18 additions and 16 deletions

View file

@ -76,15 +76,12 @@ void DirectXPainter::paintMesh(ID3D12GraphicsCommandList* commandList)
void DirectXPainter::paintText(ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory) void DirectXPainter::paintText(ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory)
{ {
auto scene = mDrawingContext->getSurface()->getScene(); auto scene = mDrawingContext->getSurface()->getScene();
auto width = mDrawingContext->getSurface()->getWidth();
auto height = mDrawingContext->getSurface()->getHeight();
for (const auto item : scene->getItems()) for (const auto item : scene->getItems())
{ {
if (item->getType() == SceneItem::Type::TEXT && item->isVisible()) if (item->getType() == SceneItem::Type::TEXT && item->isVisible())
{ {
auto text = dynamic_cast<SceneText*>(item); auto text = dynamic_cast<SceneText*>(item);
mTextPainter->paint(text, mDrawingContext, d2dContext, width, height); mTextPainter->paint(text, mDrawingContext, d2dContext, directWriteFactory);
} }
} }
} }

View file

@ -28,7 +28,6 @@ DirectXTextPainter::DirectXTextPainter()
void DirectXTextPainter::initialize(ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory) void DirectXTextPainter::initialize(ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory)
{ {
initializeBrush(d2dContext); initializeBrush(d2dContext);
initializeTextFormat(directWriteFactory);
} }
void DirectXTextPainter::initializeBrush(ID2D1DeviceContext2* d2dContext) void DirectXTextPainter::initializeBrush(ID2D1DeviceContext2* d2dContext)
@ -36,7 +35,7 @@ void DirectXTextPainter::initializeBrush(ID2D1DeviceContext2* d2dContext)
d2dContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &mTextBrush); d2dContext->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &mTextBrush);
} }
void DirectXTextPainter::initializeTextFormat(IDWriteFactory* directWriteFactory) void DirectXTextPainter::updateTextFormat(IDWriteFactory* directWriteFactory, unsigned fontSize)
{ {
directWriteFactory->CreateTextFormat( directWriteFactory->CreateTextFormat(
L"Verdana", L"Verdana",
@ -44,23 +43,26 @@ void DirectXTextPainter::initializeTextFormat(IDWriteFactory* directWriteFactory
DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_WEIGHT_NORMAL,
DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STRETCH_NORMAL,
50, fontSize,
L"en-us", L"en-us",
&mTextFormat &mTextFormat
); );
mTextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER); //mTextFormat->SetTextAlignment(DWRITE_TEXT_ALIGNMENT_CENTER);
mTextFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER); //mTextFormat->SetParagraphAlignment(DWRITE_PARAGRAPH_ALIGNMENT_CENTER);
} }
void DirectXTextPainter::paint(SceneText* text, DrawingContext* context, ID2D1DeviceContext2* d2dContext, float width, float height) void DirectXTextPainter::paint(SceneText* text, DrawingContext* context, ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory)
{ {
D2D1_RECT_F textRect = D2D1::RectF(0, 0, width, height); 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); auto content = StringUtils::convert(text->getTextData().mContent);
d2dContext->BeginDraw(); d2dContext->BeginDraw();
d2dContext->SetTransform(D2D1::Matrix3x2F::Identity()); d2dContext->SetTransform(D2D1::Matrix3x2F::Identity());
d2dContext->DrawText(content.c_str(), static_cast<UINT32>(content.size()) - 1, mTextFormat.Get(), &textRect, mTextBrush.Get()); d2dContext->DrawText(content.c_str(), static_cast<UINT32>(content.size()), mTextFormat.Get(), &textRect, mTextBrush.Get());
d2dContext->EndDraw(); d2dContext->EndDraw();
} }

View file

@ -26,11 +26,11 @@ public:
void initialize(ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory); void initialize(ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory);
void paint(SceneText* text, DrawingContext* context, ID2D1DeviceContext2* d2dContext, float width, float height); void paint(SceneText* text, DrawingContext* context, ID2D1DeviceContext2* d2dContext, IDWriteFactory* directWriteFactory);
private: private:
void initializeBrush(ID2D1DeviceContext2* d2dContext); void initializeBrush(ID2D1DeviceContext2* d2dContext);
void initializeTextFormat(IDWriteFactory* directWriteFactory); void updateTextFormat(IDWriteFactory* directWriteFactory, unsigned fontSize);
Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> mTextBrush; Microsoft::WRL::ComPtr<ID2D1SolidColorBrush> mTextBrush;
Microsoft::WRL::ComPtr<IDWriteTextFormat> mTextFormat; Microsoft::WRL::ComPtr<IDWriteTextFormat> mTextFormat;

View file

@ -8,6 +8,7 @@
#include "DesktopManager.h" #include "DesktopManager.h"
#include "MeshPrimitives.h" #include "MeshPrimitives.h"
#include "MeshNode.h" #include "MeshNode.h"
#include "TextNode.h"
#include "Scene.h" #include "Scene.h"
#include "Widget.h" #include "Widget.h"
@ -25,10 +26,12 @@ TEST_CASE(TestDirectXRendering, "graphics")
auto mesh = MeshPrimitives::buildRectangleAsTriMesh(); auto mesh = MeshPrimitives::buildRectangleAsTriMesh();
auto mesh_node = std::make_unique<MeshNode>(DiscretePoint(0, 0)); auto mesh_node = std::make_unique<MeshNode>(DiscretePoint(0, 0));
mesh_node->setMesh(mesh.get()); mesh_node->setMesh(mesh.get());
scene->addNode(mesh_node.get()); scene->addNode(mesh_node.get());
auto text_node = std::make_unique<TextNode>("Test", DiscretePoint(100, 100));
scene->addNode(text_node.get());
scene->update(nullptr); scene->update(nullptr);
gui_app->run(); gui_app->run();
}; };