d3d9max2d.bmx 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. Strict
  2. Rem
  3. bbdoc: Graphics/Direct3D9 Max2D
  4. about:
  5. The Direct3D9 Max2D module provides a Direct3D9 driver for #Max2D.
  6. End Rem
  7. Module BRL.D3D9Max2D
  8. ModuleInfo "License: zlib/libpng"
  9. ModuleInfo "Copyright: Blitz Research Ltd"
  10. ?Win32
  11. Import BRL.Max2D
  12. Import BRL.DXGraphics
  13. Import BRL.D3D7Max2D
  14. Const LOG_ERRS=True'False
  15. Private
  16. Global _gw,_gh,_gd,_gr,_gf
  17. Global _color
  18. Global _clscolor
  19. Global _ix#,_iy#,_jx#,_jy#
  20. Global _fverts#[24]
  21. Global _iverts:Int Ptr=Int Ptr( Varptr _fverts[0] )
  22. Global _lineWidth#
  23. Global _bound_texture:IDirect3DTexture9
  24. Global _texture_enabled
  25. Global _active_blend
  26. Global _driver:TD3D9Max2DDriver
  27. Global _d3dDev:IDirect3DDevice9
  28. Global _d3d9Graphics:TD3D9Graphics
  29. Global _max2dGraphics:TMax2dGraphics
  30. Function Pow2Size( n )
  31. Local t=1
  32. While t<n
  33. t:*2
  34. Wend
  35. Return t
  36. End Function
  37. Function DisableTex()
  38. If Not _texture_enabled Return
  39. _d3dDev.SetTextureStageState 0,D3DTSS_COLOROP,D3DTOP_SELECTARG2
  40. _d3dDev.SetTextureStageState 0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG2
  41. _texture_enabled=False
  42. End Function
  43. Function d3derr( str$ )
  44. If LOG_ERRS WriteStdout "D3DERR: "+str+"~n"
  45. End Function
  46. Public
  47. Type TD3D9ImageFrame Extends TImageFrame
  48. Method Delete()
  49. If _texture
  50. If _seq=GraphicsSeq
  51. If _texture=_bound_texture
  52. _d3dDev.SetTexture 0,Null
  53. _bound_texture=Null
  54. EndIf
  55. _d3d9Graphics.ReleaseNow _texture
  56. EndIf
  57. _texture=Null
  58. EndIf
  59. End Method
  60. Method Create:TD3D9ImageFrame( pixmap:TPixmap,flags )
  61. Local width=pixmap.width,pow2width=Pow2Size( width )
  62. Local height=pixmap.height,pow2height=Pow2Size( height )
  63. If width<pow2width Or height<pow2height
  64. Local src:TPixmap=pixmap
  65. pixmap=TPixmap.Create( pow2width,pow2height,PF_BGRA8888 )
  66. pixmap.Paste src,0,0
  67. If width<pow2width
  68. pixmap.Paste pixmap.Window( width-1,0,1,height ),width,0
  69. EndIf
  70. If height<pow2height
  71. pixmap.Paste pixmap.Window( 0,height-1,width,1 ),0,height
  72. If width<pow2width
  73. pixmap.Paste pixmap.Window( width-1,height-1,1,1 ),width,height
  74. EndIf
  75. EndIf
  76. Else
  77. If pixmap.Format<>PF_BGRA8888 pixmap=pixmap.Convert( PF_BGRA8888 )
  78. EndIf
  79. Local levels=(flags & MIPMAPPEDIMAGE)=0
  80. Local format=D3DFMT_A8R8G8B8
  81. Local usage=0
  82. Local pool=D3DPOOL_MANAGED
  83. If _d3dDev.CreateTexture( pow2width,pow2height,levels,usage,format,pool,_texture,Null )<0
  84. d3derr "Unable to create texture~n"
  85. Return
  86. EndIf
  87. _d3d9Graphics.AutoRelease _texture
  88. Local level
  89. Repeat
  90. Local dstsurf:IDirect3DSurface9
  91. If _texture.GetSurfaceLevel( level,dstsurf )<0
  92. If level=0
  93. d3derr "_texture.GetSurfaceLevel failed~n"
  94. EndIf
  95. Exit
  96. EndIf
  97. Local lockedrect:D3DLOCKED_RECT=New D3DLOCKED_RECT
  98. If dstsurf.LockRect( lockedrect,Null,0 )<0
  99. d3derr "dstsurf.LockRect failed~n"
  100. EndIf
  101. For Local y=0 Until pixmap.height
  102. Local src:Byte Ptr=pixmap.pixels+y*pixmap.pitch
  103. Local dst:Byte Ptr=lockedrect.pBits+y*lockedrect.Pitch
  104. MemCopy dst,src,pixmap.width*4
  105. Next
  106. dstsurf.UnlockRect
  107. dstsurf.Release_
  108. If (flags & MIPMAPPEDIMAGE)=0 Exit
  109. level:+1
  110. If pixmap.width>1 And pixmap.height>1
  111. pixmap=ResizePixmap( pixmap,pixmap.width/2,pixmap.height/2 )
  112. Else If pixmap.width>1
  113. pixmap=ResizePixmap( pixmap,pixmap.width/2,pixmap.height )
  114. Else If pixmap.height>1
  115. pixmap=ResizePixmap( pixmap,pixmap.width,pixmap.height/2 )
  116. EndIf
  117. Forever
  118. _uscale=1.0/pow2width
  119. _vscale=1.0/pow2height
  120. Local u0#,u1#=width * _uscale
  121. Local v0#,v1#=height * _vscale
  122. _fverts[4]=u0
  123. _fverts[5]=v0
  124. _fverts[10]=u1
  125. _fverts[11]=v0
  126. _fverts[16]=u1
  127. _fverts[17]=v1
  128. _fverts[22]=u0
  129. _fverts[23]=v1
  130. If flags & FILTEREDIMAGE
  131. _magfilter=D3DTFG_LINEAR
  132. _minfilter=D3DTFG_LINEAR
  133. _mipfilter=D3DTFG_LINEAR
  134. Else
  135. _magfilter=D3DTFG_POINT
  136. _minfilter=D3DTFG_POINT
  137. _mipfilter=D3DTFG_POINT
  138. EndIf
  139. _seq=GraphicsSeq
  140. Return Self
  141. End Method
  142. Method Draw( x0#,y0#,x1#,y1#,tx#,ty#,sx#,sy#,sw#,sh# )
  143. Local u0#=sx * _uscale
  144. Local v0#=sy * _vscale
  145. Local u1#=(sx+sw) * _uscale
  146. Local v1#=(sy+sh) * _vscale
  147. _fverts[0]=x0*_ix+y0*_iy+tx
  148. _fverts[1]=x0*_jx+y0*_jy+ty
  149. _iverts[3]=_color
  150. _fverts[4]=u0
  151. _fverts[5]=v0
  152. _fverts[6]=x1*_ix+y0*_iy+tx
  153. _fverts[7]=x1*_jx+y0*_jy+ty
  154. _iverts[9]=_color
  155. _fverts[10]=u1
  156. _fverts[11]=v0
  157. _fverts[12]=x1*_ix+y1*_iy+tx
  158. _fverts[13]=x1*_jx+y1*_jy+ty
  159. _iverts[15]=_color
  160. _fverts[16]=u1
  161. _fverts[17]=v1
  162. _fverts[18]=x0*_ix+y1*_iy+tx
  163. _fverts[19]=x0*_jx+y1*_jy+ty
  164. _iverts[21]=_color
  165. _fverts[22]=u0
  166. _fverts[23]=v1
  167. If _texture<>_bound_texture
  168. _d3dDev.SetTexture 0,_texture
  169. _d3dDev.SetTextureStageState 0,D3DTSS_MAGFILTER,_magfilter
  170. _d3dDev.SetTextureStageState 0,D3DTSS_MINFILTER,_minfilter
  171. _d3dDev.SetTextureStageState 0,D3DTSS_MIPFILTER,_mipfilter
  172. _bound_texture=_texture
  173. EndIf
  174. If Not _texture_enabled
  175. _d3dDev.SetTextureStageState 0,D3DTSS_COLOROP,D3DTOP_MODULATE
  176. _d3dDev.SetTextureStageState 0,D3DTSS_ALPHAOP,D3DTOP_MODULATE
  177. _texture_enabled=True
  178. EndIf
  179. _d3dDev.DrawPrimitiveUP D3DPT_TRIANGLEFAN,2,_fverts,24
  180. End Method
  181. Field _texture:IDirect3DTexture9,_seq
  182. Field _magfilter,_minfilter,_mipfilter,_uscale#,_vscale#
  183. Field _fverts#[24],_iverts:Int Ptr=Int Ptr( Varptr _fverts[0] )
  184. End Type
  185. Type TD3D9Max2DDriver Extends TMax2dDriver
  186. Method ToString$()
  187. Return "DirectX9"
  188. End Method
  189. Method Create:TD3D9Max2DDriver()
  190. If Not D3D9GraphicsDriver() Return Null
  191. Local d3d:IDirect3D9=D3D9GraphicsDriver().GetDirect3D()
  192. If d3d.CheckDeviceFormat( D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,D3DFMT_X8R8G8B8,0,D3DRTYPE_TEXTURE,D3DFMT_A8R8G8B8 )<0
  193. Return Null
  194. EndIf
  195. Return Self
  196. End Method
  197. '***** TGraphicsDriver *****
  198. Method GraphicsModes:TGraphicsMode[]()
  199. Return D3D9GraphicsDriver().GraphicsModes()
  200. End Method
  201. Method AttachGraphics:TGraphics( widget,flags )
  202. Local g:TD3D9Graphics=D3D9GraphicsDriver().AttachGraphics( widget,flags )
  203. If g Return TMax2DGraphics.Create( g,Self )
  204. End Method
  205. Method CreateGraphics:TGraphics( width,height,depth,hertz,flags )
  206. Local g:TD3D9Graphics=D3D9GraphicsDriver().CreateGraphics( width,height,depth,hertz,flags )
  207. If Not g Return Null
  208. Return TMax2DGraphics.Create( g,Self )
  209. End Method
  210. Method SetGraphics( g:TGraphics )
  211. If Not g
  212. If _d3dDev
  213. _d3dDev.EndScene
  214. _d3dDev=Null
  215. EndIf
  216. _d3d9graphics=Null
  217. _max2dGraphics=Null
  218. TMax2DGraphics.ClearCurrent
  219. D3D9GraphicsDriver().SetGraphics Null
  220. Return
  221. EndIf
  222. _max2dGraphics=TMax2dGraphics( g )
  223. _d3d9graphics=TD3D9Graphics( _max2dGraphics._graphics )
  224. Assert _max2dGraphics And _d3d9graphics
  225. _d3dDev=_d3d9Graphics.GetDirect3DDevice()
  226. D3D9GraphicsDriver().SetGraphics _d3d9Graphics
  227. If _d3dDev.TestCooperativeLevel()<>D3D_OK Return
  228. ResetDevice
  229. _max2dGraphics.MakeCurrent
  230. End Method
  231. Method Flip( sync )
  232. _d3dDev.EndScene
  233. If D3D9GraphicsDriver().Flip( sync )
  234. _d3dDev.BeginScene
  235. Else If _d3dDev.TestCooperativeLevel()=D3D_OK
  236. ResetDevice
  237. _max2dGraphics.MakeCurrent
  238. EndIf
  239. End Method
  240. Method ResetDevice()
  241. _d3d9graphics.ValidateSize
  242. _d3d9graphics.GetSettings _gw,_gh,_gd,_gr,_gf
  243. Local viewport:D3DVIEWPORT9=New D3DVIEWPORT9
  244. viewport.X=0
  245. viewport.Y=0
  246. viewport.Width=_gw
  247. viewport.Height=_gh
  248. viewport.MinZ=0.0
  249. viewport.MaxZ=1.0
  250. _d3dDev.SetViewport viewport
  251. _d3dDev.SetRenderState D3DRS_ALPHAREF,$80
  252. _d3dDev.SetRenderState D3DRS_ALPHAFUNC,D3DCMP_GREATEREQUAL
  253. _d3dDev.SetRenderState D3DRS_ALPHATESTENABLE,False
  254. _d3dDev.SetRenderState D3DRS_ALPHABLENDENABLE,False
  255. _active_blend=SOLIDBLEND
  256. _d3dDev.SetRenderState D3DRS_LIGHTING,False
  257. _d3dDev.SetRenderState D3DRS_CULLMODE,D3DCULL_NONE
  258. _d3dDev.SetTexture 0,Null
  259. _bound_texture=Null
  260. _d3dDev.SetFVF D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1
  261. _d3dDev.SetTextureStageState 0,D3DTSS_COLORARG1,D3DTA_TEXTURE
  262. _d3dDev.SetTextureStageState 0,D3DTSS_COLORARG2,D3DTA_DIFFUSE
  263. _d3dDev.SetTextureStageState 0,D3DTSS_COLOROP,D3DTOP_SELECTARG2
  264. _d3dDev.SetTextureStageState 0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE
  265. _d3dDev.SetTextureStageState 0,D3DTSS_ALPHAARG2,D3DTA_DIFFUSE
  266. _d3dDev.SetTextureStageState 0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG2
  267. _texture_enabled=False
  268. _d3dDev.SetTextureStageState 0,D3DTSS_ADDRESS,D3DTADDRESS_CLAMP
  269. _d3dDev.SetTextureStageState 0,D3DTSS_MAGFILTER,D3DTFG_POINT
  270. _d3dDev.SetTextureStageState 0,D3DTSS_MINFILTER,D3DTFN_POINT
  271. _d3dDev.SetTextureStageState 0,D3DTSS_MIPFILTER,D3DTFP_POINT
  272. _d3dDev.BeginScene
  273. End Method
  274. '***** TMax2DDriver *****
  275. Method CreateFrameFromPixmap:TImageFrame( pixmap:TPixmap,flags )
  276. Return New TD3D9ImageFrame.Create( pixmap,flags )
  277. End Method
  278. Method SetBlend( blend )
  279. If blend=_active_blend Return
  280. Select blend
  281. Case SOLIDBLEND
  282. _d3dDev.SetRenderState D3DRS_ALPHATESTENABLE,False
  283. _d3dDev.SetRenderState D3DRS_ALPHABLENDENABLE,False
  284. Case MASKBLEND
  285. _d3dDev.SetRenderState D3DRS_ALPHATESTENABLE,True
  286. _d3dDev.SetRenderState D3DRS_ALPHABLENDENABLE,False
  287. Case ALPHABLEND
  288. _d3dDev.SetRenderState D3DRS_ALPHATESTENABLE,False
  289. _d3dDev.SetRenderState D3DRS_ALPHABLENDENABLE,True
  290. _d3dDev.SetRenderState D3DRS_SRCBLEND,D3DBLEND_SRCALPHA
  291. _d3dDev.SetRenderState D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA
  292. Case LIGHTBLEND
  293. _d3dDev.SetRenderState D3DRS_ALPHATESTENABLE,False
  294. _d3dDev.SetRenderState D3DRS_ALPHABLENDENABLE,True
  295. _d3dDev.SetRenderState D3DRS_SRCBLEND,D3DBLEND_SRCALPHA
  296. _d3dDev.SetRenderState D3DRS_DESTBLEND,D3DBLEND_ONE
  297. Case SHADEBLEND
  298. _d3dDev.SetRenderState D3DRS_ALPHATESTENABLE,False
  299. _d3dDev.SetRenderState D3DRS_ALPHABLENDENABLE,True
  300. _d3dDev.SetRenderState D3DRS_SRCBLEND,D3DBLEND_ZERO
  301. _d3dDev.SetRenderState D3DRS_DESTBLEND,D3DBLEND_SRCCOLOR
  302. End Select
  303. _active_blend=blend
  304. End Method
  305. Method SetAlpha( alpha# )
  306. alpha=Max(Min(alpha,1),0)
  307. _color=(Int(255*alpha) Shl 24)|(_color&$ffffff)
  308. _iverts[3]=_color
  309. _iverts[9]=_color
  310. _iverts[15]=_color
  311. _iverts[21]=_color
  312. End Method
  313. Method SetColor( red,green,blue )
  314. red=Max(Min(red,255),0)
  315. green=Max(Min(green,255),0)
  316. blue=Max(Min(blue,255),0)
  317. _color=(_color&$ff000000)|(red Shl 16)|(green Shl 8)|blue
  318. _iverts[3]=_color
  319. _iverts[9]=_color
  320. _iverts[15]=_color
  321. _iverts[21]=_color
  322. End Method
  323. Method SetClsColor( red,green,blue )
  324. red=Max(Min(red,255),0)
  325. green=Max(Min(green,255),0)
  326. blue=Max(Min(blue,255),0)
  327. _clscolor=$ff000000|(red Shl 16)|(green Shl 8)|blue
  328. End Method
  329. Method SetViewport( x,y,width,height )
  330. If x=0 And y=0 And width=_gw And height=_gh 'GraphicsWidth() And height=GraphicsHeight()
  331. _d3dDev.SetRenderState D3DRS_SCISSORTESTENABLE,False
  332. Else
  333. _d3dDev.SetRenderState D3DRS_SCISSORTESTENABLE,True
  334. Local rect[]=[x,y,x+width,y+height]
  335. _d3dDev.SetScissorRect rect
  336. EndIf
  337. End Method
  338. Method SetTransform( xx#,xy#,yx#,yy# )
  339. _ix=xx
  340. _iy=xy
  341. _jx=yx
  342. _jy=yy
  343. End Method
  344. Method SetLineWidth( width# )
  345. _lineWidth=width
  346. End Method
  347. Method Cls()
  348. _d3dDev.Clear 0,Null,D3DCLEAR_TARGET,_clscolor,0,0
  349. End Method
  350. Method Plot( x#,y# )
  351. _fverts[0]=x+.5
  352. _fverts[1]=y+.5
  353. DisableTex
  354. _d3dDev.DrawPrimitiveUP D3DPT_POINTLIST,1,_fverts,24
  355. End Method
  356. Method DrawLine( x0#,y0#,x1#,y1#,tx#,ty# )
  357. Local lx0# = x0*_ix + y0*_iy + tx
  358. Local ly0# = x0*_jx + y0*_jy + ty
  359. Local lx1# = x1*_ix + y1*_iy + tx
  360. Local ly1# = x1*_jx + y1*_jy + ty
  361. If _lineWidth<=1
  362. _fverts[0]=lx0+.5
  363. _fverts[1]=ly0+.5
  364. _fverts[6]=lx1+.5
  365. _fverts[7]=ly1+.5
  366. DisableTex
  367. _d3dDev.DrawPrimitiveUP D3DPT_LINELIST,1,_fverts,24
  368. Return
  369. EndIf
  370. Local lw#=_lineWidth*.5
  371. If Abs(ly1-ly0)>Abs(lx1-lx0)
  372. _fverts[0]=lx0-lw
  373. _fverts[1]=ly0
  374. _fverts[6]=lx0+lw
  375. _fverts[7]=ly0
  376. _fverts[12]=lx1-lw
  377. _fverts[13]=ly1
  378. _fverts[18]=lx1+lw
  379. _fverts[19]=ly1
  380. Else
  381. _fverts[0]=lx0
  382. _fverts[1]=ly0-lw
  383. _fverts[6]=lx0
  384. _fverts[7]=ly0+lw
  385. _fverts[12]=lx1
  386. _fverts[13]=ly1-lw
  387. _fverts[18]=lx1
  388. _fverts[19]=ly1+lw
  389. EndIf
  390. DisableTex
  391. _d3dDev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP,2,_fverts,24
  392. End Method
  393. Method DrawRect( x0#,y0#,x1#,y1#,tx#,ty# )
  394. _fverts[0] = x0*_ix + y0*_iy + tx
  395. _fverts[1] = x0*_jx + y0*_jy + ty
  396. _fverts[6] = x1*_ix + y0*_iy + tx
  397. _fverts[7] = x1*_jx + y0*_jy + ty
  398. _fverts[12] = x0*_ix + y1*_iy + tx
  399. _fverts[13] = x0*_jx + y1*_jy + ty
  400. _fverts[18] = x1*_ix + y1*_iy + tx
  401. _fverts[19] = x1*_jx + y1*_jy + ty
  402. DisableTex
  403. _d3dDev.DrawPrimitiveUP D3DPT_TRIANGLESTRIP,2,_fverts,24
  404. End Method
  405. Method DrawOval( x0#,y0#,x1#,y1#,tx#,ty# )
  406. Local xr#=(x1-x0)*.5
  407. Local yr#=(y1-y0)*.5
  408. Local segs=Abs(xr)+Abs(yr)
  409. segs=Max(segs,12)&~3
  410. x0:+xr
  411. y0:+yr
  412. Local fverts#[segs*6]
  413. Local iverts:Int Ptr=Int Ptr( Varptr fverts[0] )
  414. For Local i=0 Until segs
  415. Local th#=-i*360#/segs
  416. Local x#=x0+Cos(th)*xr
  417. Local y#=y0-Sin(th)*yr
  418. fverts[i*6+0]=x*_ix+y*_iy+tx
  419. fverts[i*6+1]=x*_jx+y*_jy+ty
  420. iverts[i*6+3]=_color
  421. Next
  422. DisableTex
  423. _d3dDev.DrawPrimitiveUP D3DPT_TRIANGLEFAN,segs-2,fverts,24
  424. End Method
  425. Method DrawPoly( verts#[],handlex#,handley#,tx#,ty# )
  426. If verts.length<6 Or (verts.length&1) Return
  427. Local segs=verts.length/2
  428. Local fverts#[segs*6]
  429. Local iverts:Int Ptr=Int Ptr( Varptr fverts[0] )
  430. For Local i=0 Until segs
  431. Local x#=verts[i*2+0]+handlex
  432. Local y#=verts[i*2+1]+handley
  433. fverts[i*6+0]= x*_ix + y*_iy + tx
  434. fverts[i*6+1]= x*_jx + y*_jy + ty
  435. iverts[i*6+3]=_color
  436. Next
  437. DisableTex
  438. _d3dDev.DrawPrimitiveUP D3DPT_TRIANGLEFAN,segs-2,fverts,24
  439. End Method
  440. 'GetDC/BitBlt MUCH faster than locking backbuffer!
  441. Method DrawPixmap( pixmap:TPixmap,x,y )
  442. Local width=pixmap.width,height=pixmap.height
  443. Local dstsurf:IDirect3DSurface9
  444. If _d3dDev.GetRenderTarget( 0,dstsurf )<0
  445. d3derr "GetRenderTarget failed~n"
  446. Return
  447. EndIf
  448. Local desc:D3DSURFACE_DESC=New D3DSURFACE_DESC
  449. If dstsurf.GetDesc( desc )<0
  450. d3derr "GetDesc failed~n"
  451. EndIf
  452. Local rect[]=[x,y,x+width,y+height]
  453. Local lockedrect:D3DLOCKED_RECT=New D3DLOCKED_RECT
  454. If dstsurf.LockRect( lockedrect,rect,0 )<0
  455. d3derr "Unable to lock render target surface~n"
  456. dstsurf.Release_
  457. Return
  458. EndIf
  459. Local dstpixmap:TPixmap=CreateStaticPixmap( lockedrect.pBits,width,height,lockedrect.Pitch,PF_BGRA8888 );
  460. dstpixmap.Paste pixmap,0,0
  461. dstsurf.UnlockRect
  462. dstsurf.Release_
  463. End Method
  464. 'GetDC/BitBlt MUCH faster than locking backbuffer!
  465. Method GrabPixmap:TPixmap( x,y,width,height )
  466. Local srcsurf:IDirect3DSurface9
  467. If _d3dDev.GetRenderTarget( 0,srcsurf )<0
  468. d3derr "GetRenderTarget failed~n"
  469. EndIf
  470. Local dstsurf:IDirect3DSurface9
  471. If _d3dDev.CreateOffscreenPlainSurface( width,height,D3DFMT_X8R8G8B8,D3DPOOL_SYSTEMMEM,dstsurf,Null )<0
  472. d3derr "CreateOffscreenPlainSurface failed~n"
  473. EndIf
  474. Local srcdc:Byte Ptr
  475. If srcsurf.GetDC( srcdc )<0
  476. d3derr "srcsurf.GetDC failed~n"
  477. EndIf
  478. Local dstdc:Byte Ptr
  479. If dstsurf.GetDC( dstdc )<0
  480. d3derr "dstsurf.GetDC failed~n"
  481. EndIf
  482. BitBlt Int(dstdc),0,0,width,height,Int(srcdc),x,y,ROP_SRCCOPY
  483. srcsurf.ReleaseDC srcdc
  484. dstsurf.ReleaseDC dstdc
  485. Local lockedrect:D3DLOCKED_RECT=New D3DLOCKED_RECT
  486. If dstsurf.LockRect( lockedrect,Null,D3DLOCK_READONLY )<0
  487. d3derr "dstsurf.LockRect failed~n"
  488. EndIf
  489. Local pixmap:TPixmap=CreatePixmap( width,height,PF_BGRA8888 )
  490. 'Copy and set alpha in the process...
  491. For Local y=0 Until height
  492. Local src:Int Ptr=Int Ptr( lockedrect.pBits+y*lockedrect.Pitch )
  493. Local dst:Int Ptr=Int Ptr( pixmap.PixelPtr( 0,y ) )
  494. For Local x=0 Until width
  495. dst[x]=src[x] | $ff000000
  496. Next
  497. Next
  498. srcsurf.Release_
  499. dstsurf.Release_
  500. Return pixmap
  501. End Method
  502. Method SetResolution( width#,height# )
  503. Local matrix#[]=[..
  504. 2.0/width,0.0,0.0,0.0,..
  505. 0.0,-2.0/height,0.0,0.0,..
  506. 0.0,0.0,1.0,0.0,..
  507. -1-(1.0/width),1+(1.0/height),1.0,1.0]
  508. _d3dDev.SetTransform D3DTS_PROJECTION,matrix
  509. End Method
  510. End Type
  511. Rem
  512. bbdoc: Get Direct3D9 Max2D Driver
  513. about:
  514. The returned driver can be used with #SetGraphicsDriver to enable Direct3D9 Max2D rendering.
  515. End Rem
  516. Function D3D9Max2DDriver:TD3D9Max2DDriver()
  517. Global _done
  518. If Not _done
  519. _driver=New TD3D9Max2DDriver.Create()
  520. _done=True
  521. EndIf
  522. Return _driver
  523. End Function
  524. Local driver:TD3D9Max2DDriver=D3D9Max2DDriver()
  525. If driver SetGraphicsDriver driver
  526. ?