-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
1510 lines (1331 loc) · 79.4 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;
using GlobalStructures;
using static GlobalStructures.GlobalTools;
using Direct2D;
using static Direct2D.D2DTools;
using DXGI;
using static DXGI.DXGITools;
using WIC;
using static WIC.WICTools;
using DWrite;
using static DWrite.DWriteTools;
using Microsoft.UI.Xaml.Shapes;
using System.Text;
using Microsoft.UI.Xaml.Media.Imaging;
using System.Xml.Linq;
using Windows.Graphics.Imaging;
using System.Reflection;
using System.Collections.ObjectModel;
using System.Collections;
// To learn more about WinUI, the WinUI project structure,
// and more about our project templates, see: http://aka.ms/winui-project-info.
namespace WinUI3_SwapChainPanel_DWriteCore
{
/// <summary>
/// An empty window that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainWindow : Window
{
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr GetForegroundWindow();
[DllImport("Kernel32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern int GetUserDefaultLocaleName(StringBuilder lpLocaleName, int cchLocaleName);
public const int LOCALE_NAME_MAX_LENGTH = 85;
[DllImport("User32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern IntPtr GetCapture();
private IntPtr hWndMain = IntPtr.Zero;
ID2D1Factory m_pD2DFactory = null;
ID2D1Factory1 m_pD2DFactory1 = null;
IWICImagingFactory m_pWICImagingFactory = null;
IWICImagingFactory2 m_pWICImagingFactory2 = null;
IntPtr m_pD3D11DevicePtr = IntPtr.Zero; // Used in CreateSwapChain
ID3D11DeviceContext m_pD3D11DeviceContext = null; // Released in Clean : not used
IDXGIDevice1 m_pDXGIDevice = null;
ID2D1DeviceContext m_pD2DDeviceContext = null;
ID2D1DeviceContext3 m_pD2DDeviceContext3 = null;
IDXGISwapChain1 m_pDXGISwapChain1 = null;
ID2D1Bitmap1 m_pD2DTargetBitmap = null;
ID2D1SolidColorBrush m_pD2DMainBrush = null;
ID2D1SolidColorBrush m_pD2DSolidColorBrushRed = null;
ID2D1SolidColorBrush m_pD2DSolidColorBrushGreen = null;
ID2D1SolidColorBrush m_pD2DSolidColorBrushBlue = null;
ID2D1SolidColorBrush m_pD2DSolidColorBrushWhite = null;
ID2D1SolidColorBrush m_pD2DSolidColorBrushPink = null;
ID2D1LinearGradientBrush m_pD2DLinearGradientBrush1 = null;
ID2D1Bitmap m_pD2DBitmap1 = null;
ID2D1BitmapBrush m_pD2DBitmapBrush1 = null;
ID2D1Bitmap m_pD2DBitmap2 = null;
IDWriteFactory7 m_pDWriteFactory7 = null;
ID2D1Geometry m_pD2DGeometry1 = null;
ID2D1Geometry m_pD2DGeometry2 = null;
ID2D1Geometry m_pD2DGeometry3 = null;
ID2D1Geometry m_pD2DGeometry4 = null;
ID2D1Geometry m_pD2DGeometry5 = null;
ID2D1Geometry m_pD2DGeometry6 = null;
ID2D1Geometry m_pD2DGeometry7 = null;
float m_nComputedHeight1 = 0, m_nComputedHeight2 = 0, m_nComputedHeight3 = 0, m_nComputedHeight4 = 0,
m_nComputedHeight5 = 0, m_nComputedHeight6 = 0, m_nComputedHeight7 = 0;
IDWriteTextLayout m_pTextLayout = null;
CustomTextRenderer m_pCTR = null;
public System.Collections.ObjectModel.ObservableCollection<Font> CustomFonts = new System.Collections.ObjectModel.ObservableCollection<Font>();
public System.Collections.ObjectModel.ObservableCollection<Font> SystemFonts;// = new System.Collections.ObjectModel.ObservableCollection<Font>();
public double m_nXPos, m_nYPos, m_nWidth, m_nHeight = 0;
public MainWindow()
{
this.InitializeComponent();
hWndMain = WinRT.Interop.WindowNative.GetWindowHandle(this);
this.Title = "WinUI 3 - Test DWriteCore";
Application.Current.Resources["ComboBoxBackgroundPointerOver"] = new SolidColorBrush(Microsoft.UI.Colors.RoyalBlue);
Application.Current.Resources["ComboBoxItemBackgroundSelected"] = new SolidColorBrush(Microsoft.UI.Colors.RoyalBlue);
Application.Current.Resources["ComboBoxItemBackgroundPointerOver"] = new SolidColorBrush(Microsoft.UI.Colors.BlueViolet);
double nDisplayWidth = (Microsoft.UI.Windowing.DisplayArea.Primary.WorkArea.Width);
double nDisplayHeight = (Microsoft.UI.Windowing.DisplayArea.Primary.WorkArea.Height);
m_nWidth = 1280;
m_nHeight = 960;
m_nXPos = (nDisplayWidth - m_nWidth) / 2;
m_nYPos = (nDisplayHeight - m_nHeight) / 2;
UpdateWindowSize(hWndMain);
m_pWICImagingFactory = (IWICImagingFactory)Activator.CreateInstance(Type.GetTypeFromCLSID(WICTools.CLSID_WICImagingFactory));
m_pWICImagingFactory2 = (IWICImagingFactory2)m_pWICImagingFactory;
HRESULT hr = CreateD2D1Factory();
if (hr == HRESULT.S_OK)
{
IntPtr pDWriteFactoryPtr = IntPtr.Zero;
//hr = DWriteCreateFactory(DWrite.DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_SHARED, ref CLSID_DWriteFactory7, out pDWriteFactoryPtr);
hr = DWriteCoreCreateFactory(DWrite.DWRITE_FACTORY_TYPE.DWRITE_FACTORY_TYPE_SHARED, ref CLSID_DWriteFactory7, out pDWriteFactoryPtr);
if (hr == HRESULT.S_OK)
{
m_pDWriteFactory7 = Marshal.GetObjectForIUnknown(pDWriteFactoryPtr) as IDWriteFactory7;
if (pDWriteFactoryPtr != IntPtr.Zero)
Marshal.Release(pDWriteFactoryPtr);
string sExePath = AppContext.BaseDirectory;
var pFontCollectionLoader = new FontCollectionLoader();
hr = m_pDWriteFactory7.RegisterFontCollectionLoader(pFontCollectionLoader);
//hr = m_pDWriteFactory7.RegisterFontFileLoader(pFontCollectionLoader);
string sFullPath = System.IO.Path.Combine(sExePath, "Assets");
var pFontCollectionKey = Marshal.StringToHGlobalUni(sFullPath);
IDWriteFontCollection pFontCollection = null;
hr = m_pDWriteFactory7.CreateCustomFontCollection(pFontCollectionLoader, pFontCollectionKey, (sFullPath.Length+1)*2, out pFontCollection);
if (hr == HRESULT.S_OK)
{
Marshal.FreeHGlobal(pFontCollectionKey);
IDWriteFontCollection2 pFontCollection2 = (IDWriteFontCollection2)pFontCollection;
if (pFontCollection2 != null)
{
IDWriteFontSet1 pFontSet1;
hr = pFontCollection2.GetFontSet(out pFontSet1);
if (hr == HRESULT.S_OK)
{
hr = LoadFonts(pFontSet1, CustomFonts, true);
SafeRelease(ref pFontSet1);
}
}
//IDWriteFontCollection pFontCollection = null;
//hr = m_pDWriteFactory7.GetSystemFontCollection(out pFontCollection);
//uint nFonts = pFontCollection.GetFontFamilyCount();
IDWriteFontSet2 pFontSet2 = null;
hr = m_pDWriteFactory7.GetSystemFontSet7(false, out pFontSet2);
if (hr == HRESULT.S_OK)
{
var unorderedSystemFonts = new System.Collections.ObjectModel.ObservableCollection<Font>();
hr = LoadFonts(pFontSet2, unorderedSystemFonts, false);
SystemFonts = new ObservableCollection<Font>(unorderedSystemFonts.OrderBy(x => x.Name));
unorderedSystemFonts.Clear();
SafeRelease(ref pFontSet2);
}
string sPathFont = System.IO.Path.Combine(sExePath, "Assets\\Lovely Home.ttf");
hr = CreateDWriteTextGeometry("This is a text with slow animated gradient", sPathFont, 60.0f, false, false, out m_pD2DGeometry1, out m_nComputedHeight1);
sPathFont = System.IO.Path.Combine(sExePath, "Assets\\Lemon Shake.ttf");
CreateDWriteTextGeometry("This is a text with shadow", sPathFont, 80.0f, false, false, out m_pD2DGeometry2, out m_nComputedHeight2);
sPathFont = System.IO.Path.Combine(sExePath, "Assets\\Play Story.otf");
CreateDWriteTextGeometry("This is a text with turbulence", sPathFont, 60.0f, false, false, out m_pD2DGeometry3, out m_nComputedHeight3);
sPathFont = System.IO.Path.Combine(sExePath, "Assets\\Simplisicky Fill.ttf");
CreateDWriteTextGeometry("This is a text with glowing", sPathFont, 80.0f, false, false, out m_pD2DGeometry4, out m_nComputedHeight4);
sPathFont = System.IO.Path.Combine(sExePath, "Assets\\Bloody Scene.otf");
CreateDWriteTextGeometry("This is a text with Bitmap brush", sPathFont, 70.0f, false, false, out m_pD2DGeometry5, out m_nComputedHeight5);
sPathFont = System.IO.Path.Combine(sExePath, "Assets\\Love Craft.ttf");
CreateDWriteTextGeometry("This is a scrolling text", sPathFont, 60.0f, false, false, out m_pD2DGeometry6, out m_nComputedHeight6);
sPathFont = System.IO.Path.Combine(sExePath, "Assets\\MagicSchoolOne.ttf");
CreateDWriteTextGeometry("This is a Magic School text", sPathFont, 90.0f, false, false, out m_pD2DGeometry7, out m_nComputedHeight7);
//string sFontName = "Segoe UI Emoji";
string sFontName = "Tolkien";
//string sString = "This is a text from Layout";
string sString = "";
for (int nCode = 0x1F980; nCode <= 0x1F980 + 15; nCode++)
sString += char.ConvertFromUtf32(nCode);
int nStringLength = sString.Length;
IDWriteTextFormat pTextFormat = null;
//hr = m_pDWriteFactory7.CreateTextFormat(sFontName, pFontCollection2,
hr = m_pDWriteFactory7.CreateTextFormat("Segoe UI Emoji", null,
DWRITE_FONT_WEIGHT.DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_STYLE.DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH.DWRITE_FONT_STRETCH_NORMAL,
30, "", out pTextFormat);
if (hr == HRESULT.S_OK)
{
hr = pTextFormat.SetTextAlignment(DWRITE_TEXT_ALIGNMENT.DWRITE_TEXT_ALIGNMENT_CENTER);
hr = m_pDWriteFactory7.CreateTextLayout(sString, nStringLength, pTextFormat, 800, 60, out m_pTextLayout);
if (hr == HRESULT.S_OK)
{
//IDWriteTypography pTypography = null;
//hr = m_pDWriteFactory7.CreateTypography(out pTypography);
//if (hr == HRESULT.S_OK)
//{
// DWRITE_FONT_FEATURE ff = new DWRITE_FONT_FEATURE (
// DWRITE_FONT_FEATURE_TAG.DWRITE_FONT_FEATURE_TAG_STYLISTIC_SET_7, 1);
// hr = pTypography.AddFontFeature(ff);
// if (hr == HRESULT.S_OK)
// {
// DWRITE_TEXT_RANGE tr = new DWRITE_TEXT_RANGE (0, (uint)nStringLength);
// hr = m_pTextLayout.SetTypography(pTypography, tr);
// }
// SafeRelease(ref pTypography);
//}
}
SafeRelease(ref pTextFormat);
}
SafeRelease(ref pFontCollection);
}
}
hr = CreateDeviceContext();
hr = CreateDeviceResources();
hr = CreateSwapChain(IntPtr.Zero);
if (hr == HRESULT.S_OK)
{
hr = ConfigureSwapChain(hWndMain);
ISwapChainPanelNative panelNative = WinRT.CastExtensions.As<ISwapChainPanelNative>(scp1);
hr = panelNative.SetSwapChain(m_pDXGISwapChain1);
scp1.SizeChanged += scp1_SizeChanged;
}
m_pCTR = new CustomTextRenderer((IDWriteFactory4)m_pDWriteFactory7, (ID2D1DeviceContext4)m_pD2DDeviceContext3, new ColorF(ColorF.Enum.Orange, 1.0f), hWndMain);
CompositionTarget.Rendering += CompositionTarget_Rendering;
}
this.Closed += MainWindow_Closed;
}
private void UpdateWindowSize(IntPtr hWnd)
{
//uint nDPI = GetDpiForWindow(hWnd);
//double nScaleX = (double)nDPI / 96.0f;
//double nScaleY = (double)nDPI / 96.0f;
this.AppWindow.MoveAndResize(new Windows.Graphics.RectInt32((int)(m_nXPos), (int)(m_nYPos), (int)(m_nWidth), (int)(m_nHeight)));
// this.AppWindow.MoveAndResize(new Windows.Graphics.RectInt32((int)(m_nXPos * nScaleX), (int)(m_nYPos * nScaleY), (int)(m_nWidth * nScaleX), (int)(m_nHeight * nScaleY)));
//Console.Beep(5000, 10);
}
private HRESULT LoadFonts(IDWriteFontSet pFontSet, System.Collections.ObjectModel.ObservableCollection<Font> pFonts, bool bCustom)
{
HRESULT hr = HRESULT.S_OK;
string sExePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
uint nFontCount = pFontSet.GetFontCount();
for (uint i = 0; i < nFontCount; i++)
{
string sPath = "";
string sFamilyName = "";
string sFullName = "";
string sFontWeight = "Normal";
int nWeightValue = 400;
string sStyle = "Normal";
string sStretch = "Normal";
bool bPropertyExists = false;
IDWriteLocalizedStrings pLocalizedStrings = null;
hr = pFontSet.GetPropertyValues(i, DWRITE_FONT_PROPERTY_ID.DWRITE_FONT_PROPERTY_ID_FULL_NAME,
out bPropertyExists, out pLocalizedStrings);
if (hr == HRESULT.S_OK)
{
uint nIndex = 0;
bool bExists = false;
StringBuilder sbLocaleName = new StringBuilder(LOCALE_NAME_MAX_LENGTH);
int nDefaultLocaleSuccess = GetUserDefaultLocaleName(sbLocaleName, LOCALE_NAME_MAX_LENGTH);
if (nDefaultLocaleSuccess > 0)
{
hr = pLocalizedStrings.FindLocaleName(sbLocaleName.ToString(), out nIndex, out bExists);
}
if (hr == HRESULT.S_OK && !bExists)
{
hr = pLocalizedStrings.FindLocaleName("en-us", out nIndex, out bExists);
}
if (!bExists)
nIndex = 0;
hr = pLocalizedStrings.GetString(nIndex, sbLocaleName, LOCALE_NAME_MAX_LENGTH);
sFullName = sbLocaleName.ToString();
SafeRelease(ref pLocalizedStrings);
}
hr = pFontSet.GetPropertyValues(i, DWRITE_FONT_PROPERTY_ID.DWRITE_FONT_PROPERTY_ID_FAMILY_NAME,
out bPropertyExists, out pLocalizedStrings);
if (hr == HRESULT.S_OK)
{
uint nIndex = 0;
bool bExists = false;
StringBuilder sbLocaleName = new StringBuilder(LOCALE_NAME_MAX_LENGTH);
int nDefaultLocaleSuccess = GetUserDefaultLocaleName(sbLocaleName, LOCALE_NAME_MAX_LENGTH);
if (nDefaultLocaleSuccess > 0)
{
hr = pLocalizedStrings.FindLocaleName(sbLocaleName.ToString(), out nIndex, out bExists);
}
if (hr == HRESULT.S_OK && !bExists)
{
hr = pLocalizedStrings.FindLocaleName("en-us", out nIndex, out bExists);
}
if (!bExists)
nIndex = 0;
hr = pLocalizedStrings.GetString(nIndex, sbLocaleName, LOCALE_NAME_MAX_LENGTH);
sFamilyName = sbLocaleName.ToString();
SafeRelease(ref pLocalizedStrings);
}
hr = pFontSet.GetPropertyValues(i, DWRITE_FONT_PROPERTY_ID.DWRITE_FONT_PROPERTY_ID_WEIGHT,
out bPropertyExists, out pLocalizedStrings);
if (hr == HRESULT.S_OK)
{
StringBuilder sbLocaleName = new StringBuilder(LOCALE_NAME_MAX_LENGTH);
hr = pLocalizedStrings.GetString(0, sbLocaleName, LOCALE_NAME_MAX_LENGTH);
string sWeightString = sbLocaleName.ToString();
Int32.TryParse(sWeightString, out nWeightValue);
sFontWeight = ConvertStringToFontWeightString(sWeightString);
SafeRelease(ref pLocalizedStrings);
}
hr = pFontSet.GetPropertyValues(i, DWRITE_FONT_PROPERTY_ID.DWRITE_FONT_PROPERTY_ID_STYLE,
out bPropertyExists, out pLocalizedStrings);
if (hr == HRESULT.S_OK)
{
StringBuilder sbLocaleName = new StringBuilder(LOCALE_NAME_MAX_LENGTH);
hr = pLocalizedStrings.GetString(0, sbLocaleName, LOCALE_NAME_MAX_LENGTH);
string sStyleString = sbLocaleName.ToString();
sStyle = ConvertStringToFontStyleString(sStyleString);
SafeRelease(ref pLocalizedStrings);
}
hr = pFontSet.GetPropertyValues(i, DWRITE_FONT_PROPERTY_ID.DWRITE_FONT_PROPERTY_ID_STRETCH,
out bPropertyExists, out pLocalizedStrings);
if (hr == HRESULT.S_OK)
{
StringBuilder sbLocaleName = new StringBuilder(LOCALE_NAME_MAX_LENGTH);
hr = pLocalizedStrings.GetString(0, sbLocaleName, LOCALE_NAME_MAX_LENGTH);
string sStretchString = sbLocaleName.ToString();
sStretch = ConvertStringToFontStretchString(sStretchString);
SafeRelease(ref pLocalizedStrings);
}
IDWriteFontFaceReference pFontFaceReference = null;
hr = pFontSet.GetFontFaceReference(i, out pFontFaceReference);
if (hr == HRESULT.S_OK)
{
IDWriteFontFace3 pFontFace3;
hr = pFontFaceReference.CreateFontFace(out pFontFace3);
if (hr == HRESULT.S_OK)
{
uint nNbFiles = 0;
hr = pFontFace3.GetFiles(ref nNbFiles, null);
IDWriteFontFile[] fontFiles = new IDWriteFontFile[nNbFiles];
hr = pFontFace3.GetFiles(ref nNbFiles, fontFiles);
if (hr == HRESULT.S_OK)
{
IDWriteFontFileLoader pFontFileLoader = null;
hr = fontFiles[0].GetLoader(out pFontFileLoader);
if (hr == HRESULT.S_OK)
{
IntPtr pFontFileReferenceKey = IntPtr.Zero;
IDWriteLocalFontFileLoader pLocalFontFileLoader = null;
try
{
pLocalFontFileLoader = (IDWriteLocalFontFileLoader)pFontFileLoader;
}
catch (System.Exception ex)
{
string sError = ex.Message + "\r\n" + "HRESULT = 0x" + string.Format("{0:X}", ex.HResult);
System.Diagnostics.Debug.WriteLine(sError);
}
if (pLocalFontFileLoader != null)
{
int nFontFileReferenceKeySize = 0;
hr = fontFiles[0].GetReferenceKey(out pFontFileReferenceKey, out nFontFileReferenceKeySize);
if (hr == HRESULT.S_OK)
{
uint nFilePathLength = 0;
hr = pLocalFontFileLoader.GetFilePathLengthFromKey(pFontFileReferenceKey, (uint)nFontFileReferenceKeySize, out nFilePathLength);
if (hr == HRESULT.S_OK)
{
StringBuilder sbFilePath = new StringBuilder((int)nFilePathLength + 1);
hr = pLocalFontFileLoader.GetFilePathFromKey(pFontFileReferenceKey, (uint)nFontFileReferenceKeySize, sbFilePath, nFilePathLength + 1);
if (hr == HRESULT.S_OK)
{
sPath = sbFilePath.ToString();
}
}
}
}
SafeRelease(ref pFontFileLoader);
}
}
SafeRelease(ref pFontFace3);
}
SafeRelease(ref pFontFaceReference);
}
if (sPath != "")
{
string sRelativePath = "";
if (sPath.StartsWith(sExePath, StringComparison.CurrentCultureIgnoreCase))
sRelativePath = sPath.Substring(sExePath.Length);//.TrimStart(System.IO.Path.DirectorySeparatorChar);
else
sRelativePath = sPath;
if (bCustom)
pFonts.Add(new Font(sFullName, sPath, sRelativePath + "#" + sFamilyName, sFontWeight, nWeightValue, sStyle, sStretch));
else
pFonts.Add(new Font(sFullName, sPath, sRelativePath + "#" + sFullName, sFontWeight, nWeightValue, sStyle, sStretch));
}
}
return hr;
}
private static string ConvertStringToFontWeightString(string sString)
{
string sFWString = sString switch
{
"100" => "Thin",
"200" => "ExtraLight",
"300" => "Light",
"350" => "SemiLight",
"400" => "Normal",
"500" => "Medium",
"600" => "SemiBold",
"700" => "Bold",
"800" => "ExtraBold",
"900" => "Black",
"950" => "ExtraBlack",
_ => "Normal",
};
return sFWString;
}
private static string ConvertStringToFontStyleString(string sString)
{
string sFWString = sString switch
{
"0" => "Normal",
"1" => "Oblique",
"2" => "Italic",
_ => "Normal",
};
return sFWString;
}
private static string ConvertStringToFontStretchString(string sString)
{
string sFWString = sString switch
{
"0" => "Undefined",
"1" => "UltraCondensed",
"2" => "ExtraCondensed",
"3" => "Condensed",
"4" => "SemiCondensed",
"5" => "Normal",
"6" => "SemiExpanded",
"7" => "Expanded",
"8" => "ExtraExpanded",
"9" => "UltraExpanded",
_ => "Normal",
};
return sFWString;
}
private void CompositionTarget_Rendering(object sender, object e)
{
HRESULT hr = HRESULT.S_OK;
if (GetForegroundWindow() == hWndMain)
{
// To stop rendering when dragging
//if (GetCapture() != hWndMain)
hr = Render();
}
}
// For simplified test : Rename to Render and other Render to Render2...
HRESULT Render2()
{
HRESULT hr = HRESULT.S_OK;
if (m_pD2DDeviceContext != null)
{
m_pD2DDeviceContext.BeginDraw();
m_pD2DDeviceContext.Clear(new ColorF(ColorF.Enum.Blue, 1.0f));
m_pD2DDeviceContext.SetTransform(Matrix3x2F.Identity());
D2D1_SIZE_F size = m_pD2DDeviceContext.GetSize();
if (m_pD2DBitmap1 != null)
{
D2D1_SIZE_F sizeBmpBackground = m_pD2DBitmap1.GetSize();
D2D1_RECT_F destRectBackground = new D2D1_RECT_F(0.0f, 0.0f, size.width, size.height);
//D2D1_RECT_F destRectBackground = new D2D1_RECT_F(0.0f, 0.0f, sizeBmpBackground.width, sizeBmpBackground.height);
D2D1_RECT_F sourceRectBackground = new D2D1_RECT_F(0.0f, 0.0f, sizeBmpBackground.width, sizeBmpBackground.height);
//m_pD2DDeviceContext.DrawBitmap(m_pD2DBitmap1, ref destRectBackground, 1.0f, D2D1_BITMAP_INTERPOLATION_MODE.D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, ref sourceRectBackground);
}
m_pD2DDeviceContext.FillRectangle(RectF(10.0f, 10.0f, 200.0f, 200.0f), m_pD2DSolidColorBrushPink);
m_pD2DDeviceContext.FillEllipse(Ellipse(new Direct2D.D2D1_POINT_2F(300, 300), 100.0f, 100.0f), m_pD2DSolidColorBrushRed);
if (m_pTextLayout != null)
{
if (m_pCTR != null)
{
hr = m_pTextLayout.Draw(IntPtr.Zero, m_pCTR, 0, 0);
if (hr == HRESULT.S_OK)
{
if (m_pCTR.IsDWriteCore)
{
hr = m_pCTR.DrawBitmapRenderTarget(Point2F(0, 0));
}
}
}
// DWrite only
// m_pD2DDeviceContext.DrawTextLayout(Point2F(100, 100), m_pTextLayout, m_pD2DSolidColorBrushPink,
// D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_NO_SNAP | D2D1_DRAW_TEXT_OPTIONS.D2D1_DRAW_TEXT_OPTIONS_ENABLE_COLOR_FONT);
}
hr = m_pD2DDeviceContext.EndDraw(out ulong tag1, out ulong tag2);
if ((uint)hr == D2DTools.D2DERR_RECREATE_TARGET)
{
m_pD2DDeviceContext.SetTarget(null);
SafeRelease(ref m_pD2DDeviceContext);
hr = CreateDeviceContext();
CleanDeviceResources();
hr = CreateDeviceResources();
hr = CreateSwapChain(IntPtr.Zero);
hr = ConfigureSwapChain(hWndMain);
}
hr = m_pDXGISwapChain1.Present(1, 0);
}
return (hr);
}
float m_nY1 = 0.0f;
float m_nShadowTranslate = 4.0f;
float m_nShadowTranslateDirection = 1.0f;
float m_nTurbulenceBaseFrequencyY = 0.03f;
float m_nTurbulenceDirection = 1.0f;
float m_nShadowStandardDeviation = 0.0f;
float m_nShadowDirection = 1.0f;
float m_nShiftGlowX = 30.0f;
float m_nShiftGlowY = 20.0f;
float m_nXBlood = 0.0f;
float m_nDirectionXBlood = 1.0f;
float m_nYBlood = 0.0f;
float m_nDirectionYBlood = 1.0f;
float m_nXScroll = 300.0f;
float m_nDisplacementMapScale = 0.1f;
float m_nDisplacementMapDirection = -1.0f;
int m_nCptDisplacementPause = 0;
HRESULT Render()
{
HRESULT hr = HRESULT.S_OK;
if (m_pD2DDeviceContext != null)
{
m_pD2DDeviceContext.BeginDraw();
m_pD2DDeviceContext.Clear(new ColorF(ColorF.Enum.DarkSlateBlue, 1.0f));
m_pD2DDeviceContext.SetTransform(Matrix3x2F.Identity());
D2D1_SIZE_F size = m_pD2DDeviceContext.GetSize();
if (m_pD2DGeometry1 != null)
{
m_pD2DDeviceContext.DrawGeometry(m_pD2DGeometry1, m_pD2DMainBrush, 2.0f);
var translateMatrix = Matrix3x2F.Translation(new D2D1_SIZE_F(0, m_nY1));
m_pD2DLinearGradientBrush1.SetTransform(translateMatrix);
m_nY1 += 0.1f;
if (m_nY1 >= m_nComputedHeight1 * 1.33f)
{
m_nY1 = 0.0f;
}
m_pD2DDeviceContext.FillGeometry(m_pD2DGeometry1, m_pD2DLinearGradientBrush1);
//D2D1_LAYER_PARAMETERS lp = new D2D1_LAYER_PARAMETERS();
//lp.geometricMask = m_pD2DGeometry1;
//lp = LayerParameters(InfiniteRect(), m_pD2DGeometry1);
//m_pD2DDeviceContext.PushLayer(ref lp);
//D2D1_RECT_F rectBounds;
//hr = m_pD2DGeometry1.GetBounds(null, out rectBounds);
//m_pD2DDeviceContext.FillRectangle(ref rectBounds, m_pD2DLinearGradientBrush1);
//m_pD2DDeviceContext.PopLayer();
//SafeRelease(ref lp.geometricMask);
}
if (m_pD2DGeometry2 != null)
{
m_pD2DDeviceContext.SetTransform(Matrix3x2F.Translation(30.0f, m_nComputedHeight1 + 10.0f));
ID2D1BitmapRenderTarget pCompatibleRenderTarget = null;
Direct2D.D2D1_SIZE_U sizeU = SizeU((uint)size.width, (uint)size.height);
hr = m_pD2DDeviceContext.CreateCompatibleRenderTarget(ref size, ref sizeU, PixelFormat(DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED),
D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS.D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE, out pCompatibleRenderTarget);
if (hr == HRESULT.S_OK)
{
pCompatibleRenderTarget.BeginDraw();
pCompatibleRenderTarget.Clear(null);
pCompatibleRenderTarget.FillGeometry(m_pD2DGeometry2, m_pD2DMainBrush);
hr = pCompatibleRenderTarget.EndDraw(out UInt64 tag11, out UInt64 tag21);
ID2D1Bitmap pCompatibleBitmap = null;
hr = pCompatibleRenderTarget.GetBitmap(out pCompatibleBitmap);
ID2D1Effect pShadowEffect = null;
hr = m_pD2DDeviceContext.CreateEffect(CLSID_D2D1Shadow, out pShadowEffect);
pShadowEffect.SetInput(0, pCompatibleBitmap);
//SetEffectFloat(pShadowEffect, (uint)D2D1_SHADOW_PROP.D2D1_SHADOW_PROP_BLUR_STANDARD_DEVIATION, 4.0f);
D2D1_RECT_F rectBackground = new D2D1_RECT_F(0.0f, 0.0f, size.width, size.height);
D2D1_SIZE_F bmpSizeBackground = pCompatibleBitmap.GetSize();
Direct2D.D2D1_POINT_2F ptShadow = Point2F(m_nShadowTranslate, m_nShadowTranslate);
D2D1_RECT_F sourceRectangle = new D2D1_RECT_F(0, 0, size.width, size.height);
m_pD2DDeviceContext.DrawImage((ID2D1Image)pShadowEffect, ref ptShadow, ref sourceRectangle, D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_LINEAR, D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_OVER);
m_nShadowTranslate += 0.2f * m_nShadowTranslateDirection;
if (m_nShadowTranslate >= 16.0f && m_nShadowTranslateDirection > 0 ||
m_nShadowTranslate <= 4.0f && m_nShadowTranslateDirection < 0)
{
m_nShadowTranslateDirection = -m_nShadowTranslateDirection;
}
//ID2D1Image pOutputImage = null;
//pShadowEffect.GetOutput(out pOutputImage);
//m_pD2DDeviceContext.DrawImage(pOutputImage, ref ptShadow, ref sourceRectangle, D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_LINEAR, D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_OVER);
//SafeRelease(ref pOutputImage);
SafeRelease(ref pShadowEffect);
SafeRelease(ref pCompatibleBitmap);
SafeRelease(ref pCompatibleRenderTarget);
}
m_pD2DDeviceContext.FillGeometry(m_pD2DGeometry2, m_pD2DSolidColorBrushGreen);
}
if (m_pD2DGeometry3 != null)
{
m_pD2DDeviceContext.SetTransform(Matrix3x2F.Translation(20.0f, m_nComputedHeight1 + m_nComputedHeight2 + 10.0f));
ID2D1BitmapRenderTarget pCompatibleRenderTarget = null;
Direct2D.D2D1_SIZE_U sizeU = SizeU((uint)size.width, (uint)size.height);
hr = m_pD2DDeviceContext.CreateCompatibleRenderTarget(ref size, ref sizeU, PixelFormat(DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED),
D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS.D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE, out pCompatibleRenderTarget);
if (hr == HRESULT.S_OK)
{
pCompatibleRenderTarget.BeginDraw();
pCompatibleRenderTarget.Clear(null);
pCompatibleRenderTarget.FillGeometry(m_pD2DGeometry3, m_pD2DSolidColorBrushWhite);
hr = pCompatibleRenderTarget.EndDraw(out UInt64 tag11, out UInt64 tag21);
ID2D1Bitmap pCompatibleBitmap = null;
hr = pCompatibleRenderTarget.GetBitmap(out pCompatibleBitmap);
ID2D1Effect pDisplacementMapEffect = null;
hr = m_pD2DDeviceContext.CreateEffect(D2DTools.CLSID_D2D1DisplacementMap, out pDisplacementMapEffect);
pDisplacementMapEffect.SetInput(0, pCompatibleBitmap);
//pDisplacementMapEffect.SetInput(1, m_pD2DBitmapWave);
SetEffectFloat(pDisplacementMapEffect, (uint)D2D1_DISPLACEMENTMAP_PROP.D2D1_DISPLACEMENTMAP_PROP_SCALE, 10.0f);
ID2D1Effect pEffectTurbulence = null;
hr = m_pD2DDeviceContext.CreateEffect(D2DTools.CLSID_D2D1Turbulence, out pEffectTurbulence);
D2DTools.SetInputEffect(pDisplacementMapEffect, 1, pEffectTurbulence);
float[] aFloatArray = { 0.0f, m_nTurbulenceBaseFrequencyY };
SetEffectFloatArray(pEffectTurbulence, (uint)D2D1_TURBULENCE_PROP.D2D1_TURBULENCE_PROP_BASE_FREQUENCY, aFloatArray);
m_nTurbulenceBaseFrequencyY += 0.0005f * m_nTurbulenceDirection;
if (m_nTurbulenceBaseFrequencyY >= 0.08f && m_nTurbulenceDirection > 0 ||
m_nTurbulenceBaseFrequencyY <= 0.03f && m_nTurbulenceDirection < 0)
{
m_nTurbulenceDirection = -m_nTurbulenceDirection;
}
float[] aFloatArray2 = { size.width, size.height };
SetEffectFloatArray(pEffectTurbulence, (uint)D2D1_TURBULENCE_PROP.D2D1_TURBULENCE_PROP_SIZE, aFloatArray2);
//SetEffectInt(pDisplacementMapEffect, (uint)D2D1_DISPLACEMENTMAP_PROP.D2D1_DISPLACEMENTMAP_PROP_X_CHANNEL_SELECT, (uint)D2D1_CHANNEL_SELECTOR.D2D1_CHANNEL_SELECTOR_R);
//SetEffectInt(pDisplacementMapEffect, (uint)D2D1_DISPLACEMENTMAP_PROP.D2D1_DISPLACEMENTMAP_PROP_Y_CHANNEL_SELECT, (uint)D2D1_CHANNEL_SELECTOR.D2D1_CHANNEL_SELECTOR_R);
D2D1_RECT_F rectBackground = new D2D1_RECT_F(0.0f, 0.0f, size.width, size.height);
D2D1_SIZE_F bmpSizeBackground = pCompatibleBitmap.GetSize();
Direct2D.D2D1_POINT_2F ptShadow = Point2F(10, 10);
D2D1_RECT_F sourceRectangle = new D2D1_RECT_F(0, 0, size.width, size.height);
m_pD2DDeviceContext.DrawImage((ID2D1Image)pDisplacementMapEffect, ptShadow, sourceRectangle, D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_LINEAR, D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_OVER);
SafeRelease(ref pDisplacementMapEffect);
SafeRelease(ref pCompatibleBitmap);
SafeRelease(ref pCompatibleRenderTarget);
SafeRelease(ref pEffectTurbulence);
}
}
if (m_pD2DGeometry4 != null)
{
m_pD2DDeviceContext.SetTransform(Matrix3x2F.Translation(10.0f, m_nComputedHeight1 + m_nComputedHeight2 + m_nComputedHeight3 + 10.0f));
ID2D1BitmapRenderTarget pCompatibleRenderTarget = null;
Direct2D.D2D1_SIZE_U sizeU = SizeU((uint)size.width, (uint)size.height);
hr = m_pD2DDeviceContext.CreateCompatibleRenderTarget(ref size, ref sizeU, PixelFormat(DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED),
D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS.D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE, out pCompatibleRenderTarget);
if (hr == HRESULT.S_OK)
{
pCompatibleRenderTarget.SetTransform(Matrix3x2F.Translation(m_nShiftGlowX, m_nShiftGlowY));
pCompatibleRenderTarget.BeginDraw();
pCompatibleRenderTarget.Clear(null);
pCompatibleRenderTarget.FillGeometry(m_pD2DGeometry4, m_pD2DSolidColorBrushPink);
hr = pCompatibleRenderTarget.EndDraw(out UInt64 tag11, out UInt64 tag21);
ID2D1Bitmap pCompatibleBitmap = null;
hr = pCompatibleRenderTarget.GetBitmap(out pCompatibleBitmap);
ID2D1Effect pShadowEffect = null;
hr = m_pD2DDeviceContext.CreateEffect(CLSID_D2D1Shadow, out pShadowEffect);
pShadowEffect.SetInput(0, pCompatibleBitmap);
SetEffectFloat(pShadowEffect, (uint)D2D1_SHADOW_PROP.D2D1_SHADOW_PROP_BLUR_STANDARD_DEVIATION, m_nShadowStandardDeviation);
Windows.UI.Color ShadowColor = Microsoft.UI.Colors.DeepPink;
float[] aFloatArray = { (float)((float)ShadowColor.R / 255.0f), (float)((float)ShadowColor.G / 255.0f), (float)((float)ShadowColor.B / 255.0f), 1.0f };
SetEffectFloatArray(pShadowEffect, (uint)D2D1_SHADOW_PROP.D2D1_SHADOW_PROP_COLOR, aFloatArray);
m_nShadowStandardDeviation += 1.0f * m_nShadowDirection;
if (m_nShadowStandardDeviation >= 20.0f && m_nShadowDirection > 0 ||
m_nShadowStandardDeviation <= 0.0f && m_nShadowDirection < 0)
{
m_nShadowDirection = -m_nShadowDirection;
}
ID2D1Effect pBrightnessEffect = null;
hr = m_pD2DDeviceContext.CreateEffect(D2DTools.CLSID_D2D1Brightness, out pBrightnessEffect);
pBrightnessEffect.SetInput(0, (ID2D1Image)pShadowEffect);
float[] aFloatArray1 = { 0.15f, 0.70f };
SetEffectFloatArray(pBrightnessEffect, (uint)D2D1_BRIGHTNESS_PROP.D2D1_BRIGHTNESS_PROP_WHITE_POINT, aFloatArray1);
D2D1_RECT_F rectBackground = new D2D1_RECT_F(0.0f, 0.0f, size.width, size.height);
D2D1_SIZE_F bmpSizeBackground = pCompatibleBitmap.GetSize();
Direct2D.D2D1_POINT_2F ptShadow = Point2F(0, 0);
D2D1_RECT_F sourceRectangle = new D2D1_RECT_F(0, 0, size.width, size.height);
m_pD2DDeviceContext.DrawImage((ID2D1Image)pBrightnessEffect, ptShadow, sourceRectangle, D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_LINEAR, D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_OVER);
SafeRelease(ref pBrightnessEffect);
SafeRelease(ref pShadowEffect);
SafeRelease(ref pCompatibleBitmap);
SafeRelease(ref pCompatibleRenderTarget);
}
m_pD2DDeviceContext.SetTransform(Matrix3x2F.Translation(10.0f + m_nShiftGlowX, m_nComputedHeight1 + m_nComputedHeight2 + m_nComputedHeight3 + 10.0f + m_nShiftGlowY));
m_pD2DDeviceContext.FillGeometry(m_pD2DGeometry4, m_pD2DSolidColorBrushWhite);
}
if (m_pD2DGeometry5 != null)
{
m_pD2DDeviceContext.SetTransform(Matrix3x2F.Translation(30.0f, m_nComputedHeight1 + m_nComputedHeight2 + m_nComputedHeight3 + m_nComputedHeight4 + 10.0f + m_nShiftGlowY));
//m_pD2DDeviceContext.DrawGeometry(m_pD2DGeometry5, m_pD2DSolidColorBrushRed, 2.0f);
m_pD2DDeviceContext.DrawGeometry(m_pD2DGeometry5, m_pD2DMainBrush, 2.0f);
//m_pD2DDeviceContext.FillGeometry(m_pD2DGeometry5, m_pD2DBitmapBrush);
//ID2D1Layer pD2DLayer = null;
//var sz = new D2D1_SIZE_F(0.0f, 0.0f);
//hr = m_pD2DDeviceContext.CreateLayer(ref sz, out pD2DLayer);
//ID2D1RoundedRectangleGeometry pRRectangleGeometry;
//D2D1_ROUNDED_RECT rectangle1 = RoundedRect(RectF(100.0f, 100.0f, 100.0f + 300.0f, 300.0f), 30.0f, 30.0f);
//hr = m_pD2DFactory1.CreateRoundedRectangleGeometry(rectangle1, out pRRectangleGeometry);
D2D1_LAYER_PARAMETERS lp = new D2D1_LAYER_PARAMETERS();
lp.geometricMask = m_pD2DGeometry5;
lp = LayerParameters(InfiniteRect(), m_pD2DGeometry5);
//m_pD2DDeviceContext.PushLayer(ref lp, pD2DLayer);
m_pD2DDeviceContext.PushLayer(ref lp);
D2D1_RECT_F rectf2 = new D2D1_RECT_F();
rectf2.left = 0;
rectf2.top = 0;
rectf2.right = size.width;
rectf2.bottom = size.height;
//m_pD2DDeviceContext.FillRectangle(ref rectf2, m_pD2DSolidColorBrushRed);
m_pD2DDeviceContext.FillRectangle(ref rectf2, m_pD2DBitmapBrush1);
var translateMatrix = Matrix3x2F.Translation(new D2D1_SIZE_F(m_nXBlood, m_nYBlood));
m_pD2DBitmapBrush1.SetTransform(translateMatrix);
m_nXBlood += 0.1f * m_nDirectionXBlood;
m_nYBlood += 0.1f * m_nDirectionYBlood;
if (m_nXBlood >= 20.0f && m_nDirectionXBlood > 0 ||
m_nXBlood <= 0.0f && m_nDirectionXBlood < 0)
{
m_nDirectionXBlood = -m_nDirectionXBlood;
}
if (m_nYBlood >= 20.0f && m_nDirectionYBlood > 0 ||
m_nYBlood <= 0.0f && m_nDirectionYBlood < 0)
{
m_nDirectionYBlood = -m_nDirectionYBlood;
}
m_pD2DDeviceContext.PopLayer();
SafeRelease(ref lp.geometricMask);
}
if (m_pD2DGeometry6 != null)
{
m_pD2DDeviceContext.SetTransform(Matrix3x2F.Translation(m_nXScroll, m_nComputedHeight1 + m_nComputedHeight2 + m_nComputedHeight3 + m_nComputedHeight4 + m_nComputedHeight5 + 10.0f + m_nShiftGlowY));
m_pD2DDeviceContext.DrawGeometry(m_pD2DGeometry6, m_pD2DMainBrush, 2.0f);
m_pD2DDeviceContext.FillGeometry(m_pD2DGeometry6, m_pD2DSolidColorBrushPink);
D2D1_RECT_F rectBounds;
hr = m_pD2DGeometry6.GetBounds(null, out rectBounds);
m_nXScroll -= 2.0f;
if (m_nXScroll <= -(rectBounds.right - rectBounds.left))
m_nXScroll = size.width;
}
if (m_pD2DGeometry7 != null)
{
float nTextLayoutHeight = 0;
if (m_pTextLayout != null)
{
DWRITE_TEXT_METRICS textMetrics;
hr = m_pTextLayout.GetMetrics(out textMetrics);
nTextLayoutHeight = textMetrics.layoutHeight;
}
m_pD2DDeviceContext.SetTransform(Matrix3x2F.Translation(30.0f, m_nComputedHeight1 + m_nComputedHeight2 + m_nComputedHeight3
+ m_nComputedHeight4 + m_nComputedHeight5 + m_nComputedHeight6 + nTextLayoutHeight /*+ nShiftGlowY*/));
ID2D1BitmapRenderTarget pCompatibleRenderTarget = null;
Direct2D.D2D1_SIZE_U sizeU = SizeU((uint)size.width, (uint)size.height);
hr = m_pD2DDeviceContext.CreateCompatibleRenderTarget(ref size, ref sizeU, PixelFormat(DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE.D2D1_ALPHA_MODE_PREMULTIPLIED),
D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS.D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE, out pCompatibleRenderTarget);
if (hr == HRESULT.S_OK)
{
pCompatibleRenderTarget.BeginDraw();
pCompatibleRenderTarget.Clear(null);
pCompatibleRenderTarget.DrawGeometry(m_pD2DGeometry7, m_pD2DMainBrush, 2.0f);
pCompatibleRenderTarget.FillGeometry(m_pD2DGeometry7, m_pD2DSolidColorBrushRed);
hr = pCompatibleRenderTarget.EndDraw(out UInt64 tag11, out UInt64 tag21);
ID2D1Bitmap pCompatibleBitmap = null;
hr = pCompatibleRenderTarget.GetBitmap(out pCompatibleBitmap);
ID2D1Effect pDisplacementMapEffect = null;
hr = m_pD2DDeviceContext.CreateEffect(D2DTools.CLSID_D2D1DisplacementMap, out pDisplacementMapEffect);
pDisplacementMapEffect.SetInput(0, pCompatibleBitmap);
// m_pD2DBitmap2 size must be > m_pD2DGeometry7 size
pDisplacementMapEffect.SetInput(1, m_pD2DBitmap2);
SetEffectFloat(pDisplacementMapEffect, (uint)D2D1_DISPLACEMENTMAP_PROP.D2D1_DISPLACEMENTMAP_PROP_SCALE, m_nDisplacementMapScale);
if (m_nCptDisplacementPause == 0)
m_nDisplacementMapScale += 5.0f * m_nDisplacementMapDirection;
//if (m_nDisplacementMapScale >= 20.0f && m_nDisplacementMapDirection > 0 ||
// m_nDisplacementMapScale <= -20.0f && m_nDisplacementMapDirection < 0)
//{
// //m_nDisplacementMapDirection = -m_nDisplacementMapDirection;
//}
if (m_nDisplacementMapScale <= -450.0f && m_nDisplacementMapDirection < 0)
{
m_nDisplacementMapDirection = -m_nDisplacementMapDirection;
}
if (m_nDisplacementMapScale >= 0.0f && m_nDisplacementMapDirection > 0)
{
m_nCptDisplacementPause++;
if (m_nCptDisplacementPause >= 60 * 3)
{
m_nCptDisplacementPause = 0;
m_nDisplacementMapDirection = -m_nDisplacementMapDirection;
}
}
SetEffectInt(pDisplacementMapEffect, (uint)D2D1_DISPLACEMENTMAP_PROP.D2D1_DISPLACEMENTMAP_PROP_X_CHANNEL_SELECT, (uint)D2D1_CHANNEL_SELECTOR.D2D1_CHANNEL_SELECTOR_G);
SetEffectInt(pDisplacementMapEffect, (uint)D2D1_DISPLACEMENTMAP_PROP.D2D1_DISPLACEMENTMAP_PROP_Y_CHANNEL_SELECT, (uint)D2D1_CHANNEL_SELECTOR.D2D1_CHANNEL_SELECTOR_B);
D2D1_RECT_F rectBackground = new D2D1_RECT_F(0.0f, 0.0f, size.width, size.height);
D2D1_SIZE_F bmpSizeBackground = pCompatibleBitmap.GetSize();
Direct2D.D2D1_POINT_2F ptShadow = Point2F(0, 0);
D2D1_RECT_F sourceRectangle = new D2D1_RECT_F(0, 1, size.width, size.height - 1);
m_pD2DDeviceContext.DrawImage((ID2D1Image)pDisplacementMapEffect, ptShadow, sourceRectangle, D2D1_INTERPOLATION_MODE.D2D1_INTERPOLATION_MODE_LINEAR, D2D1_COMPOSITE_MODE.D2D1_COMPOSITE_MODE_SOURCE_OVER);
SafeRelease(ref pDisplacementMapEffect);
SafeRelease(ref pCompatibleBitmap);
SafeRelease(ref pCompatibleRenderTarget);
}
}
if (m_pTextLayout != null)
{
m_pD2DDeviceContext.SetTransform(Matrix3x2F.Translation(0.0f, m_nComputedHeight1 + m_nComputedHeight2 + m_nComputedHeight3
+ m_nComputedHeight4 + m_nComputedHeight5 + m_nComputedHeight6 + m_nShiftGlowY + 10.0f));
if (m_pCTR != null)
{
hr = m_pTextLayout.Draw(IntPtr.Zero, m_pCTR, 0, 0);
if (hr == HRESULT.S_OK)
{
if (m_pCTR.IsDWriteCore)
{
//DWRITE_TEXT_METRICS textMetrics;
//hr = m_pTextLayout.GetMetrics(out textMetrics);
//float nX = (size.width - textMetrics.layoutWidth) / 2.0f;
//hr = m_pCTR.DrawBitmapRenderTarget(Point2F(nX, 0));
hr = m_pCTR.DrawBitmapRenderTarget(Point2F(0.0f, 0.0f));
}
}
}
}
hr = m_pD2DDeviceContext.EndDraw(out ulong tag1, out ulong tag2);
if ((uint)hr == D2DTools.D2DERR_RECREATE_TARGET)
{
m_pD2DDeviceContext.SetTarget(null);
SafeRelease(ref m_pD2DDeviceContext);
hr = CreateDeviceContext();
CleanDeviceResources();
hr = CreateDeviceResources();
hr = CreateSwapChain(IntPtr.Zero);
hr = ConfigureSwapChain(hWndMain);
}
hr = m_pDXGISwapChain1.Present(1, 0);
}
return (hr);
}
private HRESULT CreateDWriteTextGeometry(string sText, string sPathFont, float nHeight, bool bBold, bool bItalic, out ID2D1Geometry pD2DGeometry, out float nComputedHeight)
{
HRESULT hr = HRESULT.S_OK;
pD2DGeometry = null;
nComputedHeight = 0;
IDWriteFontFile pDWriteFontFile = null;
hr = m_pDWriteFactory7.CreateFontFileReference(sPathFont, IntPtr.Zero, out pDWriteFontFile);
if (hr == HRESULT.S_OK)
{
bool bSupportedFontType = false;
DWRITE_FONT_FILE_TYPE FontFileType;
DWRITE_FONT_FACE_TYPE FontFaceType;
int nNumberOfFaces = 0;
hr = pDWriteFontFile.Analyze(out bSupportedFontType, out FontFileType, out FontFaceType, out nNumberOfFaces);
if (hr == HRESULT.S_OK)
{
DWRITE_FONT_SIMULATIONS fs = DWRITE_FONT_SIMULATIONS.DWRITE_FONT_SIMULATIONS_NONE;
if (bBold && bItalic)
fs = DWRITE_FONT_SIMULATIONS.DWRITE_FONT_SIMULATIONS_BOLD | DWRITE_FONT_SIMULATIONS.DWRITE_FONT_SIMULATIONS_OBLIQUE;
else if (bBold)
fs = DWRITE_FONT_SIMULATIONS.DWRITE_FONT_SIMULATIONS_BOLD;
else if (bItalic)
fs = DWRITE_FONT_SIMULATIONS.DWRITE_FONT_SIMULATIONS_OBLIQUE;
IDWriteFontFace pDWriteFontFace = null;
hr = m_pDWriteFactory7.CreateFontFace(FontFaceType, 1,
new IDWriteFontFile[] { pDWriteFontFile }, 0, fs, out pDWriteFontFace);
if (hr == HRESULT.S_OK)
{
uint[] codePoints = ToCodePoints(sText).ToArray();
ushort[] glyphIndices = new ushort[codePoints.Length];
hr = pDWriteFontFace.GetGlyphIndices(codePoints, sText.Length, glyphIndices);
if (hr == HRESULT.S_OK)
{
ID2D1PathGeometry pD2DPathGeometry = null;
hr = m_pD2DFactory1.CreatePathGeometry(out pD2DPathGeometry);
if (hr == HRESULT.S_OK)
{
uint nDPI = GetDpiForWindow(hWndMain);
ID2D1GeometrySink pD2DGeometrySink = null;
hr = pD2DPathGeometry.Open(out pD2DGeometrySink);
if (hr == HRESULT.S_OK)
{
hr = pDWriteFontFace.GetGlyphRunOutline(nHeight * 96.0f / (float)nDPI, glyphIndices, null, null, sText.Length,
false, false, (DWrite.ID2D1SimplifiedGeometrySink)pD2DGeometrySink);
pD2DGeometrySink.Close();
SafeRelease(ref pD2DGeometrySink);
}
DWRITE_FONT_METRICS fontFaceMetrics;
pDWriteFontFace.GetMetrics(out fontFaceMetrics);