bgfxplatform.h 5.4 KB

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