Exemple #1
0
    def test_fill_rect(self):
        rectlist = (rect.SDL_Rect(0, 0, 0, 0),
                    rect.SDL_Rect(0, 0, 10, 10),
                    rect.SDL_Rect(0, 0, -10, 10),
                    rect.SDL_Rect(0, 0, -10, -10),
                    rect.SDL_Rect(-10, -10, 10, 10),
                    rect.SDL_Rect(10, -10, 10, 10),
                    rect.SDL_Rect(10, 10, 10, 10),
                    )

        for fmt in pixels.ALL_PIXELFORMATS:
            if pixels.SDL_ISPIXELFORMAT_FOURCC(fmt):
                continue
            if pixels.SDL_BITSPERPIXEL(fmt) < 8:
                continue  # Skip < 8bpp, SDL_FillRect does not work on those
            for w in range(1, 100, 5):
                for h in range(1, 100, 5):
                    bpp, rmask, gmask, bmask, amask = \
                        pixels.pixelformat_enum_to_masks(fmt)
                    sf = surface.create_rgb_surface(w, h, bpp, rmask, gmask,
                                                    bmask, amask)
                    self.assertIsInstance(sf, surface.SDL_Surface)
                    for r in rectlist:
                        # TODO: check for changed pixels
                        surface.fill_rect(sf, r, 0xff00ff00)
                    surface.free_surface(sf)
Exemple #2
0
def fill(target, color, area=None):
    """Fills a certain rectangular area on the passed target with a color.

    If no area is provided, the entire target will be filled with
    the passed color. If an iterable item is provided as area (such as a list
    or tuple), it will be first checked, if the item denotes a single
    rectangular area (4 integer values) before assuming it to be a sequence
    of rectangular areas.
    """
    color = prepare_color(color, target)
    rtarget = _get_target_surface(target)

    varea = None
    if area is not None and isiterable(area):
        # can be either a single rect or a list of rects)
        if len(area) == 4:
            # is it a rect?
            try:
                varea = rect.SDL_Rect(int(area[0]), int(area[1]),
                                      int(area[2]), int(area[3]))
            except:
                # No, not a rect, assume a seq of rects.
                pass
        if not varea:  # len(area) == 4 AND varea set.
            varea = []
            for r in area:
                varea.append(rect.SDL_Rect(r[0], r[1], r[2], r[3]))

    if varea is None or isinstance(varea, rect.SDL_Rect):
        sdlsurface.fill_rect(rtarget, varea, color)
    else:
        sdlsurface.fill_rects(rtarget, varea, color)
Exemple #3
0
 def from_color(self, color, size=(0, 0), bpp=32, masks=None):
     """Creates a sprite with a certain color.
     """
     color = convert_to_color(color)
     if masks:
         rmask, gmask, bmask, amask = masks
     else:
         rmask = gmask = bmask = amask = 0
     sf = sdlsurface.create_rgb_surface(size[0], size[1], bpp, rmask, gmask, bmask, amask)
     fmt = sf.format
     if fmt.Amask != 0:
         # Target has an alpha mask
         c = pixels.map_rgba(fmt, color.r, color.g, color.b, color.a)
     else:
         c = pixels.map_rgb(fmt, color.r, color.g, color.b)
     sdlsurface.fill_rect(sf, None, c)
     return self.from_surface(sf, True)
Exemple #4
0
    def test_blit_surface(self):
        bpp = 32
        w, h = 10, 10
        # no alpha to prevent blending
        masks = (0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000)
        dest = surface.create_rgb_surface(w, h, bpp, masks[0], masks[1],
                                          masks[2], masks[3])
        pixelsize = h * dest.pitch
        rowlen = dest.pitch // 4

        sources = []
        for width, height in blitsizes:
            src = surface.create_rgb_surface(width, height, bpp, masks[0],
                                             masks[1], masks[2], masks[3])
            surface.fill_rect(src, None, 0xFFFFFFFF)  # fill with white
            sources.append(src)

        for src in sources:
            for pos in blitpositions:
                drect = pos.__copy__()
                surface.fill_rect(dest, None, 0x0)  # fill with black
                surface.blit_surface(src, None, dest, drect)
                buf = ctypes.cast(dest.pixels,
                                  ctypes.POINTER(ctypes.c_ubyte * pixelsize))
                pbuf = pgarray.CTypesView(buf.contents, itemsize=1,
                                          objsize=pixelsize)
                iview = pbuf.to_uint32()
                pw = drect.x + drect.w
                ph = drect.y + drect.h
                for y in range(dest.size[1]):
                    for x in range(dest.size[0]):
                        col = iview[y * rowlen + x]
                        if y >= drect.y and y < ph and \
                                x >= drect.x and x < pw:
                            self.assertEqual(col, 0xFFFFFFFF, msg="""color
mismatch at %d,%d for %s: %d != %d""" % (y, x, pos, col, 0xFFFFFFFF))
                        else:
                            self.assertEqual(col, 0x0, msg="""color mismatch
at %d,%d for %s: %d != %d""" % (y, x, pos, col, 0x0))

        while len(sources) > 0:
            sf = sources.pop()
            surface.free_surface(sf)
        surface.free_surface(dest)
Exemple #5
0
 def fill(self, color):
     """ expects RGBA8888 4-tuple """
     color = sdlpixels.map_rgba(self._surf.format, *color)
     sdlsurface.fill_rect(self._surf, None, color)