How to use the tcod.loader.ffi.new 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 / tileset.py View on Github external
height = console.height * self.tile_height
        out = np.empty((height, width, 4), np.uint8)
        out[:] = 9
        surface_p = ffi.gc(
            lib.SDL_CreateRGBSurfaceWithFormatFrom(
                ffi.from_buffer("void*", out),
                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
github libtcod / python-tcod / tcod / libtcodpy.py View on Github external
def heightmap_get_minmax(hm: np.ndarray) -> Tuple[float, float]:
    """Return the min and max values of this heightmap.

    Args:
        hm (numpy.ndarray): A numpy.ndarray formatted for heightmap functions.

    Returns:
        Tuple[float, float]: The (min, max) values.

    .. deprecated:: 2.0
        Use ``hm.min()`` or ``hm.max()`` instead.
    """
    mi = ffi.new("float *")
    ma = ffi.new("float *")
    lib.TCOD_heightmap_get_minmax(_heightmap_cdata(hm), mi, ma)
    return mi[0], ma[0]
github libtcod / python-tcod / tcod / map.py View on Github external
def __as_cdata(self) -> Any:
        return ffi.new(
            "struct TCOD_Map*",
            (
                self.width,
                self.height,
                self.width * self.height,
                ffi.from_buffer("struct TCOD_MapCell*", self.__buffer),
            ),
github libtcod / python-tcod / tcod / path.py View on Github external
def _compile_rules(self) -> Any:
        """Compile this graph into a C struct array."""
        if not self._edge_rules_p:
            self._edge_rules_keep_alive = []
            rules = []
            for rule_ in self._graph.values():
                rule = rule_.copy()
                rule["edge_count"] = len(rule["edge_list"])
                # Edge rule format: [i, j, cost, ...] etc.
                edge_obj = ffi.new(
                    "int[]", len(rule["edge_list"]) * (self._ndim + 1)
                )
                edge_obj[0 : len(edge_obj)] = itertools.chain(
                    *rule["edge_list"]
                )
                self._edge_rules_keep_alive.append(edge_obj)
                rule["edge_array"] = edge_obj
                self._edge_rules_keep_alive.append(rule["cost"])
                rule["cost"] = _export_dict(rule["cost"])
                if "condition" in rule:
                    self._edge_rules_keep_alive.append(rule["condition"])
                    rule["condition"] = _export_dict(rule["condition"])
                del rule["edge_list"]
                rules.append(rule)
            self._edge_rules_p = ffi.new("struct PathfinderRule[]", rules)
        return self._edge_rules_p, self._edge_rules_keep_alive
github libtcod / python-tcod / tcod / libtcodpy.py View on Github external
typ: int = NOISE_DEFAULT,
) -> float:
    """Return the turbulence noise sampled from the ``f`` coordinate.

    Args:
        n (Noise): A Noise instance.
        f (Sequence[float]): The point to sample the noise from.
        typ (int): The noise algorithm to use.
        octaves (float): The level of level.  Should be more than 1.

    Returns:
        float: The sampled noise value.
    """
    return float(
        lib.TCOD_noise_get_turbulence_ex(
            n.noise_c, ffi.new("float[4]", f), oc, typ
        )
github libtcod / python-tcod / tcod / libtcodpy.py View on Github external
def sys_get_current_resolution() -> Tuple[int, int]:
    """Return the current resolution as (width, height)

    Returns:
        Tuple[int,int]: The current resolution.
    """
    w = ffi.new("int *")
    h = ffi.new("int *")
    lib.TCOD_sys_get_current_resolution(w, h)
    return w[0], h[0]
github libtcod / python-tcod / tcod / _internal.py View on Github external
"Array must have RGB channels.  Shape is: %r"
                % (self._array.shape,)
            )
        self._buffer = ffi.from_buffer("TCOD_color_t[]", self._array)
        self._mipmaps = ffi.new(
            "struct TCOD_mipmap_*",
            {
                "width": width,
                "height": height,
                "fwidth": width,
                "fheight": height,
                "buf": self._buffer,
                "dirty": True,
            },
        )
        self.image_c = ffi.new(
            "TCOD_Image*",
            {
                "nb_mipmaps": 1,
                "mipmaps": self._mipmaps,
                "has_key_color": False,
            },
github libtcod / python-tcod / tcod / sdl.py View on Github external
def position(self) -> Tuple[int, int]:
        """Return the (x, y) position of the window.

        This attribute can be set the move the window.
        The constants tcod.lib.SDL_WINDOWPOS_CENTERED or
        tcod.lib.SDL_WINDOWPOS_UNDEFINED can be used.
        """
        xy = ffi.new("int[2]")
        lib.SDL_GetWindowPosition(self.p, xy, xy + 1)
        return xy[0], xy[1]
github libtcod / python-tcod / tcod / libtcodpy.py View on Github external
def parser_run(parser: Any, filename: str, listener: Any = None) -> None:
    global _parser_listener
    if not listener:
        lib.TCOD_parser_run(parser, _bytes(filename), ffi.NULL)
        return

    propagate_manager = _PropagateException()

    clistener = ffi.new(
        "TCOD_parser_listener_t *",
        {
            "new_struct": lib._pycall_parser_new_struct,
            "new_flag": lib._pycall_parser_new_flag,
            "new_property": lib._pycall_parser_new_property,
            "end_struct": lib._pycall_parser_end_struct,
            "error": lib._pycall_parser_error,
        },
    )

    with _parser_callback_lock:
        _parser_listener = listener
        with propagate_manager:
            lib.TCOD_parser_run(parser, _bytes(filename), clistener)
github libtcod / python-tcod / tcod / path.py View on Github external
def _export(array: np.array) -> Any:
    """Convert a NumPy array into a ctype object."""
    return ffi.new("struct NArray*", _export_dict(array))