Graphics.frame_rate = 60
class FPS_Display
#===CONFIGURATION===#
TOGGLE = Input::F5
#=END CONFIGURATION=#
attr_accessor :frame_count
def initialize
# Create Sprite to display FPS
@counter = Sprite.new
# Position Sprite
@counter.x, @counter.z = 500, 99999999
# Create / Setup Bitmap
@counter.bitmap = Bitmap.new(200, 24)
#@counter.bitmap.fill_rect(0, 0, 200, 24, Color.new(0, 0, 0))
@counter.bitmap.font.size = 15
@counter.bitmap.font.name = '나눔고딕'
@counter.bitmap.font.bold = true
#@counter.bitmap.draw_text(100, 0, 100, 24, ' fps')
# Get Target Frame Rate
@frame_rate = Graphics.frame_rate
# Set Start time
@start = nil
@on = true
@frame_count = 0
@fps = @frame_rate
refresh
end
def refresh
# Clear FPS
#@counter.bitmap.fill_rect(0, 0, 100, 24, Color.new(0,0,0))
@counter.bitmap.clear
# Redraw Current FPS
@counter.bitmap.font.color = Color.new(0,0,0,120)
@counter.bitmap.draw_text(100-1, 0, 100, 24, ' fps')
@counter.bitmap.draw_text(100-1, -1, 100, 24, ' fps')
@counter.bitmap.draw_text(100-1, +1, 100, 24, ' fps')
@counter.bitmap.draw_text(100+1, -1, 100, 24, ' fps')
@counter.bitmap.draw_text(100+1, 1, 100, 24, ' fps')
@counter.bitmap.draw_text(100+1, 0, 100, 24, ' fps')
@counter.bitmap.draw_text(100, 1, 100, 24, ' fps')
@counter.bitmap.draw_text(100, -1, 100, 24, ' fps')
@counter.bitmap.draw_text(-1, 0, 100, 24, @fps.to_s ,2)
@counter.bitmap.draw_text(1, 0, 100, 24, @fps.to_s ,2)
@counter.bitmap.draw_text(0, 1, 100, 24, @fps.to_s ,2)
@counter.bitmap.draw_text(1, -1, 100, 24, @fps.to_s ,2)
@counter.bitmap.draw_text(1, -1, 100, 24, @fps.to_s ,2)
@counter.bitmap.draw_text(1, 1, 100, 24, @fps.to_s ,2)
@counter.bitmap.draw_text(-1, -1, 100, 24, @fps.to_s ,2)
@counter.bitmap.draw_text(-1, 1, 100, 24, @fps.to_s ,2)
@counter.bitmap.font.color = Color.new(255,255,255)
@counter.bitmap.draw_text(100, 0, 100, 24, ' fps')
@counter.bitmap.draw_text(0, 0, 100, 24, @fps.to_s ,2)#+ " / " +
#@frame_rate.to_s, 2)
end
def update
# If F5 is pressed, toggle FPS display
if Input.trigger?(TOGGLE)
@on = !@on
@counter.visible = @on
end
(@frame_count = 0; return) if !@on
if @start == nil
@start = Time.now
@frame_count = 0
end
# Get time elapsed in seconds
cur_time = Time.now - @start
if cur_time >= 1
# Get Frames per second
fps = (@frame_count / cur_time).to_i
# If during check interval and the Frame Rate has changed
if @fps != fps
@fps = fps
refresh
end
@start = nil
end
end
def dispose
@counter.dispose; @counter = nil
end
end
module Graphics
class << self
alias :new_upd_fps :update unless method_defined?(:new_upd_fps)
end
def self.update
self.new_upd_fps
$fps = FPS_Display.new if $fps == nil
#return if $fps == nil
$fps.frame_count += 1
$fps.update
end
end