Commits

otus  committed 303befb

Implement subsurface function using SDL_CreateRGBSurfaceFrom

  • Participants
  • Parent commits 9ac5ac2
  • Branches sub-sprite

Comments (0)

Files changed (1)

File sdl2/ext/surface.py

+"""Surface manipulation."""
+from ..surface import SDL_CreateRGBSurfaceFrom
+
+
+def subsurface(surface, area):
+    """Creates a surface from a part of another surface.
+    
+    The two surfaces share pixel data. The subsurface *must not* be used after
+    its parent has been freed!
+    """
+    surface_format = surface.format[0]
+    subpixels = (surface.pixels + surface.pitch*area[1] +
+                 surface_format.BytesPerPixel*area[0])
+    return SDL_CreateRGBSurfaceFrom(subpixels, area[2], area[3],
+                                    surface_format.BitsPerPixel,
+                                    surface.pitch, surface_format.Rmask,
+                                    surface_format.Gmask, surface_format.Bmask,
+                                    surface_format.Amask)[0]
+