Class: SFML::View
- Inherits:
-
Object
- Object
- SFML::View
- 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
-
#handle ⇒ Object
readonly
:nodoc:.
Class Method Summary collapse
-
.from_rect(rect) ⇒ Object
Build a View whose rectangle covers the given world Rect.
Instance Method Summary collapse
-
#center ⇒ Object
Centre point of the view in world coordinates.
-
#center=(value) ⇒ Object
Set the center.
-
#initialize(center: nil, size: nil, rotation: nil, viewport: nil, _handle: nil) ⇒ View
constructor
Build a View.
-
#move(offset) ⇒ Object
Pan the camera by an offset in world units.
-
#rotate(degrees) ⇒ Object
Rotate the camera by
degrees, relative to current rotation. -
#rotation ⇒ Object
Rotation in degrees (counter-clockwise).
-
#rotation=(degrees) ⇒ Object
Set the rotation.
-
#scissor ⇒ Object
Scissor rect in normalised [0,1] window coords — pixels outside this rect are clipped before rendering.
-
#scissor=(rect) ⇒ Object
Set the scissor.
-
#size ⇒ Object
Visible area in world units (NOT pixels).
-
#size=(value) ⇒ Object
Set the size.
-
#viewport ⇒ Object
Viewport in normalised [0,1] window coordinates.
-
#viewport=(rect) ⇒ Object
Set the viewport.
-
#zoom(factor) ⇒ Object
Multiply the visible area by
factor.
Constructor Details
#initialize(center: nil, size: nil, rotation: nil, viewport: nil, _handle: nil) ⇒ View
Build a View. All four kwargs may be passed up-front or set
later. _handle: is for internal wrapping of CSFML pointers.
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sfml/graphics/view.rb', line 24 def initialize(center: nil, size: nil, rotation: nil, viewport: nil, _handle: nil) ptr = _handle || C::Graphics.sfView_create raise GraphicsError, "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. = if end |
Instance Attribute Details
#handle ⇒ Object (readonly)
:nodoc:
136 137 138 |
# File 'lib/sfml/graphics/view.rb', line 136 def handle @handle end |
Class Method Details
.from_rect(rect) ⇒ Object
Build a View whose rectangle covers the given world Rect.
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/sfml/graphics/view.rb', line 36 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 GraphicsError, "sfView_createFromRect returned NULL" if ptr.null? new(_handle: ptr) end |
Instance Method Details
#center ⇒ Object
Centre point of the view in world coordinates.
61 62 63 |
# File 'lib/sfml/graphics/view.rb', line 61 def center Vector2.from_native(C::Graphics.sfView_getCenter(@handle)) end |
#center=(value) ⇒ Object
Set the center.
66 67 68 |
# File 'lib/sfml/graphics/view.rb', line 66 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.
118 119 120 121 |
# File 'lib/sfml/graphics/view.rb', line 118 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.
124 125 126 127 |
# File 'lib/sfml/graphics/view.rb', line 124 def rotate(degrees) C::Graphics.sfView_rotate(@handle, degrees.to_f) self end |
#rotation ⇒ Object
Rotation in degrees (counter-clockwise).
81 82 83 |
# File 'lib/sfml/graphics/view.rb', line 81 def rotation C::Graphics.sfView_getRotation(@handle) end |
#rotation=(degrees) ⇒ Object
Set the rotation.
86 87 88 |
# File 'lib/sfml/graphics/view.rb', line 86 def rotation=(degrees) C::Graphics.sfView_setRotation(@handle, degrees.to_f) end |
#scissor ⇒ Object
Scissor rect in normalised [0,1] window coords — pixels
outside this rect are clipped before rendering. Default
[0, 0, 1, 1] (no clipping). Use it to render UI inside a
sub-region of the window without re-projecting.
107 108 109 |
# File 'lib/sfml/graphics/view.rb', line 107 def scissor Rect.from_native(C::Graphics.sfView_getScissor(@handle)) end |
#scissor=(rect) ⇒ Object
Set the scissor.
112 113 114 115 |
# File 'lib/sfml/graphics/view.rb', line 112 def scissor=(rect) raise ArgumentError, "View#scissor= needs a SFML::Rect" unless rect.is_a?(Rect) C::Graphics.sfView_setScissor(@handle, _to_floatrect(rect)) end |
#size ⇒ Object
Visible area in world units (NOT pixels). Halving this zooms in.
71 72 73 |
# File 'lib/sfml/graphics/view.rb', line 71 def size Vector2.from_native(C::Graphics.sfView_getSize(@handle)) end |
#size=(value) ⇒ Object
Set the size.
76 77 78 |
# File 'lib/sfml/graphics/view.rb', line 76 def size=(value) C::Graphics.sfView_setSize(@handle, _vec2(value).to_native_f) end |
#viewport ⇒ Object
Viewport in normalised [0,1] window coordinates. Default is the full window (Rect.new([0,0], [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.
93 94 95 |
# File 'lib/sfml/graphics/view.rb', line 93 def Rect.from_native(C::Graphics.sfView_getViewport(@handle)) end |