Skip to content

Commit 625249a

Browse files
committed
Fixed Myra.PlatformAgnostic build
1 parent d4d5b70 commit 625249a

File tree

3 files changed

+125
-62
lines changed

3 files changed

+125
-62
lines changed

src/Myra/Myra.PlatformAgnostic.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
<ItemGroup>
1919
<PackageReference Include="AssetManagementBase" Version="0.7.1" />
20-
<PackageReference Include="FontStashSharp" Version="$(FontStashSharpVersion)" />
20+
<PackageReference Include="FontStashSharp.PlatformAgnostic" Version="$(FontStashSharpVersion)" />
2121
<PackageReference Include="info.lundin.math.dll" Version="$(InfoLundinMathVersion)" />
2222
</ItemGroup>
2323
</Project>
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
#if PLATFORM_AGNOSTIC
2+
3+
using FontStashSharp;
4+
using Myra;
5+
using StbImageSharp;
6+
using System.Drawing;
7+
using System.IO;
8+
9+
using Texture2D = System.Object;
10+
11+
namespace AssetManagementBase
12+
{
13+
partial class MyraAssetManagerExtensions
14+
{
15+
internal class Texture2DWrapper
16+
{
17+
public int Width { get; private set; }
18+
public int Height { get; private set; }
19+
public object Texture { get; private set; }
20+
21+
public Texture2DWrapper(int width, int height, object texture)
22+
{
23+
Width = width;
24+
Height = height;
25+
Texture = texture;
26+
}
27+
}
28+
29+
private static AssetLoader<Texture2DWrapper> _textureLoader = (manager, assetName, settings, tag) =>
30+
{
31+
ImageResult result = null;
32+
using (var stream = manager.Open(assetName))
33+
{
34+
if (stream.CanSeek)
35+
{
36+
result = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
37+
}
38+
else
39+
{
40+
// If stream doesnt provide seek functionaly, use MemoryStream instead
41+
using (var ms = new MemoryStream())
42+
{
43+
stream.CopyTo(ms);
44+
ms.Seek(0, SeekOrigin.Begin);
45+
result = ImageResult.FromStream(ms, ColorComponents.RedGreenBlueAlpha);
46+
}
47+
}
48+
}
49+
50+
// Premultiply Alpha
51+
var b = result.Data;
52+
for (var i = 0; i < result.Data.Length; i += 4)
53+
{
54+
var falpha = b[i + 3] / 255.0f;
55+
b[i] = (byte)(b[i] * falpha);
56+
b[i + 1] = (byte)(b[i + 1] * falpha);
57+
b[i + 2] = (byte)(b[i + 2] * falpha);
58+
}
59+
60+
var textureManager = MyraEnvironment.Platform.Renderer.TextureManager;
61+
var texture = textureManager.CreateTexture(result.Width, result.Height);
62+
textureManager.SetTextureData(texture, new Rectangle(0, 0, result.Width, result.Height), result.Data);
63+
return new Texture2DWrapper(result.Width, result.Height, texture);
64+
};
65+
66+
private class FontSystemLoadingSettings : IAssetSettings
67+
{
68+
public Texture2D ExistingTexture { get; set; }
69+
public Rectangle ExistingTextureUsedSpace { get; set; }
70+
public string[] AdditionalFonts { get; set; }
71+
72+
public string BuildKey() => string.Empty;
73+
}
74+
75+
private static AssetLoader<FontSystem> _fontSystemLoader = (manager, assetName, settings, tag) =>
76+
{
77+
var fontSystemSettings = new FontSystemSettings();
78+
79+
var fontSystemLoadingSettings = (FontSystemLoadingSettings)settings;
80+
if (fontSystemLoadingSettings != null)
81+
{
82+
fontSystemSettings.ExistingTexture = fontSystemLoadingSettings.ExistingTexture;
83+
fontSystemSettings.ExistingTextureUsedSpace = fontSystemLoadingSettings.ExistingTextureUsedSpace;
84+
}
85+
;
86+
87+
var fontSystem = new FontSystem(fontSystemSettings);
88+
var data = manager.ReadAsByteArray(assetName);
89+
fontSystem.AddFont(data);
90+
if (fontSystemLoadingSettings != null && fontSystemLoadingSettings.AdditionalFonts != null)
91+
{
92+
foreach (var file in fontSystemLoadingSettings.AdditionalFonts)
93+
{
94+
data = manager.ReadAsByteArray(file);
95+
fontSystem.AddFont(data);
96+
}
97+
}
98+
99+
return fontSystem;
100+
};
101+
102+
internal static Texture2DWrapper LoadTexture2D(this AssetManager assetManager, string assetName) =>
103+
assetManager.UseLoader(_textureLoader, assetName);
104+
105+
public static FontSystem LoadFontSystem(this AssetManager assetManager, string assetName, string[] additionalFonts = null, Texture2D existingTexture = null, Rectangle existingTextureUsedSpace = default(Rectangle))
106+
{
107+
FontSystemLoadingSettings settings = null;
108+
if (additionalFonts != null || existingTexture != null)
109+
{
110+
settings = new FontSystemLoadingSettings
111+
{
112+
AdditionalFonts = additionalFonts,
113+
ExistingTexture = existingTexture,
114+
ExistingTextureUsedSpace = existingTextureUsedSpace
115+
};
116+
}
117+
118+
return assetManager.UseLoader(_fontSystemLoader, assetName, settings);
119+
}
120+
}
121+
}
122+
123+
#endif

