d3d7max2d.bmx 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810
  1. Strict
  2. Rem
  3. bbdoc: Graphics/Direct3D7 Max2D
  4. about:
  5. The Direct3D7 Max2D module provides a Direct3D7 driver for #Max2D.
  6. End Rem
  7. Module BRL.D3D7Max2D
  8. ModuleInfo "Version: 1.20"
  9. ModuleInfo "Author: Simon Armstrong"
  10. ModuleInfo "License: zlib/libpng"
  11. ModuleInfo "Copyright: Blitz Research Ltd"
  12. ModuleInfo "Modserver: BRL"
  13. ModuleInfo "History: 1.20"
  14. ModuleInfo "History: Changed Assert to Throw. One can at least catch a Throw."
  15. ModuleInfo "History: 1.19 Release"
  16. ModuleInfo "History: Fixed/cleaned up a few things"
  17. ModuleInfo "History: 1.18 Release"
  18. ModuleInfo "History: Updated to work with d3d7graphics rewrite"
  19. ModuleInfo "History: Removed buffered driver"
  20. ModuleInfo "History: 1.17 Release"
  21. ModuleInfo "History: Modified TD3D7Max2DDriver.SetGraphics for new dxgraphics commands"
  22. ModuleInfo "History: 1.16 Release"
  23. ModuleInfo "History: Fixed BufferedD3D7 vertex color errors"
  24. ModuleInfo "History: 1.15 Release"
  25. ModuleInfo "History: Changed DrawImage tristrip to trifan to fix subpixel cracking"
  26. ModuleInfo "History: 1.14 Release"
  27. ModuleInfo "History: Fixed MIPMAPPEDIMAGE flag effect on FILTEREDIMAGE setting"
  28. ModuleInfo "History: 1.14 Release"
  29. ModuleInfo "History: Fixed lost device from fullscreen tabbing"
  30. ModuleInfo "History: 1.13 Release"
  31. ModuleInfo "History: Fixed memory leak in TD3d7ImageFrame"
  32. ModuleInfo "History: 1.12 Release"
  33. ModuleInfo "History: Fixed default mipmap filtering for imageframes"
  34. ModuleInfo "History: 1.11 Release"
  35. ModuleInfo "History: Added flush to BufferedD3D7Max2DDriver for SetBlend and SetViewPort and Draw/GrabPixmap"
  36. ModuleInfo "History: 1.10 Release"
  37. ModuleInfo "History: Added new BufferedD3D7Max2DDriver for optimization testing"
  38. ModuleInfo "History: 1.09 Release"
  39. ModuleInfo "History: Replaced texture factor with vertex colors to improve compatability"
  40. ModuleInfo "History: 1.08 Release"
  41. ModuleInfo "History: Added GL fallback for DX device failure"
  42. ModuleInfo "History: 1.07 Release"
  43. ModuleInfo "History: Added line width support and tweaked line positioning"
  44. ModuleInfo "History: 1.06 Release"
  45. ModuleInfo "History: Added MIPMAPPEDIMAGE support"
  46. ModuleInfo "History: 1.05 Release"
  47. ModuleInfo "History: Added a bunch of redundant state change checks"
  48. ModuleInfo "History: 1.04 Release"
  49. ModuleInfo "History: Now default driver for Win32"
  50. ModuleInfo "History: 1.03 Release"
  51. ModuleInfo "History: Fixed negative scales"
  52. ModuleInfo "History: 1.02 Release"
  53. ModuleInfo "History: Fixed LIGHTBLEND"
  54. ModuleInfo "History: Clamped alpha/color/clsColor"
  55. ModuleInfo "History: Fixed viewport (now uses clip planes)"
  56. ModuleInfo "History: Fixed Mag filter"
  57. ?disabled
  58. Import BRL.DXGraphics
  59. Import BRL.GLMax2D
  60. Type TD3D7Max2DDriver Extends TMax2DDriver
  61. Field device:IDirect3DDevice7
  62. Field d3d7graphics:TD3D7Graphics
  63. Field drawalpha '0..255
  64. Field drawcolor
  65. Field clscolor
  66. Field ix:Float,iy:Float,jx:Float,jy:Float
  67. Field linewidth:Float
  68. Field cverts:Float[16]
  69. Field vrts:Int Ptr'=Int Ptr(Varptr cverts[0])
  70. Field vp_rect[]
  71. Field activeBlend
  72. Field activeFrame:TD3D7ImageFrame
  73. Field activeFrameFlags
  74. Method New()
  75. vrts=Int Ptr(Varptr cverts[0])
  76. End Method
  77. Method ToString:String()
  78. Return "DirectX7"
  79. End Method
  80. Method IsValid()
  81. Return TD3D7graphicsDriver.IsValid
  82. End Method
  83. Method GraphicsModes:TGraphicsMode[]()
  84. Return D3D7GraphicsDriver().GraphicsModes()
  85. End Method
  86. Method AttachGraphics:TMax2DGraphics( widget,flags )
  87. Local g:TD3D7Graphics=D3D7GraphicsDriver().AttachGraphics( widget,flags )
  88. If g Return TMax2DGraphics.Create( g,Self )
  89. End Method
  90. Method CreateGraphics:TMax2DGraphics( width,height,depth,hertz,flags )
  91. Local g:TD3D7Graphics=D3D7GraphicsDriver().CreateGraphics( width,height,depth,hertz,flags )
  92. If g Return TMax2DGraphics.Create( g,Self )
  93. End Method
  94. Method SetGraphics( g:TGraphics )
  95. If Not g
  96. TMax2DGraphics.ClearCurrent
  97. D3D7GraphicsDriver().EndScene
  98. D3D7GraphicsDriver().SetGraphics Null
  99. Return
  100. EndIf
  101. Local t:TMax2DGraphics=TMax2DGraphics( g )
  102. If Not t Or Not TD3D7Graphics( t._graphics ) Then
  103. Throw "SetGraphics failed for D3D7"
  104. End If
  105. D3D7GraphicsDriver().SetGraphics t._graphics
  106. ResetD3DDevice t
  107. t.MakeCurrent
  108. D3D7GraphicsDriver().BeginScene
  109. End Method
  110. Method ResetD3DDevice( g:TGraphics )
  111. If Not IsValid() Return
  112. Local gw,gh,gd,gr,gf
  113. g.GetSettings gw,gh,gd,gr,gf
  114. device=D3D7GraphicsDriver().Direct3DDevice7()
  115. Local viewport:D3DVIEWPORT7=New D3DVIEWPORT7
  116. viewport.dwX=0
  117. viewport.dwY=0
  118. viewport.dwWidth=gw
  119. viewport.dwHeight=gh
  120. viewport.dvMinZ=0.0
  121. viewport.dvMaxZ=1.0
  122. device.SetViewport(viewport)
  123. device.SetTexture 0,Null
  124. device.SetRenderState D3DRS_ALPHAREF,$80
  125. device.SetRenderState D3DRS_ALPHAFUNC,D3DCMP_GREATEREQUAL
  126. device.SetRenderState D3DRS_ALPHATESTENABLE,False
  127. device.SetRenderState D3DRS_ALPHABLENDENABLE,False
  128. device.SetRenderState D3DRS_LIGHTING,False
  129. device.SetRenderState D3DRS_CULLMODE,D3DCULL_NONE
  130. device.SetTextureStageState 0,D3DTSS_COLOROP,D3DTOP_SELECTARG2
  131. device.SetTextureStageState 0,D3DTSS_COLORARG1,D3DTA_TEXTURE
  132. device.SetTextureStageState 0,D3DTSS_COLORARG2,D3DTA_DIFFUSE
  133. device.SetTextureStageState 0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG2
  134. device.SetTextureStageState 0,D3DTSS_ALPHAARG1,D3DTA_TEXTURE
  135. device.SetTextureStageState 0,D3DTSS_ALPHAARG2,D3DTA_DIFFUSE
  136. device.SetTextureStageState 0,D3DTSS_ADDRESS,D3DTADDRESS_CLAMP
  137. device.SetTextureStageState 0,D3DTSS_MAGFILTER,D3DTFG_POINT
  138. device.SetTextureStageState 0,D3DTSS_MINFILTER,D3DTFN_POINT
  139. device.SetTextureStageState 0,D3DTSS_MIPFILTER,D3DTFP_POINT
  140. activeFrame=Null
  141. activeFrameFlags=0
  142. activeBlend=SOLIDBLEND
  143. End Method
  144. Method Flip( sync )
  145. Local seq=GraphicsSeq
  146. Local wasValid=IsValid()
  147. D3D7GraphicsDriver().Flip sync
  148. If Not IsValid() Return
  149. If wasValid And seq=GraphicsSeq Return
  150. ResetD3DDevice TMax2DGraphics.Current()
  151. TMax2DGraphics.Current().Validate
  152. End Method
  153. Method CreateFrameWithSize:TImageFrame( width,height,flags )
  154. If Not IsValid() Return
  155. Local frame:TD3D7ImageFrame=TD3D7ImageFrame.Create(Self,width,height,flags)
  156. Return frame
  157. End Method
  158. Method CreateFrameFromPixmap:TImageFrame( pixmap:TPixmap,flags )
  159. If Not IsValid() Return
  160. Local frame:TD3D7ImageFrame=TD3D7ImageFrame.Create(Self,pixmap.Width,pixmap.Height,flags)
  161. Local locked:TPixmap=frame.Lock( False,True )
  162. locked.Paste pixmap,0,0
  163. frame.Unlock()
  164. Return frame
  165. End Method
  166. Method SetBlend( blend )
  167. If blend=activeBlend Return
  168. activeBlend=blend
  169. If Not IsValid() Return
  170. Select activeBlend
  171. Case SOLIDBLEND
  172. device.SetRenderState D3DRS_ALPHATESTENABLE,False
  173. device.SetRenderState D3DRS_ALPHABLENDENABLE,False
  174. Case MASKBLEND
  175. device.SetRenderState D3DRS_ALPHATESTENABLE,True
  176. device.SetRenderState D3DRS_ALPHABLENDENABLE,False
  177. Case ALPHABLEND
  178. device.SetRenderState D3DRS_ALPHATESTENABLE,False
  179. device.SetRenderState D3DRS_ALPHABLENDENABLE,True
  180. device.SetRenderState D3DRS_SRCBLEND,D3DBLEND_SRCALPHA
  181. device.SetRenderState D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA
  182. Case LIGHTBLEND
  183. device.SetRenderState D3DRS_ALPHATESTENABLE,False
  184. device.SetRenderState D3DRS_ALPHABLENDENABLE,True
  185. device.SetRenderState D3DRS_SRCBLEND,D3DBLEND_SRCALPHA
  186. device.SetRenderState D3DRS_DESTBLEND,D3DBLEND_ONE
  187. Case SHADEBLEND
  188. device.SetRenderState D3DRS_ALPHATESTENABLE,False
  189. device.SetRenderState D3DRS_ALPHABLENDENABLE,True
  190. device.SetRenderState D3DRS_SRCBLEND,D3DBLEND_ZERO
  191. device.SetRenderState D3DRS_DESTBLEND,D3DBLEND_SRCCOLOR
  192. End Select
  193. End Method
  194. Method SetAlpha( alpha:Float )
  195. alpha=Max(Min(alpha,1),0)
  196. drawcolor=(Int(255*alpha) Shl 24)|(drawcolor&$ffffff)
  197. vrts[3]=drawcolor
  198. vrts[7]=drawcolor
  199. vrts[11]=drawcolor
  200. vrts[15]=drawcolor
  201. End Method
  202. Method SetColor( red,green,blue )
  203. red=Max(Min(red,255),0)
  204. green=Max(Min(green,255),0)
  205. blue=Max(Min(blue,255),0)
  206. drawcolor=(drawcolor&$ff000000)|(red Shl 16)|(green Shl 8)|blue
  207. vrts[3]=drawcolor
  208. vrts[7]=drawcolor
  209. vrts[11]=drawcolor
  210. vrts[15]=drawcolor
  211. End Method
  212. Method SetClsColor( red,green,blue )
  213. red=Max(Min(red,255),0)
  214. green=Max(Min(green,255),0)
  215. blue=Max(Min(blue,255),0)
  216. clscolor=$ff000000|(red Shl 16)|(green Shl 8)|blue
  217. End Method
  218. Method SetViewport( x,y,width,height )
  219. vp_rect=[x,y,x+width,y+height]
  220. If Not IsValid() Return
  221. If x=0 And y=0 And width=GraphicsWidth() And height=GraphicsHeight()
  222. device.SetRenderState D3DRS_CLIPPLANEENABLE,0
  223. Else
  224. Local err
  225. If device.SetClipPlane(0,[1.0,0.0,0.0,-Float(x)]) err=True
  226. If device.SetClipPlane(1,[-1.0,0.0,0.0,Float(x+width)]) err=True
  227. If device.SetClipPlane(2,[0.0,1.0,0.0,-Float(y)]) err=True
  228. If device.SetClipPlane(3,[0.0,-1.0,0.0,Float(y+height)]) err=True
  229. If err Throw "device does not support clipplanes"
  230. device.SetRenderState D3DRS_CLIPPLANEENABLE,15
  231. EndIf
  232. End Method
  233. Method SetTransform( xx:Float,xy:Float,yx:Float,yy:Float )
  234. ix=xx
  235. iy=xy
  236. jx=yx
  237. jy=yy
  238. End Method
  239. Method SetLineWidth( width:Float )
  240. linewidth=width
  241. End Method
  242. Method Cls()
  243. If Not IsValid() Return
  244. device.Clear 1,vp_rect,D3DCLEAR_TARGET,clscolor,0,0
  245. End Method
  246. Method Plot( x:Float,y:Float )
  247. If Not IsValid() Return
  248. cverts[0]=x+.5001
  249. cverts[1]=y+.5001
  250. SetActiveFrame Null
  251. device.DrawPrimitive(D3DPT_POINTLIST,D3DFVF_XYZ|D3DFVF_DIFFUSE,cverts,1,0)
  252. End Method
  253. Method DrawLine( x0:Float,y0:Float,x1:Float,y1:Float,tx:Float,ty:Float )
  254. If Not IsValid() Return
  255. Local lx0:Float,ly0:Float,lx1:Float,ly1:Float
  256. lx0=x0*ix+y0*iy+tx
  257. ly0=x0*jx+y0*jy+ty
  258. lx1=x1*ix+y1*iy+tx
  259. ly1=x1*jx+y1*jy+ty
  260. If linewidth<=1
  261. cverts[0]=lx0+.5001
  262. cverts[1]=ly0+.5001
  263. cverts[4]=lx1+.5001
  264. cverts[5]=ly1+.5001
  265. SetActiveFrame Null
  266. device.DrawPrimitive(D3DPT_LINELIST,D3DFVF_XYZ|D3DFVF_DIFFUSE,cverts,2,0)
  267. Else
  268. Local lw:Float=linewidth*0.5
  269. If Abs(ly1-ly0)>Abs(lx1-lx0)
  270. cverts[0]=lx0-lw
  271. cverts[1]=ly0
  272. cverts[4]=lx0+lw
  273. cverts[5]=ly0
  274. cverts[8]=lx1-lw
  275. cverts[9]=ly1
  276. cverts[12]=lx1+lw
  277. cverts[13]=ly1
  278. Else
  279. cverts[0]=lx0
  280. cverts[1]=ly0-lw
  281. cverts[4]=lx0
  282. cverts[5]=ly0+lw
  283. cverts[8]=lx1
  284. cverts[9]=ly1-lw
  285. cverts[12]=lx1
  286. cverts[13]=ly1+lw
  287. EndIf
  288. SetActiveFrame Null
  289. device.DrawPrimitive(D3DPT_TRIANGLESTRIP,D3DFVF_XYZ|D3DFVF_DIFFUSE,cverts,4,0)
  290. EndIf
  291. End Method
  292. Method DrawRect( x0:Float,y0:Float,x1:Float,y1:Float,tx:Float,ty:Float )
  293. If Not IsValid() Return
  294. cverts[0]=x0*ix+y0*iy+tx
  295. cverts[1]=x0*jx+y0*jy+ty
  296. cverts[4]=x1*ix+y0*iy+tx
  297. cverts[5]=x1*jx+y0*jy+ty
  298. cverts[8]=x0*ix+y1*iy+tx
  299. cverts[9]=x0*jx+y1*jy+ty
  300. cverts[12]=x1*ix+y1*iy+tx
  301. cverts[13]=x1*jx+y1*jy+ty
  302. SetActiveFrame Null
  303. device.DrawPrimitive(D3DPT_TRIANGLESTRIP,D3DFVF_XYZ|D3DFVF_DIFFUSE,cverts,4,0)
  304. End Method
  305. Method DrawOval( x0:Float,y0:Float,x1:Float,y1:Float,tx:Float,ty:Float )
  306. If Not IsValid() Return
  307. Local xr:Float=(x1-x0)*.5
  308. Local yr:Float=(y1-y0)*.5
  309. Local segs=Abs(xr)+Abs(yr)
  310. segs=Max(segs,12)&~3
  311. x0:+xr
  312. y0:+yr
  313. Local vrts:Float[]=New Float[segs*4]
  314. Local c:Int Ptr=Int Ptr(Float Ptr(vrts))
  315. For Local i=0 Until segs
  316. Local th:Float=-i*360:Float/segs
  317. Local x:Float=x0+Cos(th)*xr
  318. Local y:Float=y0-Sin(th)*yr
  319. vrts[i*4+0]=x*ix+y*iy+tx
  320. vrts[i*4+1]=x*jx+y*jy+ty
  321. c[i*4+3]=drawcolor
  322. Next
  323. SetActiveFrame Null
  324. device.DrawPrimitive(D3DPT_TRIANGLEFAN,D3DFVF_XYZ|D3DFVF_DIFFUSE,vrts,segs,0)
  325. End Method
  326. Method DrawPoly( xy:Float[],handlex:Float,handley:Float,tx:Float,ty:Float, indices:Int[] )
  327. If Not IsValid() Return
  328. If xy.length<6 Or (xy.length&1) Return
  329. Local segs=xy.length/2
  330. Local vrts:Float[]=New Float[segs*4]
  331. Local c:Int Ptr=Int Ptr(Float Ptr(vrts))
  332. For Local i=0 Until Len xy Step 2
  333. Local x:Float=xy[i+0]+handlex
  334. Local y:Float=xy[i+1]+handley
  335. vrts[i*2+0]=x*ix+y*iy+tx
  336. vrts[i*2+1]=x*jx+y*jy+ty
  337. c[i*2+3]=drawcolor
  338. Next
  339. SetActiveFrame Null
  340. device.DrawPrimitive(D3DPT_TRIANGLEFAN,D3DFVF_XYZ|D3DFVF_DIFFUSE,vrts,segs,0)
  341. End Method
  342. Method DrawFrame( frame:TD3D7ImageFrame,x0:Float,y0:Float,x1:Float,y1:Float,tx:Float,ty:Float,sx:Float,sy:Float,sw:Float,sh:Float )
  343. If Not IsValid() Return
  344. Local u0:Float=sx * frame.uscale
  345. Local v0:Float=sy * frame.vscale
  346. Local u1:Float=(sx+sw) * frame.uscale
  347. Local v1:Float=(sy+sh) * frame.vscale
  348. frame.SetUV u0,v0,u1,v1
  349. Local uv:Float Ptr
  350. Local c:Int Ptr
  351. uv=frame.xyzuv
  352. c=Int Ptr(uv)
  353. uv[0]=x0*ix+y0*iy+tx
  354. uv[1]=x0*jx+y0*jy+ty
  355. c[3]=drawcolor
  356. uv[6]=x1*ix+y0*iy+tx
  357. uv[7]=x1*jx+y0*jy+ty
  358. c[9]=drawcolor
  359. uv[12]=x1*ix+y1*iy+tx
  360. uv[13]=x1*jx+y1*jy+ty
  361. c[15]=drawcolor
  362. uv[18]=x0*ix+y1*iy+tx
  363. uv[19]=x0*jx+y1*jy+ty
  364. c[21]=drawcolor
  365. SetActiveFrame frame
  366. device.DrawPrimitive(D3DPT_TRIANGLEFAN,D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1,uv,4,0)
  367. End Method
  368. Method DrawPixmap( pixmap:TPixmap,x,y )
  369. If Not IsValid() Return
  370. Local srcdc,destdc
  371. Local surf:IDirectDrawSurface7
  372. Local renderSurf:IDirectDrawSurface7
  373. D3D7GraphicsDriver().EndScene
  374. device.GetRenderTarget Varptr renderSurf
  375. renderSurf.GetDC Varptr destdc
  376. surf=surffrompixmap( pixmap )
  377. surf.GetDC Varptr srcdc
  378. BitBlt destdc,x,y,pixmap.width,pixmap.height,srcdc,0,0,ROP_SRCCOPY
  379. surf.ReleaseDC srcdc
  380. renderSurf.ReleaseDC destdc
  381. surf.Release_
  382. D3D7GraphicsDriver().BeginScene
  383. End Method
  384. Method GrabPixmap:TPixmap( x,y,width,height )
  385. If Not IsValid() Return
  386. Local pixmap:TPixmap
  387. Local srcdc,destdc
  388. Local surf:IDirectDrawSurface7
  389. Local renderSurf:IDirectDrawSurface7
  390. D3D7GraphicsDriver().EndScene
  391. device.GetRenderTarget Varptr renderSurf
  392. pixmap=TPixmap.Create( width,height,PF_BGR888 )
  393. renderSurf.GetDC Varptr srcdc
  394. surf=surffrompixmap( pixmap )
  395. surf.GetDC Varptr destdc
  396. BitBlt destdc,0,0,width,height,srcdc,x,y,ROP_SRCCOPY
  397. surf.ReleaseDC destdc
  398. renderSurf.ReleaseDC srcdc
  399. surf.Release_()
  400. D3D7GraphicsDriver().BeginScene
  401. Return pixmap
  402. End Method
  403. Method SetResolution( width:Float,height:Float )
  404. Local gw=GraphicsWidth()
  405. Local gh=GraphicsHeight()
  406. Local world:Float[]=[..
  407. gw/width,0.0,0.0,0.0,..
  408. 0.0,gh/height,0.0,0.0,..
  409. 0.0,0.0,1.0,0.0,..
  410. 0.0,0.0,0.0,1.0 ]
  411. device.SetTransform D3DTS_WORLD,world
  412. Local proj:Float[]=[..
  413. 2.0/gw,0.0,0.0,0.0,..
  414. 0.0,-2.0/gh,0.0,0.0,..
  415. 0.0,0.0,1.0,0.0,..
  416. -1-(1.0/gw),1+(1.0/gh),1.0,1.0]
  417. device.SetTransform D3DTS_PROJECTION,proj
  418. End Method
  419. Method surffrompixmap:IDirectDrawSurface7(pixmap:TPixmap)
  420. Local surf:IDirectDrawSurface7
  421. Local desc:DDSURFACEDESC2=New DDSURFACEDESC2
  422. Local res
  423. If pixmap.format=PF_I8 pixmap=pixmap.convert(PF_BGR888)
  424. If pixmap.format=PF_A8 pixmap=pixmap.convert(PF_BGRA8888)
  425. desc.dwSize=SizeOf(desc)
  426. desc.dwFlags=DDSD_CAPS|DDSD_WIDTH|DDSD_HEIGHT|DDSD_PITCH|DDSD_LPSURFACE|DDSD_PIXELFORMAT
  427. desc.dwWidth=pixmap.width
  428. desc.dwHeight=pixmap.height
  429. desc.lPitch=pixmap.pitch
  430. desc.lpSurface=pixmap.pixels
  431. desc.ddsCaps=DDSCAPS_SYSTEMMEMORY|DDSCAPS_OFFSCREENPLAIN
  432. desc.ddpf_dwSize=SizeOf(DDPIXELFORMAT)
  433. Select pixmap.format
  434. Case PF_BGR888
  435. desc.ddpf_dwFlags=DDPF_RGB
  436. desc.ddpf_BitCount=24
  437. desc.ddpf_BitMask_0=$ff0000
  438. desc.ddpf_BitMask_1=$00ff00
  439. desc.ddpf_BitMask_2=$0000ff
  440. Case PF_RGB888
  441. desc.ddpf_dwFlags=DDPF_RGB
  442. desc.ddpf_BitCount=24
  443. desc.ddpf_BitMask_0=$0000ff
  444. desc.ddpf_BitMask_1=$00ff00
  445. desc.ddpf_BitMask_2=$ff0000
  446. Case PF_BGRA8888
  447. desc.ddpf_dwFlags=DDPF_RGB|DDPF_ALPHAPIXELS
  448. desc.ddpf_BitCount=32
  449. desc.ddpf_BitMask_0=$ff0000
  450. desc.ddpf_BitMask_1=$00ff00
  451. desc.ddpf_BitMask_2=$0000ff
  452. desc.ddpf_BitMask_3=$ff000000
  453. Case PF_RGBA8888
  454. desc.ddpf_dwFlags=DDPF_RGB|DDPF_ALPHAPIXELS
  455. desc.ddpf_BitCount=32
  456. desc.ddpf_BitMask_0=$0000ff
  457. desc.ddpf_BitMask_1=$00ff00
  458. desc.ddpf_BitMask_2=$ff0000
  459. desc.ddpf_BitMask_3=$ff000000
  460. End Select
  461. res=D3D7GraphicsDriver().DirectDraw7().CreateSurface( desc,Varptr surf,Null )
  462. If res<>DD_OK RuntimeError "D3D7Max2D Create System Surface Failed"
  463. Return surf
  464. End Method
  465. Method SetActiveFrame( frame:TD3D7ImageFrame )
  466. If frame=activeFrame Return
  467. If frame
  468. device.SetTexture 0,frame.surface
  469. If Not activeFrame
  470. device.SetTextureStageState 0,D3DTSS_COLOROP,D3DTOP_MODULATE
  471. device.SetTextureStageState 0,D3DTSS_ALPHAOP,D3DTOP_MODULATE
  472. EndIf
  473. Local flags=frame.flags & FILTEREDIMAGE
  474. If flags<>activeFrameFlags
  475. If flags & FILTEREDIMAGE
  476. device.SetTextureStageState 0,D3DTSS_MAGFILTER,D3DTFG_LINEAR
  477. device.SetTextureStageState 0,D3DTSS_MINFILTER,D3DTFG_LINEAR
  478. device.SetTextureStageState 0,D3DTSS_MIPFILTER,D3DTFG_LINEAR
  479. Else
  480. device.SetTextureStageState 0,D3DTSS_MAGFILTER,D3DTFG_POINT
  481. device.SetTextureStageState 0,D3DTSS_MINFILTER,D3DTFG_POINT
  482. device.SetTextureStageState 0,D3DTSS_MIPFILTER,D3DTFG_POINT
  483. EndIf
  484. activeFrameFlags=flags
  485. EndIf
  486. Else
  487. device.SetTexture 0,Null
  488. device.SetTextureStageState 0,D3DTSS_COLOROP,D3DTOP_SELECTARG2
  489. device.SetTextureStageState 0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG2
  490. EndIf
  491. activeFrame=frame
  492. End Method
  493. End Type
  494. Type TD3D7ImageFrame Extends TImageFrame
  495. Field seq
  496. Field driver:TD3D7Max2DDriver
  497. Field surface:IDirectDrawSurface7
  498. Field sinfo:DDSurfaceDesc2
  499. Field xyzuv:Float[24]
  500. Field width,height,flags
  501. Field uscale:Float,vscale:Float
  502. Method Delete()
  503. If Not surface Return
  504. If seq=GraphicsSeq D3D7GraphicsDriver().DestroySurface surface
  505. surface=Null
  506. End Method
  507. Method SetUV(u0:Float,v0:Float,u1:Float,v1:Float)
  508. xyzuv[4]=u0
  509. xyzuv[5]=v0
  510. xyzuv[10]=u1
  511. xyzuv[11]=v0
  512. xyzuv[16]=u1
  513. xyzuv[17]=v1
  514. xyzuv[22]=u0
  515. xyzuv[23]=v1
  516. End Method
  517. Function Create:TD3D7ImageFrame( driver:TD3D7Max2DDriver,width,height,flags )
  518. Function Pow2Size( n )
  519. Local t=1
  520. While t<n
  521. t:*2
  522. Wend
  523. Return t
  524. End Function
  525. Local swidth=Pow2Size(width)
  526. Local sheight=Pow2Size(height)
  527. Local desc:DDSURFACEDESC2=New DDSURFACEDESC2
  528. Local res
  529. desc.dwSize=SizeOf(desc)
  530. desc.dwFlags=DDSD_WIDTH|DDSD_HEIGHT|DDSD_CAPS|DDSD_PIXELFORMAT
  531. desc.dwWidth=swidth
  532. desc.dwHeight=sheight
  533. desc.ddsCaps=DDSCAPS_TEXTURE
  534. 'don't manage fast DYNAMICIMAGEs
  535. If flags<>DYNAMICIMAGE desc.ddsCaps2=DDSCAPS2_TEXTUREMANAGE
  536. desc.ddpf_dwSize=SizeOf(DDPIXELFORMAT)
  537. desc.ddpf_dwFlags=DDPF_RGB|DDPF_ALPHAPIXELS
  538. desc.ddpf_BitCount=32
  539. desc.ddpf_BitMask_0=$ff0000
  540. desc.ddpf_BitMask_1=$00ff00
  541. desc.ddpf_BitMask_2=$0000ff
  542. desc.ddpf_BitMask_3=$ff000000
  543. If flags & MIPMAPPEDIMAGE desc.ddsCaps:|DDSCAPS_MIPMAP|DDSCAPS_COMPLEX
  544. Local surf:IDirectDrawSurface7=D3D7GraphicsDriver().CreateSurface( desc )
  545. If Not surf Throw "Create DX7 surface Failed"
  546. Local frame:TD3D7ImageFrame=New TD3D7ImageFrame
  547. frame.seq=GraphicsSeq
  548. frame.driver=driver
  549. frame.surface=surf
  550. frame.sinfo=New DDSurfaceDesc2
  551. frame.sinfo.dwSize=SizeOf(frame.sinfo)
  552. frame.xyzuv=New Float[24]
  553. frame.width=width
  554. frame.height=height
  555. frame.flags=flags
  556. frame.uscale=1.0/swidth
  557. frame.vscale=1.0/sheight
  558. frame.SetUV 0.0,0.0,width * frame.uscale,height * frame.vscale
  559. Return frame
  560. End Function
  561. Method Lock:TPixmap( read_lock,write_lock )
  562. Local lflags=DDLOCK_WAIT|DDLOCK_SURFACEMEMORYPTR
  563. If read_lock And Not write_lock lflags:|DDLOCK_READONLY
  564. If write_lock And Not read_lock lflags:|DDLOCK_WRITEONLY
  565. Local res=surface.Lock(Null,sinfo,lflags,Null)
  566. If res<>DD_OK RuntimeError "DD3D7ImageFrame Lock failed" ' Return
  567. Return TPixmap.CreateStatic( sinfo.lpSurface,sinfo.dwWidth,sinfo.dwHeight,sinfo.lPitch,PF_BGRA8888 )
  568. End Method
  569. Method Unlock()
  570. SmearEdges
  571. surface.Unlock Null
  572. If flags & MIPMAPPEDIMAGE BuildMipMaps
  573. End Method
  574. Method Draw( x0:Float,y0:Float,x1:Float,y1:Float,tx:Float,ty:Float,sx:Float,sy:Float,sw:Float,sh:Float )
  575. driver.DrawFrame Self,x0:Float,y0:Float,x1:Float,y1:Float,tx:Float,ty:Float,sx,sy,sw,sh
  576. End Method
  577. Function Mix(c0,c1)
  578. Local c
  579. c=((c0 Shr 1)&$7f7f7f7f)+((c1 Shr 1)&$7f7f7f7f)
  580. c:+(c Shr 3)&$01010101
  581. Return c
  582. End Function
  583. Method SmearEdges()
  584. Local p:Byte Ptr
  585. Local n,x,y,c
  586. If width<>sinfo.dwWidth
  587. n=1
  588. If flags & MIPMAPPEDIMAGE n=sinfo.dwWidth-width
  589. For y=0 Until height
  590. p=sinfo.lpSurface+y*sinfo.lPitch
  591. c=Int Ptr(p)[width-1]
  592. For x=0 Until n
  593. Int Ptr(p)[width+x]=c
  594. Next
  595. Next
  596. EndIf
  597. If height<>sinfo.dwHeight
  598. n=1
  599. If flags & MIPMAPPEDIMAGE n=sinfo.dwHeight-height
  600. p=sinfo.lpSurface+(height-1)*sinfo.lPitch
  601. For y=1 To n
  602. MemCopy p+y*sinfo.lPitch,p,sinfo.dwWidth*4
  603. Next
  604. EndIf
  605. End Method
  606. Method BuildMipMaps()
  607. Type TMip
  608. Field surf:IDirectDrawSurface7
  609. Field info:DDSURFACEDESC2=New DDSURFACEDESC2
  610. Method Lock(srf:IDirectDrawSurface7)
  611. surf=srf
  612. info.dwSize=SizeOf(info)
  613. Return surf.Lock(Null,info,DDLOCK_WAIT,0)
  614. End Method
  615. Method Write(x,y,argb)
  616. Local p:Byte Ptr
  617. x=Min(x,info.dwWidth-1)
  618. y=Min(y,info.dwHeight-1)
  619. p=info.lpSurface+y*info.lPitch
  620. Int Ptr(p)[x]=argb
  621. End Method
  622. Method Read(x,y)
  623. Local p:Byte Ptr
  624. x=Min(x,info.dwWidth-1)
  625. y=Min(y,info.dwHeight-1)
  626. p=info.lpSurface+y*info.lPitch
  627. Return Int Ptr(p)[x]
  628. End Method
  629. Method Unlock()
  630. surf.Unlock Null
  631. End Method
  632. End Type
  633. Local caps2:DDSCAPS2
  634. Local src:IDirectDrawSurface7
  635. Local dest:IDirectDrawSurface7
  636. Local srcmip:TMip
  637. Local dstmip:TMip
  638. Local res,x,y,w,h,c0,c1,c2,c3
  639. caps2=New DDSCAPS2
  640. caps2.dwCaps=DDSCAPS_TEXTURE
  641. caps2.dwCaps2=DDSCAPS2_MIPMAPSUBLEVEL
  642. srcmip=New TMip
  643. dstmip=New TMip
  644. src=surface
  645. While True
  646. res=src.GetAttachedSurface(caps2,Varptr dest)
  647. If res Exit
  648. res=srcmip.Lock(src)
  649. If res RuntimeError "BuildMipMaps: lock failed"
  650. res=dstmip.Lock(dest)
  651. If res RuntimeError "BuildMipMaps: lock failed"
  652. w=dstmip.info.dwWidth
  653. h=dstmip.info.dwHeight
  654. For y=0 Until h
  655. For x=0 Until w
  656. c0=srcmip.read(x*2,y*2)
  657. c1=srcmip.read(x*2+1,y*2)
  658. c2=srcmip.read(x*2,y*2+1)
  659. c3=srcmip.read(x*2+1,y*2+1)
  660. dstmip.write x,y,mix(mix(c0,c1),mix(c2,c3))
  661. Next
  662. Next
  663. srcmip.Unlock
  664. dstmip.Unlock
  665. dest.Release_
  666. src=dest
  667. Wend
  668. End Method
  669. End Type
  670. Rem
  671. bbdoc: Get Direct3D7 Max2D Driver
  672. about:
  673. The returned driver can be used with #SetGraphicsDriver to enable Direct3D Max2D rendering.
  674. End Rem
  675. Function D3D7Max2DDriver:TD3D7Max2DDriver()
  676. If D3D7GraphicsDriver()
  677. Global _driver:TD3D7Max2DDriver=New TD3D7Max2DDriver
  678. Return _driver
  679. EndIf
  680. End Function
  681. Local driver:TD3D7Max2DDriver=D3D7Max2DDriver()
  682. If driver SetGraphicsDriver driver