Skip to content

Commit f91a080

Browse files
committed
removed "cache" parameter
1 parent 2d2a1c0 commit f91a080

File tree

4 files changed

+100
-111
lines changed

4 files changed

+100
-111
lines changed

buildconfig/stubs/pygame/surface.pyi

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,13 @@ class Surface:
8080
) -> Union[List[Rect], None]: ...
8181
def fblits(
8282
self,
83-
blit_sequence: Iterable[Tuple[Surface, Union[Coordinate, RectValue]]],
83+
blit_sequence: Iterable[
84+
Union[
85+
Tuple[Surface, Union[Coordinate, RectValue]],
86+
Tuple[Surface, Sequence[Union[Coordinate, RectValue]]],
87+
]
88+
],
8489
special_flags: int = 0,
85-
cache: bool = False,
8690
/,
8791
) -> None: ...
8892
@overload

docs/reST/ref/surface.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@
197197
.. method:: fblits
198198

199199
| :sl:`draw many surfaces onto the calling surface at their corresponding location and the same special_flags`
200-
| :sg:`fblits(blit_sequence=((source, dest), ...), special_flags=0, cache=False/) -> None`
200+
| :sg:`fblits(blit_sequence=((source, dest), ...), special_flags=0/) -> None`
201+
| :sg:`fblits(blit_sequence=((source, [dest1, dest2, ...]), ...), special_flags=0/) -> None`
201202
202203
This method takes a sequence of tuples (source, dest) as input, where source is a Surface
203204
object and dest is its destination position on this Surface. It draws each source Surface
@@ -208,8 +209,6 @@
208209
:param blit_sequence: a sequence of (source, dest)
209210
:param special_flags: the flag(s) representing the blend mode used for each surface.
210211
See :doc:`special_flags_list` for a list of possible values.
211-
:param cache: a boolean value that determines whether the surface should be cached
212-
for better performance for repeated blitting.
213212

214213
:returns: ``None``
215214

src_c/doc/surface_doc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define DOC_SURFACE "Surface((width, height), flags=0, depth=0, masks=None) -> Surface\nSurface((width, height), flags=0, Surface) -> Surface\npygame object for representing images"
33
#define DOC_SURFACE_BLIT "blit(source, dest, area=None, special_flags=0) -> Rect\ndraw another surface onto this one"
44
#define DOC_SURFACE_BLITS "blits(blit_sequence=((source, dest), ...), doreturn=True) -> [Rect, ...] or None\nblits(((source, dest, area), ...)) -> [Rect, ...]\nblits(((source, dest, area, special_flags), ...)) -> [Rect, ...]\ndraw many images onto another"
5-
#define DOC_SURFACE_FBLITS "fblits(blit_sequence=((source, dest), ...), special_flags=0, cache=False/) -> None\ndraw many surfaces onto the calling surface at their corresponding location and the same special_flags"
5+
#define DOC_SURFACE_FBLITS "fblits(blit_sequence=((source, dest), ...), special_flags=0/) -> None\nfblits(blit_sequence=((source, [dest1, dest2, ...]), ...), special_flags=0/) -> None\ndraw many surfaces onto the calling surface at their corresponding location and the same special_flags"
66
#define DOC_SURFACE_CONVERT "convert(surface, /) -> Surface\nconvert(depth, flags=0, /) -> Surface\nconvert(masks, flags=0, /) -> Surface\nconvert() -> Surface\nchange the pixel format of an image"
77
#define DOC_SURFACE_CONVERTALPHA "convert_alpha() -> Surface\nchange the pixel format of an image including per pixel alphas"
88
#define DOC_SURFACE_COPY "copy() -> Surface\ncreate a new copy of a Surface"

src_c/surface.c

Lines changed: 91 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -2159,91 +2159,36 @@ surf_blits(pgSurfaceObject *self, PyObject *args, PyObject *keywds)
21592159
#define FBLITS_ERR_NO_MEMORY 18
21602160
#define FBLITS_ERR_INVALID_SEQUENCE_LENGTH 19
21612161
#define FBLITS_ERR_INVALID_DESTINATION 20
2162+
#define FBLITS_ERR_INVALID_SEQUENCE 21
21622163