src/Myra/MyraAssetManagerExtensions.cs

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -16,73 +16,13 @@
1616
using Texture2D = Stride.Graphics.Texture;
1717
#else
1818
using System.Drawing;
19-
using SolidBrush = Myra.Graphics2D.Brushes.SolidBrush;
20-
using Color = FontStashSharp.FSColor;
2119
using Texture2D = System.Object;
22-
using StbImageSharp;
23-
using System.IO;
2420
#endif
2521

2622
namespace AssetManagementBase
2723
{
28-
public static class MyraAssetManagerExtensions
24+
public static partial class MyraAssetManagerExtensions
2925
{
30-
#if PLATFORM_AGNOSTIC
31-
internal class Texture2DWrapper
32-
{
33-
public int Width { get; private set; }
34-
public int Height { get; private set; }
35-
public object Texture { get; private set; }
36-
37-
public Texture2DWrapper(int width, int height, object texture)
38-
{
39-
Width = width;
40-
Height = height;
41-
Texture = texture;
42-
}
43-
}
44-
45-
private static AssetLoader<Texture2DWrapper> _textureLoader = (manager, assetName, settings, tag) =>
46-
{
47-
ImageResult result = null;
48-
using (var stream = manager.Open(assetName))
49-
{
50-
if (stream.CanSeek)
51-
{
52-
result = ImageResult.FromStream(stream, ColorComponents.RedGreenBlueAlpha);
53-
}
54-
else
55-
{
56-
// If stream doesnt provide seek functionaly, use MemoryStream instead
57-
using (var ms = new MemoryStream())
58-
{
59-
stream.CopyTo(ms);
60-
ms.Seek(0, SeekOrigin.Begin);
61-
result = ImageResult.FromStream(ms, ColorComponents.RedGreenBlueAlpha);
62-
}
63-
}
64-
}
65-
66-
// Premultiply Alpha
67-
var b = result.Data;
68-
for (var i = 0; i < result.Data.Length; i += 4)
69-
{
70-
var falpha = b[i + 3] / 255.0f;
71-
b[i] = (byte)(b[i] * falpha);
72-
b[i + 1] = (byte)(b[i + 1] * falpha);
73-
b[i + 2] = (byte)(b[i + 2] * falpha);
74-
}
75-
76-
var textureManager = MyraEnvironment.Platform.Renderer.TextureManager;
77-
var texture = textureManager.CreateTexture(result.Width, result.Height);
78-
textureManager.SetTextureData(texture, new Rectangle(0, 0, result.Width, result.Height), result.Data);
79-
return new Texture2DWrapper(result.Width, result.Height, texture);
80-
};
81-
82-
internal static Texture2DWrapper LoadTexture2D(this AssetManager assetManager, string assetName) =>
83-
assetManager.UseLoader(_textureLoader, assetName);
84-
#endif
85-
8626
private static AssetLoader<TextureRegionAtlas> _atlasLoader = (manager, assetName, settings, tag) =>
8727
{
8828
var data = manager.ReadAsString(assetName);

0 commit comments

Comments
 (0)