Class: ParticleFX2D::Ruby2D::CanvasRendererFactory

Inherits:
Object
  • Object
show all
Includes:
ParticleFX2D::Renderer, ParticleFX2D::RendererFactory
Defined in:
lib/particlefx2d/ruby2d/canvas_renderer_factory.rb

Overview

A shared surface renderer that uses Ruby2D’s Canvas to draw into. This is both a factory and the renderer/

This approach uses the same renderer for all particles in an emitter.

Instance Method Summary collapse

Methods included from ParticleFX2D::Renderer

for

Constructor Details

#initialize(canvas) ⇒ CanvasRendererFactory

Instantiate a shape renderer factory.

Parameters:

  • canvas (Ruby2D::Canvas)

    The partice effects will be drawn into this canvas. Disable auto-update for the Canvas so that each attempt to draw into it will not cause a texture update.



19
20
21
22
# File 'lib/particlefx2d/ruby2d/canvas_renderer_factory.rb', line 19

def initialize(canvas)
  @canvas = canvas
  @fill_method = canvas.class.method_defined?(:fill_rectangle, true)
end

Instance Method Details

#draw_particle(particle) ⇒ Object

Updates the shape’s properties; no explicit drawing needed.



50
51
52
53
54
55
# File 'lib/particlefx2d/ruby2d/canvas_renderer_factory.rb', line 50

def draw_particle(particle)
  size = particle.size
  x = particle.x - (size / 2)
  y = particle.y - (size / 2)
  fill_rect(x, y, size, size, particle.color.to_a)
end

#hide_particle(_particle) ⇒ Object

Hide the particle. Used when a particle is deactivated.



47
# File 'lib/particlefx2d/ruby2d/canvas_renderer_factory.rb', line 47

def hide_particle(_particle); end

#on_update_endObject

Update the canvas at the end of the next update cycle



37
38
39
# File 'lib/particlefx2d/ruby2d/canvas_renderer_factory.rb', line 37

def on_update_end
  @canvas.update
end

#on_update_startObject

Clear the canvas before the next update cycle



32
33
34
# File 'lib/particlefx2d/ruby2d/canvas_renderer_factory.rb', line 32

def on_update_start
  fill_rect(0, 0, @canvas.width, @canvas.height, [0, 0, 0, 0])
end

#renderer_for(_particle) ⇒ Renderer

Return a particle renderer.

Returns:



27
28
29
# File 'lib/particlefx2d/ruby2d/canvas_renderer_factory.rb', line 27

def renderer_for(_particle)
  self
end

#show_particle(_particle) ⇒ Object

Show the particle. Used when a particle is activated.



44
# File 'lib/particlefx2d/ruby2d/canvas_renderer_factory.rb', line 44

def show_particle(_particle); end