How to use the tcod.loader.ffi function in tcod

To help you get started, we’ve selected a few tcod examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github libtcod / python-tcod / tcod / _internal.py View on Github external
def _console(console: Any) -> Any:
    """Return a cffi console."""
    try:
        return console.console_c
    except AttributeError:
        warnings.warn(
            (
                "Falsy console parameters are deprecated, "
                "always use the root console instance returned by "
                "console_init_root."
            ),
            DeprecationWarning,
            stacklevel=3,
        )
        return ffi.NULL
github libtcod / python-tcod / tcod / libtcodpy.py View on Github external
self,
        dest: tcod.console.Console,
        fill_fore: bool = True,
        fill_back: bool = True,
    ) -> None:
        """Use libtcod's "fill" functions to write the buffer to a console.

        Args:
            dest (Console): Console object to modify.
            fill_fore (bool):
                If True, fill the foreground color and characters.
            fill_back (bool):
                If True, fill the background color.
        """
        if not dest:
            dest = tcod.console.Console._from_cdata(ffi.NULL)
        if dest.width != self.width or dest.height != self.height:
            raise ValueError(
                "ConsoleBuffer.blit: "
                "Destination console has an incorrect size."
            )

        if fill_back:
            bg = dest.bg.ravel()
            bg[0::3] = self.back_r
            bg[1::3] = self.back_g
            bg[2::3] = self.back_b

        if fill_fore:
            fg = dest.fg.ravel()
            fg[0::3] = self.fore_r
            fg[1::3] = self.fore_g
github libtcod / python-tcod / tcod / event.py View on Github external
def _pixel_to_tile(x: float, y: float) -> Tuple[float, float]:
    """Convert pixel coordinates to tile coordinates."""
    if not lib.TCOD_ctx.engine:
        return 0, 0
    xy = ffi.new("double[2]", (x, y))
    lib.TCOD_sys_pixel_to_tile(xy, xy + 1)
    return xy[0], xy[1]
github libtcod / python-tcod / tcod / console.py View on Github external
def close(self) -> None:
        """Close the active window managed by libtcod.

        This must only be called on the root console, which is returned from
        :any:`tcod.console_init_root`.

        .. versionadded:: 11.11
        """
        if self.console_c != ffi.NULL:
            raise NotImplementedError(
                "Only the root console can be used to close libtcod's window."
            )
        lib.TCOD_console_delete(self.console_c)
github libtcod / python-tcod / tcod / _internal.py View on Github external
def _get_cdata_from_args(*args: Any, **kargs: Any) -> Any:
        if len(args) == 1 and isinstance(args[0], ffi.CData) and not kargs:
            return args[0]
        else:
            return None
github libtcod / python-tcod / tcod / libtcodpy.py View on Github external
def __getattr__(self, attr: str) -> Any:
        if attr in self._BOOL_ATTRIBUTES:
            return bool(getattr(self.cdata, attr))
        if attr == "c":
            return ord(self.cdata.c)
        if attr == "text":
            return ffi.string(self.cdata.text).decode()
        return super(Key, self).__getattr__(attr)
github libtcod / python-tcod / tcod / libtcodpy.py View on Github external
@ffi.def_extern()  # type: ignore
def _pycall_parser_new_struct(struct: Any, name: str) -> Any:
    return _parser_listener.new_struct(struct, _unpack_char_p(name))
github libtcod / python-tcod / tcod / console.py View on Github external
order: str = "C",
        buffer: Optional[np.ndarray] = None,
    ):
        self._key_color = None  # type: Optional[Tuple[int, int, int]]
        self._order = tcod._internal.verify_order(order)
        if buffer is not None:
            if self._order == "F":
                buffer = buffer.transpose()
            self._tiles = np.ascontiguousarray(buffer, self.DTYPE)
        else:
            self._tiles = np.ndarray((height, width), dtype=self.DTYPE)

        # libtcod uses the root console for defaults.
        default_bg_blend = 0
        default_alignment = 0
        if lib.TCOD_ctx.root != ffi.NULL:
            default_bg_blend = lib.TCOD_ctx.root.bkgnd_flag
            default_alignment = lib.TCOD_ctx.root.alignment

        self._console_data = self.console_c = ffi.new(
            "struct TCOD_Console*",
            {
                "w": width,
                "h": height,
                "elements": width * height,
                "tiles": ffi.from_buffer(
                    "struct TCOD_ConsoleTile*", self._tiles
                ),
                "bkgnd_flag": default_bg_blend,
                "alignment": default_alignment,
                "fore": (255, 255, 255),
                "back": (0, 0, 0),
github libtcod / python-tcod / tcod / noise.py View on Github external
def _setstate_old(self, state: Any) -> None:
        self._random = state[0]
        self.noise_c = ffi.new("struct TCOD_Noise*")
        self.noise_c.ndim = state[3]
        ffi.buffer(self.noise_c.map)[:] = state[4]
        ffi.buffer(self.noise_c.buffer)[:] = state[5]
        self.noise_c.H = state[6]
        self.noise_c.lacunarity = state[7]
        ffi.buffer(self.noise_c.exponent)[:] = state[8]
        if state[9]:
            # high change of this being prematurely garbage collected!
            self.__waveletTileData = ffi.new("float[]", 32 * 32 * 32)
            ffi.buffer(self.__waveletTileData)[:] = state[9]
        self.noise_c.noise_type = state[10]
        self._tdl_noise_c = ffi.new(
            "TDLNoise*", (self.noise_c, self.noise_c.ndim, state[1], state[2])
        )
github libtcod / python-tcod / tcod / random.py View on Github external
def __setstate__(self, state: Any) -> None:
        """Create a new cdata object with the stored paramaters."""
        try:
            cdata = state["random_c"]
        except KeyError:  # old/deprecated format
            cdata = state["cdata"]
            del state["cdata"]
        state["random_c"] = ffi.new("mersenne_data_t*", cdata)
        self.__dict__.update(state)