-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_buffer.rb
49 lines (38 loc) · 1.51 KB
/
simple_buffer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
require 'ruby-processing'
class SimpleBuffer < Processing::App
def setup
# With Processing, buffers allow you to draw off-screen to an image,
# and then use that image in your sketch. This can speed up rendering
# times quite a bit, as it's not only faster to draw off-screen, but
# it also allows you to redraw only portions of the screen.
# Ruby-Processing provides a convenience method, "buffer", which
# sets up a buffer for you with the same width and height and
# renderer as the current sketch, and yields it to you. It also
# takes care of calling begin_draw and end_draw at the start and
# end of the block. Use it like so:
@buffer = buffer do |b|
b.no_stroke
b.fill 255, 0, 0
b.rect 100, 200, 100, 100
end
# Those lines are equivalent to the following lines:
@buffer_2 = create_graphics(500, 500, P3D)
@buffer_2.begin_draw
@buffer_2.no_stroke
@buffer_2.fill(255, 0, 0)
@buffer_2.rect(100, 200, 100, 100)
@buffer_2.end_draw
# If you'd like to set the size or renderer for the buffer block,
# just pass it in like normal:
@buffer_3 = buffer(150, 150, P3D) do |b|
# b.whatever goes here.
end
# And now we go ahead and grab the rendered image from the first buffer.
@img = @buffer.get(0, 0, @buffer.width, @buffer.height)
end
def draw
background 0
image @img, frame_count, 0
end
end
SimpleBuffer.new :title => "Simple Buffer", :width => 500, :height => 500