1- """Drawing routines for software surfaces."""
21import ctypes
32from .compat import isiterable , UnsupportedError
43from .array import to_ctypes
54from .color import convert_to_color
65from .. import surface , pixels , rect
76from .algorithms import clipline
8- from .sprite import SoftwareSprite
7+ from .surface import _get_target_surface
98
109__all__ = ["prepare_color" , "fill" , "line" ]
1110
1211
13- def _get_target_surface (target , argname = "target" ):
14- """Gets the SDL_surface from the passed target."""
15- if isinstance (target , surface .SDL_Surface ):
16- rtarget = target
17- elif isinstance (target , SoftwareSprite ):
18- rtarget = target .surface
19- elif "SDL_Surface" in str (type (target )):
20- rtarget = target .contents
21- else :
22- raise TypeError ("{0} must be a Sprite or SDL_Surface" .format (argname ))
23- return rtarget
24-
25-
2612def prepare_color (color , target ):
2713 """Prepares a given color for a specific target.
2814
15+ Targets can be :obj:`~sdl2.SDL_PixelFormat`, :obj:`~sdl2.SDL_Surface`,
16+ or :obj:`~sdl2.ext.SoftwareSprite` objects.
17+
2918 Colors can be provided in any form supported by
3019 :func:`sdl2.ext.convert_to_color`.
3120
3221 Args:
3322 color (:obj:`sdl2.ext.Color`): The color to prepare for the pixel format
3423 of the given target.
35- target (:obj:`~sdl2. SDL_PixelFormat`, :obj:`~sdl2. SDL_Surface`,
36- :obj:`~sdl2.ext.SoftwareSprite`): The target pixel format, surface,
37- or sprite for which the color should be prepared.
24+ target (:obj:`SDL_PixelFormat`, :obj:`SDL_Surface`, :obj:`SoftwareSprite`): The
25+ target pixel format, surface, or sprite for which the color should be
26+ prepared.
3827
3928 Returns:
4029 int: An integer approximating the given color in the target's pixel
41- format.
30+ format.
4231
43- """
32+ """
4433 color = convert_to_color (color )
4534 pformat = None
4635 # Software surfaces
@@ -58,7 +47,7 @@ def prepare_color(color, target):
5847def fill (target , color , area = None ):
5948 """Fills one or more rectangular areas on a surface with a given color.
6049
61- Fill areas can be specified as 4-item (x, y, w, h) tuples,
50+ Fill areas can be specified as 4-item `` (x, y, w, h)`` tuples,
6251 :obj:`~sdl2.rect.SDL_Rect` objects, or a list containing multiple areas to
6352 fill in either format. If no area is provided, the entire target will be
6453 filled with the provided color.
@@ -121,9 +110,9 @@ def line(target, color, dline, width=1):
121110 target (:obj:`~sdl2.SDL_Surface`, :obj:`~sdl2.ext.SoftwareSprite`): The
122111 target surface or sprite to modify.
123112 color (:obj:`sdl2.ext.Color`): The color with which to draw lines.
124- dline (tuple, list): The (x1, y1, x2, y2) integer coordinates of a line
125- to draw, or a list of multiple sets of (x1, y1, x2, y2) coordinates
126- for multiple lines.
113+ dline (tuple, list): The `` (x1, y1, x2, y2)`` integer coordinates of a
114+ line to draw, or a list of multiple sets of `` (x1, y1, x2, y2)``
115+ coordinates for multiple lines.
127116 width (int, optional): The width of the line(s) in pixels. Defaults to
128117 1 if not specified.
129118
@@ -155,7 +144,7 @@ def line(target, color, dline, width=1):
155144 top , bottom = clip_rect .y , clip_rect .y + clip_rect .h - 1
156145
157146 if bpp == 3 :
158- raise UnsupportedError (line , "24bpp are currently not supported" )
147+ raise UnsupportedError ("24bpp surfaces are not currently supported. " )
159148 if bpp == 2 :
160149 pxbuf = ctypes .cast (rtarget .pixels , ctypes .POINTER (ctypes .c_uint16 ))
161150 elif bpp == 4 :
@@ -182,7 +171,7 @@ def line(target, color, dline, width=1):
182171 fillrect (rtarget , varea , color )
183172 continue
184173 if width != 1 :
185- raise UnsupportedError ( line , "width > 1 is not supported " )
174+ raise ValueError ( "Diagonal lines must have a width of 1. " )
186175 if width == 1 :
187176 # Bresenham
188177 x1 , y1 , x2 , y2 = clipline (left , top , right , bottom , x1 , y1 , x2 , y2 )
0 commit comments