image.bmx 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. Strict
  2. Import "driver.bmx"
  3. Rem
  4. bbdoc: Max2D Image type
  5. End Rem
  6. Type TImage
  7. Field width,height,flags
  8. Field mask_r,mask_g,mask_b
  9. Field handle_x#,handle_y#
  10. Field pixmaps:TPixmap[]
  11. Field frames:TImageFrame[]
  12. Field seqs[]
  13. Method _pad()
  14. End Method
  15. Method Frame:TImageFrame( index )
  16. If seqs[index]=GraphicsSeq Return frames[index]
  17. frames[index]=_max2dDriver.CreateFrameFromPixmap( Lock(index,True,False),flags )
  18. If frames[index] seqs[index]=GraphicsSeq Else seqs[index]=0
  19. Return frames[index]
  20. End Method
  21. Method Lock:TPixmap( index,read,write )
  22. If write
  23. seqs[index]=0
  24. frames[index]=Null
  25. EndIf
  26. If Not pixmaps[index]
  27. pixmaps[index]=CreatePixmap( width,height,PF_RGBA8888 )
  28. EndIf
  29. Return pixmaps[index]
  30. End Method
  31. Method SetPixmap( index,pixmap:TPixmap )
  32. If (flags & MASKEDIMAGE) And AlphaBitsPerPixel[pixmap.format]=0
  33. pixmap=MaskPixmap( pixmap,mask_r,mask_g,mask_b )
  34. EndIf
  35. pixmaps[index]=pixmap
  36. seqs[index]=0
  37. frames[index]=Null
  38. End Method
  39. Function Create:TImage( width,height,frames,flags,mr,mg,mb )
  40. Local t:TImage=New TImage
  41. t.width=width
  42. t.height=height
  43. t.flags=flags
  44. t.mask_r=mr
  45. t.mask_g=mg
  46. t.mask_b=mb
  47. t.pixmaps=New TPixmap[frames]
  48. t.frames=New TImageFrame[frames]
  49. t.seqs=New Int[frames]
  50. Return t
  51. End Function
  52. Function Load:TImage( url:Object,flags,mr,mg,mb )
  53. Local pixmap:TPixmap=TPixmap(url)
  54. If Not pixmap pixmap=LoadPixmap(url)
  55. If Not pixmap Return
  56. Local t:TImage=Create( pixmap.width,pixmap.height,1,flags,mr,mg,mb )
  57. t.SetPixmap 0,pixmap
  58. Return t
  59. End Function
  60. Function LoadAnim:TImage( url:Object,cell_width,cell_height,first,count,flags,mr,mg,mb )
  61. Local pixmap:TPixmap=TPixmap(url)
  62. If Not pixmap pixmap=LoadPixmap(url)
  63. If Not pixmap Return
  64. Local x_cells=pixmap.width/cell_width
  65. Local y_cells=pixmap.height/cell_height
  66. If first+count>x_cells*y_cells Return
  67. Local t:TImage=Create( cell_width,cell_height,count,flags,mr,mg,mb )
  68. For Local cell=first To first+count-1
  69. Local x=cell Mod x_cells * cell_width
  70. Local y=cell / x_cells * cell_height
  71. Local window:TPixmap=pixmap.Window( x,y,cell_width,cell_height )
  72. t.SetPixmap cell-first,window.Copy()
  73. Next
  74. Return t
  75. End Function
  76. End Type