A lot of FPS agnosticism, added def lerp

This commit is contained in:
Maruno17
2023-05-20 18:37:54 +01:00
parent 62e372f4d7
commit d112e2361a
38 changed files with 619 additions and 628 deletions

View File

@@ -555,16 +555,17 @@ end
# Fades out the screen before a block is run and fades it back in after the
# block exits. z indicates the z-coordinate of the viewport used for this effect
def pbFadeOutIn(z = 99999, nofadeout = false)
duration = 0.4 # In seconds
col = Color.new(0, 0, 0, 0)
viewport = Viewport.new(0, 0, Graphics.width, Graphics.height)
viewport.z = z
numFrames = (Graphics.frame_rate * 0.4).floor
alphaDiff = (255.0 / numFrames).ceil
(0..numFrames).each do |j|
col.set(0, 0, 0, j * alphaDiff)
timer_start = System.uptime
loop do
col.set(0, 0, 0, lerp(0, 255, duration, timer_start, System.uptime))
viewport.color = col
Graphics.update
Input.update
break if col.alpha == 255
end
pbPushFade
begin
@@ -574,11 +575,13 @@ def pbFadeOutIn(z = 99999, nofadeout = false)
ensure
pbPopFade
if !nofadeout
(0..numFrames).each do |j|
col.set(0, 0, 0, (numFrames - j) * alphaDiff)
timer_start = System.uptime
loop do
col.set(0, 0, 0, lerp(255, 0, duration, timer_start, System.uptime))
viewport.color = col
Graphics.update
Input.update
break if col.alpha == 0
end
end
viewport.dispose
@@ -586,17 +589,18 @@ def pbFadeOutIn(z = 99999, nofadeout = false)
end
def pbFadeOutInWithUpdate(z, sprites, nofadeout = false)
duration = 0.4 # In seconds
col = Color.new(0, 0, 0, 0)
viewport = Viewport.new(0, 0, Graphics.width, Graphics.height)
viewport.z = z
numFrames = (Graphics.frame_rate * 0.4).floor
alphaDiff = (255.0 / numFrames).ceil
(0..numFrames).each do |j|
col.set(0, 0, 0, j * alphaDiff)
timer_start = System.uptime
loop do
col.set(0, 0, 0, lerp(0, 255, duration, timer_start, System.uptime))
viewport.color = col
pbUpdateSpriteHash(sprites)
Graphics.update
Input.update
break if col.alpha == 255
end
pbPushFade
begin
@@ -604,12 +608,14 @@ def pbFadeOutInWithUpdate(z, sprites, nofadeout = false)
ensure
pbPopFade
if !nofadeout
(0..numFrames).each do |j|
col.set(0, 0, 0, (numFrames - j) * alphaDiff)
timer_start = System.uptime
loop do
col.set(0, 0, 0, lerp(255, 0, duration, timer_start, System.uptime))
viewport.color = col
pbUpdateSpriteHash(sprites)
Graphics.update
Input.update
break if col.alpha == 0
end
end
viewport.dispose
@@ -633,13 +639,16 @@ def pbFadeOutInWithMusic(zViewport = 99999)
end
def pbFadeOutAndHide(sprites)
duration = 0.4 # In seconds
col = Color.new(0, 0, 0, 0)
visiblesprites = {}
numFrames = (Graphics.frame_rate * 0.4).floor
alphaDiff = (255.0 / numFrames).ceil
pbDeactivateWindows(sprites) do
(0..numFrames).each do |j|
pbSetSpritesToColor(sprites, Color.new(0, 0, 0, j * alphaDiff))
timer_start = System.uptime
loop do
col.alpha = lerp(0, 255, duration, timer_start, System.uptime)
pbSetSpritesToColor(sprites, col)
(block_given?) ? yield : pbUpdateSpriteHash(sprites)
break if col.alpha == 255
end
end
sprites.each do |i|
@@ -652,6 +661,8 @@ def pbFadeOutAndHide(sprites)
end
def pbFadeInAndShow(sprites, visiblesprites = nil)
duration = 0.4 # In seconds
col = Color.new(0, 0, 0, 0)
if visiblesprites
visiblesprites.each do |i|
if i[1] && sprites[i[0]] && !pbDisposed?(sprites[i[0]])
@@ -659,12 +670,13 @@ def pbFadeInAndShow(sprites, visiblesprites = nil)
end
end
end
numFrames = (Graphics.frame_rate * 0.4).floor
alphaDiff = (255.0 / numFrames).ceil
pbDeactivateWindows(sprites) do
(0..numFrames).each do |j|
pbSetSpritesToColor(sprites, Color.new(0, 0, 0, ((numFrames - j) * alphaDiff)))
timer_start = System.uptime
loop do
col.alpha = lerp(255, 0, duration, timer_start, System.uptime)
pbSetSpritesToColor(sprites, col)
(block_given?) ? yield : pbUpdateSpriteHash(sprites)
break if col.alpha == 0
end
end
end

