2
0

UrhoSurface.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using Android.App;
  3. using Android.Content;
  4. using Android.Views;
  5. using Org.Libsdl.App;
  6. namespace Urho.Droid
  7. {
  8. /// <summary>
  9. /// A controller that provides a SDLSurface that can be used in any activity.
  10. /// Make sure you handle these events in your Activity:
  11. /// - OnResume
  12. /// - OnPause
  13. /// - OnLowMemory
  14. /// - OnDestroy
  15. /// - DispatchKeyEvent
  16. /// - OnWindowFocusChanged
  17. /// </summary>
  18. public static class UrhoSurface
  19. {
  20. /// <summary>
  21. /// Creates a view (SurfaceView) that can be added anywhere
  22. /// </summary>
  23. public static SDLSurface CreateSurface<TApplication>(Activity activity, ApplicationOptions options = null) where TApplication : Application
  24. {
  25. return CreateSurface(activity, typeof (TApplication), options);
  26. }
  27. /// <summary>
  28. /// Creates a view (SurfaceView) that can be added anywhere
  29. /// </summary>
  30. public static SDLSurface CreateSurface(Activity activity, Type applicationType, ApplicationOptions options = null)
  31. {
  32. UrhoEngine.Init();
  33. UrhoEngine.RegisterSdlLauncher(contextPtr => Application.CreateInstance(applicationType, options).Run());
  34. return SDLActivity.CreateSurface(activity);
  35. }
  36. public static void OnResume()
  37. {
  38. SDLActivity.OnResume();
  39. }
  40. public static void OnPause()
  41. {
  42. SDLActivity.OnPause();
  43. }
  44. public static void OnLowMemory()
  45. {
  46. SDLActivity.OnLowMemory();
  47. }
  48. public static void OnDestroy()
  49. {
  50. SDLActivity.OnDestroy();
  51. }
  52. public static bool DispatchKeyEvent(KeyEvent keyEvent)
  53. {
  54. return SDLActivity.DispatchKeyEvent(keyEvent);
  55. }
  56. public static void OnWindowFocusChanged(bool focus)
  57. {
  58. SDLActivity.OnWindowFocusChanged(focus);
  59. }
  60. /// <summary>
  61. /// The simpliest way to launch a game. It opens a special full-screen activity
  62. /// </summary>
  63. public static void RunInActivity<TApplication>(ApplicationOptions options = null) where TApplication : Application
  64. {
  65. RunInActivity(typeof (TApplication), options);
  66. }
  67. /// <summary>
  68. /// The simpliest way to launch a game. It opens a special full-screen activity
  69. /// </summary>
  70. public static void RunInActivity(Type applicationType, ApplicationOptions options = null)
  71. {
  72. UrhoEngine.Init();
  73. UrhoEngine.RegisterSdlLauncher(_ => Application.CreateInstance(applicationType, options).Run());
  74. var context = Android.App.Application.Context;
  75. var intent = new Intent(context, typeof(Org.Libsdl.App.UrhoActivity));
  76. intent.AddFlags(ActivityFlags.NewTask);
  77. context.StartActivity(intent);
  78. }
  79. }
  80. }