Project1

标题: 有关Bitmap#blur的改进 [打印本页]

作者: 精灵使者    时间: 2013-9-15 08:09
标题: 有关Bitmap#blur的改进
  1. #=============================================================================
  2. # ■ Bitmap重定义
  3. #=============================================================================

  4. class Bitmap
  5.   def blur(strength = 1, opacity = 10)
  6.    (-strength).upto(strength) do |x|
  7.      (-strength).upto(strength) do |y|
  8.       next if x.zero? and y.zero?
  9.       src_rect = Rect.new(0, 0, width - x, height - y)
  10.       blt(x, y, self, src_rect, opacity)
  11.      end
  12.    end
  13.   end
  14. end
复制代码
这个blur如果反复递归的话会出现bitmap大小越来越大(也就是黑边填充)的现象。
求改进,求更新
顺便召唤 @忧雪の伤
作者: R-零    时间: 2013-9-15 10:44
  1. #=============================================================================
  2. # ■ Bitmap重定义
  3. #=============================================================================

  4. class Sprite
  5.   alias ud update
  6.   def update
  7.     self.bitmap = self.bitmap.exc
  8.     ud
  9.   end
  10. end

  11. class Bitmap
  12.   def blur(strength = 1, opacity = 5)
  13.     blur_in(strength, opacity)
  14.     extend(strength,strength,strength,strength)
  15.   end
  16.   def exc
  17.     return self if not @excb
  18.     self.dispose
  19.     return @excb
  20.   end
  21.   def blur_in(strength = 1, opacity = 10)
  22.       (-strength).upto(strength) do |x|
  23.      (-strength).upto(strength) do |y|
  24.       next if x.zero? and y.zero?
  25.       src_rect = Rect.new(0, 0, width - x, height - y)
  26.       blt(x, y, self, src_rect, opacity)
  27.      end
  28.    end
  29. end
  30. def extend(lx,ly,rx,ry)
  31.    new_bitmap = Bitmap.new(width+lx+rx,height+ly+ry)
  32.    new_bitmap.blt(lx,ly,self,Rect.new(0, 0, width, height))
  33.    @excb = new_bitmap
  34. end
  35. end
  36. s = Sprite.new
  37. s.bitmap = Bitmap.new(32,32)
  38. s.bitmap.fill_rect(0,0,32,32,Color.new(255,0,0))
  39. s.x = 320
  40. s.y = 240
  41. s.ox = s.bitmap.width / 2
  42. s.oy = s.bitmap.height / 2
  43. loop{Graphics.update;s.update;s.bitmap.blur;s.ox = s.bitmap.width / 2;
  44. s.oy = s.bitmap.height / 2
  45. }
复制代码
边blur边扩充边缘
不过这个东西还不够卫生不能多用




欢迎光临 Project1 (https://rpg.blue/) Powered by Discuz! X3.1