Class: SFML::View

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

Overview

A 2D camera. A View defines what part of the world is visible in the window: a centre point, a size in world units, an optional rotation, and a viewport rectangle (in normalised window coords [0,1]) that places the view inside the window — useful for split-screen or for rendering a mini-map alongside the main camera.

Two flows you’ll typically use:

# 1. Define from a centre + size:
camera = SFML::View.new(center: [400, 300], size: [800, 600])

# 2. Define from a world rectangle (top-left + size):
camera = SFML::View.from_rect(SFML::Rect.new([0, 0], [1600, 1200]))

window.view = camera         # apply
window.draw(stuff)           # everything draws through the camera
window.view = window.default_view   # restore the 1:1 default

All coordinate inputs accept either a SFML::Vector2 or a [x, y] array.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(center: nil, size: nil, rotation: nil, viewport: nil, _handle: nil) ⇒ View

Returns a new instance of View.

Raises:



22
23
24
25
26
27
28
29
30
31
# File 'lib/sfml/graphics/view.rb', line 22

def initialize(center: nil, size: nil, rotation: nil, viewport: nil, _handle: nil)
  ptr = _handle || C::Graphics.sfView_create
  raise Error, "sfView_create returned NULL" if ptr.null?
  @handle = FFI::AutoPointer.new(ptr, C::Graphics.method(:sfView_destroy))

  self.center   = center   if center
  self.size     = size     if size
  self.rotation = rotation if rotation
  self.viewport = viewport if viewport
end

Instance Attribute Details

#handleObject (readonly)

:nodoc:



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

def handle
  @handle
end

Class Method Details

.from_rect(rect) ⇒ Object

Build a View whose rectangle covers the given world Rect.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sfml/graphics/view.rb', line 34

def self.from_rect(rect)
  raise ArgumentError, "View.from_rect needs a SFML::Rect" unless rect.is_a?(Rect)

  native = C::Graphics::FloatRect.new
  native[:position][:x] = rect.x.to_f
  native[:position][:y] = rect.y.to_f
  native[:size][:x]     = rect.width.to_f
  native[:size][:y]     = rect.height.to_f

  ptr = C::Graphics.sfView_createFromRect(native)
  raise Error, "sfView_createFromRect returned NULL" if ptr.null?
  new(_handle: ptr)
end

Instance Method Details

#centerObject



58
59
60
# File 'lib/sfml/graphics/view.rb', line 58

def center
  Vector2.from_native(C::Graphics.sfView_getCenter(@handle))
end

#center=(value) ⇒ Object



62
63
64
# File 'lib/sfml/graphics/view.rb', line 62

def center=(value)
  C::Graphics.sfView_setCenter(@handle, _vec2(value).to_native_f)
end

#move(offset) ⇒ Object

Pan the camera by an offset in world units.



100
101
102
103
# File 'lib/sfml/graphics/view.rb', line 100

def move(offset)
  C::Graphics.sfView_move(@handle, _vec2(offset).to_native_f)
  self
end

#rotate(degrees) ⇒ Object

Rotate the camera by ‘degrees`, relative to current rotation.



106
107
108
109
# File 'lib/sfml/graphics/view.rb', line 106

def rotate(degrees)
  C::Graphics.sfView_rotate(@handle, degrees.to_f)
  self
end

#rotationObject



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

def rotation
  C::Graphics.sfView_getRotation(@handle)
end

#rotation=(degrees) ⇒ Object



78
79
80
# File 'lib/sfml/graphics/view.rb', line 78

def rotation=(degrees)
  C::Graphics.sfView_setRotation(@handle, degrees.to_f)
end

#sizeObject



66
67
68
# File 'lib/sfml/graphics/view.rb', line 66

def size
  Vector2.from_native(C::Graphics.sfView_getSize(@handle))
end

#size=(value) ⇒ Object



70
71
72
# File 'lib/sfml/graphics/view.rb', line 70

def size=(value)
  C::Graphics.sfView_setSize(@handle, _vec2(value).to_native_f)
end

#viewportObject

Viewport in normalised [0,1] window coordinates. Default is the full window (Rect.new(, [1,1])). Use [0, 0, 0.5, 1] for left half,

0.75, 0, 0.25, 0.25

for a top-right mini-map, etc.



85
86
87
# File 'lib/sfml/graphics/view.rb', line 85

def viewport
  Rect.from_native(C::Graphics.sfView_getViewport(@handle))
end

#viewport=(rect) ⇒ Object

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
# File 'lib/sfml/graphics/view.rb', line 89

def viewport=(rect)
  raise ArgumentError, "View#viewport= needs a SFML::Rect" unless rect.is_a?(Rect)
  native = C::Graphics::FloatRect.new
  native[:position][:x] = rect.x.to_f
  native[:position][:y] = rect.y.to_f
  native[:size][:x]     = rect.width.to_f
  native[:size][:y]     = rect.height.to_f
  C::Graphics.sfView_setViewport(@handle, native)
end

#zoom(factor) ⇒ Object

Multiply the visible area by ‘factor`. Factor < 1.0 zooms in; factor > 1.0 zooms out.



113
114
115
116
# File 'lib/sfml/graphics/view.rb', line 113

def zoom(factor)
  C::Graphics.sfView_zoom(@handle, factor.to_f)
  self
end