21632164
int
2164-
_surf_fblits_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
2165+
_surf_fblits_item_check_and_blit(PyObject *src_surf, SDL_Surface *src,
2166+
pgSurfaceObject *dest, int x, int y,
21652167
int blend_flags)
21662168
{
2167-
PyObject *src_surf, *blit_pos;
2168-
SDL_Surface *src;
2169-
SDL_Rect *src_rect, temp, dest_rect;
2170-
2171-
/* Check that the item is a tuple of length 2 */
2172-
if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
2173-
return FBLITS_ERR_TUPLE_REQUIRED;
2174-
}
2175-
2176-
/* Extract the Surface and destination objects from the
2177-
* (Surface, dest) tuple */
2178-
src_surf = PyTuple_GET_ITEM(item, 0);
2179-
blit_pos = PyTuple_GET_ITEM(item, 1);
2180-
2181-
/* Check that the source is a Surface */
2182-
if (!pgSurface_Check(src_surf)) {
2183-
return BLITS_ERR_SOURCE_NOT_SURFACE;
2184-
}
2185-
if (!(src = pgSurface_AsSurface(src_surf))) {
2186-
return BLITS_ERR_SEQUENCE_SURF;
2187-
}
2169+
SDL_Rect dest_rect = {x, y, src->w, src->h};
21882170

2189-
/* Try to extract a valid blit position */
2190-
if (pg_TwoIntsFromObj(blit_pos, &dest_rect.x, &dest_rect.y)) {
2191-
}
2192-
else if ((src_rect = pgRect_FromObject(blit_pos, &temp))) {
2193-
dest_rect.x = src_rect->x;
2194-
dest_rect.y = src_rect->y;
2195-
}
2196-
else {
2197-
return BLITS_ERR_INVALID_DESTINATION;
2198-
}
2199-
2200-
dest_rect.w = src->w;
2201-
dest_rect.h = src->h;
2202-
2203-
/* Perform the blit */
2204-
if (pgSurface_Blit(self, (pgSurfaceObject *)src_surf, &dest_rect, NULL,
2205-
blend_flags)) {
2171+
if (pgSurface_Blit(dest, (pgSurfaceObject *)src_surf, &dest_rect, NULL,
2172+
blend_flags))
22062173
return BLITS_ERR_BLIT_FAIL;
2207-
}
22082174

22092175
return 0;
22102176
}
22112177

22122178
int
2213-
_surf_fblits_cached_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
2179+
_surf_fblits_cached_item_check_and_blit(pgSurfaceObject *self,
2180+
SDL_Surface *src, SDL_Surface *dst,
2181+
PyObject *pos_sequence,
22142182
int blend_flags,
22152183
BlitSequence *destinations)
22162184
{
2217-
PyObject *src_surf, *pos_list;
2218-
SDL_Surface *src, *dst = pgSurface_AsSurface(self);
2185+
PyObject *src_surf;
22192186
SDL_Surface *subsurface;
22202187
int suboffsetx = 0, suboffsety = 0;
22212188
SDL_Rect orig_clip, sub_clip;
22222189
int error = 0;
22232190
Py_ssize_t i;
22242191

2225-
/* Check that the item is a tuple of length 2 */
2226-
if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) {
2227-
return FBLITS_ERR_TUPLE_REQUIRED;
2228-
}
2229-
2230-
/* Extract the Surface and sequence of destination objects from the
2231-
* (Surface, positions) tuple */
2232-
src_surf = PyTuple_GET_ITEM(item, 0);
2233-
pos_list = PyTuple_GET_ITEM(item, 1);
2234-
2235-
if (!PyList_Check(pos_list)) {
2236-
return BLITS_ERR_SEQUENCE_REQUIRED;
2237-
}
2238-
2239-
/* Check that the source is a Surface */
2240-
if (!pgSurface_Check(src_surf)) {
2241-
return BLITS_ERR_SOURCE_NOT_SURFACE;
2242-
}
2243-
if (!(src = pgSurface_AsSurface(src_surf))) {
2244-
return BLITS_ERR_SEQUENCE_SURF;
2245-
}
2246-
22472192
/* Check that the source and destination surfaces have the same format */
22482193
if (src->format->format != dst->format->format ||
22492194
src->format->BytesPerPixel != dst->format->BytesPerPixel ||
@@ -2258,7 +2203,7 @@ _surf_fblits_cached_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
22582203
}
22592204

