-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.rb
84 lines (63 loc) · 2.03 KB
/
utils.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# allows default arguments for attr_accessor
class Module
def attr_pp(*argv, &block)
defaults = {}
proxy = Object.new
eigen = class << proxy; self; end
eigen.send(:define_method, :method_missing) { |name, *argvt|
defaults[name] = argvt.size > 1 ? argvt : argvt.first
}
proxy.instance_eval(&block)
defaults.each_pair { |name, default_val|
attr_writer(name)
define_method(name) do
class << self; self; end.class_eval do
attr_reader( name )
end
if instance_variable_defined? "@#{name}"
instance_variable_get( "@#{name}" )
else
instance_variable_set( "@#{name}", default_val )
end
end
}
(argv - defaults.keys).each { |name|
raise ArgumentError if !(name.instance_of?(Symbol))
attr_accessor name
}
end
end
# logging and default logging_level & hash arg checking
class Object
def check_args(hash_args, *args)
msg = "Some required hash keys were missing for #{self.class}:"
raise ArgumentError, "#{msg} #{args}" if !hash_args.instance_of?(Hash)
if (hash_args.keys & args).size != args.size then
raise ArgumentError, "#{msg} #{args - hash_args.keys}"
end
end
def message(content, options={})
check_args(options, :log_level)
puts content if logging_level >= options[:log_level]
end
def logging_level
0
end
end
class FPSCounter
attr_reader :fps
def initialize
@current_second = Gosu::milliseconds / 1000
@accum_fps = 0
@fps = 0
end
def register_tick
@accum_fps += 1
current_second = Gosu::milliseconds / 1000
if current_second != @current_second
@current_second = current_second
@fps = @accum_fps
@accum_fps = 0
end
end
end