| 赞 | 68 |
| VIP | |
| 好人卡 | |
| 积分 | 65 |
| 经验 | |
| 最后登录 | 2023-7-2 |
| 在线时间 | 119 小时 |
- 梦石
- 0
- 星屑
- 6483
- 在线时间
- 119 小时
- 注册时间
- 2020-1-8
- 回帖
- 205
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有账号?注册会员
×
本帖最后由 RPGzh500223 于 2022-4-29 13:11 编辑
直接代码
[pre lang="ruby"]class Bitmap
def to_int
self.object_id
end
# src_bitmap 源位图,必须与自身位图同尺寸
# wave_length 【无符号整型】 波长对应一个Math::PI
# wave_amp 【无符号整型】 波高
# wave_speed 【基本整型】 影响波动快慢
# wave_phase 【基本整型】 波动起始的角度值(一般0...360)
def wave(src_bitmap, wave_length, wave_amp, wave_speed = 1, wave_phase = 0)
@src_bitmap = src_bitmap
@wave_length = wave_length
@wave_amp = wave_amp
@wave_speed = wave_speed
@wave_phase = wave_phase
@waving = (wave_length * wave_amp * wave_speed != 0)
# 说明用变量
@wave_step = Math::PI / wave_length
@wave_dcos = Math.cos(@wave_step) * 2
end
# 说明用方法: WaveH 的RGSS 实现
def wave_update
return unless @waving
self.clear
rad = @wave_phase * Math::A2R
s0 = Math.sin(rad)
s1 = Math.sin(rad + @wave_step)
s2 = 0
src_rect = Rect.new(0, 0, @src_bitmap.width, 1)
self.blt((s0 * @wave_amp).round, 0, @src_bitmap, src_rect)
src_rect.y += 1
self.blt((s1 * @wave_amp).round, 1, @src_bitmap, src_rect)
2.upto(self.height - 1) do |y|
s2 = @wave_dcos * s1 - s0
s0, s1 = s1, s2
src_rect.y += 1
self.blt((s2 * @wave_amp).round, y, @src_bitmap, src_rect)
end
@wave_phase = (@wave_phase + @wave_speed) % 360
end
dll = "Wave.dll"
WaveH = Win32API.new(dll, 'bitmapWaveH', 'LLLLLL', 'V')
def wave_update_dllH
return unless @waving
@wave_phase = WaveH.call(self, @src_bitmap, @wave_phase,
@wave_length, @wave_amp, @wave_speed)
end
# 适用于250 * 250大小以下的位图
WaveV = Win32API.new(dll, 'bitmapWaveV', 'LLLLLL', 'L')
def wave_update_dllV
return unless @waving
@wave_phase = WaveV.call(self, @src_bitmap, @wave_phase,
@wave_length, @wave_amp, @wave_speed)
end
WaveV_line4 = Win32API.new(dll, 'bitmapWaveV_line4', 'LLLLLL', 'L')
def wave_update_dllV_line4
return unless @waving
@wave_phase = WaveV_line4.call(self, @src_bitmap, @wave_phase,
@wave_length, @wave_amp, @wave_speed)
end
WaveHV = Win32API.new(dll, 'bitmapWaveHV', 'LLLLLL', 'L')
def wave_update_dllHV
return unless @waving
@wave_phase = WaveHV.call(self, @src_bitmap, @wave_phase,
@wave_length, @wave_amp, @wave_speed)
end
end
src = Bitmap.new("temp2.jpeg")
#src.fill_rect(src.rect, Color.new(0, 255, 255))
sprite = Sprite.new
sprite.bitmap = Bitmap.new(src.width, src.height)
sprite.bitmap.wave(src, 50, 10, 2, 0)
sprite.x = 320
sprite.y = 240
sprite.ox = src.width / 2
sprite.oy = src.height / 2
str = "Bitmap(#{src.width} * #{src.height})\n"
count, t = 0, Time.now
loop do
sprite.bitmap.wave_update_dllH
break if Time.now - t >= 1.0
count += 1
end
str << "Bitmap#wave_update_dllH => #{count} 次/秒\n"
count, t = 0, Time.now
loop do
sprite.bitmap.wave_update_dllV
break if Time.now - t >= 1.0
count += 1
end
str << "Bitmap#wave_update_dllV => #{count} 次/秒\n"
count, t = 0, Time.now
loop do
sprite.bitmap.wave_update_dllV_line4
break if Time.now - t >= 1.0
count += 1
end
str << "Bitmap#wave_update_dllV_line4 => #{count} 次/秒\n"
count, t = 0, Time.now
loop do
sprite.bitmap.wave_update_dllHV
break if Time.now - t >= 1.0
count += 1
end
str << "Bitmap#wave_update_dllHV => #{count} 次/秒\n"
print str
loop do
Graphics.update
#sprite.bitmap.wave_update_dllH
#sprite.bitmap.wave_update_dllV
#sprite.bitmap.wave_update_dllV_line4
sprite.bitmap.wave_update_dllHV
end
[/pre]
迭代计算正弦值(百度“快速计算正弦波”),所以就没考虑周期(实际只能提速一丢丢)
最后还是希望能回复一下测试结果
|
-
迭代计算的误差
-
WaveHV的示意图
-
-
-
Wave.zip
4.31 KB, 下载次数: 5
Wave.dll
|