当前位置: 首页>>代码示例>>Python>>正文


Python ffi.cast函数代码示例

本文整理汇总了Python中pygame._sdl.ffi.cast函数的典型用法代码示例。如果您正苦于以下问题:Python cast函数的具体用法?Python cast怎么用?Python cast使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了cast函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _set_at

 def _set_at(self, x, y, c_color):
     bpp = self._format.BytesPerPixel
     if bpp == 1:
         pixels = ffi.cast("uint8_t*", self._c_surface.pixels)
         pixels[y * self._c_surface.pitch // bpp + x] = c_color
     elif bpp == 2:
         pixels = ffi.cast("uint16_t*", self._c_surface.pixels)
         pixels[y * self._c_surface.pitch // bpp + x] = c_color
     elif bpp == 3:
         pixels = ffi.cast("uint8_t*", self._c_surface.pixels)
         base = y * self._c_surface.pitch + x * 3
         fmt = self._format
         if get_sdl_byteorder() == sdl.SDL_LIL_ENDIAN:
             pixels[base + (fmt.Rshift >> 3)] = ffi.cast('uint8_t', c_color >> 16)
             pixels[base + (fmt.Gshift >> 3)] = ffi.cast('uint8_t', c_color >> 8)
             pixels[base + (fmt.Bshift >> 3)] = ffi.cast('uint8_t', c_color)
         else:
             pixels[base + 2 - (fmt.Rshift >> 3)] = ffi.cast('uint8_t', c_color >> 16)
             pixels[base + 2 - (fmt.Gshift >> 3)] = ffi.cast('uint8_t', c_color >> 8)
             pixels[base + 2 - (fmt.Bshift >> 3)] = ffi.cast('uint8_t', c_color)
     elif bpp == 4:
         pixels = ffi.cast("uint32_t*", self._c_surface.pixels)
         pixels[y * (self._c_surface.pitch // bpp) + x] = c_color
     else:
         raise RuntimeError("invalid color depth for surface")
开发者ID:caseyc37,项目名称:pygame_cffi,代码行数:25,代码来源:surface.py

示例2: check_surface_overlap

def check_surface_overlap(c_src, srcrect, c_dest, destrect):
    srcx, srcy, destx, desty, w, h = (srcrect.x, srcrect.y,
                                      destrect.x, destrect.y,
                                      srcrect.w, srcrect.h)
    if srcx < 0:
        w += srcx
        destx -= srcx
        srcx = 0
    maxw = c_src.w - srcx
    if maxw < w:
        w = maxw
    if srcy < 0:
        h += srcy
        desty -= srcy
        srcy = 0
    maxh = c_src.h - srcy
    if maxh < h:
        h = maxh

    clip = c_dest.clip_rect
    x = clip.x - destx
    if x > 0:
        w -= x
        destx += x
        srcx += x
    x = destx + w - clip.x - clip.w
    if x > 0:
        w -= x
    y = clip.y - desty
    if y > 0:
        h -= y
        desty += y
        srcy += y
    y = desty + h - clip.y - clip.h
    if y > 0:
        h -= y

    if w <= 0 or h <= 0:
        return None

    srcpixels = ffi.cast("uint8_t*", c_src.pixels)
    srcpixels += c_src.offset + srcy * c_src.pitch + \
                 srcx * c_src.format.BytesPerPixel
    destpixels = ffi.cast("uint8_t*", c_dest.pixels)
    destpixels += c_src.offset + desty * c_dest.pitch + \
                  destx * c_dest.format.BytesPerPixel

    if destpixels <= srcpixels:
        return False
    span = w * c_src.format.BytesPerPixel
    if destpixels >= (srcpixels + (h - 1) * c_src.pitch + span):
        return False
    destoffset = (destpixels - srcpixels) % c_src.pitch
    return (destoffset < span) or (destoffset > c_src.pitch - span)
开发者ID:caseyc37,项目名称:pygame_cffi,代码行数:54,代码来源:surface.py

示例3: save_jpg

def save_jpg(surf, filename):
    if (surf.format.BytesPerPixel == 3
            and not (surf.flags & sdl.SDL_SRCALPHA)
            and surf.format.Rshift == 0):
        ss_surf = surf
    else:
        if get_sdl_byteorder() == sdl.SDL_LIL_ENDIAN:
            rmask, gmask, bmask, amask = 0xff, 0xff00, 0xff0000, 0xff000000
        else:
            rmask, gmask, bmask, amask = 0xff00, 0xff, 0xff0000, 0xff000000
        ss_surf = sdl.SDL_CreateRGBSurface(sdl.SDL_SWSURFACE, surf.w, surf.h,
                                           24, rmask, gmask, bmask, amask)
        if not ss_surf:
            return -1
        rect = ffi.new('SDL_Rect*')
        rect.w = surf.w
        rect.h = surf.h
        sdl.SDL_BlitSurface(surf, rect, ss_surf, ffi.NULL)

    ss_rows = ffi.new('unsigned char*[]', surf.h)
    ss_pixels = ffi.cast('unsigned char*', ss_surf.pixels)
    for i in range(surf.h):
        ss_rows[i] = ss_pixels + i * ss_surf.pitch
    err_msg = ffi.new('char**')
    result = jpglib.write_jpeg(filename, ss_rows, surf.w, surf.h, 85, err_msg)

    if ss_surf is not surf:
        sdl.SDL_FreeSurface(ss_surf)
    if result == -1:
        raise IOError("JPGError: %s" % ffi.string(err_msg[0]))
    return result
开发者ID:berteaa,项目名称:pygame_cffi,代码行数:31,代码来源:image.py

示例4: save_png

def save_png(surf, filename):
    alpha = bool(surf.format.Amask)
    if get_sdl_byteorder() == sdl.SDL_LIL_ENDIAN:
        rmask, gmask, bmask, amask = 0xff, 0xff00, 0xff0000, 0xff000000
    else:
        rmask, gmask, bmask, amask = 0xff00, 0xff0000, 0xff, 0x000000ff
    ss_surf = sdl.SDL_CreateRGBSurface(sdl.SDL_SWSURFACE | sdl.SDL_SRCALPHA,
                                       surf.w, surf.h, 32 if alpha else 24,
                                       rmask, gmask, bmask, amask)
    if not ss_surf:
        return -1
    with opaque(surf):
        rect = ffi.new('SDL_Rect*')
        rect.w = surf.w
        rect.h = surf.h
        sdl.SDL_BlitSurface(surf, rect, ss_surf, ffi.NULL)

    ss_rows = ffi.new('unsigned char*[]', surf.h)
    ss_pixels = ffi.cast('unsigned char*', ss_surf.pixels)
    for i in range(surf.h):
        ss_rows[i] = ss_pixels + i * ss_surf.pitch
    err_msg = ffi.new('char**')
    result = pnglib.write_png(filename, ss_rows, surf.w, surf.h,
                              (pnglib.PNG_COLOR_TYPE_RGB_ALPHA if alpha else
                               pnglib.PNG_COLOR_TYPE_RGB), 8, err_msg)

    sdl.SDL_FreeSurface(ss_surf)
    if result == -1:
        raise IOError("PNGError: %s" % ffi.string(err_msg[0]))
    return result
开发者ID:berteaa,项目名称:pygame_cffi,代码行数:30,代码来源:image.py

示例5: _get_at

 def _get_at(self, x, y):
     bpp = self._format.BytesPerPixel
     if bpp == 1:
         pixels = ffi.cast("uint8_t*", self._c_surface.pixels)
         return pixels[y * self._c_surface.pitch // bpp + x]
     elif bpp == 2:
         pixels = ffi.cast("uint16_t*", self._c_surface.pixels)
         return pixels[y * self._c_surface.pitch // bpp + x]
     elif bpp == 3:
         pixels = ffi.cast("uint8_t*", self._c_surface.pixels)
         base = y * self._c_surface.pitch + x * 3
         return (pixels[base + BYTE0] +
                 (pixels[base + BYTE1] << 8) +
                 (pixels[base + BYTE2] << 16))
     elif bpp == 4:
         pixels = ffi.cast("uint32_t*", self._c_surface.pixels)
         return pixels[y * self._c_surface.pitch // bpp + x]
开发者ID:caseyc37,项目名称:pygame_cffi,代码行数:17,代码来源:surface.py

示例6: smoothscale

def smoothscale(surface, size, dest_surface=None):
    """ smoothscale(Surface, (width, height), DestSurface = None) -> Surface
    scale a surface to an arbitrary size smoothly
    """
    width, height = size
    if width < 0 or height < 0:
        raise ValueError("Cannot scale to negative size")

    c_surf = surface._c_surface

    bpp = c_surf.format.BytesPerPixel
    if bpp < 3 or bpp > 4:
        raise ValueError("Only 24-bit or 32-bit surfaces can be"
                         " smoothly scaled")

    if dest_surface is None:
        new_surf = new_surface_from_surface(c_surf, width, height)
    else:
        new_surf = dest_surface._c_surface

    if new_surf.w != width or new_surf.h != height:
        raise ValueError("Destination surface not the given width or height.")

    if (width * bpp + 3) // 4 > new_surf.pitch:
        raise ValueError("SDL Error: destination surface pitch not"
                         " 4-byte aligned.")

    if width and height:
        with locked(new_surf):
            with locked(c_surf):
                if c_surf.w == width and c_surf.h == height:
                    # Non-scaling case, so just copy the correct pixels
                    c_pitch = c_surf.pitch
                    n_pitch = new_surf.pitch
                    srcpixels = ffi.cast('uint8_t*', c_surf.pixels)
                    destpixels = ffi.cast('uint8_t*', new_surf.pixels)
                    step = width * bpp
                    for y in range(0, height):
                        offset_n = y * n_pitch
                        offset_c = y * c_pitch
                        destpixels[offset_n:offset_n + step] = srcpixels[offset_c:offset_c + step]
                else:
                    sdl.scalesmooth(c_surf, new_surf)
    if dest_surface:
        return dest_surface
    return Surface._from_sdl_surface(new_surf)
开发者ID:CTPUG,项目名称:pygame_cffi,代码行数:46,代码来源:transform.py

示例7: unmap_rgb

 def unmap_rgb(self, mapped_int):
     """ unmap_rgb(mapped_int) -> Color
     convert a mapped integer color value into a Color
     """
     self.check_surface()
     mapped_int = ffi.cast('uint32_t', mapped_int)
     r, g, b, a = [ffi.new('uint8_t*') for i in range(4)]
     sdl.SDL_GetRGBA(mapped_int, self._format, r, g, b, a)
     return Color(r[0], g[0], b[0], a[0])
开发者ID:caseyc37,项目名称:pygame_cffi,代码行数:9,代码来源:surface.py

示例8: scroll

    def scroll(self, dx=0, dy=0):
        """ scroll(dx=0, dy=0) -> None
        Shift the surface image in place
        """

        self.check_surface()
        if self.is_pure_opengl():
            raise SDLError("Cannot scroll an OPENGL Surfaces (OPENGLBLIT is ok)")

        if not (dx or dy):
            return None

        clip_rect = self._c_surface.clip_rect
        w = clip_rect.w
        h = clip_rect.h
        if dx >= w or dx <= -w or dy >= h or dy <= -h:
            return None

        with locked(self._c_surface):
            bpp = self._c_surface.format.BytesPerPixel
            pitch = self._c_surface.pitch
            pixels = ffi.cast("uint8_t*", self._c_surface.pixels)
            src = dst = pixels + clip_rect.y * pitch + clip_rect.x * bpp
            if dx >= 0:
                w -= dx
                if dy > 0:
                    h -= dy
                    dst += dy * pitch + dx * bpp
                else:
                    h += dy
                    src -= dy * pitch
                    dst += dx * bpp
            else:
                w += dx
                if dy > 0:
                    h -= dy
                    src -= dx * bpp
                    dst += dy * pitch
                else:
                    h += dy
                    src -= dy * pitch + dx * bpp

            if src < dst:
                src += (h - 1) * pitch
                dst += (h - 1) * pitch
                pitch = -pitch

            span = w * bpp
            for _ in range(h):
                sdl.memmove(dst, src, span)
                src += pitch
                dst += pitch

        return None
开发者ID:caseyc37,项目名称:pygame_cffi,代码行数:54,代码来源:surface.py

示例9: smoothscale

def smoothscale(surface, size, dest_surface=None):
    """ smoothscale(Surface, (width, height), DestSurface = None) -> Surface
    scale a surface to an arbitrary size smoothly
    """
    width, height = size
    if width < 0 or height < 0:
        raise ValueError("Cannot scale to negative size")

    c_surf = surface._c_surface

    bpp = c_surf.format.BytesPerPixel
    if bpp < 3 or bpp > 4:
        raise ValueError("Only 24-bit or 32-bit surfaces can be"
                         " smoothly scaled")

    if dest_surface is None:
        new_surf = new_surface_from_surface(c_surf, width, height)
    else:
        new_surf = dest_surface._c_surface

    if new_surf.w != width or new_surf.h != height:
        raise ValueError("Destination surface not the given width or height.")

    if (width * bpp + 3) // 4 > new_surf.pitch:
        raise ValueError("SDL Error: destination surface pitch not"
                         " 4-byte aligned.")

    if width and height:
        with locked(new_surf):
            with locked(c_surf):
                if c_surf.w == width and c_surf.h == height:
                    pitch = c_surf.pitch
                    # Trivial case
                    srcpixels = ffi.cast('uint8_t*', c_surf.pixels)
                    destpixels = ffi.cast('uint8_t*', new_surf.pixels)
                    destpixels[0:height * pitch] = srcpixels[0:height * pitch]
                else:
                    sdl.scalesmooth(c_surf, new_surf)
    if dest_surface:
        return dest_surface
    return Surface._from_sdl_surface(new_surf)
开发者ID:vavilon,项目名称:pygame_cffi,代码行数:41,代码来源:transform.py

示例10: set_shifts

 def set_shifts(self, shifts):
     """ set_shifts((r,g,b,a)) -> None
     sets the bit shifts needed to convert between a color and a mapped integer
     """
     self.check_surface()
     try:
         r, g, b, a = [ffi.cast('uint8_t', s) for s in shifts]
         format = self._format
         format.Rshift = r
         format.Gshift = g
         format.Bshift = b
         format.Ashift = a
     except (ValueError, TypeError):
         raise TypeError("invalid argument for shifts")
开发者ID:caseyc37,项目名称:pygame_cffi,代码行数:14,代码来源:surface.py

示例11: set_masks

 def set_masks(self, masks):
     """ set_masks((r,g,b,a)) -> None
     set the bitmasks needed to convert between a color and a mapped integer
     """
     self.check_surface()
     try:
         r, g, b, a = [ffi.cast('uint32_t', m) for m in masks]
         format = self._format
         format.Rmask = r
         format.Gmask = g
         format.Bmask = b
         format.Amask = a
     except (ValueError, TypeError):
         raise TypeError("invalid argument for masks")
开发者ID:caseyc37,项目名称:pygame_cffi,代码行数:14,代码来源:surface.py

示例12: flip

def flip(surface, xaxis, yaxis):
    c_surf = surface._c_surface
    w, h = c_surf.w, c_surf.h
    new_surf = new_surface_from_surface(c_surf, w, h)
    bpp = c_surf.format.BytesPerPixel
    pitch = c_surf.pitch

    with locked(new_surf):
        with locked(surface._c_surface):
            # only have to deal with rows
            if not xaxis:
                srcpixels = ffi.cast('uint8_t*', c_surf.pixels)
                destpixels = ffi.cast('uint8_t*', new_surf.pixels)
                if not yaxis:
                    # no changes - just copy pixels
                    destpixels[0:h * pitch] = srcpixels[0:h * pitch]
                else:
                    for y in range(h):
                        dest_start = (h - y - 1) * pitch
                        src_start = y * pitch
                        destpixels[dest_start:dest_start + pitch] = \
                                srcpixels[src_start:src_start + pitch]
            # have to calculate position for individual pixels
            else:
                if not yaxis:
                    def get_y(y):
                        return y
                else:
                    def get_y(y):
                        return h - y - 1

                if bpp in (1, 2, 4):
                    ptr_type = 'uint%s_t*' % c_surf.format.BitsPerPixel
                    srcpixels = ffi.cast(ptr_type, c_surf.pixels)
                    destpixels = ffi.cast(ptr_type, new_surf.pixels)
                    for y in range(h):
                        dest_row_start = get_y(y) * w
                        src_row_start = y * w
                        for x in range(w):
                            destpixels[dest_row_start + (w - x - 1)] = \
                                    srcpixels[src_row_start + x]
                else:
                    srcpixels = ffi.cast('uint8_t*', c_surf.pixels)
                    destpixels = ffi.cast('uint8_t*', new_surf.pixels)
                    for y in range(h):
                        dest_row_start = get_y(y) * pitch
                        src_row_start = y * pitch
                        for x in range(0, pitch, 3):
                            dest_pix_start = dest_row_start + (pitch - x - 3)
                            src_pix_start = src_row_start + x
                            destpixels[dest_pix_start:dest_pix_start + 3] = \
                                srcpixels[src_pix_start:src_pix_start + 3]

    return Surface._from_sdl_surface(new_surf)
开发者ID:vavilon,项目名称:pygame_cffi,代码行数:54,代码来源:transform.py

示例13: post

def post(event):
    """post(Event): return None
       place a new event on the queue"""
    # SDL requires video to be initialised before PushEvent does the right thing
    check_video()
    is_blocked = sdl.SDL_EventState(event.type, sdl.SDL_QUERY) == sdl.SDL_IGNORE
    if is_blocked:
        raise RuntimeError("event post blocked for %s" % event_name(event.type))

    sdl_event = ffi.new("SDL_Event *")
    sdl_event.type = event.type
    sdl_event.user.code = _USEROBJECT_CHECK1
    sdl_event.user.data1 = _USEROBJECT_CHECK2
    sdl_event.user.data2 = ffi.cast("void*", sdl_event)
    _user_events[sdl_event] = event
    if sdl.SDL_PushEvent(sdl_event) == -1:
        raise SDLError.from_sdl_error()
开发者ID:GertBurger,项目名称:pygame_cffi,代码行数:17,代码来源:event.py

示例14: post

def post(event):
    """post(Event): return None
       place a new event on the queue"""
    # SDL requires video to be initialised before PushEvent does the right thing
    check_video()
    is_blocked = sdl.SDL_EventState(event.type, sdl.SDL_QUERY) == sdl.SDL_IGNORE
    if is_blocked:
        # Silently drop blocked events, since that's what pygame does
        # (maybe worth logging somehow?)
        return None

    sdl_event = ffi.new("SDL_Event *")
    sdl_event.type = event.type
    sdl_event.user.code = _USEROBJECT_CHECK1
    sdl_event.user.data1 = _USEROBJECT_CHECK2
    sdl_event.user.data2 = ffi.cast("void*", sdl_event)
    _user_events[sdl_event] = event
    if sdl.SDL_PushEvent(sdl_event) == -1:
        raise SDLError.from_sdl_error()
开发者ID:vavilon,项目名称:pygame_cffi,代码行数:19,代码来源:event.py

示例15: subsurface

    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
开发者ID:caseyc37,项目名称:pygame_cffi,代码行数:42,代码来源:surface.py


注:本文中的pygame._sdl.ffi.cast函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。