Skip to content

Commit 0700f2e

Browse files
committed
Tidy and autodoc ext.draw
1 parent 45cfb4d commit 0700f2e

File tree

2 files changed

+22
-68
lines changed

2 files changed

+22
-68
lines changed

doc/modules/ext/draw.rst

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,9 @@
1-
.. currentmodule:: sdl2.ext
1+
`sdl2.ext.draw` - Drawing Routines for Software Surfaces
2+
========================================================
23

3-
2D drawing routines for software surfaces
4-
=========================================
4+
The `sdl2.ext.draw` module provides some basic methods for drawing to
5+
unaccelerated 2D SDL surfaces. At present, the functions in this module can
6+
fill the surface with a given color and draw rectangles and lines.
57

6-
.. note::
7-
8-
The drawing functions within this module are unoptimised and should not be
9-
considered fast. If you want improved drawing of 2D primitives, including
10-
hardware acceleration, you should use the methods of the
11-
:class:`Renderer` instead.
12-
13-
.. function:: prepare_color(color : object, target : object) -> int
14-
15-
Prepares the passed *color* for a specific *target*. *color* can be any
16-
object type that can be processed by
17-
:func:`convert_to_color()`. *target* can be any
18-
:class:`sdl2.SDL_PixelFormat`, :class:`sdl2.SDL_Surface` or
19-
:class:`SoftwareSprite` instance.
20-
21-
The returned integer will be a color value matching the target's pixel
22-
format.
23-
24-
.. function:: fill(target : object, color : object[, area=None]) -> None
25-
26-
Fills a certain area on the passed *target* with a *color*. If no *area* is
27-
provided, the entire target will be filled with the passed color. If an
28-
iterable item is provided as *area* (such as a list or tuple), it will be
29-
first checked, if the item denotes a single rectangular area
30-
(4 integer values) before assuming it to be a sequence of rectangular areas
31-
to fill with the color.
32-
33-
*target* can be any :class:`sdl2.SDL_Surface` or :class:`SoftwareSprite`
34-
instance.
35-
36-
.. function:: line(target : object, color : object[, width=1]) -> None
37-
38-
Draws one or multiple lines on the passed *target*. *line* can be a
39-
sequence of four integers for a single line in the form ``(x1, y1,
40-
x2, y2)`` or a sequence of a multiple of 4 for drawing multiple lines
41-
at once, e.g. ``(x1, y1, x2, y2, x3, y3, x4, y4, ...)``.
42-
43-
*target* can be any :class:`sdl2.SDL_Surface` or :class:`SoftwareSprite`
44-
instance.
8+
.. automodule:: sdl2.ext.draw
9+
:members:

sdl2/ext/draw.py

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,35 @@
1-
"""Drawing routines for software surfaces."""
21
import ctypes
32
from .compat import isiterable, UnsupportedError
43
from .array import to_ctypes
54
from .color import convert_to_color
65
from .. import surface, pixels, rect
76
from .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-
2612
def 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):
5847
def 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

Comments
 (0)