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.
 
 
 

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