Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ParadoxApplicationDelegate.iOS.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 #if SILICONSTUDIO_PLATFORM_IOS
4 using System;
5 using System.Drawing;
6 using MonoTouch.CoreAnimation;
7 using MonoTouch.Foundation;
8 using MonoTouch.ObjCRuntime;
9 using MonoTouch.UIKit;
10 using OpenTK.Graphics.ES20;
11 using OpenTK.Platform.iPhoneOS;
12 using SiliconStudio.Paradox.Games;
13 using SiliconStudio.Paradox.UI;
14 
15 namespace SiliconStudio.Paradox.Starter
16 {
17  public class ParadoxApplicationDelegate : UIApplicationDelegate
18  {
19  /// <summary>
20  /// The instance of the game to run.
21  /// </summary>
22  protected Game Game;
23 
24  /// <summary>
25  /// The main windows of the application.
26  /// </summary>
27  protected UIWindow MainWindow { get; private set; }
28 
29  public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
30  {
31  if(Game == null)
32  throw new InvalidOperationException("Please set 'Game' to a valid instance of Game before calling this method.");
33 
34  var bounds = UIScreen.MainScreen.Bounds;
35 
36  // create the game main windows
37  MainWindow = new UIWindow(bounds);
38 
39  // create the paradox game view
40  var paradoxGameView = new iOSParadoxView(bounds) {ContentScaleFactor = UIScreen.MainScreen.Scale};
41 
42  // create the view controller used to display the paradox game
43  var paradoxGameController = new ParadoxGameController { View = paradoxGameView };
44 
45  // create the game context
46  var gameContext = new GameContext(MainWindow, paradoxGameView, paradoxGameController);
47 
48  // Force fullscreen
49  UIApplication.SharedApplication.SetStatusBarHidden(true, false);
50 
51  // Added UINavigationController to switch between UIViewController because the game is killed if the FinishedLaunching (in the AppDelegate) method doesn't return true in 10 sec.
52  var navigationController = new UINavigationController {NavigationBarHidden = true};
53  navigationController.PushViewController(gameContext.GameViewController, false);
54  MainWindow.RootViewController = navigationController;
55 
56  // launch the main window
57  MainWindow.MakeKeyAndVisible();
58 
59  // launch the game
60  Game.Run(gameContext);
61 
62  return Game.IsRunning;
63  }
64 
65  // note: for more information on iOS application life cycle,
66  // see http://docs.xamarin.com/guides/cross-platform/application_fundamentals/backgrounding/part_1_introduction_to_backgrounding_in_ios/y
67 
68  [Register("iOSParadoxView")]
69  internal class iOSParadoxView : iPhoneOSGameView, IAnimatedGameView
70  {
71  CADisplayLink displayLink;
72  private bool isRunning;
73 
74  public iOSParadoxView(RectangleF frame)
75  : base(frame)
76  {
77  }
78 
79  protected override void CreateFrameBuffer()
80  {
81  base.CreateFrameBuffer();
82 
83  // TODO: PDX-364: depth format is currently hard coded (need to investigate how it can be transmitted)
84  // Create a depth renderbuffer
85  uint depthRenderBuffer;
86  GL.GenRenderbuffers(1, out depthRenderBuffer);
87  GL.BindRenderbuffer(RenderbufferTarget.Renderbuffer, depthRenderBuffer);
88 
89  // Allocate storage for the new renderbuffer
90  GL.RenderbufferStorage(All.Renderbuffer, All.Depth24Stencil8Oes, (int)(Size.Width * Layer.ContentsScale), (int)(Size.Height * Layer.ContentsScale));
91 
92  // Attach the renderbuffer to the framebuffer's depth attachment point
93  GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferSlot.DepthAttachment, RenderbufferTarget.Renderbuffer, depthRenderBuffer);
94  GL.FramebufferRenderbuffer(FramebufferTarget.Framebuffer, FramebufferSlot.StencilAttachment, RenderbufferTarget.Renderbuffer, depthRenderBuffer);
95  }
96 
97  [Export("layerClass")]
98  public static Class LayerClass()
99  {
100  return GetLayerClass();
101  }
102 
103  public void StartAnimating()
104  {
105  if (isRunning)
106  return;
107 
108  CreateFrameBuffer();
109 
110  var displayLink = UIScreen.MainScreen.CreateDisplayLink(this, new Selector("drawFrame"));
111  displayLink.FrameInterval = 0;
112  displayLink.AddToRunLoop(NSRunLoop.Current, NSRunLoop.NSDefaultRunLoopMode);
113  this.displayLink = displayLink;
114 
115  isRunning = true;
116  }
117 
118  public void StopAnimating()
119  {
120  if (!isRunning)
121  return;
122 
123  displayLink.Invalidate();
124  displayLink = null;
125 
126  DestroyFrameBuffer();
127 
128  isRunning = false;
129  }
130 
131  [Export("drawFrame")]
132  void DrawFrame()
133  {
134  OnRenderFrame(new OpenTK.FrameEventArgs());
135  }
136  }
137  }
138 }
139 
140 #endif
SiliconStudio.Core.Mathematics.RectangleF RectangleF
Definition: SpriteFont.cs:17