Project1

标题: 位图wave效果 [打印本页]

作者: RPGzh500223    时间: 2022-4-28 18:35
标题: 位图wave效果
本帖最后由 RPGzh500223 于 2022-4-29 13:11 编辑

直接代码
RUBY 代码复制
  1. class Bitmap
  2.   def to_int
  3.     self.object_id
  4.   end
  5.  
  6.   # src_bitmap    源位图,必须与自身位图同尺寸
  7.   # wave_length  【无符号整型】   波长对应一个Math::PI
  8.   # wave_amp     【无符号整型】   波高
  9.   # wave_speed   【基本整型】     影响波动快慢
  10.   # wave_phase   【基本整型】     波动起始的角度值(一般0...360)
  11.   def wave(src_bitmap, wave_length, wave_amp, wave_speed = 1, wave_phase = 0)
  12.     @src_bitmap = src_bitmap
  13.     @wave_length = wave_length
  14.     @wave_amp = wave_amp
  15.     @wave_speed = wave_speed   
  16.     @wave_phase = wave_phase
  17.     @waving = (wave_length * wave_amp * wave_speed != 0)
  18.  
  19.     # 说明用变量
  20.     @wave_step = Math::PI / wave_length
  21.     @wave_dcos = Math.cos(@wave_step) * 2
  22.   end
  23.  
  24.   # 说明用方法: WaveH 的RGSS 实现
  25.   def wave_update
  26.     return unless @waving
  27.     self.clear
  28.  
  29.     rad = @wave_phase * Math::A2R
  30.     s0 = Math.sin(rad)
  31.     s1 = Math.sin(rad + @wave_step)
  32.     s2 = 0
  33.  
  34.     src_rect = Rect.new(0, 0, @src_bitmap.width, 1)
  35.     self.blt((s0 * @wave_amp).round, 0, @src_bitmap, src_rect)
  36.  
  37.     src_rect.y += 1
  38.     self.blt((s1 * @wave_amp).round, 1, @src_bitmap, src_rect)
  39.  
  40.     2.upto(self.height - 1) do |y|
  41.       s2 = @wave_dcos * s1 - s0
  42.       s0, s1 = s1, s2
  43.  
  44.       src_rect.y += 1
  45.       self.blt((s2 * @wave_amp).round, y, @src_bitmap, src_rect)
  46.     end
  47.  
  48.     @wave_phase = (@wave_phase + @wave_speed) % 360
  49.   end
  50.  
  51.  
  52.   dll = "Wave.dll"
  53.   WaveH = Win32API.new(dll, 'bitmapWaveH', 'LLLLLL', 'V')
  54.   def wave_update_dllH
  55.     return unless @waving
  56.     @wave_phase = WaveH.call(self, @src_bitmap, @wave_phase,
  57.       @wave_length, @wave_amp, @wave_speed)
  58.   end
  59.  
  60.   # 适用于250 * 250大小以下的位图
  61.   WaveV = Win32API.new(dll, 'bitmapWaveV', 'LLLLLL', 'L')
  62.   def wave_update_dllV
  63.     return unless @waving
  64.     @wave_phase = WaveV.call(self, @src_bitmap, @wave_phase,
  65.       @wave_length, @wave_amp, @wave_speed)
  66.   end
  67.  
  68.   WaveV_line4 = Win32API.new(dll, 'bitmapWaveV_line4', 'LLLLLL', 'L')
  69.   def wave_update_dllV_line4
  70.     return unless @waving
  71.     @wave_phase = WaveV_line4.call(self, @src_bitmap, @wave_phase,
  72.       @wave_length, @wave_amp, @wave_speed)
  73.   end
  74.  
  75.   WaveHV = Win32API.new(dll, 'bitmapWaveHV', 'LLLLLL', 'L')
  76.   def wave_update_dllHV
  77.     return unless @waving
  78.     @wave_phase = WaveHV.call(self, @src_bitmap, @wave_phase,
  79.       @wave_length, @wave_amp, @wave_speed)
  80.   end
  81. end
  82.  
  83.  
  84.  
  85. src = Bitmap.new("temp2.jpeg")
  86. #src.fill_rect(src.rect, Color.new(0, 255, 255))
  87.  
  88. sprite = Sprite.new
  89. sprite.bitmap = Bitmap.new(src.width, src.height)
  90. sprite.bitmap.wave(src, 50, 10, 2, 0)
  91. sprite.x = 320
  92. sprite.y = 240
  93. sprite.ox = src.width / 2
  94. sprite.oy = src.height / 2
  95.  
  96.  
  97. str = "Bitmap(#{src.width} * #{src.height})\n"
  98. count, t = 0, Time.now
  99. loop do
  100.   sprite.bitmap.wave_update_dllH
  101.   break if Time.now - t >= 1.0
  102.   count += 1
  103. end
  104. str << "Bitmap#wave_update_dllH => #{count} 次/秒\n"
  105.  
  106. count, t = 0, Time.now
  107. loop do
  108.   sprite.bitmap.wave_update_dllV
  109.   break if Time.now - t >= 1.0
  110.   count += 1
  111. end
  112. str << "Bitmap#wave_update_dllV => #{count} 次/秒\n"
  113.  
  114. count, t = 0, Time.now
  115. loop do
  116.   sprite.bitmap.wave_update_dllV_line4
  117.   break if Time.now - t >= 1.0
  118.   count += 1
  119. end
  120. str << "Bitmap#wave_update_dllV_line4 => #{count} 次/秒\n"
  121.  
  122. count, t = 0, Time.now
  123. loop do
  124.   sprite.bitmap.wave_update_dllHV
  125.   break if Time.now - t >= 1.0
  126.   count += 1
  127. end
  128. str << "Bitmap#wave_update_dllHV => #{count} 次/秒\n"
  129.  
  130. print str
  131.  
  132.  
  133. loop do
  134.   Graphics.update
  135.   #sprite.bitmap.wave_update_dllH
  136.   #sprite.bitmap.wave_update_dllV
  137.   #sprite.bitmap.wave_update_dllV_line4
  138.   sprite.bitmap.wave_update_dllHV
  139. end



迭代计算正弦值(百度“快速计算正弦波”),所以就没考虑周期(实际只能提速一丢丢)
最后还是希望能回复一下测试结果

temp.png (18.04 KB, 下载次数: 17)

迭代计算的误差

迭代计算的误差

waveHV.jpg (114.15 KB, 下载次数: 14)

WaveHV的示意图

WaveHV的示意图

waveHV.gif (900.74 KB, 下载次数: 17)

waveHV.gif

Wave.zip

4.31 KB, 下载次数: 6

Wave.dll


作者: 百里_飞柳    时间: 2022-4-28 20:29
整点gif看看啊,和va的wave的效果哪里不一样()

gif录制工具
作者: guoxiaomi    时间: 2022-4-28 22:47
来了!





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