Class: Rugl::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/rugl/context.rb

Defined Under Namespace

Classes: NoopStateManager

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width: nil, height: nil, title: "Rugl", pixel_ratio: 1.0, debug: true, version: [4, 1], profile: :core, samples: nil, window: nil, gl: nil, glfw: nil, state_manager: nil, auto_init: true, **_opts) ⇒ Context

Returns a new instance of Context.



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
53
54
55
# File 'lib/rugl/context.rb', line 17

def initialize(
  width: nil,
  height: nil,
  title: "Rugl",
  pixel_ratio: 1.0,
  debug: true,
  version: [4, 1],
  profile: :core,
  samples: nil,
  window: nil,
  gl: nil,
  glfw: nil,
  state_manager: nil,
  auto_init: true,
  **_opts
)
  @width = width
  @height = height
  @title = title
  @pixel_ratio = pixel_ratio
  @debug = debug
  @version = version
  @profile = profile
  @samples = samples
  @window = window
  @gl = gl
  @glfw = glfw
  @state_manager = state_manager || default_state_manager
  @resources = []
  @shader_cache = {}
  @scope_stack = []
  @start_time = monotonic_time
  @tick = 0
  @running = false
  @destroyed = false
  @compiler = nil

  setup_default_window if auto_init
end

Instance Attribute Details

#debugObject (readonly)

Returns the value of attribute debug.



15
16
17
# File 'lib/rugl/context.rb', line 15

def debug
  @debug
end

#glObject (readonly)

Returns the value of attribute gl.



15
16
17
# File 'lib/rugl/context.rb', line 15

def gl
  @gl
end

#glfwObject (readonly)

Returns the value of attribute glfw.



15
16
17
# File 'lib/rugl/context.rb', line 15

def glfw
  @glfw
end

#pixel_ratioObject (readonly)

Returns the value of attribute pixel_ratio.



15
16
17
# File 'lib/rugl/context.rb', line 15

def pixel_ratio
  @pixel_ratio
end

#shader_cacheObject (readonly)

Returns the value of attribute shader_cache.



15
16
17
# File 'lib/rugl/context.rb', line 15

def shader_cache
  @shader_cache
end

#state_managerObject (readonly)

Returns the value of attribute state_manager.



15
16
17
# File 'lib/rugl/context.rb', line 15

def state_manager
  @state_manager
end

#tickObject (readonly)

Returns the value of attribute tick.



15
16
17
# File 'lib/rugl/context.rb', line 15

def tick
  @tick
end

#windowObject (readonly)

Returns the value of attribute window.



15
16
17
# File 'lib/rugl/context.rb', line 15

def window
  @window
end

Instance Method Details

#buffer(source) ⇒ Object



221
222
223
# File 'lib/rugl/context.rb', line 221

def buffer(source)
  Resources::Buffer.new(self, source)
end

#call_gl(name, *args) ⇒ Object



241
242
243
244
245
# File 'lib/rugl/context.rb', line 241

def call_gl(name, *args)
  return nil unless @gl

  Util.gl_call(@gl, name, *args)
end

#clear(color: nil, depth: nil, stencil: nil) ⇒ Object



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
# File 'lib/rugl/context.rb', line 85

def clear(color: nil, depth: nil, stencil: nil)
  return unless @gl

  mask = 0

  if color
    r, g, b, a = normalize_color(color)
    gl_call(:ClearColor, r, g, b, a)
    mask |= gl_const(:COLOR_BUFFER_BIT)
  end

  unless depth.nil?
    if gl_respond_to?(:ClearDepth)
      gl_call(:ClearDepth, depth.to_f)
    elsif gl_respond_to?(:ClearDepthf)
      gl_call(:ClearDepthf, depth.to_f)
    end
    mask |= gl_const(:DEPTH_BUFFER_BIT)
  end

  unless stencil.nil?
    gl_call(:ClearStencil, stencil.to_i) if gl_respond_to?(:ClearStencil)
    mask |= gl_const(:STENCIL_BUFFER_BIT)
  end

  gl_call(:Clear, mask) if mask.positive? && gl_respond_to?(:Clear)
  Util.check_gl_error!(gl: @gl, debug: @debug, message: "clear")
  nil
end

#command(opts) ⇒ Object



57
58
59
60
# File 'lib/rugl/context.rb', line 57

def command(opts)
  @compiler ||= CommandCompiler.new(self)
  @compiler.compile(opts)
end

#current_contextObject



193
194
195
# File 'lib/rugl/context.rb', line 193

def current_context
  build_context
end

#current_scope_propsObject



205
206
207
208
209
# File 'lib/rugl/context.rb', line 205

def current_scope_props
  @scope_stack.each_with_object({}) do |entry, merged|
    merged.merge!(entry[:props])
  end
end

#destroyObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rugl/context.rb', line 136

