You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

216 lines
4.0 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2020 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. #include <lol/msg>
  14. #include <cassert>
  15. #if HAVE_EMSCRIPTEN_H
  16. # include <emscripten.h>
  17. #endif
  18. #include "lolgl.h"
  19. #if __ANDROID__
  20. # include "application/android-app.h"
  21. #elif __NX__
  22. # include "private/nx/nx-app.h"
  23. #elif LOL_USE_SDL
  24. # include "application/sdl-app.h"
  25. #elif HAVE_GLES_2X
  26. # include "application/egl-app.h"
  27. #endif
  28. namespace lol
  29. {
  30. class null_display
  31. {
  32. friend class ApplicationDisplayData;
  33. friend class ApplicationDisplay;
  34. public:
  35. null_display(char const *, ivec2) {}
  36. virtual ~null_display() {}
  37. protected:
  38. ivec2 resolution() const { return ivec2(0); }
  39. void set_resolution(ivec2) {}
  40. void SetPosition(ivec2) {}
  41. void Enable() {}
  42. void Disable() {}
  43. };
  44. class null_app
  45. {
  46. public:
  47. null_app(char const *, ivec2, float)
  48. {
  49. msg::error("no display library (SDL, EGL…) available");
  50. assert(false);
  51. }
  52. virtual ~null_app() {}
  53. void ShowPointer(bool) {}
  54. void Tick() {}
  55. };
  56. //-----------------------------------------------------------------------------
  57. class ApplicationDisplayData
  58. {
  59. friend class ApplicationDisplay;
  60. ApplicationDisplayData(char const *name, ivec2 res)
  61. : display(name, res)
  62. { }
  63. protected:
  64. #if __ANDROID__
  65. // TODO: implement this
  66. null_display display;
  67. #elif __NX__
  68. nx::app_display display;
  69. #elif LOL_USE_SDL
  70. sdl::app_display display;
  71. #elif HAVE_GLES_2X
  72. /* FIXME: this macro is only deactivated if we include "lolgl.h" */
  73. //NOT HANDLED YET
  74. #else
  75. null_display display;
  76. #endif
  77. };
  78. ApplicationDisplay::ApplicationDisplay(char const *name, ivec2 res)
  79. {
  80. data = new ApplicationDisplayData(name, res);
  81. }
  82. ApplicationDisplay::~ApplicationDisplay()
  83. {
  84. delete data;
  85. }
  86. ivec2 ApplicationDisplay::resolution() const
  87. {
  88. return data->display.resolution();
  89. }
  90. void ApplicationDisplay::set_resolution(ivec2 res)
  91. {
  92. super::set_resolution(res);
  93. data->display.set_resolution(res);
  94. }
  95. void ApplicationDisplay::SetPosition(ivec2 position)
  96. {
  97. super::SetPosition(position);
  98. data->display.SetPosition(position);
  99. }
  100. void ApplicationDisplay::Enable()
  101. {
  102. super::Enable();
  103. data->display.Enable();
  104. }
  105. void ApplicationDisplay::Disable()
  106. {
  107. data->display.Disable();
  108. super::Disable();
  109. }
  110. //-----------------------------------------------------------------------------
  111. class ApplicationData
  112. {
  113. friend class Application;
  114. ApplicationData(char const *name, ivec2 res, float framerate)
  115. : app(name, res, framerate)
  116. { }
  117. #if __ANDROID__
  118. AndroidApp app;
  119. #elif __NX__
  120. nx::app app;
  121. #elif LOL_USE_SDL
  122. sdl::app app;
  123. #elif HAVE_GLES_2X
  124. /* FIXME: this macro is only deactivated if we include "lolgl.h" */
  125. EglApp app;
  126. #else
  127. null_app app;
  128. #endif
  129. };
  130. #if __EMSCRIPTEN__
  131. static Application *g_app;
  132. static void AppCallback()
  133. {
  134. g_app->Tick();
  135. }
  136. #endif
  137. /*
  138. * Public Application class
  139. */
  140. Application::Application(char const *name, ivec2 res, float framerate)
  141. {
  142. ticker::setup(framerate);
  143. auto app_display = new ApplicationDisplay(name, res);
  144. SceneDisplay::Add(app_display);
  145. data = new ApplicationData(name, app_display->resolution(), framerate);
  146. }
  147. bool Application::MustTick()
  148. {
  149. return !Ticker::Finished();
  150. }
  151. void Application::Tick()
  152. {
  153. data->app.Tick();
  154. }
  155. void Application::Run()
  156. {
  157. #if __EMSCRIPTEN__
  158. g_app = this;
  159. emscripten_set_main_loop(AppCallback, 0, 1);
  160. #else
  161. while (MustTick())
  162. Tick();
  163. #endif
  164. }
  165. void Application::ShowPointer(bool show)
  166. {
  167. data->app.ShowPointer(show);
  168. }
  169. Application::~Application()
  170. {
  171. ticker::teardown();
  172. delete data;
  173. }
  174. } /* namespace lol */