Module: SDL2::Library
Overview
Extensions to FFI::Library
Instance Method Summary collapse
-
#api(func_name, args, type, options = {}) ⇒ Object
This converts the SDL Function Prototype name “SDL_XxxYyyyyZzz” to ruby’s “xxx_yyyy_zzz” convetion.
-
#boolean?(methodName, filter = nil) ⇒ Boolean
Generates an alternative ? version for methodName.
-
#metaclass ⇒ Object
Returns the ‘singleton class’ so we can define class-level methods on the fly.
-
#raise_error ⇒ Object
Raise the current error value as a RuntimeException.
-
#raise_error_if(condition) ⇒ Object
Conditionally raise an error, unless false.
-
#raise_error_unless(condition) ⇒ Object
Conditionally raise an error, unless true.
-
#returns_error(methodName, filter) ⇒ Object
Generates an alternative version of methodName that will raise a SDL Error when the return value fails the filter test.
Instance Method Details
#api(func_name, args, type, options = {}) ⇒ Object
This converts the SDL Function Prototype name “SDL_XxxYyyyyZzz” to ruby’s “xxx_yyyy_zzz” convetion
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 |
# File 'lib/sdl2/library.rb', line 10 def api(func_name, args, type, = {}) # TODO: Review ugly hack: remove_part = case self.to_s when "SDL2" "SDL_" when "SDL2::Image" "IMG_" when "SDL2::TTF" "TTF_" when "SDL2::Mixer" "Mix_" else $stderr.puts("Library#api does not know how to handle module: #{self.to_s}") /[A-Z][A-Z|0-9]*_/ end = { :error => false, :filter => type == :bool ? OK_WHEN_TRUE : OK_WHEN_ZERO, :method_name => ActiveSupport::Inflector.underscore(func_name.to_s.gsub(remove_part,'')).to_sym, }.merge() methodName = [:method_name] attach_function methodName, func_name, args, type .instance_eval do alias_method func_name, methodName end alias_method func_name, methodName if [:error] returns_error(methodName, [:filter]) end if type == :bool boolean?(methodName) end return methodName end |
#boolean?(methodName, filter = nil) ⇒ Boolean
Generates an alternative ? version for methodName.
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/sdl2/library.rb', line 77 def boolean?(methodName, filter = nil) .instance_eval do if filter.nil? alias_method "#{methodName}?".to_sym, methodName else define_method("#{methodName}?".to_sym) do |*args| filter.call(send(methodName, *args)) end end end end |
#metaclass ⇒ Object
Returns the ‘singleton class’ so we can define class-level methods on the fly. There may be a better place to put this.
57 58 59 60 |
# File 'lib/sdl2/library.rb', line 57 def class << self; self; end end |
#raise_error ⇒ Object
Raise the current error value as a RuntimeException
90 91 92 |
# File 'lib/sdl2/library.rb', line 90 def raise_error raise "SDL Error: #{SDL2.get_error()}" end |
#raise_error_if(condition) ⇒ Object
Conditionally raise an error, unless false
100 101 102 |
# File 'lib/sdl2/library.rb', line 100 def raise_error_if(condition) raise_error if condition end |
#raise_error_unless(condition) ⇒ Object
Conditionally raise an error, unless true
95 96 97 |
# File 'lib/sdl2/library.rb', line 95 def raise_error_unless(condition) raise_error unless condition end |
#returns_error(methodName, filter) ⇒ Object
Generates an alternative version of methodName that will raise a SDL Error when the return value fails the filter test. The alternative version has the same name, but with an exclamation mark (“!”) at the end, indicating the danger.
66 67 68 69 70 71 72 73 74 |
# File 'lib/sdl2/library.rb', line 66 def returns_error(methodName, filter) .instance_eval do define_method "#{methodName}!".to_sym do |*args| result = send(methodName, *args) raise_error_unless filter.call(result) result end end end |