Class: SFML::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/sfml/window/window.rb

Overview

A bare window with input + an OpenGL context, without SFML’s 2D batcher. Use this when you want raw OpenGL (or another rendering library) and just need SFML to manage the platform-level window and event loop. For 2D drawing with SFML’s API, you want SFML::RenderWindow instead.

win = SFML::Window.new(800, 600, "GL")
while win.open?
  win.each_event do |event|
    case event
    in {type: :closed} then win.close
    in {type: :key_pressed, code: :escape} then win.close
    else
    end
  end

  # ... draw with raw OpenGL calls here ...

  win.display
end

Constant Summary collapse

DEFAULT_STYLE =
C::Window::Style::DEFAULT

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **opts) ⇒ Window

Returns a new instance of Window.

Raises:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/sfml/window/window.rb', line 25

def initialize(*args, **opts)
  mode, title = parse_args(args)
  style = opts.fetch(:style, DEFAULT_STYLE)
  state = opts[:fullscreen] ? :fullscreen : :windowed

  ptr = C::Window.sfWindow_create(
    mode.to_native,
    title.to_s,
    style,
    C::Window::State[state],
    nil,
  )
  raise Error, "sfWindow_create returned NULL" if ptr.null?

  @handle       = FFI::AutoPointer.new(ptr, C::Window.method(:sfWindow_destroy))
  @event_buffer = C::Window::Event.new

  self.framerate_limit = opts[:framerate] if opts[:framerate]
  self.vsync = opts[:vsync]               unless opts[:vsync].nil?
end

Instance Attribute Details

#handleObject (readonly)

:nodoc:



132
133
134
# File 'lib/sfml/window/window.rb', line 132

def handle
  @handle
end

Instance Method Details

#active=(value) ⇒ Object

Make this window’s GL context current on the calling thread. Useful when juggling multiple windows / off-screen contexts.



128
129
130
# File 'lib/sfml/window/window.rb', line 128

def active=(value)
  C::Window.sfWindow_setActive(@handle, value ? true : false)
end

#closeObject



50
51
52
53
# File 'lib/sfml/window/window.rb', line 50

def close
  C::Window.sfWindow_close(@handle)
  self
end

#displayObject



55
56
57
58
# File 'lib/sfml/window/window.rb', line 55

def display
  C::Window.sfWindow_display(@handle)
  self
end

#each_eventObject



66
67
68
69
70
71
72
# File 'lib/sfml/window/window.rb', line 66

def each_event
  return enum_for(:each_event) unless block_given?
  while (event = poll_event)
    yield event
  end
  self
end

#focused?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/sfml/window/window.rb', line 122

def focused?
  C::Window.sfWindow_hasFocus(@handle)
end

#framerate_limit=(value) ⇒ Object



106
107
108
# File 'lib/sfml/window/window.rb', line 106

def framerate_limit=(value)
  C::Window.sfWindow_setFramerateLimit(@handle, Integer(value))
end

#key_repeat_enabled=(value) ⇒ Object



114
115
116
# File 'lib/sfml/window/window.rb', line 114

def key_repeat_enabled=(value)
  C::Window.sfWindow_setKeyRepeatEnabled(@handle, value ? true : false)
end

#open?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/sfml/window/window.rb', line 46

def open?
  C::Window.sfWindow_isOpen(@handle)
end

#poll_eventObject

Returns the next pending Event or nil. Same shape as RenderWindow#poll_event.



61
62
63
64
# File 'lib/sfml/window/window.rb', line 61

def poll_event
  return nil unless C::Window.sfWindow_pollEvent(@handle, @event_buffer)
  Event.from_native(@event_buffer)
end

#positionObject



90
91
92
93
# File 'lib/sfml/window/window.rb', line 90

def position
  v = C::Window.sfWindow_getPosition(@handle)
  Vector2.new(v[:x], v[:y])
end

#position=(value) ⇒ Object



95
96
97
98
99
100
# File 'lib/sfml/window/window.rb', line 95

def position=(value)
  vec = value.is_a?(Vector2) ? value : Vector2.new(*value)
  v = C::System::Vector2i.new
  v[:x] = Integer(vec.x); v[:y] = Integer(vec.y)
  C::Window.sfWindow_setPosition(@handle, v)
end

#request_focusObject



118
119
120
# File 'lib/sfml/window/window.rb', line 118

def request_focus
  C::Window.sfWindow_requestFocus(@handle)
end

#sizeObject



78
79
80
81
# File 'lib/sfml/window/window.rb', line 78

def size
  v = C::Window.sfWindow_getSize(@handle)
  Vector2.new(v[:x], v[:y])
end

#size=(value) ⇒ Object



83
84
85
86
87
88
# File 'lib/sfml/window/window.rb', line 83

def size=(value)
  vec = value.is_a?(Vector2) ? value : Vector2.new(*value)
  v = C::System::Vector2u.new
  v[:x] = Integer(vec.x); v[:y] = Integer(vec.y)
  C::Window.sfWindow_setSize(@handle, v)
end

#title=(value) ⇒ Object



74
75
76
# File 'lib/sfml/window/window.rb', line 74

def title=(value)
  C::Window.sfWindow_setTitle(@handle, value.to_s)
end

#visible=(value) ⇒ Object



102
103
104
# File 'lib/sfml/window/window.rb', line 102

def visible=(value)
  C::Window.sfWindow_setVisible(@handle, value ? true : false)
end

#vsync=(enabled) ⇒ Object



110
111
112
# File 'lib/sfml/window/window.rb', line 110

def vsync=(enabled)
  C::Window.sfWindow_setVerticalSyncEnabled(@handle, enabled ? true : false)
end