Esempio n. 1
0
def set_palette(palette=None):
    """ set_palette(palette=None) -> None
    Set the display color palette for indexed displays
    """
    check_video()

    screen = sdl.SDL_GetVideoSurface()
    if not screen:
        raise SDLError("Display mode not set")

    if palette and not hasattr(palette, '__iter__'):
        raise TypeError("Argument must be a sequence type")

    default_pal = screen.format.palette
    if screen.format.BytesPerPixel != 1 or default_pal is None:
        raise SDLError("Display mode is not colormapped")

    if palette is None:
        sdl.SDL_SetPalette(screen, sdl.SDL_PHYSPAL, default_pal.colors, 0,
                           default_pal.ncolors)
    else:
        ncolors = min(default_pal.ncolors, len(palette))
        colors = ffi.new('SDL_Color[]', ncolors)
        for i in range(ncolors):
            try:
                r, g, b = palette[i]
            except (ValueError, TypeError):
                raise TypeError("takes a sequence of sequence of RGB")
            try:
                colors[i].r = r
                colors[i].g = g
                colors[i].b = b
            except:
                raise TypeError("RGB sequence must contain numeric values")
        sdl.SDL_SetPalette(screen, sdl.SDL_PHYSPAL, colors, 0, ncolors)
Esempio n. 2
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