Module: SFML::Graphics::RenderTarget
- Included in:
- RenderTexture, RenderWindow
- Defined in:
- lib/sfml/graphics/render_target.rb
Overview
Shared behaviour between SFML::RenderWindow and SFML::RenderTexture. The two CSFML APIs are near-mirrors of each other (sfRenderWindow_* vs sfRenderTexture_*), so the Ruby side dispatches by the includer’s CSFML_PREFIX constant. Adding a new render target only takes:
class NewTarget
include Graphics::RenderTarget
CSFML_PREFIX = :sfNewTarget
...
end
Drawables call ‘target._draw_native(:CircleShape, handle)` to dispatch through the right CSFML draw function for whichever target they’re being rendered to.
Instance Method Summary collapse
- #clear(color = Color::BLACK) ⇒ Object
-
#default_view ⇒ Object
The default 1:1 view that matches the target’s pixel size.
- #display ⇒ Object
-
#draw(drawable, render_states: nil, **opts) ⇒ Object
Polymorphic draw: any drawable with a #draw_on(target, [states]) method.
-
#draw_primitives(vertices, primitive_type = :points, render_states: nil, **opts) ⇒ Object
Draw a one-shot batch of vertices without allocating a SFML::VertexArray.
- #map_coords_to_pixel(coord, view: nil) ⇒ Object
- #map_pixel_to_coords(pixel, view: nil) ⇒ Object
- #view ⇒ Object
- #view=(value) ⇒ Object
Instance Method Details
#clear(color = Color::BLACK) ⇒ Object
18 19 20 21 |
# File 'lib/sfml/graphics/render_target.rb', line 18 def clear(color = Color::BLACK) _csfml(:clear, @handle, color.to_native) self end |
#default_view ⇒ Object
The default 1:1 view that matches the target’s pixel size. Memoised — see the comment in render_window.rb for why.
115 116 117 |
# File 'lib/sfml/graphics/render_target.rb', line 115 def default_view @default_view ||= View.from_borrowed(_csfml(:getDefaultView, @handle)) end |
#display ⇒ Object
23 24 25 26 |
# File 'lib/sfml/graphics/render_target.rb', line 23 def display _csfml(:display, @handle) self end |
#draw(drawable, render_states: nil, **opts) ⇒ Object
Polymorphic draw: any drawable with a #draw_on(target, [states]) method. Built-in drawables call back into target._draw_native.
Pass shortcut kwargs to apply render states without instantiating SFML::RenderStates yourself:
window.draw(va, texture: tile_texture)
window.draw(glow, blend_mode: SFML::BlendMode::ADD)
window.draw(thing, texture: tex, blend_mode: SFML::BlendMode::ADD)
Or pass a pre-built object for re-use across calls:
window.draw(thing, render_states: shared_states)
41 42 43 44 45 46 |
# File 'lib/sfml/graphics/render_target.rb', line 41 def draw(drawable, render_states: nil, **opts) states = render_states || RenderStates.from_draw_opts(opts) states_ptr = states&.to_native_pointer drawable.draw_on(self, states_ptr) self end |
#draw_primitives(vertices, primitive_type = :points, render_states: nil, **opts) ⇒ Object
Draw a one-shot batch of vertices without allocating a SFML::VertexArray. Useful for tight inner loops (a few dozen primitives per frame, where the VertexArray’s per-object bookkeeping is itself the cost).
window.draw_primitives(
[SFML::Vertex.new([0, 0], color: SFML::Color.red),
SFML::Vertex.new([100, 0], color: SFML::Color.green),
SFML::Vertex.new([50, 80], color: SFML::Color.blue)],
:triangles,
)
Accepts the same render-states kwargs as #draw.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/sfml/graphics/render_target.rb', line 73 def draw_primitives(vertices, primitive_type = :points, render_states: nil, **opts) type_code = VertexArray::PRIMITIVE_INDEX.fetch(primitive_type) do raise ArgumentError, "Unknown primitive type: #{primitive_type.inspect}" end # Pack the vertex array into a contiguous buffer. n = vertices.length buf = FFI::MemoryPointer.new(C::Graphics::Vertex, n) vertices.each_with_index do |v, i| slot = C::Graphics::Vertex.new(buf + i * C::Graphics::Vertex.size) slot[:position][:x] = v.position.x.to_f slot[:position][:y] = v.position.y.to_f slot[:color][:r] = v.color.r slot[:color][:g] = v.color.g slot[:color][:b] = v.color.b slot[:color][:a] = v.color.a slot[:tex_coords][:x] = v.tex_coords.x.to_f slot[:tex_coords][:y] = v.tex_coords.y.to_f end states = render_states || RenderStates.from_draw_opts(opts) states_ptr = states&.to_native_pointer C::Graphics.public_send( :"#{self.class::CSFML_PREFIX}_drawPrimitives", @handle, buf, n, type_code, states_ptr, ) self end |
#map_coords_to_pixel(coord, view: nil) ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/sfml/graphics/render_target.rb', line 129 def map_coords_to_pixel(coord, view: nil) vec = C::System::Vector2f.new cx, cy = coord.is_a?(Vector2) ? [coord.x, coord.y] : coord vec[:x] = cx.to_f; vec[:y] = cy.to_f v_handle = view ? view.handle : _csfml(:getView, @handle) result = _csfml(:mapCoordsToPixel, @handle, vec, v_handle) Vector2.new(result[:x], result[:y]) end |
#map_pixel_to_coords(pixel, view: nil) ⇒ Object
119 120 121 122 123 124 125 126 127 |
# File 'lib/sfml/graphics/render_target.rb', line 119 def map_pixel_to_coords(pixel, view: nil) vec = C::System::Vector2i.new px, py = pixel.is_a?(Vector2) ? [pixel.x, pixel.y] : pixel vec[:x] = Integer(px); vec[:y] = Integer(py) v_handle = view ? view.handle : _csfml(:getView, @handle) result = _csfml(:mapPixelToCoords, @handle, vec, v_handle) Vector2.new(result[:x], result[:y]) end |
#view ⇒ Object
109 110 111 |
# File 'lib/sfml/graphics/render_target.rb', line 109 def view View.from_borrowed(_csfml(:getView, @handle)) end |
#view=(value) ⇒ Object
103 104 105 106 107 |
# File 'lib/sfml/graphics/render_target.rb', line 103 def view=(value) raise ArgumentError, "#{self.class}#view= requires a SFML::View" unless value.is_a?(View) _csfml(:setView, @handle, value.handle) @view = value end |