Skip to content

Commit 943b0f5

Browse files
committed
Remove RenderTargetExtensions
Wasn't really necessary and makes implementation a bit more complicated.
1 parent ee07816 commit 943b0f5

File tree

4 files changed

+125
-151
lines changed

4 files changed

+125
-151
lines changed

src/thirtytwo/Win32/Graphics/Direct2D/HwndRenderTarget.cs

+6
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,11 @@ public static HwndRenderTarget CreateForWindow<TFactory, TWindow>(
6363
return new HwndRenderTarget(renderTarget);
6464
}
6565

66+
public void Resize(Size size)
67+
{
68+
Pointer->Resize((D2D_SIZE_U)size).ThrowOnFailure();
69+
GC.KeepAlive(this);
70+
}
71+
6672
public static implicit operator ID2D1HwndRenderTarget*(HwndRenderTarget target) => target.Pointer;
6773
}

src/thirtytwo/Win32/Graphics/Direct2D/RenderTarget.cs

+119
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
33

44
using System.Drawing;
5+
using System.Numerics;
56
using Windows.Support;
67
using Windows.Win32.Graphics.Direct2D.Common;
8+
using Windows.Win32.Graphics.DirectWrite;
79
using Windows.Win32.Graphics.Dxgi.Common;
810
using Windows.Win32.Graphics.Imaging;
911

@@ -17,6 +19,123 @@ public RenderTarget(ID2D1RenderTarget* renderTarget) : base((ID2D1Resource*)rend
1719
{
1820
}
1921

22+
public void BeginDraw()
23+
{
24+
Pointer->BeginDraw();
25+
GC.KeepAlive(this);
26+
}
27+
28+
public void EndDraw(out bool recreateTarget)
29+
{
30+
HRESULT result = Pointer->EndDraw(null, null);
31+
if (result == HRESULT.D2DERR_RECREATE_TARGET)
32+
{
33+
recreateTarget = true;
34+
}
35+
else
36+
{
37+
result.ThrowOnFailure();
38+
recreateTarget = false;
39+
}
40+
41+
GC.KeepAlive(this);
42+
}
43+
44+
public SolidColorBrush CreateSolidColorBrush(Color color)
45+
{
46+
ID2D1SolidColorBrush* solidColorBrush;
47+
D2D1_COLOR_F colorf = (D2D1_COLOR_F)color;
48+
Pointer->CreateSolidColorBrush(&colorf, null, &solidColorBrush).ThrowOnFailure();
49+
GC.KeepAlive(this);
50+
return new SolidColorBrush(solidColorBrush);
51+
}
52+
53+
public void SetTransform(Matrix3x2 transform)
54+
{
55+
Pointer->SetTransform((D2D_MATRIX_3X2_F*)&transform);
56+
GC.KeepAlive(this);
57+
}
58+
59+
public void Clear(Color color)
60+
{
61+
D2D1_COLOR_F colorf = (D2D1_COLOR_F)color;
62+
Pointer->Clear(&colorf);
63+
GC.KeepAlive(this);
64+
}
65+
66+
public void DrawLine(PointF point0, PointF point1, Brush brush, float strokeWidth = 1.0f)
67+
{
68+
Pointer->DrawLine(*(D2D_POINT_2F*)&point0, *(D2D_POINT_2F*)&point1, brush.Pointer, strokeWidth, null);
69+
GC.KeepAlive(this);
70+
}
71+
72+
public void FillRectangle(RectangleF rect, Brush brush)
73+
{
74+
D2D_RECT_F rectf = (D2D_RECT_F)rect;
75+
Pointer->FillRectangle(&rectf, brush.Pointer);
76+
GC.KeepAlive(this);
77+
}
78+
79+
public void DrawRectangle(RectangleF rect, Brush brush, float strokeWidth = 1.0f)
80+
{
81+
D2D_RECT_F rectf = (D2D_RECT_F)rect;
82+
Pointer->DrawRectangle(&rectf, brush.Pointer, strokeWidth, null);
83+
GC.KeepAlive(this);
84+
}
85+
86+
public SizeF Size()
87+
{
88+
D2D_SIZE_F size = Pointer->GetSizeHack();
89+
GC.KeepAlive(this);
90+
return *(SizeF*)&size;
91+
}
92+
93+
/// <inheritdoc cref="ID2D1RenderTarget.DrawTextLayout(D2D_POINT_2F, IDWriteTextLayout*, ID2D1Brush*, D2D1_DRAW_TEXT_OPTIONS)"/>
94+
public void DrawTextLayout(
95+
PointF origin,
96+
TextLayout textLayout,
97+
Brush defaultFillBrush,
98+
DrawTextOptions options = DrawTextOptions.None)
99+
{
100+
Pointer->DrawTextLayout(
101+
origin,
102+
textLayout.Pointer,
103+
defaultFillBrush.Pointer,
104+
(D2D1_DRAW_TEXT_OPTIONS)options);
105+
106+
GC.KeepAlive(this);
107+
GC.KeepAlive(textLayout);
108+
GC.KeepAlive(defaultFillBrush);
109+
}
110+
111+
public void DrawBitmap(
112+
Bitmap bitmap,
113+
RectangleF destinationRectangle = default,
114+
float opacity = 1.0f,
115+
BitmapInterpolationMode interpolationMode = BitmapInterpolationMode.Linear)
116+
{
117+
D2D_RECT_F destination = (D2D_RECT_F)destinationRectangle;
118+
if (destinationRectangle.IsEmpty)
119+
{
120+
D2D_SIZE_F size = Pointer->GetSizeHack();
121+
destination = new D2D_RECT_F { left = 0, top = 0, right = size.width, bottom = size.height };
122+
}
123+
else
124+
{
125+
destination = (D2D_RECT_F)destinationRectangle;
126+
}
127+
128+
Pointer->DrawBitmap(
129+
bitmap.Pointer,
130+
&destination,
131+
opacity,
132+
(D2D1_BITMAP_INTERPOLATION_MODE)interpolationMode,
133+
null);
134+
135+
GC.KeepAlive(this);
136+
GC.KeepAlive(bitmap);
137+
}
138+
20139
/// <inheritdoc cref="ID2D1RenderTarget.CreateBitmapFromWicBitmap(IWICBitmapSource*, D2D1_BITMAP_PROPERTIES*, ID2D1Bitmap**)"/>
21140
public Bitmap CreateBitmapFromWicBitmap<TBitmapSource>(
22141
TBitmapSource wicBitmap)

src/thirtytwo/Win32/Graphics/Direct2D/RenderTargetExtensions.cs

-150
This file was deleted.

src/thirtytwo/Win32/Graphics/Imaging/FormatConverter.cs

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public FormatConverter(IWICFormatConverter* pointer) : base((IWICBitmapSource*)p
1313
public FormatConverter() : this(Create()) { }
1414
public FormatConverter(BitmapSource source) : this(Create()) => Initialize(source);
1515

16-
1716
private static IWICFormatConverter* Create()
1817
{
1918
IWICFormatConverter* converter;

0 commit comments

Comments
 (0)