bgfxplatform.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. };
  34. void setPlatformData(const PlatformData& _hooks);
  35. } // namespace bgfx
  36. #if BX_PLATFORM_ANDROID
  37. # include <android/native_window.h>
  38. namespace bgfx
  39. {
  40. ///
  41. inline void androidSetWindow(::ANativeWindow* _window)
  42. {
  43. PlatformData pd;
  44. pd.ndt = NULL;
  45. pd.nwh = _window;
  46. pd.context = NULL;
  47. pd.backbuffer = NULL;
  48. setPlatformData(pd);
  49. }
  50. } // namespace bgfx
  51. #elif BX_PLATFORM_IOS
  52. namespace bgfx
  53. {
  54. ///
  55. inline void iosSetEaglLayer(void* _window)
  56. {
  57. PlatformData pd;
  58. pd.ndt = NULL;
  59. pd.nwh = _window;
  60. pd.context = NULL;
  61. pd.backbuffer = NULL;
  62. setPlatformData(pd);
  63. }
  64. } // namespace bgfx
  65. #elif BX_PLATFORM_FREEBSD || BX_PLATFORM_LINUX || BX_PLATFORM_RPI
  66. namespace bgfx
  67. {
  68. ///
  69. inline void x11SetDisplayWindow(void* _display, uint32_t _window, void* _glx = NULL)
  70. {
  71. PlatformData pd;
  72. pd.ndt = _display;
  73. pd.nwh = (void*)(uintptr_t)_window;
  74. pd.context = _glx;
  75. pd.backbuffer = NULL;
  76. setPlatformData(pd);
  77. }
  78. } // namespace bgfx
  79. #elif BX_PLATFORM_NACL
  80. # include <ppapi/c/ppb_graphics_3d.h>
  81. # include <ppapi/c/ppb_instance.h>
  82. namespace bgfx
  83. {
  84. typedef void (*PostSwapBuffersFn)(uint32_t _width, uint32_t _height);
  85. ///
  86. bool naclSetInterfaces(::PP_Instance, const ::PPB_Instance*, const ::PPB_Graphics3D*, PostSwapBuffersFn);
  87. } // namespace bgfx
  88. #elif BX_PLATFORM_OSX
  89. namespace bgfx
  90. {
  91. ///
  92. inline void osxSetNSWindow(void* _window, void* _nsgl = NULL)
  93. {
  94. PlatformData pd;
  95. pd.ndt = NULL;
  96. pd.nwh = _window;
  97. pd.context = _nsgl;
  98. pd.backbuffer = NULL;
  99. setPlatformData(pd);
  100. }
  101. } // namespace bgfx
  102. #elif BX_PLATFORM_WINDOWS
  103. # include <windows.h>
  104. namespace bgfx
  105. {
  106. ///
  107. inline void winSetHwnd(::HWND _window)
  108. {
  109. PlatformData pd;
  110. pd.ndt = NULL;
  111. pd.nwh = _window;
  112. pd.context = NULL;
  113. pd.backbuffer = NULL;
  114. setPlatformData(pd);
  115. }
  116. } // namespace bgfx
  117. #elif BX_PLATFORM_WINRT
  118. # include <Unknwn.h>
  119. namespace bgfx
  120. {
  121. ///
  122. inline void winrtSetWindow(::IUnknown* _window)
  123. {
  124. PlatformData pd;
  125. pd.ndt = NULL;
  126. pd.nwh = _window;
  127. pd.context = NULL;
  128. pd.backbuffer = NULL;
  129. setPlatformData(pd);
  130. }
  131. } // namespace bgfx
  132. #endif // BX_PLATFORM_
  133. #if defined(_SDL_syswm_h)
  134. // If SDL_syswm.h is included before bgfxplatform.h we can enable SDL window
  135. // interop convenience code.
  136. namespace bgfx
  137. {
  138. ///
  139. inline bool sdlSetWindow(SDL_Window* _window)
  140. {
  141. SDL_SysWMinfo wmi;
  142. SDL_VERSION(&wmi.version);
  143. if (!SDL_GetWindowWMInfo(_window, &wmi) )
  144. {
  145. return false;
  146. }
  147. PlatformData pd;
  148. # if BX_PLATFORM_LINUX || BX_PLATFORM_FREEBSD
  149. pd.ndt = wmi.info.x11.display;
  150. pd.nwh = (void*)(uintptr_t)wmi.info.x11.window;
  151. # elif BX_PLATFORM_OSX
  152. pd.ndt = NULL;
  153. pd.nwh = wmi.info.cocoa.window;
  154. # elif BX_PLATFORM_WINDOWS
  155. pd.ndt = NULL;
  156. pd.nwh = wmi.info.win.window;
  157. # endif // BX_PLATFORM_
  158. pd.context = NULL;
  159. pd.backbuffer = NULL;
  160. setPlatformData(pd);
  161. return true;
  162. }
  163. } // namespace bgfx
  164. #elif defined(_glfw3_h_)
  165. // If GLFW/glfw3.h is included before bgfxplatform.h we can enable GLFW3
  166. // window interop convenience code.
  167. # if BX_PLATFORM_LINUX || BX_PLATFORM_FREEBSD
  168. # define GLFW_EXPOSE_NATIVE_X11
  169. # define GLFW_EXPOSE_NATIVE_GLX
  170. # elif BX_PLATFORM_OSX
  171. # define GLFW_EXPOSE_NATIVE_COCOA
  172. # define GLFW_EXPOSE_NATIVE_NSGL
  173. # elif BX_PLATFORM_WINDOWS
  174. # define GLFW_EXPOSE_NATIVE_WIN32
  175. # define GLFW_EXPOSE_NATIVE_WGL
  176. # endif //
  177. # include <GLFW/glfw3native.h>
  178. namespace bgfx
  179. {
  180. inline void glfwSetWindow(GLFWwindow* _window)
  181. {
  182. PlatformData pd;
  183. # if BX_PLATFORM_LINUX || BX_PLATFORM_FREEBSD
  184. pd.ndt = glfwGetX11Display();
  185. pd.nwh = (void*)(uintptr_t)glfwGetX11Window(_window);
  186. pd.context = glfwGetGLXContext(_window);
  187. # elif BX_PLATFORM_OSX
  188. pd.ndt = NULL;
  189. pd.nwh = glfwGetCocoaWindow(_window);
  190. pd.context = glfwGetNSGLContext(_window);
  191. # elif BX_PLATFORM_WINDOWS
  192. pd.ndt = NULL;
  193. pd.nwh = glfwGetWin32Window(_window);
  194. pd.context = NULL;
  195. # endif // BX_PLATFORM_WINDOWS
  196. pd.backbuffer = NULL;
  197. setPlatformData(pd);
  198. }
  199. } // namespace bgfx
  200. #endif // defined(_SDL_H)
  201. #endif // BGFX_PLATFORM_H_HEADER_GUARD