def destroy
  return if @destroyed

  @resources.dup.each do |resource|
    next if !resource.respond_to?(:destroyed?) || resource.destroyed?

    warn "[Rugl] Resource leak detected: #{resource.class}##{resource.respond_to?(:id) ? resource.id : "unknown"}" # rubocop:disable Style/GlobalStdStream
    resource.destroy if resource.respond_to?(:destroy)
  end

  if @window && @glfw
    glfw_call(:DestroyWindow, @window) if glfw_respond_to?(:DestroyWindow)
  end
  glfw_call(:Terminate) if @glfw && glfw_respond_to?(:Terminate)

  @destroyed = true
  @window = nil
  @running = false
  nil
end

#destroyed?Boolean

Returns:

  • (Boolean)


157
158
159
# File 'lib/rugl/context.rb', line 157

def destroyed?
  @destroyed
end

#elements(source) ⇒ Object



225
226
227
# File 'lib/rugl/context.rb', line 225

def elements(source)
  Resources::Elements.new(self, source)
end

#frame(max_frames: nil, &block) ⇒ Object

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rugl/context.rb', line 62

def frame(max_frames: nil, &block)
  raise ArgumentError, "frame requires a block" unless block
  raise ContextError, "Context already destroyed" if destroyed?

  cancel = -> { @running = false }
  @running = true
  frames = 0
  frame_limit = max_frames || default_frame_limit

  while @running
    break if frame_limit && frames >= frame_limit
    break if window_should_close?

    poll_events
    @tick += 1
    block.call(build_context)
    swap_buffers
    frames += 1
  end

  cancel
end

#framebuffer(opts) ⇒ Object



237
238
239
# File 'lib/rugl/context.rb', line 237

def framebuffer(opts)
  Resources::Framebuffer.new(self, opts)
end

#gl_constant(name) ⇒ Object



254
255
256
# File 'lib/rugl/context.rb', line 254

def gl_constant(name)
  Util.gl_const(name, gl: @gl)
end

#gl_supports?(name) ⇒ Boolean

Returns:

  • (Boolean)


247
248
249
250
251
252
# File 'lib/rugl/context.rb', line 247

def gl_supports?(name)
  return false unless @gl

  snake_name = Util.underscore(name)
  @gl.respond_to?(name) || @gl.respond_to?(snake_name)
end

#has_extension?(name) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rugl/context.rb', line 170

def has_extension?(name)
  return false if @gl.nil?

  extension_name = name.to_s
  return false if extension_name.empty?

  if gl_respond_to?(:GetStringi)
    count = query_integer(:NUM_EXTENSIONS)
    return false if count.nil?

    count.times do |index|
      ext = gl_call(:GetStringi, gl_const(:EXTENSIONS), index)
      return true if ext.to_s == extension_name
    end
    return false
  end

  return false unless gl_respond_to?(:GetString)

  all_extensions = gl_call(:GetString, gl_const(:EXTENSIONS)).to_s.split
  all_extensions.include?(extension_name)
end

#limitsObject



161
162
163
164
165
166
167
168
# File 'lib/rugl/context.rb', line 161

def limits
  {
    max_texture_size: query_integer(:MAX_TEXTURE_SIZE),
    max_viewport_dims: query_integer_array(:MAX_VIEWPORT_DIMS, size: 2),
    max_vertex_attribs: query_integer(:MAX_VERTEX_ATTRIBS),
    version: query_version
  }
end

#pop_scopeObject



201
202
203
# File 'lib/rugl/context.rb', line 201

def pop_scope
  @scope_stack.pop
end

#push_scope(state:, props:) ⇒ Object



197
198
199
# File 'lib/rugl/context.rb', line 197

def push_scope(state:, props:)
  @scope_stack << { state: state || {}, props: props || {} }
end

#read(x: 0, y: 0, width: nil, height: nil) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rugl/context.rb', line 115

def read(x: 0, y: 0, width: nil, height: nil)
  read_width, read_height = width_height_or_framebuffer(width, height)
  byte_length = [read_width * read_height * 4, 0].max
  buffer = "\0" * byte_length
  return buffer if @gl.nil?

  return buffer unless gl_respond_to?(:ReadPixels)

  gl_call(
    :ReadPixels,
    x.to_i,
    y.to_i,
    read_width,
    read_height,
    gl_const(:RGBA),
    gl_const(:UNSIGNED_BYTE),
    buffer
  )
  buffer
end

#register_resource(resource) ⇒ Object



211
212
213
214
# File 'lib/rugl/context.rb', line 211

def register_resource(resource)
  @resources << resource
  resource
end

#renderbuffer(opts) ⇒ Object



233
234
235
# File 'lib/rugl/context.rb', line 233

def renderbuffer(opts)
  Resources::Renderbuffer.new(self, opts)
end

#texture(source = {}) ⇒ Object



229
230
231
# File 'lib/rugl/context.rb', line 229

def texture(source = {})
  Resources::Texture.new(self, source)
end

#unregister_resource(resource) ⇒ Object



216
217
218
219
# File 'lib/rugl/context.rb', line 216

def unregister_resource(resource)
  @resources.delete(resource)
  resource
end