class Roglew::RenderContext

Public Class Methods

current() click to toggle source
# File lib/roglew/render_context.rb, line 6
def current
  stack.empty? ? nil : peek.last
end
new(rh) click to toggle source
# File lib/roglew/render_context.rb, line 20
def initialize(rh)
  @rh = rh

  c = singleton_class
  @rh.loaded_extensions.each do |ext|
    next unless Object.const_defined?(ext)
    mod = Object.const_get(ext)
    next unless mod.const_defined?(:RenderContext)
    c.send(:include, mod.const_get(:RenderContext))
  end
end

Private Class Methods

peek() click to toggle source
# File lib/roglew/render_context.rb, line 15
def peek
  stack.last
end
stack() click to toggle source
# File lib/roglew/render_context.rb, line 11
def stack
  @stack ||= RenderHandle.send(:stack)
end

Public Instance Methods

bind() click to toggle source
# File lib/roglew/render_context.rb, line 32
def bind
  return unless current?
  @rh.class.send(:push, @rh, self)
  @rh.send(:make_current)
  nil
end
blend_func(src, dst) click to toggle source
# File lib/roglew/render_context.rb, line 40
def blend_func(src, dst)
  @rh.glBlendFunc(src, dst)
end
clear(*flags) click to toggle source
# File lib/roglew/render_context.rb, line 45
def clear(*flags)
  @rh.glClear(flags
    .map { |f| f.is_a?(Integer) ? f : GL.const_get("#{f.to_s.upcase}_BUFFER_BIT") }
    .reduce(&:|))
end
clear_color() click to toggle source
# File lib/roglew/render_context.rb, line 52
def clear_color
  get_floats(GL::COLOR_CLEAR_VALUE, 4)
end
clear_color=(c) click to toggle source
# File lib/roglew/render_context.rb, line 57
def clear_color=(c)
  @rh.glClearColor(*c)
end
clear_depth=(v) click to toggle source
# File lib/roglew/render_context.rb, line 62
def clear_depth=(v)
  @rh.glClearDepth(v)
end
clear_stencil=(v) click to toggle source
# File lib/roglew/render_context.rb, line 67
def clear_stencil=(v)
  @rh.glClearStencil(v)
end
color_mask(r, g, b, a) click to toggle source
# File lib/roglew/render_context.rb, line 72
def color_mask(r, g, b, a)
  @rh.glColorMask(r, g, b, a)
end
create_texture2d(*args) click to toggle source
# File lib/roglew/render_context.rb, line 78
def create_texture2d(*args)
  Roglew::Texture2d.new(@rh, *args)
end
current?() click to toggle source
# File lib/roglew/render_context.rb, line 82
def current?
  RenderContext.current == self
end
disable(*caps) { || ... } click to toggle source
# File lib/roglew/render_context.rb, line 87
def disable(*caps)
  attribs = @rh.instance_variable_get(:@attribs)
  a1 = attribs.dup

  caps = Set[*caps] & attribs
  caps.each { |cap| @rh.glDisable(cap) }
  attribs.subtract(caps)
  return unless block_given?
  yield

  disable *(attribs - a1)
  enable  *(a1 - attribs)
end
enable(*caps) { || ... } click to toggle source
# File lib/roglew/render_context.rb, line 102
def enable(*caps)
  attribs = @rh.instance_variable_get(:@attribs)
  a1 = attribs.dup

  caps = Set[*caps] - attribs
  caps.each { |cap| @rh.glEnable(cap) }
  attribs.merge(caps)
  return unless block_given?
  yield

  disable *(attribs - a1)
  enable  *(a1 - attribs)
end
finished() click to toggle source
# File lib/roglew/render_context.rb, line 117
def finished
  stack = RenderContext.send(:stack)
  stack.pop
  if stack.empty?
    @rh.send(:remove_current)
  elsif stack.last.last != self
    stack.last.first.send(:make_current)
  end
  nil
end
get_errors() click to toggle source
# File lib/roglew/render_context.rb, line 129
def get_errors
  errors = []
  while (error = @rh.glGetError) != 0
    errors << GL::ERROR[error] || error
  end
  errors
end
get_floats(pname, count = 1) click to toggle source
# File lib/roglew/render_context.rb, line 138
def get_floats(pname, count = 1)
  p = FFI::MemoryPointer.new(:float, count)
  @rh.glGetFloatv(pname, p)
  result = p.read_array_of_float(count)
  count == 1 ? result[0] : result
end
get_function(function_name, parameters, return_type) click to toggle source
# File lib/roglew/render_context.rb, line 146
def get_function(function_name, parameters, return_type)
  ptr = get_proc_address(function_name.to_s)
  if ptr.null?
    LOGGER.warn "couldn't find function: #{return_type} #{function_name}(#{parameters.join(', ')})"
    return
  end
  return_type = GL.find_type(return_type) || return_type
  parameters = parameters.map { |p| GL.find_type(p) || p }
  FFI::Function.new(return_type, parameters, ptr, convention: :stdcall)
end
get_integers(pname, count = 1) click to toggle source
# File lib/roglew/render_context.rb, line 158
def get_integers(pname, count = 1)
  p = FFI::MemoryPointer.new(:int, count)
  @rh.glGetIntegerv(pname, p)
  result = p.read_array_of_int(count)
  count == 1 ? result[0] : result
end
get_proc_address(function_name) click to toggle source
# File lib/roglew/render_context.rb, line 166
def get_proc_address(function_name)
  @rh.send(:get_proc_address, function_name)
end
handle()
Alias for: render_handle
num_extensions() click to toggle source
# File lib/roglew/render_context.rb, line 170
def num_extensions
  get_integers(GL::NUM_EXTENSIONS)
end
render_handle() click to toggle source
# File lib/roglew/render_context.rb, line 174
def render_handle
  @rh
end
Also aliased as: handle, rh
rh()
Alias for: render_handle
swap_buffers() click to toggle source
# File lib/roglew/render_context.rb, line 179
def swap_buffers
  @rh.send(:swap_buffers)
end
tex_parameter(target, pname, *params) click to toggle source
# File lib/roglew/render_context.rb, line 184
def tex_parameter(target, pname, *params)
  params.flatten!
  type = params.all? { |param| param.is_a? Integer } ? 'int' : 'float'
  ptr = FFI::MemoryPointer.new(type.to_sym, params.length)
  ptr.send("write_array_of_#{type}", params)
  @rh.send("glTexParameter#{type[0]}v", target, pname, ptr)
end