Class: Stagecraft::App

Inherits:
Object
  • Object
show all
Defined in:
lib/stagecraft/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title: "Stagecraft", width: 800, height: 600, msaa: 4, window: :sdl3, renderer: nil, resizable: true) ⇒ App

Returns a new instance of App.



7
8
9
10
11
12
13
14
# File 'lib/stagecraft/app.rb', line 7

def initialize(title: "Stagecraft", width: 800, height: 600, msaa: 4,
               window: :sdl3, renderer: nil, resizable: true)
  @window = build_window(window, title:, width:, height:, resizable:)
  @renderer = renderer || Renderer.new(window: @window, width:, height:, msaa:)
rescue StandardError
  @window&.close
  raise
end

Instance Attribute Details

#rendererObject (readonly)

Returns the value of attribute renderer.



5
6
7
# File 'lib/stagecraft/app.rb', line 5

def renderer
  @renderer
end

#windowObject (readonly)

Returns the value of attribute window.



5
6
7
# File 'lib/stagecraft/app.rb', line 5

def window
  @window
end

Instance Method Details

#aspectObject



16
17
18
19
# File 'lib/stagecraft/app.rb', line 16

def aspect
  width, height = window.drawable_size
  height.zero? ? 1.0 : width.to_f / height
end

#runObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/stagecraft/app.rb', line 21

def run
  raise ArgumentError, "App#run requires a block" unless block_given?

  previous = monotonic_time
  loop do
    window.poll_events
    break if window.should_close?

    now = monotonic_time
    dt = (now - previous).clamp(0.0, 0.1)
    previous = now
    yield(dt)
    break if ENV["STAGECRAFT_SMOKE"] == "1"
  end
  self
ensure
  begin
    renderer.dispose
  ensure
    window.close
  end
end