How to replace the call to console_put_char_ex with the new API #126
-
Good morning, I am refactoring an old project that uses tcod in its Python 2 version, and currently the Heightmap rendering works and is displayed but with some differences: The current code that rendering the scene is: def BiomeMap(Chars, Colors, console: Console):
for x in range(WORLD_WIDTH):
for y in range(WORLD_HEIGHT):
console.print(x, y + SCREEN_HEIGHT // 2 - WORLD_HEIGHT // 2, str(Chars[x][y]), Colors[x][y], libtcod.black)
libtcod.console_put_char_ex(0, x, y + SCREEN_HEIGHT // 2 - WORLD_HEIGHT // 2, Chars[x][y], Colors[x][y],
libtcod.black)
libtcod.console_flush()
return I do not know what the function should be called to obtain the same result as the left image. If anyone wants to see the code: https://github.com/Andres6936/WorldGeneration.Dwarf.git |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
The linked repository is private. Your first priority should be to run the code on Python 3 and the latest version of tcod. Even code this old should still work. Once you get this working on Python 3 you replace I'm not sure what you're going for in the left image. I can't really tell which of height/rain/heat is being measured. Or did you mean you wanted the image on the right? |
Beta Was this translation helpful? Give feedback.
-
Sorry, I have already made it public In fact, the two versions coexist, if you run the code it will open two windows, the left window makes use of the new tcod API, and the right version makes use of the Python 2 API. But I don't know why when using the render of the new API, this one only draws numbers instead of drawing the icons as it happens in the right one. |
Beta Was this translation helpful? Give feedback.
-
Okay. I've done some investigation. This line: console.print(x, y + SCREEN_HEIGHT // 2 - WORLD_HEIGHT // 2, str(Chars[x][y]), Colors[x][y], libtcod.black) When you call libtcod.console_put_char_ex(0, x, y + SCREEN_HEIGHT // 2 - WORLD_HEIGHT // 2, Chars[x][y], Colors[x][y], libtcod.black) The first parameter is Next for x in range(WORLD_WIDTH):
for y in range(WORLD_HEIGHT):
ch = Chars[x][y]
if not isinstance(ch, int):
ch = ord(ch)
ch = libtcod.tileset.CHARMAP_CP437[ch] # Converts from EASCII to Unicode.
libtcod.console_put_char_ex( # Renders on root console.
0, x, y + SCREEN_HEIGHT // 2 - WORLD_HEIGHT // 2, ch, Colors[x][y], libtcod.black
)
libtcod.console_put_char_ex( # Renders on given console.
console, x, y + SCREEN_HEIGHT // 2 - WORLD_HEIGHT // 2, ch, Colors[x][y], libtcod.black
) Then I'd highly recommend converting these arrays to Numpy. import numpy as np
import tcod.camera # pip install tcod-camera, requires tcod>=14.0.0 to import
def BiomeMap(Chars, Colors, console: Console):
# Assuming order="F" for console.
world_chars = np.array( # Convert to int array.
[[ord(ch) if isinstance(ch, str) else ch for ch in row] for row in Chars],
dtype=np.int32,
order="F",
)
world_chars = np.array(libtcod.tileset.CHARMAP_CP437)[world_chars] # Converts from EASCII to Unicode.
world_colors = np.array(Colors, dtype=np.uint8, order="F") # Order is not 100% correct here. Keeping the example simple for now.
# The above conversions are slow, the remaining code is fast.
screen_slice, world_slice = tcod.camera.get_slices(
screen=(console.width, console.height),
world=(WORLD_WIDTH, WORLD_HEIGHT),
camera=(0, WORLD_HEIGHT // 2 - SCREEN_HEIGHT // 2)
)
console.rgb["ch"][screen_slice] = world_chars[world_slice]
console.rgb["fg"][screen_slice] = world_colors[world_slice]
console.rgb["bg"][screen_slice] = (0, 0, 0)
libtcod.console_flush()
return Then change |
Beta Was this translation helpful? Give feedback.
-
Thanks very much @HexDecimal, Work. Now the question I have is: Is it possible to avoid using console_put_char_ex, I see it will be deprecated soon? The idea is to use a Console function to imitate rendering (e.g. print_*) and not use global APIs like console_put_char_*. |
Beta Was this translation helpful? Give feedback.
Okay. I've done some investigation. This line:
When you call
str(Chars[x][y])
it converts numbers like176
into"176"
and then the function prints the entire string. This is where the numbers are coming from. This line:The first parameter is
0
, but this should beconsole
. Otherwise this function is ignored when a root console is missing which the new API doesn't use.Next
Chars
has mixed strings and ints and the characters are all EASCII codepoints. These should b…