Class: SFML::RenderStates

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

Overview

The state passed alongside a draw call: blend mode, texture, shader, transform, coordinate type. You rarely instantiate this directly —‘window.draw(…)` accepts `blend_mode:`, `texture:`, etc. shortcuts that build a RenderStates internally. Construct one yourself when you need to keep the same combination across many draws:

states = SFML::RenderStates.new(
  blend_mode: SFML::BlendMode::ADD,
  texture:    glow_texture,
)
window.draw(va,    render_states: states)
window.draw(other, render_states: states)

All fields default to the CSFML ‘sfRenderStates_default` value.

Constant Summary collapse

COORDINATE_TYPES =
%i[normalized pixels].freeze
COORDINATE_INDEX =
COORDINATE_TYPES.each_with_index.to_h.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(blend_mode: nil, texture: nil, shader: nil, coordinate_type: :normalized) ⇒ RenderStates

Returns a new instance of RenderStates.

Raises:

  • (ArgumentError)


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

def initialize(blend_mode: nil, texture: nil, shader: nil, coordinate_type: :normalized)
  @blend_mode      = blend_mode
  @texture         = texture
  @shader          = shader   # placeholder until SFML::Shader lands in the next P1 step
  @coordinate_type = coordinate_type
  raise ArgumentError, "unknown coordinate_type: #{coordinate_type.inspect}" unless COORDINATE_INDEX.key?(coordinate_type)
  freeze
end

Instance Attribute Details

#blend_modeObject (readonly)

Returns the value of attribute blend_mode.



20
21
22
# File 'lib/sfml/graphics/render_states.rb', line 20

def blend_mode
  @blend_mode
end

#coordinate_typeObject (readonly)

Returns the value of attribute coordinate_type.



20
21
22
# File 'lib/sfml/graphics/render_states.rb', line 20

def coordinate_type
  @coordinate_type
end

#shaderObject (readonly)

Returns the value of attribute shader.



20
21
22
# File 'lib/sfml/graphics/render_states.rb', line 20

def shader
  @shader
end

#textureObject (readonly)

Returns the value of attribute texture.



20
21
22
# File 'lib/sfml/graphics/render_states.rb', line 20

def texture
  @texture
end

Class Method Details

.from_draw_opts(opts) ⇒ Object

Convenience: build a RenderStates from the same shortcut kwargs that RenderTarget#draw accepts. Returns nil if no opts given (so the draw call uses the CSFML default without allocating anything).



51
52
53
54
# File 'lib/sfml/graphics/render_states.rb', line 51

def self.from_draw_opts(opts)
  return nil if opts.empty?
  new(**opts)
end