bgfxsdlgraphics.bmx 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. ' Copyright (c) 2015-2019 Bruce A Henderson
  2. ' All rights reserved.
  3. '
  4. ' Redistribution and use in source and binary forms, with or without
  5. ' modification, are permitted provided that the following conditions are met:
  6. '
  7. ' * Redistributions of source code must retain the above copyright notice, this
  8. ' list of conditions and the following disclaimer.
  9. '
  10. ' * Redistributions in binary form must reproduce the above copyright notice,
  11. ' this list of conditions and the following disclaimer in the documentation
  12. ' and/or other materials provided with the distribution.
  13. '
  14. ' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  15. ' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  16. ' IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. ' DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  18. ' FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. ' DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  20. ' SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  21. ' CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  22. ' OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  23. ' OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. '
  25. SuperStrict
  26. Module gfx.bgfxsdlgraphics
  27. Import "common.bmx"
  28. Private
  29. Global _currentContext:TGraphicsContext
  30. Public
  31. Type TGraphicsContext
  32. Field Mode:Int
  33. Field width:Int
  34. Field height:Int
  35. Field depth:Int
  36. Field hertz:Int
  37. Field flags:Int
  38. Field sync:Int
  39. Field window:TSDLWindow
  40. ' Field context:TSDLGLContext
  41. Field info:Byte Ptr
  42. Field data:Object
  43. End Type
  44. Private
  45. Extern
  46. Function bbGfxGraphicsShareContexts()
  47. Function bbGfxGraphicsGraphicsModes:Int( display:Int, buf:Byte Ptr, size:Int )
  48. Function bbGfxGraphicsAttachGraphics:Byte Ptr( widget:Byte Ptr,flags:Int )
  49. Function bbGfxGraphicsGetSettings( context:Byte Ptr, width:Int Var, height:Int Var, depth:Int Var, hertz:Int Var, flags:Int Var )
  50. Function bbGfxGraphicsClose( context:Byte Ptr )
  51. Function bbGfxGraphicsSetGraphics( context:Byte Ptr )
  52. Function bbGfxGraphicsFlip( sync:Int )
  53. Function bbGfxGraphicsCls()
  54. Function bbGfxSetPlatformData(handle:Byte Ptr)
  55. End Extern
  56. Public
  57. Type TGfxGraphics Extends TGraphics
  58. Method Driver:TGfxGraphicsDriver()
  59. Assert _context
  60. Return GfxGraphicsDriver()
  61. End Method
  62. Method GetSettings:Int( width:Int Var,height:Int Var,depth:Int Var,hertz:Int Var,flags:Int Var )
  63. Assert _context
  64. width=_context.width
  65. height=_context.height
  66. depth=_context.depth
  67. hertz=_context.hertz
  68. flags=_context.flags
  69. End Method
  70. Method Close:Int()
  71. If Not _context Return 0
  72. If _currentContext = _context Then
  73. _currentContext = Null
  74. End If
  75. If _context.window Then
  76. _context.window.Destroy()
  77. End If
  78. _context=Null
  79. End Method
  80. Method GetHandle:Byte Ptr()
  81. If _context Then
  82. Return _context.window.GetWindowHandle()
  83. End If
  84. End Method
  85. Method Resize:Int(width:Int, height:Int)
  86. TBGFX.Reset(width, height, BGFX_RESET_VSYNC | BGFX_RESET_HIDPI)
  87. End Method
  88. Field _context:TGraphicsContext
  89. End Type
  90. Type TGfxGraphicsDriver Extends TGraphicsDriver
  91. Global _inited:Int
  92. Method GraphicsModes:TGraphicsMode[]()
  93. Local buf:Int[1024*4]
  94. Local count:Int=bbGfxGraphicsGraphicsModes( 0,buf,1024 )
  95. Local modes:TGraphicsMode[count],p:Int Ptr=buf
  96. For Local i:Int=0 Until count
  97. Local t:TGraphicsMode=New TGraphicsMode
  98. t.width=p[0]
  99. t.height=p[1]
  100. t.depth=p[2]
  101. t.hertz=p[3]
  102. modes[i]=t
  103. p:+4
  104. Next
  105. Return modes
  106. End Method
  107. Method AttachGraphics:TGfxGraphics( widget:Byte Ptr,flags:Int )
  108. 'Local t:TGfxGraphics=New TGfxGraphics
  109. 't._context=bbGLGraphicsAttachGraphics( widget,flags )
  110. 'Return t
  111. End Method
  112. Method CreateGraphics:TGfxGraphics( width:Int, height:Int, depth:Int, hertz:Int, flags:Int )
  113. Local t:TGfxGraphics=New TGfxGraphics
  114. t._context=GfxGraphicsCreateGraphics( width,height,depth,hertz,flags )
  115. If Not _inited Then
  116. Local rtype:EBGFXRenderType
  117. ?win32
  118. ' hard coded for now until I can work out how to get the others to render correctly...
  119. rtype = EBGFXRenderType.DIRECT3D9
  120. ?Not win32
  121. rtype = EBGFXRenderType.OPENGL
  122. ?
  123. InitGraphics(width, height, rtype)
  124. End If
  125. Return t
  126. End Method
  127. Method GfxGraphicsCreateGraphics:TGraphicsContext(width:Int,height:Int,depth:Int,hertz:Int,flags:Int)
  128. Local context:TGraphicsContext = New TGraphicsContext
  129. Local windowFlags:UInt '= SDL_WINDOW_ALLOW_HIGHDPI
  130. Local gFlags:UInt
  131. Local glFlags:UInt = flags
  132. Rem
  133. If flags & SDL_GRAPHICS_NATIVE Then
  134. flags :~ SDL_GRAPHICS_NATIVE
  135. gFlags = flags & (SDL_GRAPHICS_BACKBUFFER | SDL_GRAPHICS_ALPHABUFFER | SDL_GRAPHICS_DEPTHBUFFER | SDL_GRAPHICS_STENCILBUFFER | SDL_GRAPHICS_ACCUMBUFFER)
  136. flags :~ SDL_GRAPHICS_GL
  137. flags :~ (SDL_GRAPHICS_BACKBUFFER | SDL_GRAPHICS_ALPHABUFFER | SDL_GRAPHICS_DEPTHBUFFER | SDL_GRAPHICS_STENCILBUFFER | SDL_GRAPHICS_ACCUMBUFFER)
  138. windowFlags :| flags
  139. If glFlags Then
  140. If gFlags & SDL_GRAPHICS_BACKBUFFER Then SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)
  141. If gFlags & SDL_GRAPHICS_ALPHABUFFER Then SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 1)
  142. If gFlags & SDL_GRAPHICS_DEPTHBUFFER Then SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24)
  143. If gFlags & SDL_GRAPHICS_STENCILBUFFER Then SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1)
  144. End If
  145. Else
  146. If depth Then
  147. windowFlags :| SDL_WINDOW_FULLSCREEN
  148. ' mode = MODE_DISPLAY
  149. Else
  150. If flags & $80000000 Then
  151. windowFlags :| SDL_WINDOW_FULLSCREEN_DESKTOP
  152. End If
  153. ' mode = MODE_WINDOW
  154. End If
  155. gFlags = flags & (GRAPHICS_BACKBUFFER | GRAPHICS_ALPHABUFFER | GRAPHICS_DEPTHBUFFER | GRAPHICS_STENCILBUFFER | GRAPHICS_ACCUMBUFFER)
  156. If glFlags Then
  157. If gFlags & GRAPHICS_BACKBUFFER Then SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1)
  158. If gFlags & GRAPHICS_ALPHABUFFER Then SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 1)
  159. If gFlags & GRAPHICS_DEPTHBUFFER Then SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24)
  160. If gFlags & GRAPHICS_STENCILBUFFER Then SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 1)
  161. End If
  162. End If
  163. End Rem
  164. 'End If
  165. context.window = TSDLWindow.Create(AppTitle, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, windowFlags)
  166. 'If glFlags Then
  167. ' context.context = context.window.GLCreateContext()
  168. ' SDL_GL_SetSwapInterval(-1)
  169. ' context.sync = -1
  170. 'End If
  171. context.width = width
  172. context.height = height
  173. context.depth = depth
  174. context.hertz = hertz
  175. context.flags = flags
  176. bbGfxSetPlatformData(context.window.WindowPtr)
  177. TBGFX.RenderFrame()
  178. AddHook EmitEventHook,GraphicsHook,context,0
  179. Return context
  180. End Method
  181. Method SetGraphics:Int( g:TGraphics )
  182. Local context:Byte Ptr
  183. Local t:TGfxGraphics=TGfxGraphics( g )
  184. If t context=t._context
  185. bbGfxGraphicsSetGraphics context
  186. End Method
  187. Method Flip:Int( sync:Int )
  188. bbGfxGraphicsFlip sync
  189. End Method
  190. Method SetViewRect()
  191. TBGFX.SetViewRectRatio(0, 0, 0, 0)
  192. End Method
  193. Rem
  194. bbdoc:
  195. End Rem
  196. Method InitGraphics(width:Int, height:Int, rendererType:EBGFXRenderType = EBGFXRenderType.COUNT)
  197. If Not TBGFX.Init(width, height, rendererType) Then
  198. Throw "Failed to initialise graphics"
  199. End If
  200. _inited = True
  201. End Method
  202. Function GraphicsHook:Object( id:Int,data:Object,context:Object )
  203. Local ev:TEvent=TEvent(data)
  204. If Not ev Return data
  205. Select ev.id
  206. Case EVENT_WINDOWSIZE
  207. Local ctxt:TGraphicsContext = TGraphicsContext(context)
  208. If ctxt Then
  209. If ctxt.window.GetID() = ev.data Then
  210. ctxt.width = ev.x
  211. ctxt.height = ev.y
  212. GraphicsResize(ev.x, ev.y)
  213. End If
  214. End If
  215. End Select
  216. Return data
  217. End Function
  218. Method CanResize:Int()
  219. Return True
  220. End Method
  221. End Type
  222. Function GfxGraphicsDriver:TGfxGraphicsDriver()
  223. Global _driver:TGfxGraphicsDriver=New TGfxGraphicsDriver
  224. Return _driver
  225. End Function
  226. Rem
  227. bbdoc: Create graphics
  228. returns: A graphics object
  229. about:
  230. This is a convenience function that allows you to easily create a graphics context.
  231. End Rem
  232. Function GfxGraphics:TGraphics( width:Int, height:Int, depth:Int = 0, hertz:Int = 60, flags:Int = 0 )
  233. SetGraphicsDriver GfxGraphicsDriver()
  234. Return Graphics( width,height,depth,hertz,flags )
  235. End Function
  236. SetGraphicsDriver GfxGraphicsDriver()