Application.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. namespace AtomicEngine
  3. {
  4. public interface IAtomicSDLSurface
  5. {
  6. void Remove();
  7. bool IsAlive { get; }
  8. }
  9. public partial class Application : AObject
  10. {
  11. public static AppBase AppBase { get; private set; }
  12. public static int Run<T>(string[] args) where T : AppDelegate, new()
  13. {
  14. return Run(typeof(T), args);
  15. }
  16. public static int Run(Type appDelegateType, string[] args)
  17. {
  18. // Create the Application
  19. #if ATOMIC_DESKTOP
  20. AppBase = NETIPCPlayerApp.Create(args);
  21. #endif
  22. #if ATOMIC_IOS
  23. // On iOS, we need to set main ready as main() isn't being called
  24. SDLEvents.SetMainReady();
  25. #endif
  26. #if ATOMIC_MOBILE
  27. AppBase = NETAtomicPlayer.Create(args);
  28. var renderer = AtomicNET.GetSubsystem<Renderer>();
  29. renderer.ReuseShadowMaps = false;
  30. renderer.ShadowQuality = ShadowQuality.SHADOWQUALITY_SIMPLE_16BIT;
  31. #endif
  32. CSComponentAssembly.PreloadClassAssemblies();
  33. appDelegate = (AppDelegate)Activator.CreateInstance(appDelegateType);
  34. try
  35. {
  36. appDelegate.Start();
  37. // Managed code in charge of main loop
  38. while (AppBase.RunFrame())
  39. {
  40. appDelegate.PostFrame();
  41. }
  42. appDelegate.Shutdown();
  43. // Shut 'er down
  44. AppBase.Shutdown();
  45. }
  46. catch (Exception e)
  47. {
  48. if (e.InnerException != null)
  49. {
  50. Log.Error(e.InnerException.StackTrace);
  51. // rethrow inner exception
  52. throw e.InnerException;
  53. }
  54. else
  55. {
  56. Log.Error(e.StackTrace);
  57. // rethrow
  58. throw e;
  59. }
  60. }
  61. finally
  62. {
  63. AppBase = null;
  64. }
  65. return 0;
  66. }
  67. #if ATOMIC_DESKTOP
  68. public static int RunServer<T>(string[] args) where T : AppDelegate, new()
  69. {
  70. return RunServer(typeof(T), args);
  71. }
  72. public static int RunServer(Type appDelegateType, string[] args)
  73. {
  74. // Create the Application
  75. AppBase = NETIPCServerApp.Create(args);
  76. appDelegate = (AppDelegate)Activator.CreateInstance(appDelegateType);
  77. try
  78. {
  79. appDelegate.Start();
  80. // Managed code in charge of main loop
  81. while (AppBase.RunFrame())
  82. {
  83. appDelegate.PostFrame();
  84. }
  85. appDelegate.Shutdown();
  86. // Shut 'er down
  87. AppBase.Shutdown();
  88. }
  89. catch (Exception e)
  90. {
  91. if (e.InnerException != null)
  92. {
  93. Log.Error(e.InnerException.StackTrace);
  94. // rethrow inner exception
  95. throw e.InnerException;
  96. }
  97. else
  98. {
  99. Log.Error(e.StackTrace);
  100. // rethrow
  101. throw e;
  102. }
  103. }
  104. finally
  105. {
  106. AppBase = null;
  107. }
  108. return 0;
  109. }
  110. #endif
  111. internal static AppDelegate appDelegate;
  112. }
  113. }