22602205
/* manage destinations memory */
2261-
Py_ssize_t new_size = PyList_GET_SIZE(pos_list);
2206+
Py_ssize_t new_size = PySequence_Fast_GET_SIZE(pos_sequence);
22622207
if (new_size > destinations->alloc_size) {
22632208
destinations->sequence = (CachedBlitDest *)realloc(
22642209
destinations->sequence, new_size * sizeof(CachedBlitDest));
@@ -2311,12 +2256,12 @@ _surf_fblits_cached_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
23112256
}
23122257

23132258
/* load destinations */
2314-
PyObject **list_items = PySequence_Fast_ITEMS(pos_list);
2259+
PyObject **seq_items = PySequence_Fast_ITEMS(pos_sequence);
23152260
Py_ssize_t current_size = 0;
23162261
SDL_Rect src_dest = {0, 0, src->w, src->h};
23172262
SDL_Rect temp, *argrect;
23182263
for (i = 0; i < destinations->size; i++) {
2319-
PyObject *item = list_items[i];
2264+
PyObject *item = seq_items[i];
23202265

23212266
if (pg_TwoIntsFromObj(item, &src_dest.x, &src_dest.y)) {
23222267
}
@@ -2372,6 +2317,70 @@ _surf_fblits_cached_item_check_and_blit(pgSurfaceObject *self, PyObject *item,
23722317
return error;
23732318
}
23742319

2320+
static void
2321+
_surf_fblits_blit(pgSurfaceObject *self, PyObject *item, int blend_flags,
2322+
BlitSequence *destinations, int *error)
2323+
{
2324+
PyObject *src_surf, *pos_or_seq;
2325+
SDL_Surface *src, *dst = pgSurface_AsSurface(self);
2326+
if (!dst) {
2327+
*error = BLITS_ERR_DISPLAY_SURF_QUIT;
2328+
return;
2329+
}
2330+
2331+
int x, y;
2332+
2333+
if (PyTuple_Check(item) && PyTuple_GET_SIZE(item) == 2) {
2334+
/* (Surface, dest) */
2335+
src_surf = PyTuple_GET_ITEM(item, 0);
2336+
pos_or_seq = PyTuple_GET_ITEM(item, 1);
2337+
}
2338+
else {
2339+
*error = FBLITS_ERR_TUPLE_REQUIRED;
2340+
return;
2341+
}
2342+
2343+
/* Check that the source is a Surface */
2344+
if (!pgSurface_Check(src_surf)) {
2345+
*error = BLITS_ERR_SOURCE_NOT_SURFACE;
2346+
return;
2347+
}
2348+
if (!(src = pgSurface_AsSurface(src_surf))) {
2349+
*error = BLITS_ERR_SEQUENCE_SURF;
2350+
return;
2351+
}
2352+
2353+
if (pgRect_Check(pos_or_seq)) {
2354+
SDL_Rect *r = &pgRect_AsRect(pos_or_seq);
2355+
x = r->x;
2356+
y = r->y;
2357+
*error = _surf_fblits_item_check_and_blit(src_surf, src, self, x, y,
2358+
blend_flags);
2359+
return;
2360+
}
2361+
else if (pgFRect_Check(pos_or_seq)) {
2362+
SDL_FRect *r = &pgFRect_AsRect(pos_or_seq);
2363+
x = (int)r->x;
2364+
y = (int)r->y;
2365+
*error = _surf_fblits_item_check_and_blit(src_surf, src, self, x, y,
2366+
blend_flags);
2367+
return;
2368+
}
2369+
else if (pg_TwoIntsFromObj(pos_or_seq, &x, &y)) {
2370+
*error = _surf_fblits_item_check_and_blit(src_surf, src, self, x, y,
2371+
blend_flags);
2372+
return;
2373+
}
2374+
2375+
if (!pgSequenceFast_Check(pos_or_seq)) {
2376+
*error = FBLITS_ERR_INVALID_SEQUENCE;
2377+
return;
2378+
}
2379+
2380+
*error = _surf_fblits_cached_item_check_and_blit(
2381+
self, src, dst, pos_or_seq, blend_flags, destinations);
2382+
}
2383+
23752384
static PyObject *
23762385
surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
23772386
{
@@ -2382,15 +2391,14 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
23822391
int blend_flags = 0; /* Default flag is 0, opaque */
23832392
int error = 0;
23842393
int is_generator = 0;
2385-
int cache = 0;
23862394
BlitSequence destinations = {NULL, 0, 0};
23872395

2388-
if (nargs == 0 || nargs > 3) {
2396+
if (nargs == 0 || nargs > 2) {
23892397
error = FBLITS_ERR_INCORRECT_ARGS_NUM;
23902398
goto on_error;
23912399
}
23922400
/* Get the blend flags if they are passed */
2393-
else if (nargs >= 2) {
2401+
else if (nargs == 2) {
23942402
if (!PyLong_Check(args[1])) {
23952403
error = FBLITS_ERR_FLAG_NOT_NUMERIC;
23962404
goto on_error;
@@ -2399,17 +2407,6 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
23992407
if (PyErr_Occurred()) {
24002408
return NULL;
24012409
}
2402-
2403-
if (nargs == 3) {
2404-
if (!PyBool_Check(args[2])) {
2405-
error = FBLITS_ERR_CACHE_NOT_NUMERIC;
2406-
goto on_error;
2407-
}
2408-
cache = PyObject_IsTrue(args[2]);
2409-
if (PyErr_Occurred()) {
2410-
return NULL;
2411-
}
2412-
}
24132410
}
24142411

24152412
blit_sequence = args[0];
@@ -2420,33 +2417,21 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
24202417
PyObject **sequence_items = PySequence_Fast_ITEMS(blit_sequence);
24212418
for (i = 0; i < PySequence_Fast_GET_SIZE(blit_sequence); i++) {
24222419
item = sequence_items[i];
2423-
if (cache) {
2424-
error = _surf_fblits_cached_item_check_and_blit(
2425-
self, item, blend_flags, &destinations);
2426-
}
2427-
else {
2428-
error =
2429-
_surf_fblits_item_check_and_blit(self, item, blend_flags);
2430-
}
2431-
if (error) {
2420+
2421+
_surf_fblits_blit(self, item, blend_flags, &destinations, &error);
2422+
2423+
if (error)
24322424
goto on_error;
2433-
}
24342425
}
24352426
}
24362427
else if (PyIter_Check(blit_sequence)) {
24372428
is_generator = 1;
24382429
while ((item = PyIter_Next(blit_sequence))) {
2439-
if (cache) {
2440-
error = _surf_fblits_cached_item_check_and_blit(
2441-
self, item, blend_flags, &destinations);
2442-
}
2443-
else {
2444-
error =
2445-
_surf_fblits_item_check_and_blit(self, item, blend_flags);
2446-
}
2447-
if (error) {
2430+
_surf_fblits_blit(self, item, blend_flags, &destinations, &error);
2431+
2432+
if (error)
24482433
goto on_error;
2449-
}
2434+
24502435
Py_DECREF(item);
24512436
}
24522437

@@ -2521,11 +2506,12 @@ surf_fblits(pgSurfaceObject *self, PyObject *const *args, Py_ssize_t nargs)
25212506
case FBLITS_ERR_NO_MEMORY:
25222507
return RAISE(PyExc_MemoryError, "No memory available");
25232508
case FBLITS_ERR_INVALID_SEQUENCE_LENGTH:
2524-
return RAISE(PyExc_ValueError,
2525-
"Invalid sequence length for cached blit");
2509+
return RAISE(PyExc_ValueError, "Invalid sequence length for blit");
25262510
case FBLITS_ERR_INVALID_DESTINATION:
25272511
return RAISE(PyExc_TypeError,
2528-
"Invalid destination position for cached blit");
2512+
"Invalid destination position for blit");
2513+
case FBLITS_ERR_INVALID_SEQUENCE:
2514+
return RAISE(PyExc_TypeError, "Invalid sequence for multi-blit");
25292515
}
25302516
return RAISE(PyExc_TypeError, "Unknown error");
25312517
}

0 commit comments

Comments
 (0)