glue.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Copyright (c) 2014-2019 Bruce A Henderson
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. */
  18. #include "SDL_config.h"
  19. #include "SDL_video.h"
  20. #include "SDL_mouse.h"
  21. #include "SDL_syswm.h"
  22. #include "bgfx/c99/bgfx.h"
  23. #include <brl.mod/blitz.mod/blitz.h>
  24. enum{
  25. MODE_SHARED=0,
  26. MODE_WIDGET=1,
  27. MODE_WINDOW=2,
  28. MODE_DISPLAY=3
  29. };
  30. enum{
  31. FLAGS_BACKBUFFER = 0x02,
  32. FLAGS_ALPHABUFFER = 0x04,
  33. FLAGS_DEPTHBUFFER = 0x08,
  34. FLAGS_STENCILBUFFER = 0x10,
  35. FLAGS_ACCUMBUFFER = 0x20,
  36. FLAGS_BORDERLESS = 0x40,
  37. FLAGS_RPI_TV_FULLSCREEN = 0x1000,
  38. FLAGS_DX = 0x1000000,
  39. FLAGS_FULLSCREEN = 0x80000000
  40. };
  41. typedef struct BBGfxContext BBGfxContext;
  42. struct BBGfxContext{
  43. BBGfxContext *succ;
  44. int mode,width,height,depth,hertz,flags,sync;
  45. SDL_Window * window;
  46. //SDL_GLContext context;
  47. SDL_SysWMinfo info;
  48. };
  49. int bbGfxGraphicsGraphicsModes( int display, int *imodes,int maxcount );
  50. void bbGfxGraphicsFlip( int sync );
  51. void bbGfxGraphicsSetGraphics( BBGfxContext *context );
  52. void bbGfxGraphicsGetSettings( BBGfxContext *context, int * width,int * height,int * depth,int * hertz,int * flags);
  53. void bbGfxGraphicsClose( BBGfxContext *context );
  54. void bmx_SDL_Poll();
  55. void bmx_SDL_WaitEvent();
  56. void * bbSDLGraphicsGetHandle(BBGfxContext *context);
  57. void bbGfxGraphicsCls();
  58. void bbGfxSetPlatformData(SDL_Window * window);
  59. static BBGfxContext *_contexts;
  60. static BBGfxContext *_currentContext;
  61. int bbGfxGraphicsGraphicsModes( int display, int *imodes,int maxcount ) {
  62. SDL_DisplayMode mode;
  63. int count,i;
  64. count = SDL_GetNumDisplayModes(display);
  65. if (count>maxcount) count=maxcount;
  66. for (i=0;i<count;i++) {
  67. SDL_GetDisplayMode(display, i, &mode);
  68. *imodes++=mode.w;
  69. *imodes++=mode.h;
  70. *imodes++=SDL_BITSPERPIXEL(mode.format);
  71. *imodes++=mode.refresh_rate;
  72. }
  73. return count;
  74. }
  75. static void * bbGfxGetWindowHandle(SDL_Window * window) {
  76. SDL_SysWMinfo wmi;
  77. SDL_VERSION(&wmi.version);
  78. if (!SDL_GetWindowWMInfo(window, &wmi) ) {
  79. return NULL;
  80. }
  81. #if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
  82. #if ENTRY_CONFIG_USE_WAYLAND
  83. wl_egl_window *win_impl = (wl_egl_window*)SDL_GetWindowData(window, "wl_egl_window");
  84. if (!win_impl) {
  85. int width, height;
  86. SDL_GetWindowSize(window, &width, &height);
  87. struct wl_surface* surface = wmi.info.wl.surface;
  88. if (!surface) {
  89. return NULL;
  90. }
  91. win_impl = wl_egl_window_create(surface, width, height);
  92. SDL_SetWindowData(window, "wl_egl_window", win_impl);
  93. }
  94. return (void*)(uintptr_t)win_impl;
  95. #else
  96. return (void*)wmi.info.x11.window;
  97. #endif
  98. #elif BX_PLATFORM_OSX
  99. return wmi.info.cocoa.window;
  100. #elif BX_PLATFORM_WINDOWS
  101. return wmi.info.win.window;
  102. #elif BX_PLATFORM_STEAMLINK
  103. return wmi.info.vivante.window;
  104. #endif // BX_PLATFORM_
  105. }
  106. void bbGfxSetPlatformData(SDL_Window * window) {
  107. SDL_SysWMinfo info;
  108. SDL_VERSION(&info.version);
  109. if (!SDL_GetWindowWMInfo(window, &info)) {
  110. bbExThrow("Error getting window info");
  111. }
  112. bgfx_platform_data_t pd;
  113. #if BX_PLATFORM_LINUX || BX_PLATFORM_BSD
  114. #if ENTRY_CONFIG_USE_WAYLAND
  115. pd.ndt = info.info.wl.display;
  116. #else
  117. pd.ndt = info.info.x11.display;
  118. #endif
  119. #elif BX_PLATFORM_OSX
  120. pd.ndt = NULL;
  121. #elif BX_PLATFORM_WINDOWS
  122. pd.ndt = NULL;
  123. #elif BX_PLATFORM_STEAMLINK
  124. pd.ndt = info.info.vivante.display;
  125. #endif // BX_PLATFORM_
  126. pd.nwh = bbGfxGetWindowHandle(window);
  127. pd.context = NULL;
  128. pd.backBuffer = NULL;
  129. pd.backBufferDS = NULL;
  130. bgfx_set_platform_data(&pd);
  131. }
  132. void bbGfxGraphicsFlip( int sync ) {
  133. //if( !_currentContext ) return;
  134. //printf("flip\n");fflush(stdout);
  135. //bgfx_set_view_rect_ratio(0, 0, 0, BGFX_BACKBUFFER_RATIO_EQUAL);
  136. bmx_bgfx_frame(0);
  137. bmx_SDL_Poll();
  138. }
  139. void bbGfxGraphicsClose( BBGfxContext *context ){
  140. BBGfxContext **p,*t;
  141. for( p=&_contexts;(t=*p) && (t!=context);p=&t->succ ){}
  142. if( !t ) return;
  143. if( t==_currentContext ){
  144. bbGfxGraphicsSetGraphics( 0 );
  145. }
  146. if( t->mode==MODE_DISPLAY || t->mode==MODE_WINDOW ){
  147. SDL_DestroyWindow(context->window);
  148. }
  149. }
  150. void bbGfxGraphicsSetGraphics( BBGfxContext *context ) {
  151. //if( context ){
  152. // SDL_GL_MakeCurrent(context->window, context->context);
  153. //}
  154. _currentContext=context;
  155. }
  156. void bmx_SDL_WarpMouseInWindow(int x, int y) {
  157. if( _currentContext ){
  158. SDL_WarpMouseInWindow(_currentContext->window, x, y);
  159. } else {
  160. SDL_WarpMouseInWindow(SDL_GL_GetCurrentWindow(), x, y);
  161. }
  162. }
  163. void bbGfxGraphicsGetSettings( BBGfxContext *context, int * width,int * height,int * depth,int * hertz,int * flags) {
  164. if( context ){
  165. SDL_GetWindowSize(context->window, &context->width, &context->height);
  166. *width=context->width;
  167. *height=context->height;
  168. *depth=context->depth;
  169. *hertz=context->hertz;
  170. *flags=context->flags;
  171. }
  172. }
  173. void bbGfxGraphicsCls() {
  174. bgfx_encoder_t* encoder = bgfx_encoder_begin(true);
  175. bgfx_encoder_touch(encoder, 0);
  176. bgfx_encoder_end(encoder);
  177. }
  178. /*
  179. void bbSDLExit(){
  180. bbGfxGraphicsClose( _currentContext );
  181. _currentContext=0;
  182. #ifdef __RASPBERRYPI__
  183. SDL_VideoQuit();
  184. #endif
  185. }
  186. */