Commit 8c2803a7 authored by 神楽坂玲奈's avatar 神楽坂玲奈

fps初步

parent aab554d9
class FPSTimer
FPS_COUNT = 10
attr_accessor :fps
attr_reader :real_fps, :total_skip
attr_reader :count_sleep
# +fps+ is the number of frames per second that you want to keep,
# +accurary+ is the accurary of sleep/SDL.delay in milisecond
def initialize(fps = 60, accurary = 10, skip_limit = 15)
@fps = fps
@accurary = accurary / 1000.0
@skip_limit = skip_limit
reset
end
# reset timer, you should call just before starting loop
def reset
@old = get_ticks
@skip = 0
@real_fps = @fps
@frame_count = 0
@fps_old = @old
@count_sleep = 0
@total_skip = 0
end
# execute given block and wait
def wait_frame
now = get_ticks
nxt = @old + (1.0/@fps)
if nxt > now || @skip > @skip_limit
yield
@skip = 0
wait(nxt)
@old = nxt
else
@skip += 1
@total_skip += 1
@old = get_ticks
end
calc_real_fps
end
private
def wait(nxt)
while nxt > get_ticks + @accurary
sleep(@accurary - 0.005)
@count_sleep += 1
end
while nxt > get_ticks
# busy loop, do nothing
end
end
def get_ticks
SDL.get_ticks / 1000.0
end
def calc_real_fps
@frame_count += 1
if @frame_count >= FPS_COUNT
@frame_count = 0
now = get_ticks
@real_fps = FPS_COUNT / (now - @fps_old)
@fps_old = now
end
end
end
\ No newline at end of file
......@@ -43,7 +43,8 @@ require_relative 'window'
require_relative 'window_list'
require_relative 'window_user'
require_relative 'scene_title'
require_relative 'fpstimer'
$fpstimer = FPSTimer.new
$scene = Scene_Title.new
while $scene
$scene.main
......
......@@ -14,7 +14,6 @@ class Scene
start
while $scene == self
update
sleep 0.01
end
terminate
end
......@@ -49,24 +48,26 @@ class Scene
# ● 更新画面
#--------------------------------------------------------------------------
def update
while event = Event.poll
handle(event)
end
#@fps.clear(0,0,100,24)
#@font.draw_blended_utf8(@fps.contents, @fpscount, 160, 12, 0x00,0x00,0x00)
#@fpscount += 1
$screen.put(@background,0,0)
@windows.each do |window|
if window.angle.zero?
while event = Event.poll
handle(event)
end
#@fps.clear(0,0,100,24)
#@font.draw_blended_utf8(@fps.contents, @fpscount, 160, 12, 0x00,0x00,0x00)
#@fpscount += 1
$fpstimer.wait_frame do
$screen.put(@background,0,0)
@windows.each do |window|
if window.angle.zero?
Surface.blit(window.contents, *window.viewport, $screen, window.x, window.y) if window.contents && window.visible
else
contents = window.contents.transform_surface(0x66000000,180,1,1,0)
Surface.blit(contents, *window.viewport, $screen, window.x, window.y) if window.contents && window.visible
#Surface.transform_blit(window.contents,$screen,0,1,1,100,100,100,100,Surface::TRANSFORM_AA)#,0,0)
end
#$screen.put(window.contents, window.x, window.y) if window.contents && window.visible
end
$screen.update_rect(0,0,0,0)
else
contents = window.contents.transform_surface(0x66000000,180,1,1,0)
Surface.blit(contents, *window.viewport, $screen, window.x, window.y) if window.contents && window.visible
#Surface.transform_blit(window.contents,$screen,0,1,1,100,100,100,100,Surface::TRANSFORM_AA)#,0,0)
end
#$screen.put(window.contents, window.x, window.y) if window.contents && window.visible
end
$screen.update_rect(0,0,0,0)
end
end
def handle(event)
case event
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment