|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import Any, Optional, Tuple |
| 4 | + |
| 5 | +import numpy as np |
| 6 | +from numpy.typing import NDArray |
| 7 | + |
| 8 | +from tcod.loader import ffi, lib |
| 9 | +from tcod.sdl import _check |
| 10 | + |
| 11 | + |
| 12 | +class Texture: |
| 13 | + def __init__(self, sdl_texture_p: Any, sdl_renderer_p: Any = None) -> None: |
| 14 | + self.p = sdl_texture_p |
| 15 | + self._sdl_renderer_p = sdl_renderer_p # Keep alive. |
| 16 | + |
| 17 | + def __eq__(self, other: Any) -> bool: |
| 18 | + return bool(self.p == getattr(other, "p", None)) |
| 19 | + |
| 20 | + def _query(self) -> Tuple[int, int, int, int]: |
| 21 | + """Return (format, access, width, height).""" |
| 22 | + format = ffi.new("uint32_t*") |
| 23 | + buffer = ffi.new("int[3]") |
| 24 | + lib.SDL_QueryTexture(self.p, format, buffer, buffer + 1, buffer + 2) |
| 25 | + return int(format), int(buffer[0]), int(buffer[1]), int(buffer[2]) |
| 26 | + |
| 27 | + @property |
| 28 | + def format(self) -> int: |
| 29 | + """Texture format, read only.""" |
| 30 | + buffer = ffi.new("uint32_t*") |
| 31 | + lib.SDL_QueryTexture(self.p, buffer, ffi.NULL, ffi.NULL, ffi.NULL) |
| 32 | + return int(buffer[0]) |
| 33 | + |
| 34 | + @property |
| 35 | + def access(self) -> int: |
| 36 | + """Texture access mode, read only.""" |
| 37 | + buffer = ffi.new("int*") |
| 38 | + lib.SDL_QueryTexture(self.p, ffi.NULL, buffer, ffi.NULL, ffi.NULL) |
| 39 | + return int(buffer[0]) |
| 40 | + |
| 41 | + @property |
| 42 | + def width(self) -> int: |
| 43 | + """Texture pixel width, read only.""" |
| 44 | + buffer = ffi.new("int*") |
| 45 | + lib.SDL_QueryTexture(self.p, ffi.NULL, ffi.NULL, buffer, ffi.NULL) |
| 46 | + return int(buffer[0]) |
| 47 | + |
| 48 | + @property |
| 49 | + def height(self) -> int: |
| 50 | + """Texture pixel height, read only.""" |
| 51 | + buffer = ffi.new("int*") |
| 52 | + lib.SDL_QueryTexture(self.p, ffi.NULL, ffi.NULL, ffi.NULL, buffer) |
| 53 | + return int(buffer[0]) |
| 54 | + |
| 55 | + @property |
| 56 | + def alpha_mod(self) -> int: |
| 57 | + """Texture alpha modulate value, can be set to: 0 - 255.""" |
| 58 | + return int(lib.SDL_GetTextureAlphaMod(self.p)) |
| 59 | + |
| 60 | + @alpha_mod.setter |
| 61 | + def alpha_mod(self, value: int) -> None: |
| 62 | + _check(lib.SDL_SetTextureAlphaMod(self.p, value)) |
| 63 | + |
| 64 | + @property |
| 65 | + def blend_mode(self) -> int: |
| 66 | + """Texture blend mode, can be set.""" |
| 67 | + return int(lib.SDL_GetTextureBlendMode(self.p)) |
| 68 | + |
| 69 | + @blend_mode.setter |
| 70 | + def blend_mode(self, value: int) -> None: |
| 71 | + _check(lib.SDL_SetTextureBlendMode(self.p, value)) |
| 72 | + |
| 73 | + @property |
| 74 | + def rgb_mod(self) -> Tuple[int, int, int]: |
| 75 | + """Texture RGB color modulate values, can be set.""" |
| 76 | + rgb = ffi.new("uint8_t[3]") |
| 77 | + _check(lib.SDL_GetTextureColorMod(self.p, rgb, rgb + 1, rgb + 2)) |
| 78 | + return int(rgb[0]), int(rgb[1]), int(rgb[2]) |
| 79 | + |
| 80 | + @rgb_mod.setter |
| 81 | + def rgb_mod(self, rgb: Tuple[int, int, int]) -> None: |
| 82 | + _check(lib.SDL_SetTextureColorMod(self.p, rgb[0], rgb[1], rgb[2])) |
| 83 | + |
| 84 | + |
| 85 | +class Renderer: |
| 86 | + def __init__(self, sdl_renderer_p: Any) -> None: |
| 87 | + if ffi.typeof(sdl_renderer_p) is not ffi.typeof("struct SDL_Renderer*"): |
| 88 | + raise TypeError(f"Expected a {ffi.typeof('struct SDL_Window*')} type (was {ffi.typeof(sdl_renderer_p)}).") |
| 89 | + self.p = sdl_renderer_p |
| 90 | + |
| 91 | + def __eq__(self, other: Any) -> bool: |
| 92 | + return bool(self.p == getattr(other, "p", None)) |
| 93 | + |
| 94 | + def new_texture( |
| 95 | + self, width: int, height: int, *, format: Optional[int] = None, access: Optional[int] = None |
| 96 | + ) -> Texture: |
| 97 | + """Allocate and return a new Texture for this renderer.""" |
| 98 | + if format is None: |
| 99 | + format = 0 |
| 100 | + if access is None: |
| 101 | + access = int(lib.SDL_TEXTUREACCESS_STATIC) |
| 102 | + format = int(lib.SDL_PIXELFORMAT_RGBA32) |
| 103 | + access = int(lib.SDL_TEXTUREACCESS_STATIC) |
| 104 | + texture_p = ffi.gc(lib.SDL_CreateTexture(self.p, format, access, width, height), lib.SDL_DestroyTexture) |
| 105 | + return Texture(texture_p, self.p) |
| 106 | + |
| 107 | + def upload_texture( |
| 108 | + self, pixels: NDArray[Any], *, format: Optional[int] = None, access: Optional[int] = None |
| 109 | + ) -> Texture: |
| 110 | + """Return a new Texture from an array of pixels.""" |
| 111 | + if format is None: |
| 112 | + assert len(pixels.shape) == 3 |
| 113 | + assert pixels.dtype == np.uint8 |
| 114 | + if pixels.shape[2] == 4: |
| 115 | + format = int(lib.SDL_PIXELFORMAT_RGBA32) |
| 116 | + elif pixels.shape[2] == 3: |
| 117 | + format = int(lib.SDL_PIXELFORMAT_RGB32) |
| 118 | + else: |
| 119 | + assert False |
| 120 | + |
| 121 | + texture = self.new_texture(pixels.shape[1], pixels.shape[0], format=format, access=access) |
| 122 | + if not pixels[0].flags["C_CONTIGUOUS"]: |
| 123 | + pixels = np.ascontiguousarray(pixels) |
| 124 | + _check( |
| 125 | + lib.SDL_UpdateTexture(texture.p, ffi.NULL, ffi.cast("const void*", pixels.ctypes.data), pixels.strides[0]) |
| 126 | + ) |
| 127 | + return texture |
0 commit comments