Beispiel #1
0
 def get_tcod_path_ffi(self) -> Tuple[Any, Any, Tuple[int, int]]:
     """Return (C callback, userdata handle, shape)"""
     return self._CALLBACK_P, ffi.new_handle(self._userdata), self.shape
Beispiel #2
0
def new(*,
        x: Optional[int] = None,
        y: Optional[int] = None,
        width: Optional[int] = None,
        height: Optional[int] = None,
        columns: Optional[int] = None,
        rows: Optional[int] = None,
        renderer: Optional[int] = None,
        tileset: Optional[tcod.tileset.Tileset] = None,
        vsync: bool = True,
        sdl_window_flags: Optional[int] = None,
        title: Optional[str] = None,
        argv: Optional[Iterable[str]] = None) -> Context:
    """Create a new context with the desired pixel size.

    `x`, `y`, `width`, and `height` are the desired position and size of the
    window.  If these are None then they will be derived from `columns` and
    `rows`.  So if you plan on having a console of a fixed size then you should
    set `columns` and `rows` instead of the window keywords.

    `columns` and `rows` is the desired size of the console.  Can be left as
    `None` when you're setting a context by a window size instead of a console.

    Providing no size information at all is also acceptable.

    `renderer` is the desired libtcod renderer to use.
    Typical options are :any:`tcod.context.RENDERER_OPENGL2` for a faster
    renderer or :any:`tcod.context.RENDERER_SDL2` for a reliable renderer.

    `tileset` is the font/tileset for the new context to render with.
    The fall-back tileset available from passing None is useful for
    prototyping, but will be unreliable across platforms.

    `vsync` is the Vertical Sync option for the window.  The default of True
    is recommended but you may want to use False for benchmarking purposes.

    `sdl_window_flags` is a bit-field of SDL window flags, if None is given
    then a default of :any:`tcod.context.SDL_WINDOW_RESIZABLE` is used.
    There's more info on the SDL documentation:
    https://wiki.libsdl.org/SDL_CreateWindow#Remarks

    `title` is the desired title of the window.

    `argv` these arguments are passed to libtcod and allow an end-user to make
    last second changes such as forcing fullscreen or windowed mode, or
    changing the libtcod renderer.
    By default this will pass in `sys.argv` but you can disable this feature
    by providing an empty list instead.
    Certain commands such as ``-help`` will raise a SystemExit exception from
    this function with the output message.

    When a window size is given instead of a console size then you can use
    :any:`Context.recommended_console_size` to automatically find the size of
    the console which should be used.

    .. versionadded:: 11.16
    """
    if renderer is None:
        renderer = RENDERER_OPENGL2
    if sdl_window_flags is None:
        sdl_window_flags = SDL_WINDOW_RESIZABLE
    if argv is None:
        argv = sys.argv
    argv_encoded = [  # Needs to be kept alive for argv_c.
        ffi.new("char[]", arg.encode("utf-8")) for arg in argv
    ]
    argv_c = ffi.new("char*[]", argv_encoded)

    catch_msg = []  # type: List[str]
    catch_handle = ffi.new_handle(catch_msg)  # Keep alive.

    title_p = _handle_title(title)  # Keep alive.

    params = ffi.new(
        "struct TCOD_ContextParams*",
        {
            "tcod_version": lib.TCOD_COMPILEDVERSION,
            "window_x": x if x is not None else lib.SDL_WINDOWPOS_UNDEFINED,
            "window_y": y if y is not None else lib.SDL_WINDOWPOS_UNDEFINED,
            "pixel_width": width or 0,
            "pixel_height": height or 0,
            "columns": columns or 0,
            "rows": rows or 0,
            "renderer_type": renderer,
            "tileset": _handle_tileset(tileset),
            "vsync": vsync,
            "sdl_window_flags": sdl_window_flags,
            "window_title": title_p,
            "argc": len(argv_c),
            "argv": argv_c,
            "cli_output": ffi.addressof(lib, "_pycall_cli_output"),
            "cli_userdata": catch_handle,
            "window_xy_defined": True,
        },
    )
    context_pp = ffi.new("TCOD_Context**")
    error = lib.TCOD_context_new(params, context_pp)
    if error == lib.TCOD_E_REQUIRES_ATTENTION:
        raise SystemExit(catch_msg[0])
    _check_warn(error)
    return Context._claim(context_pp[0])