Skip to content

Commit 9c7bfc0

Browse files
committed
Test and fix Renderer.geometry method
1 parent ea7c3b0 commit 9c7bfc0

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](https://semver.org/) since version
1313
### Fixed
1414

1515
- `tcod.sdl.Renderer.draw_lines` type hint was too narrow.
16+
- Fixed crash in `tcod.sdl.Renderer.geometry`.
1617

1718
## [17.0.0] - 2025-03-28
1819

tcod/sdl/render.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -675,29 +675,33 @@ def draw_lines(self, points: NDArray[np.number] | Sequence[tuple[float, float]])
675675
def geometry(
676676
self,
677677
texture: Texture | None,
678-
xy: NDArray[np.float32],
679-
color: NDArray[np.uint8],
680-
uv: NDArray[np.float32],
678+
xy: NDArray[np.float32] | Sequence[tuple[float, float]],
679+
color: NDArray[np.uint8] | Sequence[tuple[int, int, int, int]],
680+
uv: NDArray[np.float32] | Sequence[tuple[float, float]],
681681
indices: NDArray[np.uint8 | np.uint16 | np.uint32] | None = None,
682682
) -> None:
683683
"""Render triangles from texture and vertex data.
684684
685+
Args:
686+
texture: The SDL texture to render from.
687+
xy: A sequence of (x, y) points to buffer.
688+
color: A sequence of (r, g, b, a) colors to buffer.
689+
uv: A sequence of (x, y) coordinates to buffer.
690+
indices: A sequence of indexes referring to the buffered data, every 3 indexes is a triangle to render.
691+
685692
.. versionadded:: 13.5
686693
"""
687-
assert xy.dtype == np.float32
694+
xy = np.ascontiguousarray(xy, np.float32)
688695
assert len(xy.shape) == 2 # noqa: PLR2004
689696
assert xy.shape[1] == 2 # noqa: PLR2004
690-
assert xy[0].flags.c_contiguous
691697

692-
assert color.dtype == np.uint8
698+
color = np.ascontiguousarray(color, np.uint8)
693699
assert len(color.shape) == 2 # noqa: PLR2004
694700
assert color.shape[1] == 4 # noqa: PLR2004
695-
assert color[0].flags.c_contiguous
696701

697-
assert uv.dtype == np.float32
702+
uv = np.ascontiguousarray(uv, np.float32)
698703
assert len(uv.shape) == 2 # noqa: PLR2004
699704
assert uv.shape[1] == 2 # noqa: PLR2004
700-
assert uv[0].flags.c_contiguous
701705
if indices is not None:
702706
assert indices.dtype.type in (np.uint8, np.uint16, np.uint32, np.int8, np.int16, np.int32)
703707
indices = np.ascontiguousarray(indices)
@@ -709,7 +713,7 @@ def geometry(
709713
texture.p if texture else ffi.NULL,
710714
ffi.cast("float*", xy.ctypes.data),
711715
xy.strides[0],
712-
ffi.cast("uint8_t*", color.ctypes.data),
716+
ffi.cast("SDL_Color*", color.ctypes.data),
713717
color.strides[0],
714718
ffi.cast("float*", uv.ctypes.data),
715719
uv.strides[0],

tests/test_sdl.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,14 @@ def test_sdl_render(uses_window: None) -> None:
9999
with pytest.raises(TypeError, match=r"must have 2 axes"):
100100
render.draw_points(np.ones((3,), dtype=dtype))
101101

102+
render.geometry(
103+
None,
104+
np.zeros((1, 2), np.float32),
105+
np.zeros((1, 4), np.uint8),
106+
np.zeros((1, 2), np.float32),
107+
np.zeros((3,), np.uint8),
108+
)
109+
102110

103111
def test_sdl_render_bad_types() -> None:
104112
with pytest.raises(TypeError):

0 commit comments

Comments
 (0)