bgfxplatform.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright 2011-2015 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx/blob/master/LICENSE
  4. */
  5. #ifndef BGFX_PLATFORM_H_HEADER_GUARD
  6. #define BGFX_PLATFORM_H_HEADER_GUARD
  7. // NOTICE:
  8. // This header file contains platform specific interfaces. It is only
  9. // necessary to use this header in conjunction with creating windows.
  10. #include <bx/platform.h>
  11. namespace bgfx
  12. {
  13. /// Render frame enum.
  14. ///
  15. /// @attention C99 equivalent is `bgfx_render_frame_t`.
  16. ///
  17. struct RenderFrame
  18. {
  19. enum Enum
  20. {
  21. NoContext,
  22. Render,
  23. Exiting,
  24. Count
  25. };
  26. };
  27. /// Render frame.
  28. ///
  29. /// @returns Current renderer state. See: `bgfx::RenderFrame`.
  30. ///
  31. /// @warning This call should be only used on platforms that don't
  32. /// allow creating separate rendering thread. If it is called before
  33. /// to bgfx::init, render thread won't be created by bgfx::init call.
  34. RenderFrame::Enum renderFrame();
  35. /// Platform data.
  36. ///
  37. /// @attention C99 equivalent is `bgfx_platform_data_t`.
  38. ///
  39. struct PlatformData
  40. {
  41. void* ndt; //!< Native display type
  42. void* nwh; //!< Native window handle
  43. void* context; //!< GL context, or D3D device
  44. void* backBuffer; //!< GL backbuffer, or D3D render target view
  45. void* backBufferDS; //!< Backbuffer depth/stencil.
  46. };
  47. /// Set platform data.
  48. ///
  49. /// @warning Must be called before `bgfx::init`.
  50. ///
  51. /// @attention C99 equivalent is `bgfx_set_platform_data`.
  52. ///
  53. void setPlatformData(const PlatformData& _hooks);
  54. } // namespace bgfx
  55. #if BX_PLATFORM_ANDROID
  56. # include <android/native_window.h>
  57. namespace bgfx
  58. {
  59. ///
  60. inline void androidSetWindow(::ANativeWindow* _window)
  61. {
  62. PlatformData pd;
  63. pd.ndt = NULL;
  64. pd.nwh = _window;
  65. pd.context = NULL;
  66. pd.backBuffer = NULL;
  67. pd.backBufferDS = NULL;
  68. setPlatformData(pd);
  69. }
  70. } // namespace bgfx
  71. #elif BX_PLATFORM_IOS
  72. namespace bgfx
  73. {
  74. ///
  75. inline void iosSetEaglLayer(void* _window)
  76. {
  77. PlatformData pd;
  78. pd.ndt = NULL;
  79. pd.nwh = _window;
  80. pd.context = NULL;
  81. pd.backBuffer = NULL;
  82. pd.backBufferDS = NULL;
  83. setPlatformData(pd);
  84. }
  85. } // namespace bgfx
  86. #elif BX_PLATFORM_FREEBSD || BX_PLATFORM_LINUX || BX_PLATFORM_RPI
  87. namespace bgfx
  88. {
  89. ///
  90. inline void x11SetDisplayWindow(void* _display, uint32_t _window, void* _glx = NULL)
  91. {
  92. PlatformData pd;
  93. pd.ndt = _display;
  94. pd.nwh = (void*)(uintptr_t)_window;
  95. pd.context = _glx;
  96. pd.backBuffer = NULL;
  97. pd.backBufferDS = NULL;
  98. setPlatformData(pd);
  99. }
  100. } // namespace bgfx
  101. #elif BX_PLATFORM_NACL
  102. # include <ppapi/c/ppb_graphics_3d.h>
  103. # include <ppapi/c/ppb_instance.h>
  104. namespace bgfx
  105. {
  106. typedef void (*PostSwapBuffersFn)(uint32_t _width, uint32_t _height);
  107. ///
  108. bool naclSetInterfaces(::PP_Instance, const ::PPB_Instance*, const ::PPB_Graphics3D*, PostSwapBuffersFn);
  109. } // namespace bgfx
  110. #elif BX_PLATFORM_OSX
  111. namespace bgfx
  112. {
  113. ///
  114. inline void osxSetNSWindow(void* _window, void* _nsgl = NULL)
  115. {
  116. PlatformData pd;
  117. pd.ndt = NULL;
  118. pd.nwh = _window;
  119. pd.context = _nsgl;
  120. pd.backBuffer = NULL;
  121. pd.backBufferDS = NULL;
  122. setPlatformData(pd);
  123. }
  124. } // namespace bgfx
  125. #elif BX_PLATFORM_WINDOWS
  126. # include <windows.h>
  127. namespace bgfx
  128. {
  129. ///
  130. inline void winSetHwnd(::HWND _window)
  131. {
  132. PlatformData pd;
  133. pd.ndt = NULL;
  134. pd.nwh = _window;
  135. pd.context = NULL;
  136. pd.backBuffer = NULL;
  137. pd.backBufferDS = NULL;
  138. setPlatformData(pd);
  139. }
  140. } // namespace bgfx
  141. #elif BX_PLATFORM_WINRT
  142. # include <Unknwn.h>
  143. namespace bgfx
  144. {
  145. ///
  146. inline void winrtSetWindow(::IUnknown* _window)
  147. {
  148. PlatformData pd;
  149. pd.ndt = NULL;
  150. pd.nwh = _window;
  151. pd.context = NULL;
  152. pd.backBuffer = NULL;
  153. pd.backBufferDS = NULL;
  154. setPlatformData(pd);
  155. }
  156. } // namespace bgfx
  157. #endif // BX_PLATFORM_
  158. #if defined(_SDL_syswm_h)
  159. // If SDL_syswm.h is included before bgfxplatform.h we can enable SDL window
  160. // interop convenience code.
  161. namespace bgfx
  162. {
  163. ///
  164. inline bool sdlSetWindow(SDL_Window* _window)
  165. {
  166. SDL_SysWMinfo wmi;
  167. SDL_VERSION(&wmi.version);
  168. if (!SDL_GetWindowWMInfo(_window, &wmi) )
  169. {
  170. return false;
  171. }
  172. PlatformData pd;
  173. # if BX_PLATFORM_LINUX || BX_PLATFORM_FREEBSD
  174. pd.ndt = wmi.info.x11.display;
  175. pd.nwh = (void*)(uintptr_t)wmi.info.x11.window;
  176. # elif BX_PLATFORM_OSX
  177. pd.ndt = NULL;
  178. pd.nwh = wmi.info.cocoa.window;
  179. # elif BX_PLATFORM_WINDOWS
  180. pd.ndt = NULL;
  181. pd.nwh = wmi.info.win.window;
  182. # endif // BX_PLATFORM_
  183. pd.context = NULL;
  184. pd.backBuffer = NULL;
  185. pd.backBufferDS = NULL;
  186. setPlatformData(pd);
  187. return true;
  188. }
  189. } // namespace bgfx
  190. #elif defined(_glfw3_h_)
  191. // If GLFW/glfw3.h is included before bgfxplatform.h we can enable GLFW3
  192. // window interop convenience code.
  193. # if BX_PLATFORM_LINUX || BX_PLATFORM_FREEBSD
  194. # define GLFW_EXPOSE_NATIVE_X11
  195. # define GLFW_EXPOSE_NATIVE_GLX
  196. # elif BX_PLATFORM_OSX
  197. # define GLFW_EXPOSE_NATIVE_COCOA
  198. # define GLFW_EXPOSE_NATIVE_NSGL
  199. # elif BX_PLATFORM_WINDOWS
  200. # define GLFW_EXPOSE_NATIVE_WIN32
  201. # define GLFW_EXPOSE_NATIVE_WGL
  202. # endif //
  203. # include <GLFW/glfw3native.h>
  204. namespace bgfx
  205. {
  206. inline void glfwSetWindow(GLFWwindow* _window)
  207. {
  208. PlatformData pd;
  209. # if BX_PLATFORM_LINUX || BX_PLATFORM_FREEBSD
  210. pd.ndt = glfwGetX11Display();
  211. pd.nwh = (void*)(uintptr_t)glfwGetX11Window(_window);
  212. pd.context = glfwGetGLXContext(_window);
  213. # elif BX_PLATFORM_OSX
  214. pd.ndt = NULL;
  215. pd.nwh = glfwGetCocoaWindow(_window);
  216. pd.context = glfwGetNSGLContext(_window);
  217. # elif BX_PLATFORM_WINDOWS
  218. pd.ndt = NULL;
  219. pd.nwh = glfwGetWin32Window(_window);
  220. pd.context = NULL;
  221. # endif // BX_PLATFORM_WINDOWS
  222. pd.backBuffer = NULL;
  223. pd.backBufferDS = NULL;
  224. setPlatformData(pd);
  225. }
  226. } // namespace bgfx
  227. #endif // defined(_SDL_H)
  228. #endif // BGFX_PLATFORM_H_HEADER_GUARD