Ejemplo n.º 1
0
def new_surface_from_surface(c_surface, w, h):
    if 4 < c_surface.format.BytesPerPixel <= 0:
        raise ValueError("unsupported Surface bit depth for transform")

    format = c_surface.format
    newsurf = sdl.SDL_CreateRGBSurface(c_surface.flags, w, h,
                                       format.BitsPerPixel, format.Rmask,
                                       format.Gmask, format.Bmask,
                                       format.Amask)
    if not newsurf:
        SDLError.from_sdl_error()

    if format.BytesPerPixel == 1 and format.palette:
        sdl.SDL_SetColors(newsurf, format.palette.colors, 0,
                          format.palette.ncolors)

    if c_surface.flags & sdl.SDL_SRCCOLORKEY:
        sdl.SDL_SetColorKey(newsurf, (c_surface.flags & sdl.SDL_RLEACCEL)
                            | sdl.SDL_SRCCOLORKEY, format.colorkey)

    if c_surface.flags & sdl.SDL_SRCALPHA:
        result = sdl.SDL_SetAlpha(newsurf, c_surface.flags, format.alpha)
        if result == -1:
            raise SDLError.from_sdl_error()

    return newsurf
Ejemplo n.º 2
0
 def __enter__(self):
     self.flags = self.c_surface.flags & (sdl.SDL_SRCALPHA
                                          | sdl.SDL_SRCCOLORKEY)
     self.alpha = self.c_surface.format.alpha
     self.colorkey = self.c_surface.format.colorkey
     if self.flags & sdl.SDL_SRCALPHA:
         sdl.SDL_SetAlpha(self.c_surface, 0, 255)
     if self.flags & sdl.SDL_SRCCOLORKEY:
         sdl.SDL_SetColorKey(self.c_surface, 0,
                             self.c_surface.format.colorkey)
Ejemplo n.º 3
0
    def set_colorkey(self, color=None, flags=0):
        self.check_opengl()
        c_color = 0
        if color is not None:
            c_color = create_color(color, self._format)
            flags |= sdl.SDL_SRCCOLORKEY

        with locked(self._c_surface):
            if sdl.SDL_SetColorKey(self._c_surface, flags, c_color) == -1:
                raise SDLError.from_sdl_error()
Ejemplo n.º 4
0
    def subsurface(self, *rect):
        self.check_opengl()

        try:
            if hasattr(rect[0], '__iter__'):
                rect = game_rect_from_obj(rect[0])
            else:
                rect = game_rect_from_obj(rect)
        except TypeError:
            raise ValueError("not a valid rect style object")

        if (rect.x < 0 or rect.x + rect.w > self._c_surface.w or rect.y < 0 or
            rect.y + rect.h > self._c_surface.h):
            raise ValueError("subsurface rectangle outside surface area")
        with locked(self._c_surface):
            format = self._format
            pixeloffset = (rect.x * format.BytesPerPixel +
                           rect.y * self._c_surface.pitch)
            startpixel = ffi.cast("char*", self._c_surface.pixels) + pixeloffset
            surf = self._c_surface
            sub = sdl.SDL_CreateRGBSurfaceFrom(startpixel, rect.w, rect.h,
                                               format.BitsPerPixel, surf.pitch,
                                               format.Rmask, format.Gmask,
                                               format.Bmask, format.Amask)
        if not sub:
            raise SDLError.from_sdl_error()

        if format.BytesPerPixel == 1 and format.palette:
            sdl.SDL_SetPalette(sub, sdl.SDL_LOGPAL,
                               format.palette.colors, 0,
                               format.palette.ncolors);
        if surf.flags & sdl.SDL_SRCALPHA:
            sdl.SDL_SetAlpha(sub, surf.flags & sdl.SDL_SRCALPHA,
                             format.alpha);
        if surf.flags & sdl.SDL_SRCCOLORKEY:
            sdl.SDL_SetColorKey(sub, surf.flags & (sdl.SDL_SRCCOLORKEY |
                                                   sdl.SDL_RLEACCEL),
                                                   format.colorkey)
        subsurface = Surface._from_sdl_surface(sub)
        data = SubSurfaceData(self, pixeloffset, rect.x, rect.y)
        subsurface.subsurfacedata = data
        return subsurface
Ejemplo n.º 5
0
 def __exit__(self, *args):
     if self.flags & sdl.SDL_SRCALPHA:
         sdl.SDL_SetAlpha(self.c_surface, sdl.SDL_SRCALPHA, self.alpha)
     if self.flags & sdl.SDL_SRCCOLORKEY:
         sdl.SDL_SetColorKey(self.c_surface, sdl.SDL_SRCCOLORKEY,
                             self.colorkey)