Skip to content

Commit f59050b

Browse files
committed
Make SDLConsoleRender.atlas public.
Resolves #121 Also updates related docs and fixes typos.
1 parent efc3f5c commit f59050b

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@
223223
"pcpp",
224224
"PILCROW",
225225
"pilmode",
226+
"PIXELFORMAT",
226227
"PRESENTVSYNC",
227228
"PRINTF",
228229
"printn",
@@ -288,6 +289,7 @@
288289
"TCODLIB",
289290
"TEEE",
290291
"TEEW",
292+
"TEXTUREACCESS",
291293
"thirdparty",
292294
"Tileset",
293295
"tilesets",

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ Changes relevant to the users of python-tcod are documented here.
44
This project adheres to [Semantic Versioning](https://semver.org/) since version `2.0.0`.
55

66
## [Unreleased]
7+
### Added
8+
- You can new use `SDLConsoleRender.atlas` to access the `SDLTilesetAtlas` used to create it.
9+
[#121](https://github.com/libtcod/python-tcod/issues/121)
10+
711
### Fixed
812
- Fixed the parsing of SDL 2.0.22 headers. Specifically `SDL_FLT_EPSILON`.
913

tcod/render.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
"""Handles the rendering of libtcod's tilesets.
22
33
Using this module you can render a console to an SDL :any:`Texture` directly, letting you have full control over how
4-
conoles are displayed.
4+
consoles are displayed.
55
This includes rendering multiple tilesets in a single frame and rendering consoles on top of each other.
66
77
Example::
88
9-
tileset = tcod.tileset.load_tilsheet("dejavu16x16_gs_tc.png", 32, 8, tcod.tileset.CHARMAP_TCOD)
9+
tileset = tcod.tileset.load_tilesheet("dejavu16x16_gs_tc.png", 32, 8, tcod.tileset.CHARMAP_TCOD)
1010
console = tcod.Console(20, 8)
1111
console.print(0, 0, "Hello World")
1212
sdl_window = tcod.sdl.video.new_window(
@@ -31,6 +31,8 @@
3131

3232
from typing import Any, Optional
3333

34+
from typing_extensions import Final
35+
3436
import tcod.console
3537
import tcod.sdl.render
3638
import tcod.tileset
@@ -43,23 +45,32 @@ class SDLTilesetAtlas:
4345

4446
def __init__(self, renderer: tcod.sdl.render.Renderer, tileset: tcod.tileset.Tileset) -> None:
4547
self._renderer = renderer
46-
self.tileset = tileset
47-
self.p = ffi.gc(_check_p(lib.TCOD_sdl2_atlas_new(renderer.p, tileset._tileset_p)), lib.TCOD_sdl2_atlas_delete)
48+
self.tileset: Final[tcod.tileset.Tileset] = tileset
49+
"""The tileset used to create this SDLTilesetAtlas."""
50+
self.p: Final = ffi.gc(
51+
_check_p(lib.TCOD_sdl2_atlas_new(renderer.p, tileset._tileset_p)), lib.TCOD_sdl2_atlas_delete
52+
)
4853

4954
@classmethod
5055
def _from_ref(cls, renderer_p: Any, atlas_p: Any) -> SDLTilesetAtlas:
5156
self = object.__new__(cls)
57+
# Ignore Final reassignment type errors since this is an alternative constructor.
58+
# This could be a sign that the current constructor was badly implemented.
5259
self._renderer = tcod.sdl.render.Renderer(renderer_p)
53-
self.tileset = tcod.tileset.Tileset._from_ref(atlas_p.tileset)
54-
self.p = atlas_p
60+
self.tileset = tcod.tileset.Tileset._from_ref(atlas_p.tileset) # type: ignore[misc]
61+
self.p = atlas_p # type: ignore[misc]
5562
return self
5663

5764

5865
class SDLConsoleRender:
5966
"""Holds an internal cache console and texture which are used to optimized console rendering."""
6067

6168
def __init__(self, atlas: SDLTilesetAtlas) -> None:
62-
self._atlas = atlas
69+
self.atlas: Final[SDLTilesetAtlas] = atlas
70+
"""The SDLTilesetAtlas used to create this SDLConsoleRender.
71+
72+
.. versionadded:: Unreleased
73+
"""
6374
self._renderer = atlas._renderer
6475
self._cache_console: Optional[tcod.console.Console] = None
6576
self._texture: Optional[tcod.sdl.render.Texture] = None
@@ -80,16 +91,16 @@ def render(self, console: tcod.console.Console) -> tcod.sdl.render.Texture:
8091
if self._cache_console is None or self._texture is None:
8192
self._cache_console = tcod.console.Console(console.width, console.height)
8293
self._texture = self._renderer.new_texture(
83-
self._atlas.tileset.tile_width * console.width,
84-
self._atlas.tileset.tile_height * console.height,
94+
self.atlas.tileset.tile_width * console.width,
95+
self.atlas.tileset.tile_height * console.height,
8596
format=int(lib.SDL_PIXELFORMAT_RGBA32),
8697
access=int(lib.SDL_TEXTUREACCESS_TARGET),
8798
)
8899

89100
with self._renderer.set_render_target(self._texture):
90101
_check(
91102
lib.TCOD_sdl2_render_texture(
92-
self._atlas.p, console.console_c, self._cache_console.console_c, self._texture.p
103+
self.atlas.p, console.console_c, self._cache_console.console_c, self._texture.p
93104
)
94105
)
95106
return self._texture

0 commit comments

Comments
 (0)