Class: SFML::Game
- Inherits:
-
Object
- Object
- SFML::Game
- Defined in:
- lib/sfml/game.rb
Overview
Subclass-friendly main loop. Removes the boilerplate of window creation, event pumping, clock management, and clear/display so a small game fits in a few methods.
class MyGame < SFML::Game
def setup
@ball = SFML::CircleShape.new(radius: 30, position: [200, 200],
fill_color: SFML::Color.white)
end
def update(dt)
@ball.move([60 * dt.as_seconds, 30 * dt.as_seconds])
end
def draw
window.draw(@ball)
end
end
MyGame.new(title: "Hello").run
The loop auto-handles :closed and :key_pressed/:escape by calling #quit; everything else is forwarded to #on_event. Override that to handle keys, mouse, etc.
Instance Attribute Summary collapse
-
#background_color ⇒ Object
Returns the value of attribute background_color.
-
#window ⇒ Object
readonly
Returns the value of attribute window.
Instance Method Summary collapse
-
#draw ⇒ Object
Called every frame after #update.
- #height ⇒ Object
-
#initialize(width: 800, height: 600, title: nil, framerate: 60, vsync: nil, background: Color::BLACK, style: nil, fullscreen: false) ⇒ Game
constructor
A new instance of Game.
-
#on_event(event) ⇒ Object
Called for events the framework didn’t auto-handle (everything except :closed and the Esc key).
-
#quit ⇒ Object
Close the window.
-
#run ⇒ Object
The main entry point.
-
#setup ⇒ Object
Called once, just before the first frame.
-
#teardown ⇒ Object
Called once after the window closes.
-
#update(dt) ⇒ Object
Called every frame.
-
#width ⇒ Object
Width and height shortcuts that always reflect the current window size, which matters once the user is allowed to resize.
Constructor Details
#initialize(width: 800, height: 600, title: nil, framerate: 60, vsync: nil, background: Color::BLACK, style: nil, fullscreen: false) ⇒ Game
Returns a new instance of Game.
30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/sfml/game.rb', line 30 def initialize( width: 800, height: 600, title: nil, framerate: 60, vsync: nil, background: Color::BLACK, style: nil, fullscreen: false ) window_opts = { framerate: framerate, fullscreen: fullscreen } window_opts[:vsync] = vsync unless vsync.nil? window_opts[:style] = style unless style.nil? @window = RenderWindow.new(width, height, title || self.class.name, **window_opts) @background_color = background end |
Instance Attribute Details
#background_color ⇒ Object
Returns the value of attribute background_color.
28 29 30 |
# File 'lib/sfml/game.rb', line 28 def background_color @background_color end |
#window ⇒ Object (readonly)
Returns the value of attribute window.
27 28 29 |
# File 'lib/sfml/game.rb', line 27 def window @window end |
Instance Method Details
#draw ⇒ Object
Called every frame after #update. Use window.draw(…) for any drawables.
81 |
# File 'lib/sfml/game.rb', line 81 def draw; end |
#height ⇒ Object
47 |
# File 'lib/sfml/game.rb', line 47 def height = @window.size.y |
#on_event(event) ⇒ Object
Called for events the framework didn’t auto-handle (everything except :closed and the Esc key). Call #quit from here to exit on a custom key.
85 |
# File 'lib/sfml/game.rb', line 85 def on_event(event); end |
#quit ⇒ Object
Close the window. The main loop exits at the start of the next frame.
50 51 52 53 |
# File 'lib/sfml/game.rb', line 50 def quit @window.close self end |
#run ⇒ Object
The main entry point. Calls #setup once, then runs the per-frame loop until the window closes.
57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sfml/game.rb', line 57 def run setup clock = Clock.new while @window.open? dt = clock.restart @window.each_event { |event| _dispatch(event) } update(dt) @window.clear(@background_color) draw @window.display end teardown self end |
#setup ⇒ Object
Called once, just before the first frame. Build initial state here.
75 |
# File 'lib/sfml/game.rb', line 75 def setup; end |
#teardown ⇒ Object
Called once after the window closes. Useful for saving state. Default is a no-op.
89 |
# File 'lib/sfml/game.rb', line 89 def teardown; end |
#update(dt) ⇒ Object
Called every frame. ‘dt` is a SFML::Time covering the previous frame.
78 |
# File 'lib/sfml/game.rb', line 78 def update(dt); end |
#width ⇒ Object
Width and height shortcuts that always reflect the current window size, which matters once the user is allowed to resize.
46 |
# File 'lib/sfml/game.rb', line 46 def width = @window.size.x |