本文整理汇总了Python中pygame._sdl.ffi.new函数的典型用法代码示例。如果您正苦于以下问题:Python new函数的具体用法?Python new怎么用?Python new使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了new函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: 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
示例2: 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
示例3: set_cursor
def set_cursor(size, hotspot, xormasks, andmasks):
""" set_cursor(size, hotspot, xormasks, andmasks) -> None
set the image for the system mouse cursor
"""
check_video()
spotx, spoty = int(hotspot[0]), int(hotspot[1])
w, h = int(size[0]), int(size[1])
if w % 8 != 0:
raise ValueError("Cursor width must be divisible by 8")
if not hasattr(xormasks, '__iter__') or not hasattr(andmasks, '__iter__'):
raise TypeError("xormask and andmask must be sequences")
if len(xormasks) != w * h / 8.0 or len(andmasks) != w * h / 8.0:
raise ValueError("bitmasks must be sized width*height/8")
try:
xordata = ffi.new('uint8_t[]', [int(m) for m in xormasks])
anddata = ffi.new('uint8_t[]', [int(andmasks[i]) for i
in range(len(xormasks))])
except (ValueError, TypeError):
raise TypeError("Invalid number in mask array")
except OverflowError:
raise TypeError("Number in mask array is larger than 8 bits")
cursor = sdl.SDL_CreateCursor(xordata, anddata, w, h, spotx, spoty)
if not cursor:
raise SDLError.from_sdl_error()
lastcursor = sdl.SDL_GetCursor()
sdl.SDL_SetCursor(cursor)
sdl.SDL_FreeCursor(lastcursor)
开发者ID:GertBurger,项目名称:pygame_cffi,代码行数:30,代码来源:mouse.py
示例4: get_repeat
def get_repeat():
""" get_repeat() -> (delay, interval)
see how held keys are repeated
"""
check_video()
delay, interval = ffi.new('int*'), ffi.new('int*')
sdl.SDL_GetKeyRepeat(delay, interval)
return (delay[0], interval[0])
开发者ID:CTPUG,项目名称:pygame_cffi,代码行数:8,代码来源:key.py
示例5: get_rel
def get_rel():
""" get_rel() -> (x, y)
get the amount of mouse movement
"""
check_video()
x, y = ffi.new('int*'), ffi.new('int*')
sdl.SDL_GetRelativeMouseState(x, y)
return x[0], y[0]
开发者ID:GertBurger,项目名称:pygame_cffi,代码行数:8,代码来源:mouse.py
示例6: render
def render(self, text, antialias, color, background=None):
"""Font.render(text, antialias, color, background=None): return Surface
draw text on a new Surface"""
color = Color(color)
fg = ffi.new("SDL_Color [1]")
bg = ffi.new("SDL_Color [1]")
fg[0].r = color.r
fg[0].g = color.g
fg[0].b = color.b
if background:
try:
background = Color(background)
bg[0].r = background.r
bg[0].g = background.g
bg[0].b = background.b
except (TypeError, ValueError):
# Same error behaviour as pygame
bg[0].r = 0
bg[0].g = 0
bg[0].b = 0
else:
bg[0].r = 0
bg[0].g = 0
bg[0].b = 0
if text is None or text == "":
# Just return a surface of width 1 x font height
height = sdl.TTF_FontHeight(self._sdl_font)
surf = Surface((1, height))
if background and isinstance(background, Color):
surf.fill(background)
else:
# clear the colorkey
surf.set_colorkey(flags=sdl.SDL_SRCCOLORKEY)
return surf
if not isinstance(text, basestring):
raise TypeError("text must be a string or unicode")
if "\x00" in text:
raise ValueError("A null character was found in the text")
if isinstance(text, unicode):
text = text.encode("utf-8", "replace")
if utf_8_needs_UCS_4(text):
raise UnicodeError("A Unicode character above '\\uFFFF' was found;" " not supported")
if antialias:
if background is None:
sdl_surf = sdl.TTF_RenderUTF8_Blended(self._sdl_font, text, fg[0])
else:
sdl_surf = sdl.TTF_RenderUTF8_Shaded(self._sdl_font, text, fg[0], bg[0])
else:
sdl_surf = sdl.TTF_RenderUTF8_Solid(self._sdl_font, text, fg[0])
if not sdl_surf:
raise SDLError(ffi.string(sdl.TTF_GetError()))
surf = Surface._from_sdl_surface(sdl_surf)
if not antialias and background is not None:
surf.set_colorkey()
surf.set_palette([(bg[0].r, bg[0].g, bg[0].b)])
return surf
开发者ID:GertBurger,项目名称:pygame_cffi,代码行数:58,代码来源:font.py
示例7: get_caption
def get_caption():
""" get_caption() -> (title, icontitle)
Get the current window caption
"""
title = ffi.new("char*[1]")
icon = ffi.new("char*[1]")
sdl.SDL_WM_GetCaption(title, icon)
return ffi.string(title[0]), ffi.string(icon[0])
开发者ID:berteaa,项目名称:pygame_cffi,代码行数:9,代码来源:display.py
示例8: get_colorkey
def get_colorkey(self):
self.check_opengl()
if not self._c_surface.flags & sdl.SDL_SRCCOLORKEY:
return None
r = ffi.new("uint8_t[1]")
g = ffi.new("uint8_t[1]")
b = ffi.new("uint8_t[1]")
a = ffi.new("uint8_t[1]")
sdl.SDL_GetRGBA(self._format.colorkey, self._format, r, g, b, a)
return (r[0], g[0], b[0], a[0])
开发者ID:caseyc37,项目名称:pygame_cffi,代码行数:11,代码来源:surface.py
示例9: get_ball
def get_ball(self, i):
""" get_ball(ball_number) -> x, y
get the relative position of a trackball
"""
joydata = self._joydata
if i < 0 or i >= sdl.SDL_JoystickNumBalls(joydata):
raise SDLError("Invalid joystick trackball")
dx, dy = ffi.new('int*'), ffi.new('int*')
sdl.SDL_JoystickGetBall(joydata, i, dx, dy)
return (dx[0], dy[0])
开发者ID:vavilon,项目名称:pygame_cffi,代码行数:11,代码来源:joystick.py
示例10: overlap
def overlap(self, othermask, offset):
"""overlap(othermask, offset) -> x,y
Returns the point of intersection if the masks overlap with
the given offset - or None if it does not overlap."""
x, y = offset
xp = ffi.new('int[1]')
yp = ffi.new('int[1]')
val = sdl.bitmask_overlap_pos(self._mask, othermask._mask,
x, y, xp, yp)
if val:
return (xp[0], yp[0])
return None
开发者ID:CTPUG,项目名称:pygame_cffi,代码行数:13,代码来源:mask.py
示例11: size
def size(self, text):
"""Font.size(text): return (width, height)
determine the amount of space needed to render text"""
if not isinstance(text, (bytes_, unicode_)):
raise TypeError("text must be a string or unicode")
if isinstance(text, unicode_):
text = text.encode('utf-8', 'replace')
w = ffi.new("int*")
h = ffi.new("int*")
ecode = sdl.TTF_SizeUTF8(self._sdl_font, text, w, h)
if ecode == -1:
raise SDLError(ffi.string(sdl.TTF_GetError()))
return int(w[0]), int(h[0])
开发者ID:berteaa,项目名称:pygame_cffi,代码行数:14,代码来源:font.py
示例12: set_mode
def set_mode(resolution=(0, 0), flags=0, depth=0):
""" set_mode(resolution=(0,0), flags=0, depth=0) -> Surface
Initialize a window or screen for display
"""
w, h = unpack_rect(resolution)
if w < 0 or h < 0:
raise SDLError("Cannot set negative sized display mode")
if flags == 0:
flags = sdl.SDL_SWSURFACE
if not get_init():
init()
# depth and double buffering attributes need to be set specially for OpenGL
if flags & sdl.SDL_OPENGL:
if flags & sdl.SDL_DOUBLEBUF:
gl_set_attribute(sdl.SDL_GL_DOUBLEBUFFER, 1)
else:
gl_set_attribute(sdl.SDL_GL_DOUBLEBUFFER, 0)
if depth:
gl_set_attribute(sdl.SDL_GL_DEPTH_SIZE, depth)
c_surface = sdl.SDL_SetVideoMode(w, h, depth, flags)
if c_surface and gl_get_attribute(sdl.SDL_GL_DOUBLEBUFFER):
c_surface.flags |= sdl.SDL_DOUBLEBUF
else:
if depth == 0:
flags |= sdl.SDL_ANYFORMAT
c_surface = sdl.SDL_SetVideoMode(w, h, depth, flags)
if not c_surface:
raise SDLError.from_sdl_error()
title = ffi.new("char*[1]")
icon = ffi.new("char*[1]")
sdl.SDL_WM_GetCaption(title, icon)
if not title:
sdl.SDL_WM_SetCaption("pygame window", "pygame")
# pygame does this, so it's possibly a good idea
sdl.SDL_PumpEvents()
global _display_surface
_display_surface = SurfaceNoFree._from_sdl_surface(c_surface)
# TODO: set icon stuff
return _display_surface
开发者ID:berteaa,项目名称:pygame_cffi,代码行数:49,代码来源:display.py
示例13: peek
def peek(types=None):
""" peek(type) -> bool
test if event types are waiting on the queue
"""
mask = 0
if not types:
mask = sdl.SDL_ALLEVENTS
elif isinstance(types, int):
mask |= event_mask(types)
elif hasattr(types, '__iter__'):
try:
for t in types:
mask |= event_mask(int(t))
except (ValueError, TypeError):
raise TypeError("type sequence must contain valid event types")
else:
raise TypeError("peek type must be numeric or a sequence")
sdl.SDL_PumpEvents()
event = ffi.new('SDL_Event*')
result = sdl.SDL_PeepEvents(event, 1, sdl.SDL_PEEKEVENT, mask)
if not types:
return EventType(event[0])
return result == 1
开发者ID:vavilon,项目名称:pygame_cffi,代码行数:25,代码来源:event.py
示例14: fill
def fill(self, color, rect=None, special_flags=0):
""" fill(color, rect=None, special_flags=0) -> Rect
fill Surface with a solid color
"""
self.check_opengl()
c_color = create_color(color, self._format)
sdlrect = ffi.new('SDL_Rect*')
if rect is not None:
sdlrect.x, sdlrect.y, sdlrect.w, sdlrect.h = rect_vals_from_obj(rect)
else:
sdlrect.w = self._w
sdlrect.h = self._h
if self.crop_to_surface(sdlrect):
if special_flags:
res = sdl.surface_fill_blend(self._c_surface, sdlrect,
c_color, special_flags)
else:
with locked(self._c_surface):
# TODO: prep/unprep
res = sdl.SDL_FillRect(self._c_surface, sdlrect, c_color)
if res == -1:
raise SDLError.from_sdl_error()
return Rect._from4(sdlrect.x, sdlrect.y, sdlrect.w, sdlrect.h)
开发者ID:caseyc37,项目名称:pygame_cffi,代码行数:27,代码来源:surface.py
示例15: set_palette
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)
开发者ID:berteaa,项目名称:pygame_cffi,代码行数:35,代码来源:display.py
示例16: wait
def wait():
""" wait() -> EventType instance
wait for a single event from the queue
"""
event = ffi.new('SDL_Event*')
if not sdl.SDL_WaitEvent(event):
raise SDLError.from_sdl_error()
return EventType(event[0])
开发者ID:vavilon,项目名称:pygame_cffi,代码行数:8,代码来源:event.py
示例17: 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
示例18: clear
def clear(event_filter=None):
if event_filter is None:
# unfiltered list of events
mask = sdl.SDL_ALLEVENTS
else:
raise NotImplementedError("Implement me")
sdl.SDL_PumpEvents()
event = ffi.new("SDL_Event *")
while sdl.SDL_PeepEvents(event, 1, sdl.SDL_GETEVENT, mask) == 1:
pass
开发者ID:vavilon,项目名称:pygame_cffi,代码行数:10,代码来源:event.py
示例19: get_bounding_rects
def get_bounding_rects(self):
"""get_bounding_rects() -> Rects
Returns a list of bounding rects of regions of set pixels."""
num_bounding_boxes = ffi.new('int[1]')
regions = ffi.new('SDL_Rect**')
r = sdl.internal_get_bounding_rects(self._mask,
num_bounding_boxes, regions)
# internal_get_bounding_rects returns -2 on memory errors, 0 otherwise
if r == -2:
raise MemoryError("Not enough memory to get bounding rects.")
rects = []
# The C code creates an array indexed from 1.
# This turns out to be surprisingly hard to figure out.
for i in range(1, num_bounding_boxes[0] + 1):
region = regions[0][i]
rects.append(Rect((region.x, region.y,
region.w, region.h)))
return rects
开发者ID:CTPUG,项目名称:pygame_cffi,代码行数:19,代码来源:mask.py
示例20: _drawhorizline
def _drawhorizline(surface, c_color, start_x, end_x, y):
"""Draw a horizontal line using SDL_FillRect"""
sdlrect = ffi.new("SDL_Rect*")
if start_x > end_x:
end_x, start_x = start_x, end_x
sdlrect.x = start_x
sdlrect.y = y
sdlrect.w = end_x - start_x + 1
sdlrect.h = 1
sdl.SDL_FillRect(surface._c_surface, sdlrect, c_color)
开发者ID:GertBurger,项目名称:pygame_cffi,代码行数:10,代码来源:draw.py
注:本文中的pygame._sdl.ffi.new函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论