Class: Roglew::RenderHandle

Inherits:
Object
  • Object
show all
Defined in:
lib/roglew/render_handle.rb,
lib/roglew/platform/linux/render_handle.rb,
lib/roglew/platform/windows/render_handle.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hdc, version = nil) ⇒ RenderHandle

Returns a new instance of RenderHandle.



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
# File 'lib/roglew/platform/linux/render_handle.rb', line 3

def initialize(display, screen, window, version = nil)
  @display, @screen, @window = display, screen, window
  @loaded_extensions = Set.new

  extension_list(:glx).each { |ext| load_extension(ext) }

  attrList = FFI::MemoryPointer.new(:int, 11)
  attrList.write_array_of_int([
    GLX::RGBA,       GLX::DOUBLEBUFFER,
    GLX::RED_SIZE,   4,
    GLX::GREEN_SIZE, 4,
    GLX::BLUE_SIZE,  4,
    GLX::DEPTH_SIZE, 16
  ])
  @visual = GLX.ChooseVisual(@display, screen, attrList)
  @context = GLX.CreateContext(@display, @visual, nil, true)

  old_context = nil

  bind do
    #check version
    max_version = glGetString(GL::VERSION).split('.', 2).map!(&:to_i)

    #if max OpelGL version is less than requested, give error
    raise ArgumentError, "unsupported version #{version.join('.')}" if version && (max_version <=> version < 0)

    @version = version || max_version
    extension_list(:core).each { |ext| load_extension(ext) }
    old_context, @context = @context, upgrade_context if @version[0] > 2
    extension_list(:gl, :platform).each { |ext| load_extension(ext) }
  end

  GLX.delete_context(display, old_context) if old_context

  self.class.finalize(self, @display, @context)
end

Class Method Details

.currentObject



6
7
8
# File 'lib/roglew/render_handle.rb', line 6

def current
  stack.empty? ? nil : peek.first
end

Instance Method Details

#attribsObject

platform specific implementations:

#initialize
#get_proc_address
#make_current
#remove_current
#swap_buffers


53
54
55
# File 'lib/roglew/render_handle.rb', line 53

def attribs
  @attribs.dup
end

#bindObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/roglew/render_handle.rb', line 57

def bind
  if current?
    ctx = RenderContext.current
  else
    stack = self.class.send(:stack)
    i = stack.rindex { |rh, _| rh == self }
    ctx = i ? stack[i].last : create_context
    make_current
  end
  self.class.send(:push, self, ctx)

  return ctx unless block_given?
  result = yield ctx
  ctx.finished
  result
end

#current?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/roglew/render_handle.rb', line 80

def current?
  self.class.current == self
end

#extension_list(*types) ⇒ Object



74
75
76
77
78
# File 'lib/roglew/render_handle.rb', line 74

def extension_list(*types)
  list = types.flat_map { |type| send("extension_list_#{type}") }
  list.uniq!
  list
end

#load_extension(ext) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/roglew/render_handle.rb', line 84

def load_extension(ext)
  LOGGER.debug "load_extension: #{ext}"
  ext = ext.to_sym
  @loaded_extensions << ext

  unless Object.const_defined?(ext)
    reg = self.class.instance_variable_get(:@registered_extensions)
    filename = reg[ext] || File.expand_path("extensions/#{ext}.rb", __dir__)
    require filename if File.exists?(filename)
  end

  return unless Object.const_defined?(ext)
  mod = Object.const_get(ext)
  if mod.const_defined?(:RenderHandle)
    modrh = mod.const_get(:RenderHandle)
    singleton_class.send(:include, modrh)

    bind do |rc|
      modrh.functions.each do |options, list|
        list.each do |name, parameters, ret_type|
          function = if options.include?(:ffi)
                       if name =~ /^(glX|wgl)/
                         GL.platform_module.attach_function(name[3..-1], name, parameters, ret_type)
                       else
                         GL.attach_function(name[2..-1], name, parameters, ret_type)
                       end
                     else
                       rc.get_function(name, parameters, ret_type)
                     end
          define_singleton_method(name) { |*a| function.(*a) } if function
        end if !options.include?(:compatibility) || ((@version <=> [3, 0]) <= 0)
      end if modrh.respond_to?(:functions)
    end
  end

  nil
end

#loaded_extensionsObject



122
123
124
# File 'lib/roglew/render_handle.rb', line 122

def loaded_extensions
  @loaded_extensions.dup
end

#supports?(extension) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/roglew/render_handle.rb', line 126

def supports?(extension)
  !!@loaded_extensions[extension]
end

#versionObject



130
131
132
# File 'lib/roglew/render_handle.rb', line 130

def version
  @version.dup
end