sebbesthusbando

Bitmapsprite

Aug 14th, 2026
6
0
Never
Not a member of GistPad yet? Sign Up, it unlocks many cool features!
None 17.24 KB | None | 0 0
  1. #===============================================================================
  2. # Sprite class that maintains a bitmap of its own.
  3. # This bitmap can't be changed to a different one.
  4. #===============================================================================
  5. class BitmapSprite < Sprite
  6. def initialize(width, height, viewport = nil)
  7. super(viewport)
  8. self.bitmap = Bitmap.new(width, height)
  9. @initialized = true
  10. end
  11.  
  12. def bitmap=(value)
  13. super(value) if !@initialized
  14. end
  15.  
  16. def dispose
  17. self.bitmap.dispose if !self.disposed?
  18. super
  19. end
  20. end
  21.  
  22. #===============================================================================
  23. #
  24. #===============================================================================
  25. class AnimatedSprite < Sprite
  26. attr_reader :frame
  27. attr_reader :framewidth
  28. attr_reader :frameheight
  29. attr_reader :framecount
  30. attr_reader :animname
  31.  
  32. # frameskip is in 1/20ths of a second, and is the time between frame changes.
  33. def initializeLong(animname, framecount, framewidth, frameheight, frameskip)
  34. @animname = pbBitmapName(animname)
  35. @time_per_frame = [1, frameskip].max / 20.0
  36. raise _INTL("Frame width is 0") if framewidth == 0
  37. raise _INTL("Frame height is 0") if frameheight == 0
  38. begin
  39. @animbitmap = AnimatedBitmap.new(animname).deanimate
  40. rescue
  41. @animbitmap = Bitmap.new(framewidth, frameheight)
  42. end
  43. if @animbitmap.width % framewidth != 0
  44. raise _INTL("Bitmap's width ({1}) is not a multiple of frame width ({2}) [Bitmap={3}]",
  45. @animbitmap.width, framewidth, animname)
  46. end
  47. if @animbitmap.height % frameheight != 0
  48. raise _INTL("Bitmap's height ({1}) is not a multiple of frame height ({2}) [Bitmap={3}]",
  49. @animbitmap.height, frameheight, animname)
  50. end
  51. @framecount = framecount
  52. @framewidth = framewidth
  53. @frameheight = frameheight
  54. @framesperrow = @animbitmap.width / @framewidth
  55. @playing = false
  56. self.bitmap = @animbitmap
  57. self.src_rect.width = @framewidth
  58. self.src_rect.height = @frameheight
  59. self.frame = 0
  60. end
  61.  
  62. # Shorter version of AnimationSprite. All frames are placed on a single row
  63. # of the bitmap, so that the width and height need not be defined beforehand.
  64. # frameskip is in 1/20ths of a second, and is the time between frame changes.
  65. def initializeShort(animname, framecount, frameskip)
  66. @animname = pbBitmapName(animname)
  67. @time_per_frame = [1, frameskip].max / 20.0
  68. begin
  69. @animbitmap = AnimatedBitmap.new(animname).deanimate
  70. rescue
  71. @animbitmap = Bitmap.new(framecount * 4, 32)
  72. end
  73. if @animbitmap.width % framecount != 0
  74. raise _INTL("Bitmap's width ({1}) is not a multiple of frame count ({2}) [Bitmap={3}]",
  75. @animbitmap.width, framewidth, animname)
  76. end
  77. @framecount = framecount
  78. @framewidth = @animbitmap.width / @framecount
  79. @frameheight = @animbitmap.height
  80. @framesperrow = framecount
  81. @playing = false
  82. self.bitmap = @animbitmap
  83. self.src_rect.width = @framewidth
  84. self.src_rect.height = @frameheight
  85. self.frame = 0
  86. end
  87.  
  88. def initialize(*args)
  89. if args.length == 1
  90. super(args[0][3])
  91. initializeShort(args[0][0], args[0][1], args[0][2])
  92. else
  93. super(args[5])
  94. initializeLong(args[0], args[1], args[2], args[3], args[4])
  95. end
  96. end
  97.  
  98. def self.create(animname, framecount, frameskip, viewport = nil)
  99. return self.new([animname, framecount, frameskip, viewport])
  100. end
  101.  
  102. def dispose
  103. return if disposed?
  104. @animbitmap.dispose
  105. @animbitmap = nil
  106. super
  107. end
  108.  
  109. def playing?
  110. return @playing
  111. end
  112.  
  113. def frame=(value)
  114. @frame = value
  115. self.src_rect.x = @frame % @framesperrow * @framewidth
  116. self.src_rect.y = @frame / @framesperrow * @frameheight
  117. end
  118.  
  119. def start
  120. @playing = true
  121. end
  122.  
  123. alias play start
  124.  
  125. def stop
  126. @playing = false
  127. end
  128.  
  129. def update
  130. super
  131. if @playing
  132. new_frame = (System.uptime / @time_per_frame).to_i % self.framecount
  133. self.frame = new_frame if self.frame != new_frame
  134. end
  135. end
  136. end
  137.  
  138. #===============================================================================
  139. # Displays an icon bitmap in a sprite. Supports animated images.
  140. #===============================================================================
  141. class IconSprite < Sprite
  142. attr_reader :name
  143.  
  144. def initialize(*args)
  145. case args.length
  146. when 0
  147. super(nil)
  148. self.bitmap = nil
  149. when 1
  150. super(args[0])
  151. self.bitmap = nil
  152. when 2
  153. super(nil)
  154. self.x = args[0]
  155. self.y = args[1]
  156. else
  157. super(args[2])
  158. self.x = args[0]
  159. self.y = args[1]
  160. end
  161. @name = ""
  162. @_iconbitmap = nil
  163. end
  164.  
  165. def dispose
  166. clearBitmaps
  167. super
  168. end
  169.  
  170. # Sets the icon's filename. Alias for setBitmap.
  171. def name=(value)
  172. setBitmap(value)
  173. end
  174.  
  175. # Sets the icon's filename.
  176. def setBitmap(file, hue = 0)
  177. oldrc = self.src_rect
  178. clearBitmaps
  179. @name = file
  180. return if file.nil?
  181. if file == ""
  182. @_iconbitmap = nil
  183. else
  184. @_iconbitmap = AnimatedBitmap.new(file, hue)
  185. # for compatibility
  186. self.bitmap = @_iconbitmap ? @_iconbitmap.bitmap : nil
  187. self.src_rect = oldrc
  188. end
  189. end
  190.  
  191. def clearBitmaps
  192. @_iconbitmap&.dispose
  193. @_iconbitmap = nil
  194. self.bitmap = nil if !self.disposed?
  195. end
  196.  
  197. def update
  198. super
  199. return if !@_iconbitmap
  200. @_iconbitmap.update
  201. if self.bitmap != @_iconbitmap.bitmap
  202. oldrc = self.src_rect
  203. self.bitmap = @_iconbitmap.bitmap
  204. self.src_rect = oldrc
  205. end
  206. end
  207. end
  208.  
  209. #===============================================================================
  210. # Sprite class that stores multiple bitmaps, and displays only one at once.
  211. #===============================================================================
  212. class ChangelingSprite < Sprite
  213. def initialize(x = 0, y = 0, viewport = nil)
  214. super(viewport)
  215. self.x = x
  216. self.y = y
  217. @bitmaps = {}
  218. @currentBitmap = nil
  219. end
  220.  
  221. def addBitmap(key, path)
  222. @bitmaps[key]&.dispose
  223. @bitmaps[key] = AnimatedBitmap.new(path)
  224. end
  225.  
  226. def changeBitmap(key)
  227. @currentBitmap = @bitmaps[key]
  228. self.bitmap = (@currentBitmap) ? @currentBitmap.bitmap : nil
  229. end
  230.  
  231. def dispose
  232. return if disposed?
  233. @bitmaps.each_value { |bm| bm.dispose }
  234. @bitmaps.clear
  235. super
  236. end
  237.  
  238. def update
  239. return if disposed?
  240. @bitmaps.each_value { |bm| bm.update }
  241. self.bitmap = (@currentBitmap) ? @currentBitmap.bitmap : nil
  242. end
  243. end
RAW Paste Data Copied