Skip to content

Commit 0715349

Browse files
committed
Add FAQ about adding custom tiles.
1 parent 6d85d07 commit 0715349

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

docs/faq.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This can either be your own custom tool or you can copy the Clock class from the
99
`framerate.py <https://github.com/libtcod/python-tcod/blob/develop/examples/framerate.py>`_
1010
example.
1111

12+
1213
I get ``No module named 'tcod'`` when I try to ``import tcod`` in PyCharm.
1314
--------------------------------------------------------------------------
1415

@@ -29,4 +30,23 @@ Once this file is saved to your projects root directory then PyCharm will detect
2930
Alternatively you can open the `Terminal` tab in PyCharm and run ``pip install tcod`` there. This will install `tcod` to the currently open project.
3031

3132

33+
How do I add custom tiles?
34+
--------------------------
35+
36+
Libtcod uses Unicode to identify tiles.
37+
To prevent conflicts with real glyphs you should decide on codepoints from a `Private Use Area <https://en.wikipedia.org/wiki/Private_Use_Areas>`_ before continuing.
38+
If you're unsure, then use the codepoints from ``0x100000`` to ``0x10FFFD`` for your custom tiles.
39+
40+
Normally you load a font with :func:`tcod.tileset.load_tilesheet` which will return a :any:`Tileset` that gets passed to :func:`tcod.context.new`'s `tileset` parameter.
41+
:func:`tcod.tileset.load_tilesheet` assigns the codepoints from `charmap` to the tilesheet in row-major order.
42+
43+
There are two ways to extend a tileset like the above:
44+
45+
- Increase the tilesheet size vertically and update the `rows` parameter in :func:`tcod.tileset.load_tilesheet` to match the new image size, then modify the `charmap` parameter to map the new tiles to codepoints.
46+
If you edited a CP437 tileset this way then you'd add your new codepoints to the end of :any:`tcod.tileset.CHARMAP_CP437` before using the result as the `charmap` parameter.
47+
You can also use :any:`Tileset.remap` if you want to reassign tiles based on their position rather than editing `charmap`.
48+
- Or do not modify the original tilesheet.
49+
Load the tileset normally, then add new tiles with :any:`Tileset.set_tile` with manually loaded images.
50+
51+
3252
.. _PyCharm: https://www.jetbrains.com/pycharm/

tcod/tileset.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,8 @@ def load_tilesheet(path: Union[str, Path], columns: int, rows: int, charmap: Opt
278278
`columns` and `rows` is the shape of the tileset. Tiles are assumed to
279279
take up the entire space of the image.
280280
281-
`charmap` is the character mapping to use. This is a list or generator
282-
of codepoints which map the tiles like this:
283-
``charmap[tile_index] = codepoint``.
281+
`charmap` is a sequence of codepoints to map the tilesheet to in row-major order.
282+
This is a list or generator of codepoints which map the tiles like this: ``charmap[tile_index] = codepoint``.
284283
For common tilesets `charmap` should be :any:`tcod.tileset.CHARMAP_CP437`.
285284
Generators will be sliced so :any:`itertools.count` can be used which will
286285
give all tiles the same codepoint as their index, but this will not map

0 commit comments

Comments
 (0)