# Re: Tearing in my buffered animation test script

**URL:** <https://www.ruby-forum.com/t/re-tearing-in-my-buffered-animation-test-script/121848>\
**Category:** wxRuby\
**Created:** [December 1, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/re-tearing-in-my-buffered-animation-test-script/121848 "2007-12-01T21:55:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jay\_McGavren](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e79b87/32.png) [@Jay\_McGavren](https://www.ruby-forum.com/u/Jay_McGavren)\
**Post date:** [December 1, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/re-tearing-in-my-buffered-animation-test-script/121848/1 "2007-12-01T21:55:25Z")

</div>

Excellent, excellent! All tearing is now gone on Windows. (I meant  
to mention my platform before, but forgot.)

I kept evt\_paint, which I need just in case the window needs redrawing  
(or at least, I will in my final application). But I created a  
separate routine that just calls Window.paint, and that’s what I use  
in my main animation loop (as well as evt\_paint). And draw\_bitmap is  
a lot cleaner, as well, so thanks for pointing that out. Revised code  
is below.

You’ve saved me days of headaches, so many thanks!

-Jay McGavren  
[http://jay.mcgavren.com/zyps](http://jay.mcgavren.com/zyps)

require ‘rubygems’  
require ‘wx’

class MyApp \< Wx::App

def on\_init

```
#Containing frame.
frame = Wx::Frame.new(nil, :size => [300, 300])
frame.show

#Offscreen drawing buffer.
buffer = Wx::Bitmap.new(300, 300)

#Displays drawing.
window = Wx::Window.new(frame, :size => [300, 300])
window.evt_paint do |event|
  update_window(window, buffer)
end

#Animate.
(1..40).each do |i|
  #Clear screen.
  buffer.draw do |surface|
    surface.pen = Wx::Pen.new(Wx::Colour.new(0, 0, 0), 0)
    surface.brush = Wx::BLACK_BRUSH
    surface.draw_rectangle(0, 0, 300, 300)
  end
  #Draw line.
  buffer.draw do |surface|
    surface.pen = Wx::Pen.new(
      Wx::Colour.new(128, 255, 128),
      3
    )
    surface.pen.cap = Wx::CAP_ROUND
    surface.draw_line(i, 0, i+100, 100)
  end
  #Update screen.
  update_window(window, buffer)
  sleep 0.1
end

```

end

def update\_window(window, buffer)  
window.paint do |dc|  
#Copy the buffer to the viewable window.  
dc.draw\_bitmap(buffer, 0, 0, false)  
end  
end

end

app = MyApp.new  
app.main\_loop
