Class: SFML::Game

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_colorObject

Returns the value of attribute background_color.



28
29
30
# File 'lib/sfml/game.rb', line 28

def background_color
  @background_color
end

#windowObject (readonly)

Returns the value of attribute window.



27
28
29
# File 'lib/sfml/game.rb', line 27

def window
  @window
end

Instance Method Details

#drawObject

Called every frame after #update. Use window.draw(…) for any drawables.



81
# File 'lib/sfml/game.rb', line 81

def draw; end

#heightObject



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

#quitObject

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

#runObject

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

#setupObject

Called once, just before the first frame. Build initial state here.



75
# File 'lib/sfml/game.rb', line 75

def setup; end

#teardownObject

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

#widthObject

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