View File

@@ -29,11 +29,10 @@ class AnimatedSprite < Sprite
attr_reader :framecount
attr_reader :animname
# frameskip is in 1/20ths of a second, and is the time between frame changes.
def initializeLong(animname, framecount, framewidth, frameheight, frameskip)
@animname = pbBitmapName(animname)
@realframes = 0
@frameskip = [1, frameskip].max
@frameskip *= Graphics.frame_rate / 20
@time_per_frame = [1, frameskip].max / 20.0
raise _INTL("Frame width is 0") if framewidth == 0
raise _INTL("Frame height is 0") if frameheight == 0
begin
@@ -60,13 +59,12 @@ class AnimatedSprite < Sprite
self.frame = 0
end
# Shorter version of AnimationSprite. All frames are placed on a single row
# of the bitmap, so that the width and height need not be defined beforehand
# Shorter version of AnimationSprite. All frames are placed on a single row
# of the bitmap, so that the width and height need not be defined beforehand.
# frameskip is in 1/20ths of a second, and is the time between frame changes.
def initializeShort(animname, framecount, frameskip)
@animname = pbBitmapName(animname)
@realframes = 0
@frameskip = [1, frameskip].max
@frameskip *= Graphics.frame_rate / 20
@time_per_frame = [1, frameskip].max / 20.0
begin
@animbitmap = AnimatedBitmap.new(animname).deanimate
rescue
@@ -114,31 +112,27 @@ class AnimatedSprite < Sprite
def frame=(value)
@frame = value
@realframes = 0
self.src_rect.x = @frame % @framesperrow * @framewidth
self.src_rect.y = @frame / @framesperrow * @frameheight
end
def start
@playing = true
@realframes = 0
@start_time = System.uptime
end
alias play start
def stop
@playing = false
@start_time = nil
end
def update
super
if @playing
@realframes += 1
if @realframes == @frameskip
@realframes = 0
self.frame += 1
self.frame %= self.framecount
end
if @playing && System.uptime - @start_time >= @time_per_frame
self.frame = (@frame + 1) % self.framecount
@start_time += @time_per_frame
end
end
end

View File

@@ -812,14 +812,15 @@ end
def pbMessageWaitForInput(msgwindow, frames, showPause = false)
return if !frames || frames <= 0
msgwindow.startPause if msgwindow && showPause
frames = frames * Graphics.frame_rate / 20
frames.times do
timer_start = System.uptime
loop do
Graphics.update
Input.update
msgwindow&.update
pbUpdateSceneMap
break if Input.trigger?(Input::USE) || Input.trigger?(Input::BACK)
yield if block_given?
break if Input.trigger?(Input::USE) || Input.trigger?(Input::BACK)
break if System.uptime - timer_start >= frames / 20.0
end
msgwindow.stopPause if msgwindow && showPause
end