Exemple #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)
Exemple #2
0
def check_opengl():
    screen = sdl.SDL_GetVideoSurface()
    if not screen:
        raise SDLError("Display mode not set")

    if not (screen.flags & sdl.SDL_OPENGL):
        raise SDLError("Not an OPENGL display")
Exemple #3
0
def toggle_fullscreen():
    """ toggle_fullscreen() -> bool
    Switch between fullscreen and windowed displays
    """
    check_video()

    screen = sdl.SDL_GetVideoSurface()
    if not screen:
        raise SDLError("Display mode not set")
    return (sdl.SDL_WM_ToggleFullScreen(screen) != 0)
Exemple #4
0
def update(rectangle=None):
    """ update(rectangle=None) -> None
    update(rectangle_list) -> None
    Update portions of the screen for software displays
    """
    check_video()

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

    if (screen.flags & sdl.SDL_OPENGL):
        raise SDLError("Cannot update an OPENGL display")

    if not rectangle:
        sdl.SDL_UpdateRect(screen, 0, 0, 0, 0)
        return

    try:
        if hasattr(rectangle, '__iter__'):
            # it can either be a rect style 4-tuple or
            # a sequence of rects or rect styles
            try:
                int(rectangle[0])
                rects = (rectangle, )
            except (ValueError, TypeError):
                rects = rectangle
        else:
            rects = (rectangle, )

        if len(rects) == 1:
            rect = game_rect_from_obj(rects[0])
            if screen_crop_rect(rect, screen.w, screen.h):
                sdl.SDL_UpdateRect(screen, rect.x, rect.y, rect.w, rect.h)
            return

        rect_array = ffi.new('SDL_Rect[]', len(rects))
        count = 0
        for obj in rects:
            if not obj:
                continue
            rect = game_rect_from_obj(obj)
            if screen_crop_rect(rect, screen.w, screen.h):
                sdlrect = rect_array[count]
                sdlrect.x = rect.x
                sdlrect.y = rect.y
                sdlrect.w = rect.w
                sdlrect.h = rect.h
                count += 1

        sdl.SDL_UpdateRects(screen, count, rect_array)

    except (NotImplementedError, TypeError):
        raise ValueError(
            "update requires a rectstyle or sequence of recstyles")
Exemple #5
0
def flip():
    """ flip() -> None
    Update the full display Surface to the screen
    """
    check_video()

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

    if screen.flags & sdl.SDL_OPENGL:
        sdl.SDL_GL_SwapBuffers()
        status = 0
    else:
        status = sdl.SDL_Flip(screen)

    if status == -1:
        raise SDLError.from_sdl_error()
Exemple #6
0
    def __init__(self, size, flags=0, depth=0, masks=None):
        w, h = unpack_rect(size)

        if isinstance(depth, Surface):
            if masks:
                raise ValueError(
                    "cannot pass surface for depth and color masks")
            surface = depth
            depth = 0
        else:
            surface = None
            depth = int(depth)

        if depth and masks:
            Rmask, Gmask, Bmask, Amask = masks
            bpp = depth

        elif surface is None:
            if depth:
                bpp = depth
                Rmask, Gmask, Bmask, Amask = \
                    self._get_default_masks(bpp, False)
            elif sdl.SDL_GetVideoSurface():
                pix = sdl.SDL_GetVideoSurface().format
                bpp = pix.BitsPerPixel
                Amask = pix.Amask
                Rmask = pix.Rmask
                Gmask = pix.Gmask
                Bmask = pix.Bmask
            elif sdl.SDL_WasInit(sdl.SDL_INIT_VIDEO):
                pix = sdl.SDL_GetVideoInfo().vfmt
                bpp = pix.BitsPerPixel
                Amask = pix.Amask
                Rmask = pix.Rmask
                Gmask = pix.Gmask
                Bmask = pix.Bmask
            else:
                bpp = 32
                Rmask, Gmask, Bmask, Amask = \
                    self._get_default_masks(32, False)
            # the alpha mask might be different - must update
            if flags & sdl.SDL_SRCALPHA:
                Rmask, Gmask, Bmask, Amask = \
                    self._get_default_masks(bpp, True)

        # depth argument was a Surface object
        else:
            pix = surface._c_surface.format
            bpp = pix.BitsPerPixel
            if flags & sdl.SDL_SRCALPHA:
                Rmask, Gmask, Bmask, Amask = \
                    self._get_default_masks(bpp, True)
            else:
                Amask = pix.Amask
                Rmask = pix.Rmask
                Gmask = pix.Gmask
                Bmask = pix.Bmask

        self._c_surface = sdl.SDL_CreateRGBSurface(flags, w, h, bpp, Rmask,
                                                   Gmask, Bmask, Amask)
        self._format = self._c_surface.format
        self._w = self._c_surface.w
        self._h = self._c_surface.h

        if not self._c_surface:
            raise SDLError.from_sdl_error()

        if masks:
            """
            Confirm the surface was created correctly (masks were valid).
            Also ensure that 24 and 32 bit surfaces have 8 bit fields
            (no losses).
            """
            format = self._format
            Rmask = (0xFF >> format.Rloss) << format.Rshift
            Gmask = (0xFF >> format.Gloss) << format.Gshift
            Bmask = (0xFF >> format.Bloss) << format.Bshift
            Amask = (0xFF >> format.Aloss) << format.Ashift
            bad_loss = format.Rloss or format.Gloss or format.Bloss
            if flags & sdl.SDL_SRCALPHA:
                bad_loss = bad_loss or format.Aloss
            else:
                bad_loss = bad_loss or format.Aloss != 8
            if (format.Rmask != Rmask or format.Gmask != Gmask
                    or format.Bmask != Bmask or format.Amask != Amask
                    or (format.BytesPerPixel >= 3 and bad_loss)):
                # Note: don't free _c_surface here. It will
                # be done in __del__.
                raise ValueError("Invalid mask values")