Module: SDL2::Library

Included in:
SDL2, Image
Defined in:
lib/sdl2/library.rb

Overview

Extensions to FFI::Library

Instance Method Summary collapse

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
# File 'lib/sdl2/library.rb', line 10

def api(func_name, args, type, options = {})

  options = {
    :error => false,
    :filter => type == :bool ? TRUE_WHEN_TRUE : TRUE_WHEN_ZERO
  }.merge(options)

  # TODO: Review ugly hack:
  remove_part = case self.to_s
  when "SDL2"
    "SDL_"
  when "SDL2::Image"
    "IMG_"
  when "SDL2::TTF"
    "TTF_"
  else
    $stderr.puts("Library#api does not know how to handle module: #{self.to_s}")
    /[A-Z][A-Z|0-9]*_/
  end

  camelCaseName = func_name.to_s.gsub(remove_part,'')
  methodName = ActiveSupport::Inflector.underscore(camelCaseName).to_sym

  attach_function methodName, func_name, args, type

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

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

  return methodName
end

#boolean?(methodName, filter = nil) ⇒ Boolean

Generates an alternative ? version for methodName.

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/sdl2/library.rb', line 69

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

#metaclassObject

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



49
50
51
52
# File 'lib/sdl2/library.rb', line 49

def metaclass

  class << self; self; end
end

#raise_errorObject

Raise the current error value as a RuntimeException



82
83
84
# File 'lib/sdl2/library.rb', line 82

def raise_error
  raise "SDL Error: #{SDL2.get_error()}"
end

#raise_error_if(condition) ⇒ Object

Conditionally raise an error, unless false



92
93
94
# File 'lib/sdl2/library.rb', line 92

def raise_error_if(condition)
  raise_error if condition
end

#raise_error_unless(condition) ⇒ Object

Conditionally raise an error, unless true



87
88
89
# File 'lib/sdl2/library.rb', line 87

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.



58
59
60
61
62
63
64
65
66
# File 'lib/sdl2/library.rb', line 58

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