module SDL2::Library

Extensions to FFI::Library

Public Instance Methods

api(func_name, args, type, options = {}) click to toggle source

This converts the SDL Function Prototype name “SDL_XxxYyyyyZzz” to ruby’s “xxx_yyyy_zzz” convetion

# File lib/sdl2/library.rb, line 10
def api(func_name, args, type, options = {})

  
  # 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

  options = {
    :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(options)

  methodName = options[:method_name]

  attach_function methodName, func_name, args, type
        
  metaclass.instance_eval do
    alias_method func_name, methodName
  end
  alias_method func_name, methodName

  if options[:error]
    returns_error(methodName, options[:filter])
  end

  if type == :bool
    boolean?(methodName)
  end

  return methodName
end
boolean?(methodName, filter = nil) click to toggle source

Generates an alternative ? version for methodName.

# File lib/sdl2/library.rb, line 77
def boolean?(methodName, filter = nil)
  metaclass.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() click to toggle source

Returns the ‘singleton class’ so we can define class-level methods on the fly. There may be a better place to put this.

# File lib/sdl2/library.rb, line 57
def metaclass

  class << self; self; end
end
raise_error() click to toggle source

Raise the current error value as a RuntimeException

# File lib/sdl2/library.rb, line 90
def raise_error
  raise "SDL Error: #{SDL2.get_error()}"
end
raise_error_if(condition) click to toggle source

Conditionally raise an error, unless false

# File lib/sdl2/library.rb, line 100
def raise_error_if(condition)
  raise_error if condition
end
raise_error_unless(condition) click to toggle source

Conditionally raise an error, unless true

# File lib/sdl2/library.rb, line 95
def raise_error_unless(condition)
  raise_error unless condition
end
returns_error(methodName, filter) click to toggle source

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.

# File lib/sdl2/library.rb, line 66
def returns_error(methodName, filter)
  metaclass.instance_eval do
    define_method "#{methodName}!".to_sym do |*args|
      result = send(methodName, *args)
      raise_error_unless filter.call(result)
      result
    end
  end
end