Class: RBGL::GUI::Window::RenderLoop

Inherits:
Object
  • Object
show all
Defined in:
lib/rbgl/gui/window/render_loop.rb

Constant Summary collapse

DEFAULT_TIME_SOURCE =
-> { Process.clock_gettime(Process::CLOCK_MONOTONIC) }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(time_source: DEFAULT_TIME_SOURCE) ⇒ RenderLoop

Returns a new instance of RenderLoop.



11
12
13
14
15
16
# File 'lib/rbgl/gui/window/render_loop.rb', line 11

def initialize(time_source: DEFAULT_TIME_SOURCE)
  @time_source = time_source
  @fps = 0
  @dropped_frames = 0
  @running = false
end

Instance Attribute Details

#dropped_framesObject (readonly)

Returns the value of attribute dropped_frames.



9
10
11
# File 'lib/rbgl/gui/window/render_loop.rb', line 9

def dropped_frames
  @dropped_frames
end

#fpsObject (readonly)

Returns the value of attribute fps.



9
10
11
# File 'lib/rbgl/gui/window/render_loop.rb', line 9

def fps
  @fps
end

Instance Method Details

#record_present_result(result) ⇒ Object



41
42
43
44
# File 'lib/rbgl/gui/window/render_loop.rb', line 41

def record_present_result(result)
  @dropped_frames += 1 if result == false
  result
end

#run(backend:, context:, process_events:, &frame_callback) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/rbgl/gui/window/render_loop.rb', line 18

def run(backend:, context:, process_events:, &frame_callback)
  @running = true
  start_time = @time_source.call
  last_time = start_time
  frame_count = 0

  while @running && !backend.should_close?
    current_time = @time_source.call
    delta_time = current_time - last_time
    last_time = current_time

    process_events.call
    break if stop_requested?(backend)
    frame_callback&.call(context, delta_time)
    break if stop_requested?(backend)
    record_present_result(backend.present(context.framebuffer))

    frame_count += 1
    elapsed = current_time - start_time
    @fps = frame_count / elapsed if elapsed.positive?
  end
end

#stopObject



46
47
48
# File 'lib/rbgl/gui/window/render_loop.rb', line 46

def stop
  @running = false
end