210 line
4.1 KiB

  1. //
  2. // Lol Engine
  3. //
  4. // Copyright © 2010—2016 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 LOL_USE_SDL
  14. # if HAVE_SDL2_SDL_H
  15. # include <SDL2/SDL.h>
  16. # elif HAVE_SDL_SDL_H
  17. # include <SDL/SDL.h>
  18. # else
  19. # include <SDL.h>
  20. # endif
  21. #endif
  22. #include "lolgl.h"
  23. #include "platform/sdl/sdlapp.h"
  24. #include "platform/sdl/sdlinput.h"
  25. #if LOL_USE_XINPUT
  26. # include "platform/d3d9/d3d9input.h"
  27. #endif
  28. namespace lol
  29. {
  30. /*
  31. * SDL App display implementation class
  32. */
  33. class SdlAppDisplayData
  34. {
  35. friend class SdlAppDisplay;
  36. private:
  37. #if LOL_USE_SDL
  38. SDL_Window *m_window;
  39. SDL_GLContext m_glcontext;
  40. #endif
  41. };
  42. /*
  43. * Public SdlApp class
  44. */
  45. SdlAppDisplay::SdlAppDisplay(char const *title, ivec2 res)
  46. : data(new SdlAppDisplayData())
  47. {
  48. #if LOL_USE_SDL
  49. ivec2 window_size = res;
  50. ivec2 screen_size = res;
  51. /* Initialise SDL */
  52. if (!SDL_WasInit(0))
  53. {
  54. if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_NOPARACHUTE) < 0)
  55. {
  56. msg::error("cannot initialise SDL: %s\n", SDL_GetError());
  57. exit(EXIT_FAILURE);
  58. }
  59. }
  60. #if LOL_USE_SDL
  61. /* This seems to fix the swap context bug.
  62. * However, perfs warning have been may occur. */
  63. SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
  64. data->m_window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED,
  65. SDL_WINDOWPOS_UNDEFINED,
  66. window_size.x, window_size.y,
  67. SDL_WINDOW_OPENGL);
  68. if (!data->m_window)
  69. {
  70. msg::error("cannot create rendering window: %s\n", SDL_GetError());
  71. SDL_Quit();
  72. exit(EXIT_FAILURE);
  73. }
  74. SDL_GetWindowSize(data->m_window, &res.x, &res.y);
  75. data->m_glcontext = SDL_GL_CreateContext(data->m_window);
  76. #endif
  77. /* Initialise everything */
  78. Video::Setup(res); //TODO ?? Should it be here ?
  79. #endif
  80. }
  81. SdlAppDisplay::~SdlAppDisplay()
  82. {
  83. #if LOL_USE_SDL
  84. if (data->m_window)
  85. {
  86. SDL_GL_DeleteContext(data->m_glcontext);
  87. SDL_DestroyWindow(data->m_window);
  88. }
  89. #endif
  90. delete data;
  91. }
  92. void SdlAppDisplay::SetResolution(ivec2 resolution)
  93. {
  94. #if LOL_USE_SDL
  95. SDL_SetWindowSize(data->m_window, resolution.x, resolution.y);
  96. #endif
  97. }
  98. void SdlAppDisplay::SetPosition(ivec2 position)
  99. {
  100. #if LOL_USE_SDL
  101. SDL_SetWindowPosition(data->m_window, position.x, position.y);
  102. #endif
  103. }
  104. void SdlAppDisplay::Enable()
  105. {
  106. #if LOL_USE_SDL
  107. //TODO: Should we do that: ?
  108. SDL_GL_MakeCurrent(data->m_window, data->m_glcontext);
  109. #endif
  110. }
  111. void SdlAppDisplay::Disable()
  112. {
  113. #if LOL_USE_SDL
  114. SDL_GL_SwapWindow(data->m_window);
  115. #endif
  116. }
  117. #if LOL_USE_SDL
  118. int SceneDisplay::GetPhysicalCount()
  119. {
  120. return SDL_GetNumVideoDisplays();
  121. }
  122. const char* SceneDisplay::GetPhysicalName(int index)
  123. {
  124. return SDL_GetDisplayName(index);
  125. }
  126. #endif
  127. /*
  128. * SDL App implementation class
  129. */
  130. class SdlAppData
  131. {
  132. friend class SdlApp;
  133. private:
  134. #if LOL_USE_SDL
  135. SDL_Window *m_window;
  136. SDL_GLContext m_glcontext;
  137. #endif
  138. };
  139. /*
  140. * Public SdlApp class
  141. */
  142. SdlApp::SdlApp(char const *title, ivec2 res, float fps) :
  143. data(new SdlAppData())
  144. {
  145. UNUSED(title);
  146. #if LOL_USE_SDL
  147. ivec2 window_size = res;
  148. ivec2 screen_size = res;
  149. /* Initialise everything */
  150. Ticker::Setup(fps);
  151. audio::init();
  152. /* Autoreleased objects */
  153. #if defined LOL_USE_XINPUT
  154. /* Prefer D3d9 for joysticks on Windows, because the X360 pads are not
  155. * advertised with the proper number of axes. */
  156. new D3d9Input();
  157. #endif
  158. new SdlInput(res.x, res.y, screen_size.x, screen_size.y);
  159. #endif
  160. }
  161. void SdlApp::ShowPointer(bool show)
  162. {
  163. #if LOL_USE_SDL
  164. SDL_ShowCursor(show ? 1 : 0);
  165. #endif
  166. }
  167. void SdlApp::Tick()
  168. {
  169. /* Tick the renderer, show the frame and clamp to desired framerate. */
  170. Ticker::tick_draw();
  171. }
  172. SdlApp::~SdlApp()
  173. {
  174. #if LOL_USE_SDL
  175. SDL_Quit();
  176. #endif
  177. delete data;
  178. }
  179. } /* namespace lol */