sdlrender.bmx 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. ' Copyright (c) 2014-2022 Bruce A Henderson
  2. '
  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. '
  7. ' Permission is granted to anyone to use this software for any purpose,
  8. ' including commercial applications, and to alter it and redistribute it
  9. ' freely, subject to the following restrictions:
  10. '
  11. ' 1. The origin of this software must not be misrepresented; you must not
  12. ' claim that you wrote the original software. If you use this software
  13. ' in a product, an acknowledgment in the product documentation would be
  14. ' appreciated but is not required.
  15. '
  16. ' 2. Altered source versions must be plainly marked as such, and must not be
  17. ' misrepresented as being the original software.
  18. '
  19. ' 3. This notice may not be removed or altered from any source
  20. ' distribution.
  21. '
  22. SuperStrict
  23. Rem
  24. bbdoc: 2D Rendering.
  25. End Rem
  26. Module SDL.SDLRender
  27. Import SDL.SDLVideo
  28. Import "common.bmx"
  29. Rem
  30. bbdoc: A 2D rendering context.
  31. End Rem
  32. Type TSDLRenderer
  33. Field rendererPtr:Byte Ptr
  34. Function _create:TSDLRenderer(rendererPtr:Byte Ptr)
  35. If rendererPtr Then
  36. Local this:TSDLRenderer = New TSDLRenderer
  37. this.rendererPtr = rendererPtr
  38. Return this
  39. End If
  40. End Function
  41. Rem
  42. bbdoc: Creates a 2D rendering context for a window.
  43. about: Note that providing no flags gives priority to available #SDL_RENDERER_ACCELERATED renderers
  44. @flags can be one or more of the following values -
  45. |----------------------------|------------------------------------------------|
  46. | SDL_RENDERER_SOFTWARE | the renderer is a software fallback |
  47. | SDL_RENDERER_ACCELERATED | the renderer uses hardware acceleration |
  48. | SDL_RENDERER_PRESENTVSYNC | present is synchronized with the refresh rate |
  49. | SDL_RENDERER_TARGETTEXTURE | the renderer supports rendering to texture |
  50. |----------------------------|------------------------------------------------|
  51. End Rem
  52. Function Create:TSDLRenderer(window:TSDLWindow, index:Int = -1, flags:UInt = 0)
  53. Return _create(SDL_CreateRenderer(window.windowPtr, index, flags))
  54. End Function
  55. Rem
  56. bbdoc: Creates a 2D software rendering context for a surface.
  57. End Rem
  58. Function CreateSoftwareRenderer:TSDLRenderer(surface:TSDLSurface)
  59. Return _create(SDL_CreateSoftwareRenderer(surface.surfacePtr))
  60. End Function
  61. Rem
  62. bbdoc: Gets the renderer associated with a window.
  63. End Rem
  64. Function GetRenderer:TSDLRenderer(window:TSDLWindow)
  65. Return _create(SDL_GetRenderer(window.windowPtr))
  66. End Function
  67. Rem
  68. bbdoc: Creates a texture for a rendering context.
  69. End Rem
  70. Method CreateTexture:TSDLTexture(format:UInt, access:Int, width:Int, height:Int)
  71. Return TSDLTexture._create(SDL_CreateTexture(rendererPtr, format, access, width, height))
  72. End Method
  73. Rem
  74. bbdoc: Creates a texture from an existing surface.
  75. End Rem
  76. Method CreateTextureFromSurface:TSDLTexture(surface:TSDLSurface)
  77. Return TSDLTexture._create(SDL_CreateTextureFromSurface(rendererPtr, surface.surfacePtr))
  78. End Method
  79. Rem
  80. bbdoc: Gets the blend mode used for drawing operations.
  81. End Rem
  82. Method GetDrawBlendMode:Int(blendMode:Int Var)
  83. Return SDL_GetRenderDrawBlendMode(rendererPtr, Varptr blendMode)
  84. End Method
  85. Rem
  86. bbdoc: Gets the color used for drawing operations (Rect, Line and Clear).
  87. End Rem
  88. Method GetDrawColor:Int(r:Byte Var, g:Byte Var, b:Byte Var, a:Byte Var)
  89. Return SDL_GetRenderDrawColor(rendererPtr, Varptr r, Varptr g, Varptr b, Varptr a)
  90. End Method
  91. Rem
  92. bbdoc: Gets the current render target.
  93. returns: The current render target or #Null for the default render target.
  94. about: The default render target is the window for which the renderer was created, and is reported as #Null here.
  95. End Rem
  96. Method GetTarget:TSDLTexture()
  97. Return TSDLTexture._create(SDL_GetRenderTarget(rendererPtr))
  98. End Method
  99. Rem
  100. bbdoc: Gets the output size in pixels of a rendering context.
  101. End Rem
  102. Method GetOutputSize:Int(w:Int Var, h:Int Var)
  103. Return SDL_GetRendererOutputSize(rendererPtr, Varptr w, Varptr h)
  104. End Method
  105. Rem
  106. bbdoc: Clears the current rendering target with the drawing color.
  107. End Rem
  108. Method Clear:Int()
  109. Return SDL_RenderClear(rendererPtr)
  110. End Method
  111. Rem
  112. bbdoc: Copies a portion of the texture to the current rendering target.
  113. End Rem
  114. Method Copy:Int(texture:TSDLTexture, sx:Int = -1, sy:Int = -1, sw:Int = -1, sh:Int = -1, dx:Int = -1, dy:Int = -1, dw:Int = -1, dh:Int = -1)
  115. Return bmx_SDL_RenderCopy(rendererPtr, texture.texturePtr, sx, sy, sw, sh, dx, dy, dw, dh)
  116. End Method
  117. Rem
  118. bbdoc: Copies a portion of the texture to the current rendering target, optionally rotating it by angle around the given center and also flipping it top-bottom and/or left-right.
  119. End Rem
  120. Method CopyEx:Int(texture:TSDLTexture, sx:Int = -1, sy:Int = -1, sw:Int = -1, sh:Int = -1, dx:Int = -1, dy:Int = -1, dw:Int = -1, dh:Int = -1, angle:Double = 0, cx:Int = -1, cy:Int = -1, flipMode:Int = SDL_FLIP_NONE)
  121. Return bmx_SDL_RenderCopyEx(rendererPtr, texture.texturePtr, sx, sy, sw, sh, dx, dy, dw, dh, angle, cx, cy, flipMode)
  122. End Method
  123. Rem
  124. bbdoc: Draws a line on the current rendering target.
  125. End Rem
  126. Method DrawLine:Int(x1:Int, y1:Int, x2:Int, y2:Int)
  127. Return SDL_RenderDrawLine(rendererPtr, x1, y1, x2, y2)
  128. End Method
  129. Rem
  130. bbdoc: Draws a series of connected lines on the current rendering target.
  131. about: A point consists of a pair of Ints (x, y), where @count is the count of pairs.
  132. End Rem
  133. Method DrawLines:Int(points:Int Ptr, count:Int)
  134. Return SDL_RenderDrawLines(rendererPtr, points, count)
  135. End Method
  136. Rem
  137. bbdoc: Draws a point on the current rendering target.
  138. End Rem
  139. Method DrawPoint:Int(x:Int, y:Int)
  140. Return SDL_RenderDrawPoint(rendererPtr, x, y)
  141. End Method
  142. Rem
  143. bbdoc: Draws multiple points on the current rendering target.
  144. End Rem
  145. Method DrawPoints:Int(points:Int Ptr, count:Int)
  146. Return SDL_RenderDrawPoints(rendererPtr, points, count)
  147. End Method
  148. Rem
  149. bbdoc: Draws a rectangle on the current rendering target.
  150. End Rem
  151. Method DrawRect:Int(x:Int = -1, y:Int = -1, w:Int = -1, h:Int = -1)
  152. Return bmx_SDL_RenderDrawRect(rendererPtr, x, y, w, h)
  153. End Method
  154. Rem
  155. bbdoc: Draws some number of rectangles on the current rendering target.
  156. End Rem
  157. Method DrawRects:Int(rects:Int Ptr, count:Int)
  158. Return SDL_RenderDrawRects(rendererPtr, rects, count)
  159. End Method
  160. Rem
  161. bbdoc: Fills a rectangle on the current rendering target with the drawing color.
  162. End Rem
  163. Method FillRect:Int(x:Int = -1, y:Int = -1, w:Int = -1, h:Int = -1)
  164. Return bmx_SDL_RenderFillRect(rendererPtr, x, y, w, h)
  165. End Method
  166. Rem
  167. bbdoc: Fills some number of rectangles on the current rendering target with the drawing color.
  168. returns: 0 on success or a negative error code on failure; call #SDLGetError for more information.
  169. End Rem
  170. Method FillRects:Int(rects:Int Ptr, count:Int)
  171. Return SDL_RenderFillRects(rendererPtr, rects, count)
  172. End Method
  173. Rem
  174. bbdoc: Renders a list of triangles, optionally using a texture and indices into the vertex array Color and alpha modulation is done per vertex.
  175. returns: 0 on success, or -1 if the operation is not supported
  176. End Rem
  177. Method Geometry:Int(texture:TSDLTexture, vertices:SDLVertex Ptr, numVertices:Int, indices:Int Ptr, numIndices:Int)
  178. If texture Then
  179. Return SDL_RenderGeometry(rendererPtr, texture.texturePtr, vertices, numVertices, indices, numIndices)
  180. Else
  181. Return SDL_RenderGeometry(rendererPtr, Null, vertices, numVertices, indices, numIndices)
  182. End If
  183. End Method
  184. Rem
  185. bbdoc:
  186. End Rem
  187. Method GetClipRect(x:Int Var, y:Int Var, w:Int Var, h:Int Var)
  188. bmx_SDL_RenderGetClipRect(rendererPtr, Varptr x, Varptr y, Varptr w, Varptr h)
  189. End Method
  190. Rem
  191. bbdoc: Gets information about the rendering context.
  192. End Rem
  193. Method GetInfo:SDLRendererInfo()
  194. Local info:SDLRendererInfo
  195. SDL_GetRendererInfo(rendererPtr, VarPtr info)
  196. return info
  197. End Method
  198. Rem
  199. bbdoc: Gets whether integer scales are forced for resolution-independent rendering.
  200. End Rem
  201. Method GetIntegerScale:Int()
  202. Return SDL_RenderGetIntegerScale(rendererPtr)
  203. End Method
  204. Rem
  205. bbdoc: Gets device independent resolution for rendering.
  206. End Rem
  207. Method GetLogicalSize(w:Int Var, h:Int Var)
  208. SDL_RenderGetLogicalSize(rendererPtr, Varptr w, Varptr h)
  209. End Method
  210. Rem
  211. bbdoc: Gets the drawing scale for the current target.
  212. End Rem
  213. Method GetScale(x:Float Var, y:Float Var)
  214. SDL_RenderGetScale(rendererPtr, Varptr x, Varptr y)
  215. End Method
  216. Rem
  217. bbdoc: Gets the drawing area for the current target.
  218. End Rem
  219. Method GetViewport(x:Int Var, y:Int Var, w:Int Var, h:Int Var)
  220. bmx_SDL_RenderGetViewport(rendererPtr, Varptr x, Varptr y, Varptr w, Varptr h)
  221. End Method
  222. Rem
  223. bbdoc: Gets whether clipping is enabled on the given renderer.
  224. End Rem
  225. Method IsClipEnabled:Int()
  226. Return SDL_RenderIsClipEnabled(rendererPtr)
  227. End Method
  228. Rem
  229. bbdoc: Updates the screen with any rendering performed since the previous call.
  230. End Rem
  231. Method Present()
  232. SDL_RenderPresent(rendererPtr)
  233. End Method
  234. Rem
  235. bbdoc: Reads pixels from the current rendering target.
  236. End Rem
  237. Method ReadPixels:Int(format:UInt, pixels:Byte Ptr, pitch:Int, x:Int = -1, y:Int = -1, w:Int = -1, h:Int = -1)
  238. Return bmx_SDL_RenderReadPixels(rendererPtr, format, pixels, pitch, x, y, w, h)
  239. End Method
  240. Rem
  241. bbdoc: Sets the clip rectangle for rendering on the specified target.
  242. End Rem
  243. Method SetClipRect:Int(x:Int = -1, y:Int = -1, w:Int = -1, h:Int = -1)
  244. Return bmx_SDL_RenderSetClipRect(rendererPtr, x, y, w, h)
  245. End Method
  246. Rem
  247. bbdoc: Sets whether to force integer scales for resolution-independent rendering.
  248. about: Restricts the logical viewport to integer values - that is, when a resolution is between two multiples
  249. of a logical size, the viewport size is rounded down to the lower multiple.
  250. End Rem
  251. Method SetIntegerScale:Int(enable:Int)
  252. Return SDL_RenderSetIntegerScale(rendererPtr, enable)
  253. End Method
  254. Rem
  255. bbdoc: Sets a device independent resolution for rendering.
  256. about: Uses the viewport and scaling functionality to allow a fixed logical resolution for rendering,
  257. regardless of the actual output resolution. If the actual output resolution doesn't have the same aspect ratio
  258. the output rendering will be centered within the output display.
  259. If the output display is a window, mouse and touch events in the window will be filtered and scaled so they
  260. seem to arrive within the logical resolution. The SDL_HINT_MOUSE_RELATIVE_SCALING hint controls whether relative motion
  261. events are also scaled.
  262. If this method results in scaling or subpixel drawing by the rendering backend, it will be handled using the appropriate quality hints.
  263. End Rem
  264. Method SetLogicalSize:Int(w:Int, h:Int)
  265. Return SDL_RenderSetLogicalSize(rendererPtr, w, h)
  266. End Method
  267. Rem
  268. bbdoc: Sets the drawing scale for rendering on the current target.
  269. End Rem
  270. Method SetScale:Int(scaleX:Float, scaleY:Float)
  271. Return SDL_RenderSetScale(rendererPtr, scaleX, scaleY)
  272. End Method
  273. Rem
  274. bbdoc: Sets the drawing area for rendering on the current target.
  275. about: Use defaults (-1) to set viewport to entire target.
  276. End Rem
  277. Method SetViewport:Int(x:Int = -1, y:Int = -1, w:Int = -1, h:Int = -1)
  278. Return bmx_SDL_RenderSetViewport(rendererPtr, x, y, w, h)
  279. End Method
  280. Rem
  281. bbdoc: Determines whether a window supports the use of render targets.
  282. End Rem
  283. Method TargetSupported:Int()
  284. Return SDL_RenderTargetSupported(rendererPtr)
  285. End Method
  286. Rem
  287. bbdoc: Sets the blend mode used for drawing operations (Fill and Line).
  288. End Rem
  289. Method SetDrawBlendMode:Int(blendMode:Int)
  290. Return SDL_SetRenderDrawBlendMode(rendererPtr, blendMode)
  291. End Method
  292. Rem
  293. bbdoc: Sets the color used for drawing operations (Rect, Line and Clear).
  294. End Rem
  295. Method SetDrawColor:Int(r:Byte, g:Byte, b:Byte, a:Byte)
  296. Return SDL_SetRenderDrawColor(rendererPtr, r, g, b, a)
  297. End Method
  298. Rem
  299. bbdoc: Sets a texture as the current rendering target. The texture must have been created with the #SDL_TEXTUREACCESS_TARGET flag.
  300. returns: 0 on success or a negative error code on failure; call #SDLGetError() for more information.
  301. about: Before using this method, you should check the SDL_RENDERER_TARGETTEXTURE bit in the flags of SDL_RendererInfo
  302. to see if render targets are supported.
  303. The default render target is the window for which the renderer was created.
  304. To stop rendering to a texture and render to the window again, call this method with a #Null texture.
  305. End Rem
  306. Method SetTarget:Int(texture:TSDLTexture)
  307. If texture Then
  308. Return SDL_SetRenderTarget(rendererPtr, texture.texturePtr)
  309. Else
  310. Return SDL_SetRenderTarget(rendererPtr, Null)
  311. End If
  312. End Method
  313. Rem
  314. bbdoc: Gets logical coordinates of a point in the renderer when given real coordinates of point in the window.
  315. about: Logical coordinates will differ from real coordinates when render is scaled and logical renderer size set.
  316. End Rem
  317. Method WindowToLogical(windowX:Int, windowY:Int, logicalX:Float Var, logicalY:Float Var)
  318. SDL_RenderWindowToLogical(rendererPtr, windowX, windowY, logicalX, logicalY)
  319. End Method
  320. Rem
  321. bbdoc: Gets real coordinates of a point in the window when given logical coordinates of point in the renderer.
  322. about: Logical coordinates will differ from real coordinates when render is scaled and logical renderer size set.
  323. End Rem
  324. Method LogicalToWindow(logicalX:Float, logicalY:Float, windowX:Int Var, windowY:Int Var)
  325. SDL_RenderLogicalToWindow(rendererPtr, logicalX, logicalY, windowX, windowY)
  326. End Method
  327. Rem
  328. bbdoc: Toggles VSync of the renderer.
  329. returns: 0 on success, or non-zero on failure.
  330. about: @vsync is 1 for on, 0 for off. All other values are reserved.
  331. End Rem
  332. Method SetVSync:Int(vsync:Int)
  333. Return SDL_RenderSetVSync(rendererPtr, vsync)
  334. End Method
  335. Rem
  336. bbdoc: Destroys the rendering context for a window and free associated textures.
  337. End Rem
  338. Method Destroy()
  339. If rendererPtr Then
  340. SDL_DestroyRenderer(rendererPtr)
  341. rendererPtr = Null
  342. End If
  343. End Method
  344. End Type
  345. Rem
  346. bbdoc: An efficient driver-specific representation of pixel data.
  347. End Rem
  348. Type TSDLTexture
  349. Field texturePtr:Byte Ptr
  350. Function _create:TSDLTexture(texturePtr:Byte Ptr)
  351. If texturePtr Then
  352. Local this:TSDLTexture = New TSDLTexture
  353. this.texturePtr = texturePtr
  354. Return this
  355. End If
  356. End Function
  357. Rem
  358. bbdoc: Gets the additional alpha value multiplied into render copy operations.
  359. End Rem
  360. Method GetAlphaMod:Int(alpha:Byte Var)
  361. Return SDL_GetTextureAlphaMod(texturePtr, Varptr alpha)
  362. End Method
  363. Rem
  364. bbdoc: Gets the blend mode used for texture copy operations.
  365. End Rem
  366. Method GetBlendMode:Int(blendMode:Int Var)
  367. Return SDL_GetTextureBlendMode(texturePtr, Varptr blendMode)
  368. End Method
  369. Rem
  370. bbdoc: Gets the additional color value multiplied into render copy operations.
  371. End Rem
  372. Method GetColorMod:Int(r:Byte Var, g:Byte Var, b:Byte Var)
  373. Return SDL_GetTextureColorMod(texturePtr, Varptr r, Varptr g, Varptr b)
  374. End Method
  375. Rem
  376. bbdoc: Locks a portion of the texture for write-only pixel access.
  377. End Rem
  378. Method Lock:Int(pixels:Byte Ptr Ptr, pitch:Int Var, x:Int = -1, y:Int = -1, w:Int = -1, h:Int = -1)
  379. Return bmx_SDL_LockTexture(texturePtr, pixels, Varptr pitch, x, y, w, h)
  380. End Method
  381. Rem
  382. bbdoc: Queries the attributes of a texture.
  383. End Rem
  384. Method Query:Int(format:UInt Var, access:Int Var, w:Int Var, h:Int Var)
  385. Return SDL_QueryTexture(texturePtr, Varptr format, Varptr access, Varptr w, Varptr h)
  386. End Method
  387. Rem
  388. bbdoc: Sets an additional alpha value multiplied into render copy operations.
  389. End Rem
  390. Method SetAlphaMod:Int(alpha:Byte)
  391. Return SDL_SetTextureAlphaMod(texturePtr, alpha)
  392. End Method
  393. Rem
  394. bbdoc: Sets the blend mode for a texture, used by TSDLRenderer.Copy().
  395. End Rem
  396. Method SetBlendMode:Int(blendMode:Int)
  397. Return SDL_SetTextureBlendMode(texturePtr, blendMode)
  398. End Method
  399. Rem
  400. bbdoc: Sets an additional color value multiplied into render copy operations.
  401. End Rem
  402. Method SetColorMod:Int(r:Byte, g:Byte, b:Byte)
  403. Return SDL_SetTextureColorMod(texturePtr, r, g, b)
  404. End Method
  405. Rem
  406. bbdoc: Unlocks a texture, uploading the changes to video memory, if needed.
  407. End Rem
  408. Method Unlock()
  409. SDL_UnlockTexture(texturePtr)
  410. End Method
  411. Rem
  412. bbdoc: Updates the given texture rectangle with new pixel data.
  413. End Rem
  414. Method Update:Int(pixels:Byte Ptr, pitch:Int, x:Int = -1, y:Int = -1, w:Int = -1, h:Int = -1)
  415. Return bmx_SDL_UpdateTexture(texturePtr, pixels, pitch, x, y, w, h)
  416. End Method
  417. Rem
  418. bbdoc: Updates a rectangle within a planar YV12 or IYUV texture with new pixel data.
  419. End Rem
  420. Method UpdateYUV:Int(yPlane:Byte Ptr, yPitch:Int, uPlane:Byte Ptr, uPitch:Int, vPlane:Byte Ptr, vPitch:Int, x:Int = -1, y:Int = -1, w:Int = -1, h:Int = -1)
  421. Return bmx_SDL_UpdateYUVTexture(texturePtr, yPlane, yPitch, uPlane, uPitch, vPlane, vPitch, x, y, w, h)
  422. End Method
  423. Rem
  424. bbdoc: Destroys the texture.
  425. End Rem
  426. Method Destroy()
  427. If texturePtr Then
  428. SDL_DestroyTexture(texturePtr)
  429. texturePtr = Null
  430. End If
  431. End Method
  432. End Type
  433. Rem
  434. bbdoc: Gets the number of 2D rendering drivers available for the current display.
  435. End Rem
  436. Function SDLGetNumRenderDrivers:Int()
  437. Return SDL_GetNumRenderDrivers()
  438. End Function
  439. Rem
  440. bbdoc: Gets info about a specific 2D rendering driver for the current display.
  441. returns: 0 on success or a negative error code on failure; call #SDLGetError for more information.
  442. End Rem
  443. Function SDLGetRenderDriverInfo:Int(index:Int, info:SDLRendererInfo Var)
  444. Return SDL_GetRenderDriverInfo(index, info)
  445. End Function