image.bmx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. SuperStrict
  2. Import "driver.bmx"
  3. Rem
  4. bbdoc: Max2D Image type
  5. End Rem
  6. Type TImage
  7. Field width:UInt,height:UInt,flags:Int
  8. Field mask_r:Int,mask_g:Int,mask_b:Int
  9. Field handle_x#,handle_y#
  10. Field pixmaps:TPixmap[]
  11. Field frames:TImageFrame[]
  12. Field seqs:Int[]
  13. Field frameDuration:Int[]
  14. Method _pad()
  15. End Method
  16. Method Frame:TImageFrame( index:Int )
  17. Assert index < seqs.length And index < frames.length Else "Index out of bounds"
  18. If seqs[index]=GraphicsSeq Return frames[index]
  19. frames[index]=_max2dDriver.CreateFrameFromPixmap( Lock(index,True,False),flags )
  20. If frames[index] seqs[index]=GraphicsSeq Else seqs[index]=0
  21. Return frames[index]
  22. End Method
  23. Method Clear(r:UInt, g:UInt, b:UInt, a:Float = 1.0, frameIndex:Int = -1)
  24. Local clearColor:Int = Int(int(255*a) * $1000000) + Int(r * $10000) + Int(g * $100) + b
  25. If frameIndex < 0
  26. For Local i:Int = 0 until pixmaps.length
  27. Local p:TPixmap = Lock(i, True, True)
  28. p.ClearPixels(clearColor)
  29. Next
  30. Else
  31. Local p:TPixmap = Lock(frameIndex, True, True)
  32. p.ClearPixels(clearColor)
  33. EndIf
  34. End Method
  35. Method Lock:TPixmap( index:Int,read:Int,write:Int )
  36. Assert index < seqs.length And index < frames.length Else "Index out of bounds"
  37. If write
  38. seqs[index]=0
  39. frames[index]=Null
  40. EndIf
  41. If Not pixmaps[index]
  42. pixmaps[index]=CreatePixmap( width,height,PF_RGBA8888 )
  43. EndIf
  44. Return pixmaps[index]
  45. End Method
  46. Method SetPixmap( index:Int,pixmap:TPixmap, duration:Int = 0 )
  47. Assert index < seqs.length And index < frames.length And index < pixmaps.length Else "Index out of bounds"
  48. If (flags & MASKEDIMAGE) And AlphaBitsPerPixel[pixmap.format]=0
  49. pixmap=MaskPixmap( pixmap,mask_r,mask_g,mask_b )
  50. EndIf
  51. pixmap.dds_fmt=GreenBitsPerPixel[0] ' set dds format
  52. pixmap.tex_name=BlueBitsPerPixel[0] ' set texture name
  53. pixmaps[index]=pixmap
  54. seqs[index]=0
  55. frames[index]=Null
  56. frameDuration[index]=duration
  57. End Method
  58. Function Create:TImage( width:Int,height:Int,frames:Int,flags:Int,mr:Int,mg:Int,mb:Int )
  59. Assert width > 0 And height > 0 Else "Image dimensions out of bounds"
  60. Local t:TImage=New TImage
  61. t.width=width
  62. t.height=height
  63. t.flags=flags
  64. t.mask_r=mr
  65. t.mask_g=mg
  66. t.mask_b=mb
  67. t.pixmaps=New TPixmap[frames]
  68. t.frames=New TImageFrame[frames]
  69. t.seqs=New Int[frames]
  70. t.frameDuration=New Int[frames]
  71. Return t
  72. End Function
  73. Function Load:TImage( url:Object,flags:Int,mr:Int,mg:Int,mb:Int )
  74. Local pixmap:TPixmap=TPixmap(url)
  75. If Not pixmap pixmap=LoadPixmap(url)
  76. If Not pixmap Return Null
  77. Local t:TImage=Create( pixmap.width,pixmap.height,1,flags,mr,mg,mb )
  78. t.SetPixmap 0,pixmap
  79. Return t
  80. End Function
  81. Function LoadAnim:TImage( url:Object,cell_width:Int,cell_height:Int,first:Int,count:Int,flags:Int,mr:Int,mg:Int,mb:Int )
  82. Assert cell_width > 0 And cell_height > 0 Else "Cell dimensions out of bounds"
  83. Local pixmap:TPixmap=TPixmap(url)
  84. If Not pixmap pixmap=LoadPixmap(url)
  85. If Not pixmap Return Null
  86. Local x_cells:Int=pixmap.width/cell_width
  87. Local y_cells:Int=pixmap.height/cell_height
  88. If first+count>x_cells*y_cells Return Null
  89. Local t:TImage=Create( cell_width,cell_height,count,flags,mr,mg,mb )
  90. For Local cell:Int=first To first+count-1
  91. Local x:Int=cell Mod x_cells * cell_width
  92. Local y:Int=cell / x_cells * cell_height
  93. Local window:TPixmap=pixmap.Window( x,y,cell_width,cell_height )
  94. t.SetPixmap cell-first,window.Copy()
  95. Next
  96. Return t
  97. End Function
  98. End Type
  99. Rem
  100. bbdoc: Max2D Render Image type
  101. EndRem
  102. Type TRenderImage Extends TImage
  103. Method Frame:TImageFrame(index:Int) Override
  104. Assert index < seqs.length And index < frames.length Else "Index out of bounds"
  105. If seqs[index] = GraphicsSeq
  106. Return frames[index]
  107. EndIf
  108. frames[index] = _max2dDriver.CreateRenderImageFrame(width, height, flags)
  109. If frames[index]
  110. seqs[index] = GraphicsSeq
  111. Else
  112. seqs[index] = 0
  113. EndIf
  114. Return frames[index]
  115. End Method
  116. Function Create:TRenderImage(width:Int, height:Int, flags:Int, MaskRed:Int, MaskGreen:Int, MaskBlue:Int)
  117. Local t:TRenderImage = New TRenderImage
  118. t.width = width
  119. t.height = height
  120. t.flags = flags
  121. t.mask_r = MaskRed
  122. t.mask_g = MaskGreen
  123. t.mask_b = MaskBlue
  124. t.pixmaps = New TPixmap[1]
  125. t.frames = New TImageFrame[1]
  126. t.seqs = New Int[1]
  127. t.frameDuration = New Int[1]
  128. Return t
  129. EndFunction
  130. EndType