def init(*flags): """Loads dynamic libraries and prepares them for use. Args: *flags (Set[ImageInitFlag]): The desired image file formats. """ check_int_err(lib.IMG_Init(enumtools.get_mask(flags)))
def draw_color(self): """Tuple[int, int, int, int]: The color used for drawing operations in (red, green, blue, alpha) format.""" rgba = ffi.new('Uint8[]', 4) check_int_err( lib.SDL_GetRenderDrawColor(self._ptr, rgba + 0, rgba + 1, rgba + 2, rgba + 3)) return (rgba[0], rgba[1], rgba[2], rgba[3])
def copy(self, texture, source_rect=None, dest_rect=None, rotation=0, center=None, flip=lib.SDL_FLIP_NONE): """Copy a portion of the source texture to the current rendering target, rotating it by angle around the given center. Args: texture (Texture): The source texture. source_rect (Rect): The source rectangle, or None for the entire texture. dest_rect (Rect): The destination rectangle, or None for the entire rendering target. rotation (float): An angle in degrees that indicates the rotation that will be applied to dest_rect. center (Point): The point around which dest_rect will be rotated (if None, rotation will be done around dest_rect.w/2, dest_rect.h/2). flip (int): A value stating which flipping actions should be performed on the texture. Raises: SDLError: If an error is encountered. """ if source_rect == None: source_rect_ptr = ffi.NULL else: source_rect_ptr = source_rect._ptr if dest_rect == None: dest_rect_ptr = ffi.NULL else: dest_rect_ptr = dest_rect._ptr if center == None: center_ptr = ffi.NULL else: center_ptr = center._ptr check_int_err(lib.SDL_RenderCopyEx(self._ptr, texture._ptr, source_rect_ptr, dest_rect_ptr, rotation, center_ptr, flip))
def format(self): """PixelFormat: The raw format of the texture. The actual format may differ, but pixel transfers will use this format. """ fmt = ffi.new('Uint32 *') check_int_err(lib.SDL_QueryTexture(self._ptr, fmt, ffi.NULL, ffi.NULL, ffi.NULL)) return PixelFormat(fmt[0])
def access(self): """TextureAccess: The actual access to the texture.""" access = ffi.new('int *') check_int_err( lib.SDL_QueryTexture(self._ptr, ffi.NULL, access, ffi.NULL, ffi.NULL)) return TextureAccess(access[0])
def color_mod(self): """Tuple[int, int, int]: The additional color value used in render copy operations in (red, green, blue) format. """ rgb = ffi.new('Uint8[]', 3) check_int_err(lib.SDL_GetTextureColorMod(self._ptr, rgb + 0, rgb + 1, rgb + 2)) return (rgb[0], rgb[1], rgb[2])
def save(surface, file): """Save a png image of the surface. Args: surface: The surface to save. file: The file path to save to. """ check_int_err(lib.IMG_SavePNG(surface._ptr, file))
def format(self): """PixelFormat: The raw format of the texture. The actual format may differ, but pixel transfers will use this format. """ fmt = ffi.new('Uint32 *') check_int_err( lib.SDL_QueryTexture(self._ptr, fmt, ffi.NULL, ffi.NULL, ffi.NULL)) return PixelFormat(fmt[0])
def color_mod(self): """Tuple[int, int, int]: The additional color value used in render copy operations in (red, green, blue) format. """ rgb = ffi.new('Uint8[]', 3) check_int_err( lib.SDL_GetTextureColorMod(self._ptr, rgb + 0, rgb + 1, rgb + 2)) return (rgb[0], rgb[1], rgb[2])
def clear(self): """Clear the current rendering target with the drawing color. This function clears the entire rendering target, ignoring the viewport. Raises: SDLError: If an error is encountered. """ check_int_err(lib.SDL_RenderClear(self._ptr))
def init(*flags): """This function initializes the subsystems specified by flags. Args: *flags (InitFlag): Flags specifying which subsystems to initialize. Raises: SDLError: If there's an error initializing SDL. """ check_int_err(lib.SDL_Init(enumtools.get_mask(flags)))
def draw_rect(self, rect): """Draw a rectangle on the current rendering target. Args: rect (Rect): The destination rectangle, or None to outline the entire rendering target. Raises: SDLError: If an error is encountered. """ check_int_err(lib.SDL_RenderDrawRect(self._ptr, rect._ptr))
def fill_rect(self, rect): """Fill a rectangle on the current rendering target with the drawing color. Args: rect (Rect): The destination rectangle, or None to fill the entire rendering target. Raises: SDLError: If an error is encountered. """ check_int_err(lib.SDL_RenderFillRect(self._ptr, rect._ptr))
def draw_point(self, x, y): """Draw a point on the current rendering target. Args: x (int): The x coordinate of the point. y (int): The y coordinate of the point. Raises: SDLError: If an error is encountered. """ check_int_err(lib.SDL_RenderDrawPoint(self._ptr, x, y))
def open_audio(frequency=44100, format=AudioFormat.default, channels=2, chunksize=1024): """Open the mixer with a certain audio format. Args: frequency (int): Output sampling frequency in samples per second (Hz). format (AudioFormat): Output sample format. channels (int): Number of sound channels in output. Set to 2 for stereo, 1 for mono. chunksize (int): Bytes used per output sample. Raises: SDLError: If the audio device cannot be opened. """ check_int_err(lib.Mix_OpenAudio(frequency, format, channels, chunksize))
def draw_line(self, x1, y1, x2, y2): """Draw a line on the current rendering target. Args: x1 (int): The x coordinate of the start point. y1 (int): The y coordinate of the start point. x2 (int): The x coordinate of the end point. y2 (int): The y coordinate of the end point. Raises: SDLError: If an error is encountered. """ check_int_err(lib.SDL_RenderDrawLine(self._ptr, x1, y1, x2, y2))
def draw_circle(self, x, y, r, color): """Draw a circle. Args: x (int): The x coordinate of the center of the circle. y (int): The y coordinate of the center of the circle. r (int): The radius of the circle. color (Tuple[int, int, int, int]): The color of the circle. Raises: SDLError: If an error is encountered. """ check_int_err(lib.circleRGBA(self._ptr, x, y, r, color[0], color[1], color[2], color[3]))
def fill_rects(self, *rects): """Fill some number of rectangles on the current rendering target with the drawing color. Args: *rects (Rect): The destination rectangles. Raises: SDLError: If an error is encountered. """ rect_array = ffi.new('SDL_Rect[]', len(rects)) for i, r in enumerate(rects): rect_array[i] = r._ptr[0] check_int_err(lib.SDL_RenderFillRects(self._ptr, rect_array, len(rects)))
def draw_lines(self, *points): """Draw a series of connected lines on the current rendering target. Args: *points (Point): The points along the lines. Raises: SDLError: If an error is encountered. """ point_array = ffi.new('SDL_Point[]', len(points)) for i, p in enumerate(points): point_array[i] = p._ptr[0] check_int_err(lib.SDL_RenderDrawLines(self._ptr, point_array, len(points)))
def draw_points(self, *points): """Draw multiple points on the current rendering target. Args: *points (Point): The points to draw. Raises: SDLError: If an error is encountered. """ point_array = ffi.new('SDL_Point[]', len(points)) for i, p in enumerate(points): point_array[i] = p._ptr[0] check_int_err(lib.SDL_RenderDrawPoints(self._ptr, point_array, len(points)))
def draw_lines(self, *points): """Draw a series of connected lines on the current rendering target. Args: *points (Point): The points along the lines. Raises: SDLError: If an error is encountered. """ point_array = ffi.new('SDL_Point[]', len(points)) for i, p in enumerate(points): point_array[i] = p._ptr[0] check_int_err( lib.SDL_RenderDrawLines(self._ptr, point_array, len(points)))
def fill_rects(self, *rects): """Fill some number of rectangles on the current rendering target with the drawing color. Args: *rects (Rect): The destination rectangles. Raises: SDLError: If an error is encountered. """ rect_array = ffi.new('SDL_Rect[]', len(rects)) for i, r in enumerate(rects): rect_array[i] = r._ptr[0] check_int_err( lib.SDL_RenderFillRects(self._ptr, rect_array, len(rects)))
def draw_points(self, *points): """Draw multiple points on the current rendering target. Args: *points (Point): The points to draw. Raises: SDLError: If an error is encountered. """ point_array = ffi.new('SDL_Point[]', len(points)) for i, p in enumerate(points): point_array[i] = p._ptr[0] check_int_err( lib.SDL_RenderDrawPoints(self._ptr, point_array, len(points)))
def draw_arc(self, x, y, r, start, end, color): """Draw an arc. Args: x (int): The x coordinate of the center of the arc. y (int): The y coordinate of the center of the arc. r (int): The radius of the arc. start (int): The start of the arc. end (int): The end of the arc. color (Tuple[int, int, int, int]): The color of the circle. Raises: SDLError: If an error is encountered. """ check_int_err(lib.arcRGBA(self._ptr, x, y, r, start, end, color[0], color[1], color[2], color[3]))
def draw_line(self, x1, y1, x2, y2, color): """Draw a line. Args: x1 (int): The x coordinate of the start of the line. y1 (int): The y coordinate of the start of the line. x2 (int): The x coordinate of the end of the line. y2 (int): The y coordinate of the end of the line. color (Tuple[int, int, int, int]): The color of the circle. Raises: SDLError: If an error is encountered. """ check_int_err(lib.lineRGBA(self._ptr, x1, y1, x2, y2, color[0], color[1], color[2], color[3]))
def draw_filled_circle(self, x, y, r, color): """Draw a filled circle. Args: x (int): The x coordinate of the center of the circle. y (int): The y coordinate of the center of the circle. r (int): The radius of the circle. color (Tuple[int, int, int, int]): The color of the circle. Raises: SDLError: If an error is encountered. """ check_int_err( lib.filledCircleRGBA(self._ptr, x, y, r, color[0], color[1], color[2], color[3]))
def blit(self, src_rect, dst_surf, dst_rect): """Performs a fast blit from the source surface to the destination surface. This assumes that the source and destination rectangles are the same size. If either src_rect or dst_rect are None, the entire surface is copied. The final blit rectangles are saved in src_rect and dst_rect after all clipping is performed. Args: src_rect (Rect): Source rect. dst_surf (Surface): Destination surface. dst_rect (Rect): Destination rect. Raises: SDLError: If the blit fails. """ check_int_err(lib.SDL_UpperBlit(self._ptr, src_rect._ptr, dst_surf._ptr, dst_rect._ptr))
def draw_arc(self, x, y, r, start, end, color): """Draw an arc. Args: x (int): The x coordinate of the center of the arc. y (int): The y coordinate of the center of the arc. r (int): The radius of the arc. start (int): The start of the arc. end (int): The end of the arc. color (Tuple[int, int, int, int]): The color of the circle. Raises: SDLError: If an error is encountered. """ check_int_err( lib.arcRGBA(self._ptr, x, y, r, start, end, color[0], color[1], color[2], color[3]))
def draw_line(self, x1, y1, x2, y2, color): """Draw a line. Args: x1 (int): The x coordinate of the start of the line. y1 (int): The y coordinate of the start of the line. x2 (int): The x coordinate of the end of the line. y2 (int): The y coordinate of the end of the line. color (Tuple[int, int, int, int]): The color of the circle. Raises: SDLError: If an error is encountered. """ check_int_err( lib.lineRGBA(self._ptr, x1, y1, x2, y2, color[0], color[1], color[2], color[3]))
def draw_filled_trigon(self, x1, y1, x2, y2, x3, y3, color): """Draw a filled trigon Args: x1 (int): The x cooridinate of the first corner of the trigon y1 (int): The y cooridinate of the first corner of the trigon x2 (int): The x cooridinate of the second corner of the trigon y2 (int): The y cooridinate of the second corner of the trigon x3 (int): The x cooridinate of the third corner of the trigon y3 (int): The y cooridinate of the third corner of the trigon color (Tuple[int, int, int, int]): The color of the trigon. Raises: SDLError: If an error is encountered. """ check_int_err( lib.filledTrigonRGBA(self._ptr, x1, y1, x2, y2, x3, y3, color[0], color[1], color[2], color[3]))
def _peep(quantity, action, min_type, max_type): events = ffi.new('SDL_Event[]', quantity) quantity_retrieved = check_int_err(lib.SDL_PeepEvents(events, quantity, action, min_type, max_type)) result = [] for i in range(quantity_retrieved): event_ptr = events + i _event_reference_map[event_ptr] = events result.append(Event._from_ptr(event_ptr)) return result
def _peep(quantity, action, min_type, max_type): events = ffi.new('SDL_Event[]', quantity) quantity_retrieved = check_int_err( lib.SDL_PeepEvents(events, quantity, action, min_type, max_type)) result = [] for i in range(quantity_retrieved): event_ptr = events + i _event_reference_map[event_ptr] = events result.append(Event._from_ptr(event_ptr)) return result
def copy(self, texture, source_rect=None, dest_rect=None, rotation=0, center=None, flip=lib.SDL_FLIP_NONE): """Copy a portion of the source texture to the current rendering target, rotating it by angle around the given center. Args: texture (Texture): The source texture. source_rect (Rect): The source rectangle, or None for the entire texture. dest_rect (Rect): The destination rectangle, or None for the entire rendering target. rotation (float): An angle in degrees that indicates the rotation that will be applied to dest_rect. center (Point): The point around which dest_rect will be rotated (if None, rotation will be done around dest_rect.w/2, dest_rect.h/2). flip (int): A value stating which flipping actions should be performed on the texture. Raises: SDLError: If an error is encountered. """ if source_rect == None: source_rect_ptr = ffi.NULL else: source_rect_ptr = source_rect._ptr if dest_rect == None: dest_rect_ptr = ffi.NULL else: dest_rect_ptr = dest_rect._ptr if center == None: center_ptr = ffi.NULL else: center_ptr = center._ptr check_int_err( lib.SDL_RenderCopyEx(self._ptr, texture._ptr, source_rect_ptr, dest_rect_ptr, rotation, center_ptr, flip))
def h(self): """int: The height of the texture in pixels.""" h = ffi.new('int *') check_int_err(lib.SDL_QueryTexture(self._ptr, ffi.NULL, ffi.NULL, ffi.NULL, h)) return h[0]
def color_mod(self, rgb): r, g, b = rgb check_int_err(lib.SDL_SetTextureColorMod(self._ptr, r, g, b))
def _get_renderer_info(self): info = ffi.new('SDL_RendererInfo *') check_int_err(lib.SDL_GetRendererInfo(self._ptr, info)) return info
def blend_mode(self, blend_mode): check_int_err(lib.SDL_SetTextureBlendMode(self._ptr, blend_mode))
def alpha_mod(self, a): check_int_err(lib.SDL_SetTextureAlphaMod(self._ptr, a))
def draw_color(self): """Tuple[int, int, int, int]: The color used for drawing operations in (red, green, blue, alpha) format.""" rgba = ffi.new('Uint8[]', 4) check_int_err(lib.SDL_GetRenderDrawColor(self._ptr, rgba + 0, rgba + 1, rgba + 2, rgba + 3)) return (rgba[0], rgba[1], rgba[2], rgba[3])
def alpha_mod(self): """int: The additional alpha value used in render copy operations.""" a = ffi.new('Uint8 *') check_int_err(lib.SDL_GetTextureAlphaMod(self._ptr, a)) return a[0]
def h(self): """int: The height of the texture in pixels.""" h = ffi.new('int *') check_int_err( lib.SDL_QueryTexture(self._ptr, ffi.NULL, ffi.NULL, ffi.NULL, h)) return h[0]
def w(self): """int: The width of the texture in pixels.""" w = ffi.new('int *') check_int_err( lib.SDL_QueryTexture(self._ptr, ffi.NULL, ffi.NULL, w, ffi.NULL)) return w[0]
def w(self): """int: The width of the texture in pixels.""" w = ffi.new('int *') check_int_err(lib.SDL_QueryTexture(self._ptr, ffi.NULL, ffi.NULL, w, ffi.NULL)) return w[0]
def set_fullscreen(self, flag): check_int_err(lib.SDL_SetWindowFullscreen(self._ptr, flag))
def get_desktop_size(self): """Get the size of the desktop display""" _ptr = ffi.new('SDL_DisplayMode *') check_int_err(lib.SDL_GetDesktopDisplayMode(self._index, _ptr)) return (_ptr.w, _ptr.h)