6
6
from __future__ import annotations
7
7
8
8
import enum
9
- from collections .abc import Sequence
10
9
from typing import TYPE_CHECKING , Any , Final , Literal
11
10
12
11
import numpy as np
18
17
from tcod .sdl ._internal import Properties , _check , _check_p
19
18
20
19
if TYPE_CHECKING :
20
+ from collections .abc import Sequence
21
+
21
22
from numpy .typing import NDArray
22
23
23
24
@@ -186,7 +187,7 @@ class Texture:
186
187
Create a new texture using :any:`Renderer.new_texture` or :any:`Renderer.upload_texture`.
187
188
"""
188
189
189
- def __init__ (self , sdl_texture_p : Any , sdl_renderer_p : Any = None ) -> None :
190
+ def __init__ (self , sdl_texture_p : Any , sdl_renderer_p : Any = None ) -> None : # noqa: ANN401
190
191
"""Encapsulate an SDL_Texture pointer. This function is private."""
191
192
self .p = sdl_texture_p
192
193
self ._sdl_renderer_p = sdl_renderer_p # Keep alive.
@@ -213,6 +214,10 @@ def __eq__(self, other: object) -> bool:
213
214
return bool (self .p == other .p )
214
215
return NotImplemented
215
216
217
+ def __hash__ (self ) -> int :
218
+ """Return hash for the owned pointer."""
219
+ return hash (self .p )
220
+
216
221
def update (self , pixels : NDArray [Any ], rect : tuple [int , int , int , int ] | None = None ) -> None :
217
222
"""Update the pixel data of this texture.
218
223
@@ -294,7 +299,7 @@ def __exit__(self, *_: object) -> None:
294
299
class Renderer :
295
300
"""SDL Renderer."""
296
301
297
- def __init__ (self , sdl_renderer_p : Any ) -> None :
302
+ def __init__ (self , sdl_renderer_p : Any ) -> None : # noqa: ANN401
298
303
"""Encapsulate an SDL_Renderer pointer. This function is private."""
299
304
if ffi .typeof (sdl_renderer_p ) is not ffi .typeof ("struct SDL_Renderer*" ):
300
305
msg = f"Expected a { ffi .typeof ('struct SDL_Window*' )} type (was { ffi .typeof (sdl_renderer_p )} )."
@@ -310,6 +315,10 @@ def __eq__(self, other: object) -> bool:
310
315
return bool (self .p == other .p )
311
316
return NotImplemented
312
317
318
+ def __hash__ (self ) -> int :
319
+ """Return hash for the owned pointer."""
320
+ return hash (self .p )
321
+
313
322
def copy ( # noqa: PLR0913
314
323
self ,
315
324
texture : Texture ,
@@ -355,7 +364,7 @@ def set_render_target(self, texture: Texture) -> _RestoreTargetContext:
355
364
_check (lib .SDL_SetRenderTarget (self .p , texture .p ))
356
365
return restore
357
366
358
- def new_texture (self , width : int , height : int , * , format : int | None = None , access : int | None = None ) -> Texture :
367
+ def new_texture (self , width : int , height : int , * , format : int | None = None , access : int | None = None ) -> Texture : # noqa: A002
359
368
"""Allocate and return a new Texture for this renderer.
360
369
361
370
Args:
@@ -366,13 +375,13 @@ def new_texture(self, width: int, height: int, *, format: int | None = None, acc
366
375
See :any:`TextureAccess` for more options.
367
376
"""
368
377
if format is None :
369
- format = 0
378
+ format = 0 # noqa: A001
370
379
if access is None :
371
380
access = int (lib .SDL_TEXTUREACCESS_STATIC )
372
381
texture_p = ffi .gc (lib .SDL_CreateTexture (self .p , format , access , width , height ), lib .SDL_DestroyTexture )
373
382
return Texture (texture_p , self .p )
374
383
375
- def upload_texture (self , pixels : NDArray [Any ], * , format : int | None = None , access : int | None = None ) -> Texture :
384
+ def upload_texture (self , pixels : NDArray [Any ], * , format : int | None = None , access : int | None = None ) -> Texture : # noqa: A002
376
385
"""Return a new Texture from an array of pixels.
377
386
378
387
Args:
@@ -385,9 +394,9 @@ def upload_texture(self, pixels: NDArray[Any], *, format: int | None = None, acc
385
394
assert len (pixels .shape ) == 3 # noqa: PLR2004
386
395
assert pixels .dtype == np .uint8
387
396
if pixels .shape [2 ] == 4 : # noqa: PLR2004
388
- format = int (lib .SDL_PIXELFORMAT_RGBA32 )
397
+ format = int (lib .SDL_PIXELFORMAT_RGBA32 ) # noqa: A001
389
398
elif pixels .shape [2 ] == 3 : # noqa: PLR2004
390
- format = int (lib .SDL_PIXELFORMAT_RGB24 )
399
+ format = int (lib .SDL_PIXELFORMAT_RGB24 ) # noqa: A001
391
400
else :
392
401
msg = f"Can't determine the format required for an array of shape { pixels .shape } ."
393
402
raise TypeError (msg )
@@ -529,7 +538,7 @@ def viewport(self) -> tuple[int, int, int, int] | None:
529
538
def viewport (self , rect : tuple [int , int , int , int ] | None ) -> None :
530
539
_check (lib .SDL_SetRenderViewport (self .p , (rect ,)))
531
540
532
- def set_vsync (self , enable : bool ) -> None :
541
+ def set_vsync (self , enable : bool ) -> None : # noqa: FBT001
533
542
"""Enable or disable VSync for this renderer.
534
543
535
544
.. versionadded:: 13.5
@@ -652,7 +661,7 @@ def fill_rects(self, rects: NDArray[np.number] | Sequence[tuple[float, float, fl
652
661
.. versionadded:: 13.5
653
662
"""
654
663
rects = self ._convert_array (rects , item_length = 4 )
655
- _check (lib .SDL_RenderFillRects (self .p , tcod . ffi .from_buffer ("SDL_FRect*" , rects ), rects .shape [0 ]))
664
+ _check (lib .SDL_RenderFillRects (self .p , ffi .from_buffer ("SDL_FRect*" , rects ), rects .shape [0 ]))
656
665
657
666
def draw_rects (self , rects : NDArray [np .number ] | Sequence [tuple [float , float , float , float ]]) -> None :
658
667
"""Draw multiple outlined rectangles from an array.
@@ -665,7 +674,7 @@ def draw_rects(self, rects: NDArray[np.number] | Sequence[tuple[float, float, fl
665
674
rects = self ._convert_array (rects , item_length = 4 )
666
675
assert len (rects .shape ) == 2 # noqa: PLR2004
667
676
assert rects .shape [1 ] == 4 # noqa: PLR2004
668
- _check (lib .SDL_RenderRects (self .p , tcod . ffi .from_buffer ("SDL_FRect*" , rects ), rects .shape [0 ]))
677
+ _check (lib .SDL_RenderRects (self .p , ffi .from_buffer ("SDL_FRect*" , rects ), rects .shape [0 ]))
669
678
670
679
def draw_points (self , points : NDArray [np .number ] | Sequence [tuple [float , float ]]) -> None :
671
680
"""Draw an array of points.
@@ -676,7 +685,7 @@ def draw_points(self, points: NDArray[np.number] | Sequence[tuple[float, float]]
676
685
.. versionadded:: 13.5
677
686
"""
678
687
points = self ._convert_array (points , item_length = 2 )
679
- _check (lib .SDL_RenderPoints (self .p , tcod . ffi .from_buffer ("SDL_FPoint*" , points ), points .shape [0 ]))
688
+ _check (lib .SDL_RenderPoints (self .p , ffi .from_buffer ("SDL_FPoint*" , points ), points .shape [0 ]))
680
689
681
690
def draw_lines (self , points : NDArray [np .number ] | Sequence [tuple [float , float ]]) -> None :
682
691
"""Draw a connected series of lines from an array.
@@ -687,7 +696,7 @@ def draw_lines(self, points: NDArray[np.number] | Sequence[tuple[float, float]])
687
696
.. versionadded:: 13.5
688
697
"""
689
698
points = self ._convert_array (points , item_length = 2 )
690
- _check (lib .SDL_RenderLines (self .p , tcod . ffi .from_buffer ("SDL_FPoint*" , points ), points .shape [0 ]))
699
+ _check (lib .SDL_RenderLines (self .p , ffi .from_buffer ("SDL_FPoint*" , points ), points .shape [0 ]))
691
700
692
701
def geometry (
693
702
self ,
0 commit comments