Skip to content

Commit

Permalink
Add macOS implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
frenzibyte committed Dec 28, 2024
1 parent cb07a37 commit 9dea008
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
5 changes: 5 additions & 0 deletions osu.Framework/Platform/MacOS/MacOSGameHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using osu.Framework.Graphics.Textures;
using osu.Framework.Input;
using osu.Framework.Input.Bindings;
using osu.Framework.Input.Handlers;
using osu.Framework.Input.Handlers.Mouse;
using osu.Framework.IO.Stores;
using osu.Framework.Logging;
using osu.Framework.Platform.MacOS.Native;

Expand Down Expand Up @@ -44,6 +46,9 @@ public override IEnumerable<string> UserStoragePaths

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

public override IResourceStore<TextureUpload> CreateTextureLoaderStore(IResourceStore<byte[]> underlyingStore)
=> new MacOSTextureLoaderStore(underlyingStore);

protected override void Swap()
{
base.Swap();
Expand Down
37 changes: 37 additions & 0 deletions osu.Framework/Platform/MacOS/MacOSTextureLoaderStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.IO;
using osu.Framework.IO.Stores;
using osu.Framework.Platform.Apple;
using osu.Framework.Platform.Apple.Native;
using osu.Framework.Platform.MacOS.Native;
using SixLabors.ImageSharp;

namespace osu.Framework.Platform.MacOS
{
internal class MacOSTextureLoaderStore : AppleTextureLoaderStore
{
public MacOSTextureLoaderStore(IResourceStore<byte[]> store)
: base(store)
{
}

protected override unsafe Image<TPixel> ImageFromStream<TPixel>(Stream stream)
{
int length = (int)(stream.Length - stream.Position);
using var nativeData = NSMutableData.FromLength(length);

var bytesSpan = new Span<byte>(nativeData.MutableBytes, length);
stream.ReadExactly(bytesSpan);

using var nsImage = NSImage.LoadFromData(nativeData);
if (nsImage.Handle == IntPtr.Zero)
throw new ArgumentException($"{nameof(Image)} could not be created from {nameof(stream)}.");

var cgImage = nsImage.CGImage;
return ImageFromCGImage<TPixel>(cgImage);
}
}
}

0 comments on commit 9dea008

Please sign in to comment.