A game framework written with osu! in mind.
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
4using System;
5using System.Drawing;
6using System.Runtime.CompilerServices;
7using AVFoundation;
8using Foundation;
9using SixLabors.ImageSharp.Formats.Png;
10using SixLabors.ImageSharp.PixelFormats;
11using UIKit;
12
13namespace osu.Framework.iOS
14{
15 public abstract class GameAppDelegate : UIApplicationDelegate
16 {
17 private const string output_volume = "outputVolume";
18
19 public override UIWindow Window { get; set; }
20
21 private IOSGameView gameView;
22 private IOSGameHost host;
23
24 protected abstract Game CreateGame();
25
26 public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
27 {
28 aotImageSharp();
29
30 Window = new UIWindow(UIScreen.MainScreen.Bounds);
31
32 gameView = new IOSGameView(new RectangleF(0.0f, 0.0f, (float)Window.Frame.Size.Width, (float)Window.Frame.Size.Height));
33 host = new IOSGameHost(gameView);
34
35 Window.RootViewController = new GameViewController(gameView, host);
36 Window.MakeKeyAndVisible();
37
38 // required to trigger the osuTK update loop, which is used for input handling.
39 gameView.Run();
40
41 host.Run(CreateGame());
42
43 // Watch for the volume button changing in order to change audio policy
44 AVAudioSession audioSession = AVAudioSession.SharedInstance();
45 audioSession.AddObserver(this, output_volume, NSKeyValueObservingOptions.New, IntPtr.Zero);
46
47 return true;
48 }
49
50 private void aotImageSharp()
51 {
52 Unsafe.SizeOf<Rgba32>();
53 Unsafe.SizeOf<long>();
54
55 try
56 {
57 new PngDecoder().Decode<Rgba32>(SixLabors.ImageSharp.Configuration.Default, null);
58 }
59 catch
60 {
61 }
62 }
63
64 public override void ObserveValue(NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context)
65 {
66 switch (keyPath)
67 {
68 case output_volume:
69 AVAudioSession.SharedInstance().SetCategory(AVAudioSessionCategory.Playback);
70 break;
71 }
72 }
73
74 public override void DidEnterBackground(UIApplication application) => host.Suspend();
75
76 public override void WillEnterForeground(UIApplication application) => host.Resume();
77 }
78}