Skip to content

Commit b2bbc2a

Browse files
committed
Add macOS implementation
1 parent 5886101 commit b2bbc2a

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

osu.Framework/Platform/MacOS/MacOSGameHost.cs

+5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
using System.Collections.Generic;
88
using System.IO;
99
using System.Linq;
10+
using osu.Framework.Graphics.Textures;
1011
using osu.Framework.Input;
1112
using osu.Framework.Input.Bindings;
1213
using osu.Framework.Input.Handlers;
1314
using osu.Framework.Input.Handlers.Mouse;
15+
using osu.Framework.IO.Stores;
1416
using osu.Framework.Logging;
1517
using osu.Framework.Platform.MacOS.Native;
1618

@@ -44,6 +46,9 @@ public override IEnumerable<string> UserStoragePaths
4446

4547
protected override ReadableKeyCombinationProvider CreateReadableKeyCombinationProvider() => new MacOSReadableKeyCombinationProvider();
4648

49+
public override IResourceStore<TextureUpload> CreateTextureLoaderStore(IResourceStore<byte[]> underlyingStore)
50+
=> new MacOSTextureLoaderStore(underlyingStore);
51+
4752
protected override void Swap()
4853
{
4954
base.Swap();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2+
// See the LICENCE file in the repository root for full licence text.
3+
4+
using System;
5+
using System.IO;
6+
using osu.Framework.IO.Stores;
7+
using osu.Framework.Platform.Apple;
8+
using osu.Framework.Platform.Apple.Native;
9+
using osu.Framework.Platform.MacOS.Native;
10+
using SixLabors.ImageSharp;
11+
12+
namespace osu.Framework.Platform.MacOS
13+
{
14+
internal class MacOSTextureLoaderStore : AppleTextureLoaderStore
15+
{
16+
public MacOSTextureLoaderStore(IResourceStore<byte[]> store)
17+
: base(store)
18+
{
19+
}
20+
21+
protected override unsafe Image<TPixel> ImageFromStream<TPixel>(Stream stream)
22+
{
23+
int length = (int)(stream.Length - stream.Position);
24+
using var nativeData = NSMutableData.FromLength(length);
25+
26+
var bytesSpan = new Span<byte>(nativeData.MutableBytes, length);
27+
stream.ReadExactly(bytesSpan);
28+
29+
using var nsImage = NSImage.LoadFromData(nativeData);
30+
if (nsImage.Handle == IntPtr.Zero)
31+
throw new ArgumentException($"{nameof(Image)} could not be created from {nameof(stream)}.");
32+
33+
var cgImage = nsImage.CGImage;
34+
return ImageFromCGImage<TPixel>(cgImage);
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)