Skip to content

Commit 6f1b22c

Browse files
committed
Add extended copy parameters.
Fix tests.
1 parent c0e7401 commit 6f1b22c

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

tcod/sdl/render.py

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ class TextureAccess(enum.IntEnum):
2626
"""Texture will be used as a render target."""
2727

2828

29+
class RendererFlip(enum.IntFlag):
30+
"""Flip parameter for :any:`Renderer.copy`."""
31+
32+
NONE = 0
33+
"""Default value, no flip."""
34+
HORIZONTAL = 1
35+
"""Flip the image horizontally."""
36+
VERTICAL = 2
37+
"""Flip the image vertically."""
38+
39+
2940
class Texture:
3041
"""SDL hardware textures."""
3142

@@ -146,16 +157,37 @@ def __eq__(self, other: Any) -> bool:
146157
def copy(
147158
self,
148159
texture: Texture,
149-
source: Optional[Tuple[int, int, int, int]] = None,
150-
dest: Optional[Tuple[int, int, int, int]] = None,
160+
source: Optional[Tuple[float, float, float, float]] = None,
161+
dest: Optional[Tuple[float, float, float, float]] = None,
162+
angle: float = 0,
163+
center: Optional[Tuple[float, float]] = None,
164+
flip: RendererFlip = RendererFlip.NONE,
151165
) -> None:
152166
"""Copy a texture to the rendering target.
153167
154-
`source` and `dest` are (x, y, width, height) regions of the texture parameter and target texture respectively.
168+
Args:
169+
texture: The texture to copy onto the current texture target.
170+
source: The (x, y, width, height) region of `texture` to copy. If None then the entire texture is copied.
171+
dest: The (x, y, width, height) region of the target. If None then the entire target is drawn over.
172+
angle: The angle in degrees to rotate the image clockwise.
173+
center: The (x, y) point where rotation is applied. If None then the center of `dest` is used.
174+
flip: Flips the `texture` when drawing it.
175+
176+
.. versionchanged:: unreleased
177+
`source` and `dest` can now be float tuples.
178+
Added the `angle`, `center`, and `flip` parameters.
155179
"""
156-
source_ = ffi.NULL if source is None else ffi.new("SDL_Rect*", source)
157-
dest_ = ffi.NULL if dest is None else ffi.new("SDL_Rect*", dest)
158-
_check(lib.SDL_RenderCopy(self.p, texture.p, source_, dest_))
180+
_check(
181+
lib.SDL_RenderCopyExF(
182+
self.p,
183+
texture.p,
184+
(source,) if source is not None else ffi.NULL,
185+
(dest,) if dest is not None else ffi.NULL,
186+
angle,
187+
(center,) if center is not None else ffi.NULL,
188+
flip,
189+
)
190+
)
159191

160192
def present(self) -> None:
161193
"""Present the currently rendered image to the screen."""

tests/test_sdl.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
import tcod.sdl.render
7+
import tcod.sdl.sys
78
import tcod.sdl.video
89

910

@@ -44,6 +45,7 @@ def test_sdl_window_bad_types() -> None:
4445

4546

4647
def test_sdl_screen_saver() -> None:
48+
tcod.sdl.sys.init()
4749
assert tcod.sdl.video.screen_saver_allowed(False) is False
4850
assert tcod.sdl.video.screen_saver_allowed(True) is True
4951
assert tcod.sdl.video.screen_saver_allowed() is True

0 commit comments

Comments
 (0)