Class: SFML::Scene
Overview
A chunk of game state with its own lifecycle — menu, gameplay, game-over, settings overlay, etc. The host ‘SFML::App` keeps a `current_scene` and forwards `update` / `draw` / `on_event` / `on_resize` to it. Switch between scenes with `app.switch_to` (or `switch_to` from inside a scene, which delegates).
class TitleScene < SFML::Scene
on_key :enter, :start_game
def setup
@gui = SFML::GUI::App.new(window: window, stylesheet: SHEET)
# ... build UI ...
end
def update(dt) = @gui.update(dt)
def draw = @gui.draw
def start_game
switch_to PlayScene
end
end
class PlayScene < SFML::Scene
# …
end
class MyApp < SFML::App
initial_scene TitleScene # auto-switches to it on app setup
end
‘SFML::Scene` carries its own `on_key` DSL — scene-level bindings shadow app-level bindings while the scene is active.
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
Instance Method Summary collapse
-
#draw ⇒ Object
Called every frame after #update.
- #height ⇒ Object
-
#initialize(app) ⇒ Scene
constructor
A new instance of Scene.
-
#on_event(_event) ⇒ Object
Called for every event the framework didn’t auto-handle (closed, resized, and on_key bindings).
-
#on_resize(_width, _height) ⇒ Object
Window resize.
-
#setup ⇒ Object
Called once when the host app activates this scene (replacing whatever scene was active before).
-
#switch_to(scene_or_class) ⇒ Object
Switch the host app to a new scene from inside this one.
-
#teardown ⇒ Object
Called once when the host app switches away (or when the app loop exits while this scene is active).
-
#update(_dt) ⇒ Object
Called every frame the scene is active and the app isn’t paused.
- #width ⇒ Object
-
#window ⇒ Object
Convenience accessors that match ‘SFML::App`’s.
Methods included from Keybindings
Constructor Details
#initialize(app) ⇒ Scene
Returns a new instance of Scene.
41 42 43 |
# File 'lib/sfml/scene.rb', line 41 def initialize(app) @app = app end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
39 40 41 |
# File 'lib/sfml/scene.rb', line 39 def app @app end |
Instance Method Details
#draw ⇒ Object
Called every frame after #update.
67 |
# File 'lib/sfml/scene.rb', line 67 def draw; end |
#height ⇒ Object
48 |
# File 'lib/sfml/scene.rb', line 48 def height = @app.height |
#on_event(_event) ⇒ Object
Called for every event the framework didn’t auto-handle (closed, resized, and on_key bindings). Override to handle game-specific input patterns.
72 |
# File 'lib/sfml/scene.rb', line 72 def on_event(_event); end |
#on_resize(_width, _height) ⇒ Object
Window resize. Default: no-op.
75 |
# File 'lib/sfml/scene.rb', line 75 def on_resize(_width, _height); end |
#setup ⇒ Object
Called once when the host app activates this scene (replacing whatever scene was active before). Build per-scene state here.
61 |
# File 'lib/sfml/scene.rb', line 61 def setup; end |
#switch_to(scene_or_class) ⇒ Object
Switch the host app to a new scene from inside this one. Accepts a Scene class (instantiated with ‘app`) or an already- constructed Scene instance.
53 54 55 |
# File 'lib/sfml/scene.rb', line 53 def switch_to(scene_or_class) @app.switch_to(scene_or_class) end |
#teardown ⇒ Object
Called once when the host app switches away (or when the app loop exits while this scene is active). Free per-scene resources here.
80 |
# File 'lib/sfml/scene.rb', line 80 def teardown; end |
#update(_dt) ⇒ Object
Called every frame the scene is active and the app isn’t paused.
64 |
# File 'lib/sfml/scene.rb', line 64 def update(_dt); end |
#width ⇒ Object
47 |
# File 'lib/sfml/scene.rb', line 47 def width = @app.width |
#window ⇒ Object
Convenience accessors that match ‘SFML::App`’s.
46 |
# File 'lib/sfml/scene.rb', line 46 def window = @app.window |