[Pythonmac-SIG] DoubleBuffer drawing example

Nick Collier nick@src.uchicago.edu
Tue, 30 Jan 2001 16:36:26 -0600


Here's my code to do offscreen drawing -

import Qd, Qdoffs, QDOffscreen

...

def init_buffer(self):
       """ Any subsequent drawing operations will draw on offscreen buffer
       until copy_buffer is called"""
       rect = (0, 0, self.width, self.height)
       self.old_port, self.old_dev = Qdoffs.GetGWorld()
       self.offGWorld = Qdoffs.NewGWorld(0, rect, None, 
                                        None, QDOffscreen.keepLocal)
       self.pixmap = self.offGWorld.GetGWorldPixMap()
       Qdoffs.LockPixels(self.pixmap)
       Qdoffs.SetGWorld(self.offGWorld.as_GrafPtr(), None)
       Qd.EraseRect(rect)
          
def copy_buffer(self):
       """ will copy the contents of the offscreen buffer to screen, and
       make screen the current drawing device"""
       Qdoffs.SetGWorld(self.old_port, self.old_dev)
       sourceRect = (0, 0, self.width, self.height)
       destRect = (0, 0, self.width, self.height)
       Qd.CopyBits(self.offGWorld.as_GrafPtr().portBits,
                      self.old_port.portBits, sourceRect, destRect, 
                      QuickDraw.srcCopy, None)
       Qdoffs.UnlockPixels(self.pixmap)
       self.offGWorld = None

These methods are part of larger class that provides a drawing canvas for 
drawing on the Mac via methods like "create_circle(...)." The self.* 
variables of course refer to ivars of this class defined elsewhere. The basic 
idea here is that init_buffer() setups an offscreen buffer and redirect any 
Qd drawing operations to that buffer. copy_buffer() copies the offscreen 
memory to the screen and cleans up the offscreen buffer. The overall flow is 
then to call init_buffer(), do any of the drawing for this frame of the 
animation and then copy_buffer(). This code redraws the whole canvas each 
time. There is no optimization checking for unchanged areas of the canvas and 
so forth. (The target application just draws large shapes to the entire 
screen -- psychology vision experiment).

Thanks to Jack Jansen for pointing me in the right direction with this.

hope this is useful,

Nick




On Tuesday 30 January 2001 16:00, Jack Jansen wrote:
> The gworld object is automatically disposed of when the last reference 
> to the Python object goes away.
> 
> This is the general philosophy of the Python Toolbox interfaces, but
> unfortunately there are some necessary exceptions, such as Res objects 
> (which can be either Handles or Resources, which need to be disposed
> in different ways) or Ctl objects, which should be disposed if Python
> created them, but not if Python obtained them through, say,
> GetDialogItemAsControl (because they're owned by the dialog in that
> case, and disposing of the control because we no longer reference it
> from Python is definitely not what we want).
> 
> The interfaces are slowly being updated to a new scheme, whereby each
> object carries a pointer to the disposal routine. Initially the
> modules will set this pointer "correct", i.e. to the MacOS dispose
> routine for objects that were created by Python and to an empty
> routine for objects that Python obtains through other means. There
> will be an interface to change this disposal behaviour from Python
> code. 
> --
> Jack Jansen             | ++++ stop the execution of Mumia Abu-Jamal ++++
> Jack.Jansen@oratrix.com | ++++ if you agree copy these lines to your sig 
++++
> www.oratrix.nl/~jack    | see 
http://www.xs4all.nl/~tank/spg-l/sigaction.htm 
> 
> _______________________________________________
> Pythonmac-SIG maillist  -  Pythonmac-SIG@python.org
> http://mail.python.org/mailman/listinfo/pythonmac-sig

-- 
Nick Collier
Social Science Research Computing
University of Chicago
http://repast.sourceforge.net