#!ruby # -*- mode: ruby; coding: utf-8 -*- # Last updated: <2016/12/11 15:49:55 +0900> # # Gosuの動作確認 # フレームレートの測定をしてみる。 # # 動作確認環境: # Windows10 x64 + Ruby 2.2.6 p396 mingw32 + Gosu 0.10.8 x86-mingw32 require 'gosu' # フレームレートを測定するクラス class FrameRate attr_reader :framerate def initialize @cnt = 0 @framerate = 0 @start_time = Time.now end # 毎フレーム呼ぶ def update @cnt += 1 n = Time.now nd = n - @start_time return if nd < 1.0 @framerate = @cnt / nd @start_time = n @cnt = 0 end end # メインウインドウ class MyWindow < Gosu::Window # コンストラクタ def initialize super 640, 480, false self.caption = 'Sprite Test' @font = Gosu::Font.new(20) @frate = FrameRate.new end # 更新処理 # def update # end # 描画処理 def draw @frate.update f = @frate.framerate.to_s @font.draw("FPS #{f}", 10, 10, 0) end # キーボードチェック def button_down(id) # ESCキーが押されたらウインドウを閉じて終了する # Gosu::Window.close() を呼ぶとウインドウが閉じる close if id == Gosu::KbEscape end end window = MyWindow.new window.show