[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[pygame] Subclassing^H^H^H^H hooking Surface



I have an application where the user has to draw with pixel precision with
the mouse so I magnify the surface for the user.  I'm drawing on a small
surface (e.g. with `draw.ellipse`) and currently I just scale and blit the
whole thing it to the screen after each update.  This is slow so I was
forced to use a timer for dropping redraws (to remain responsive)...

So I thought about creating a Surface that would automatically update the
screen whenever it's changed.  Then I can as well make it efficient
(whilekeeping my code elegant) by implementing some diry-rectangles
approach.  My first thought was to subclass Surface.  I would have to
catch methods like `set_at` (as if they aren't slow enough already) but I
dont't see any way to catch C code that draws on the surface (e.g.
`draw.ellipse`).

So I have a different idea that's more useful than [only] subclassing:
allow to set a hook (callback) on a Surface that will be called from the C
side with the changed rect after any update to the surface.  For better
performance, all updates between locking and unlocking could be combined
into one callback with the common rect.  In cases that are too hard (e.g.
change through memory-sharing surfarray), falling back to the whole
surface size is OK too.

Then I could do something like this:

   def autoscale_surface(src, dst, (scalex, scaley)):
       def hook(dirty_rect):
           dst.blit(transform.scale(src.subsurface(dirty_rect),
                                    (scalex * dirty_rect.w,
                                     scaley * dirty_rect.h)),
                    (scalex * dirty_rect.x,
                     scaley * dirty_rect.y))
       src.set_dirty_hook(hook)

A whole lot of simple and easy to use possibilities opens up...
Can this be implemented?

-- 
Beni Cherniavsky <cben@tx.technion.ac.il>

Do not feed the Bugzillas.
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org