def __init__(self, cost: Any, diagonal: float = 1.41): self.cost = cost self.diagonal = diagonal self._path_c = None # type: Any self._callback = self._userdata = None if hasattr(self.cost, "map_c"): self.shape = self.cost.width, self.cost.height self._path_c = ffi.gc( self._path_new_using_map(self.cost.map_c, diagonal), self._path_delete, ) return if not hasattr(self.cost, "get_tcod_path_ffi"): assert not callable(self.cost), ( "Any callback alone is missing shape information. " "Wrap your callback in tcod.path.EdgeCostCallback") self.cost = NodeCostArray(self.cost) ( self._callback, self._userdata, self.shape, ) = self.cost.get_tcod_path_ffi() self._path_c = ffi.gc( self._path_new_using_function( self.cost.shape[0], self.cost.shape[1], self._callback, self._userdata, diagonal, ), self._path_delete, )
def _claim(cls, cdata: Any) -> "Tileset": """Return a new Tileset that owns the provided TCOD_Tileset* object.""" self = object.__new__(cls) # type: Tileset if cdata == ffi.NULL: raise RuntimeError("Tileset initialized with nullptr.") self._tileset_p = ffi.gc(cdata, lib.TCOD_tileset_delete) return self
def __init__( self, dimensions: int, algorithm: int = 2, implementation: int = SIMPLE, hurst: float = 0.5, lacunarity: float = 2.0, octaves: float = 4, seed: Optional[tcod.random.Random] = None, ): if not 0 < dimensions <= 4: raise ValueError( "dimensions must be in range 0 < n <= 4, got %r" % (dimensions,) ) self._random = seed _random_c = seed.random_c if seed else ffi.NULL self._algorithm = algorithm self.noise_c = ffi.gc( ffi.cast( "struct TCOD_Noise*", lib.TCOD_noise_new(dimensions, hurst, lacunarity, _random_c), ), lib.TCOD_noise_delete, ) self._tdl_noise_c = ffi.new( "TDLNoise*", (self.noise_c, dimensions, 0, octaves) ) self.implementation = implementation # sanity check
def __init__( self, algorithm: int = MERSENNE_TWISTER, seed: Optional[Hashable] = None, ): """Create a new instance using this algorithm and seed.""" if seed is None: seed = random.getrandbits(32) elif not isinstance(seed, int): warnings.warn( "In the future this class will only accept integer seeds.", DeprecationWarning, stacklevel=2, ) if __debug__ and "PYTHONHASHSEED" not in os.environ: warnings.warn( "Python's hash algorithm is not configured to be" " deterministic so this non-integer seed will not be" " deterministic." "\nYou should do one of the following to fix this error:" "\n* Use an integer as a seed instead (recommended.)" "\n* Set the PYTHONHASHSEED environment variable before" " starting Python.", RuntimeWarning, stacklevel=2, ) seed = hash(seed) self.random_c = ffi.gc( ffi.cast( "mersenne_data_t*", lib.TCOD_random_new_from_seed(algorithm, seed & 0xFFFFFFFF), ), lib.TCOD_random_delete, )
def _as_cdata(self) -> Any: cdata = ffi.gc( lib.TCOD_bsp_new_with_size(self.x, self.y, self.width, self.height), lib.TCOD_bsp_delete, ) cdata.level = self.level return cdata
def __init__(self, graph: CustomGraph): self._graph = graph self._frontier_p = ffi.gc(lib.TCOD_frontier_new(self._graph._ndim), lib.TCOD_frontier_delete) self._distance = maxarray(self._graph._shape) self._travel = _world_array(self._graph._shape) assert self._travel.flags["C_CONTIGUOUS"] self._distance_p = _export(self._distance) self._travel_p = _export(self._travel) self._heuristic = ( None) # type: Optional[Tuple[int, int, int, int, Tuple[int, ...]]] self._heuristic_p = ffi.NULL # type: Any
def __init__( self, algorithm: int = MERSENNE_TWISTER, seed: Optional[Hashable] = None, ): """Create a new instance using this algorithm and seed.""" if seed is None: seed = random.getrandbits(32) self.random_c = ffi.gc( ffi.cast( "mersenne_data_t*", lib.TCOD_random_new_from_seed(algorithm, hash(seed) % (1 << 32)), ), lib.TCOD_random_delete, )
def load(filename: str) -> np.ndarray: """Load a PNG file as an RGBA array. `filename` is the name of the file to load. The returned array is in the shape: `(height, width, RGBA)`. .. versionadded:: 11.4 """ image = Image._from_cdata( ffi.gc(lib.TCOD_image_load(filename.encode()), lib.TCOD_image_delete)) array = np.asarray(image, dtype=np.uint8) height, width, depth = array.shape if depth == 3: array = np.concatenate( ( array, np.full((height, width, 1), fill_value=255, dtype=np.uint8), ), axis=2, ) return array
def render(self, console: tcod.console.Console) -> np.ndarray: """Render an RGBA array, using console with this tileset. `console` is the Console object to render, this can not be the root console. The output array will be a np.uint8 array with the shape of: ``(con_height * tile_height, con_width * tile_width, 4)``. .. versionadded:: 11.9 """ if not console: raise ValueError("'console' must not be the root console.") width = console.width * self.tile_width height = console.height * self.tile_height out = np.empty((height, width, 4), np.uint8) out[:] = 9 surface_p = ffi.gc( lib.SDL_CreateRGBSurfaceWithFormatFrom( ffi.cast("void*", out.ctypes.data), width, height, 32, out.strides[0], lib.SDL_PIXELFORMAT_RGBA32, ), lib.SDL_FreeSurface, ) with surface_p: with ffi.new("SDL_Surface**", surface_p) as surface_p_p: _check( lib.TCOD_tileset_render_to_surface( self._tileset_p, _console(console), ffi.NULL, surface_p_p, ) ) return out
def __init__(self, pixels: np.ndarray) -> None: self._array = np.ascontiguousarray(pixels, dtype=np.uint8) if len(self._array) != 3: raise TypeError("NumPy shape must be 3D [y, x, ch] (got %r)" % (self._array.shape, )) if 3 <= self._array.shape[2] <= 4: raise TypeError( "NumPy array must have RGB or RGBA channels. (got %r)" % (self._array.shape, )) self.p = ffi.gc( lib.SDL_CreateRGBSurfaceFrom( ffi.from_buffer("void*", self._array), self._array.shape[1], # Width. self._array.shape[0], # Height. self._array.shape[2] * 8, # Bit depth. self._array.strides[1], # Pitch. 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000 if self._array.shape[2] == 4 else 0, ), lib.SDL_FreeSurface, )
def __init__(self, tile_width: int, tile_height: int) -> None: self._tileset_p = ffi.gc( lib.TCOD_tileset_new(tile_width, tile_height), lib.TCOD_tileset_delete, )
def __init__(self, width: int, height: int): self.width, self.height = width, height self.image_c = ffi.gc(lib.TCOD_image_new(width, height), lib.TCOD_image_delete)
def _claim(cls, context_p: Any) -> "Context": return cls(ffi.gc(context_p, lib.TCOD_context_delete))