202 lines
3.7 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2019 Sam Hocevar <sam@hocevar.net>
  5. //
  6. // Lol Engine is free software. It comes without any warranty, to
  7. // the extent permitted by applicable law. You can redistribute it
  8. // and/or modify it under the terms of the Do What the Fuck You Want
  9. // to Public License, Version 2, as published by the WTFPL Task Force.
  10. // See http://www.wtfpl.net/ for more details.
  11. //
  12. #include <lol/engine-internal.h>
  13. #if HAVE_EMSCRIPTEN_H
  14. # include <emscripten.h>
  15. #endif
  16. #include "lolgl.h"
  17. #if __ANDROID__
  18. # include "platform/android/androidapp.h"
  19. #elif LOL_USE_SDL
  20. # include "application/sdl-app.h"
  21. #elif HAVE_GLES_2X
  22. # include "eglapp.h"
  23. #endif
  24. namespace lol
  25. {
  26. class null_display
  27. {
  28. friend class ApplicationDisplayData;
  29. friend class ApplicationDisplay;
  30. public:
  31. null_display(char const *, ivec2) {}
  32. virtual ~null_display() {}
  33. protected:
  34. ivec2 resolution() const { return ivec2(0); }
  35. void set_resolution(ivec2) {}
  36. void SetPosition(ivec2) {}
  37. void Enable() {}
  38. void Disable() {}
  39. };
  40. class null_app
  41. {
  42. public:
  43. null_app(char const *, ivec2, float)
  44. {
  45. ASSERT(false, "no display library (SDL, EGL…) available");
  46. }
  47. virtual ~null_app() {}
  48. void ShowPointer(bool) {}
  49. void Tick() {}
  50. };
  51. //-----------------------------------------------------------------------------
  52. class ApplicationDisplayData
  53. {
  54. friend class ApplicationDisplay;
  55. ApplicationDisplayData(char const *name, ivec2 res)
  56. : display(name, res)
  57. { }
  58. protected:
  59. #if __ANDROID__
  60. //NOT HANDLED YET
  61. #elif LOL_USE_SDL
  62. SdlAppDisplay display;
  63. #elif HAVE_GLES_2X
  64. /* FIXME: this macro is only deactivated if we include "lolgl.h" */
  65. //NOT HANDLED YET
  66. #else
  67. null_display display;
  68. #endif
  69. };
  70. ApplicationDisplay::ApplicationDisplay(char const *name, ivec2 res)
  71. {
  72. data = new ApplicationDisplayData(name, res);
  73. }
  74. ApplicationDisplay::~ApplicationDisplay()
  75. {
  76. delete data;
  77. }
  78. ivec2 ApplicationDisplay::resolution() const
  79. {
  80. return data->display.resolution();
  81. }
  82. void ApplicationDisplay::set_resolution(ivec2 res)
  83. {
  84. super::set_resolution(res);
  85. data->display.set_resolution(res);
  86. }
  87. void ApplicationDisplay::SetPosition(ivec2 position)
  88. {
  89. super::SetPosition(position);
  90. data->display.SetPosition(position);
  91. }
  92. void ApplicationDisplay::Enable()
  93. {
  94. super::Enable();
  95. data->display.Enable();
  96. }
  97. void ApplicationDisplay::Disable()
  98. {
  99. data->display.Disable();
  100. super::Disable();
  101. }
  102. //-----------------------------------------------------------------------------
  103. class ApplicationData
  104. {
  105. friend class Application;
  106. ApplicationData(char const *name, ivec2 res, float framerate)
  107. : app(name, res, framerate)
  108. { }
  109. #if __ANDROID__
  110. AndroidApp app;
  111. #elif LOL_USE_SDL
  112. SdlApp app;
  113. #elif HAVE_GLES_2X
  114. /* FIXME: this macro is only deactivated if we include "lolgl.h" */
  115. EglApp app;
  116. #else
  117. null_app app;
  118. #endif
  119. };
  120. #if __EMSCRIPTEN__
  121. static Application *g_app;
  122. static void AppCallback()
  123. {
  124. g_app->Tick();
  125. }
  126. #endif
  127. /*
  128. * Public Application class
  129. */
  130. Application::Application(char const *name, ivec2 res, float framerate)
  131. {
  132. auto app_display = new ApplicationDisplay(name, res);
  133. SceneDisplay::Add(app_display);
  134. data = new ApplicationData(name, app_display->resolution(), framerate);
  135. }
  136. bool Application::MustTick()
  137. {
  138. return !Ticker::Finished();
  139. }
  140. void Application::Tick()
  141. {
  142. data->app.Tick();
  143. }
  144. void Application::Run()
  145. {
  146. #if __EMSCRIPTEN__
  147. g_app = this;
  148. emscripten_set_main_loop(AppCallback, 0, 1);
  149. #else
  150. while (MustTick())
  151. Tick();
  152. #endif
  153. }
  154. void Application::ShowPointer(bool show)
  155. {
  156. data->app.ShowPointer(show);
  157. }
  158. Application::~Application()
  159. {
  160. delete data;
  161. }
  162. } /* namespace lol */