Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork37
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: defBiomeMap(Chars,Colors,console:Console):forxinrange(WORLD_WIDTH):foryinrange(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 |
BetaWas this translation helpful?Give feedback.
All reactions
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 callstr(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:
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 is0
, but this should beconsole
. Otherwise this function is ignored when a root console is missing which the new API doesn't use.
NextChars
has mixed strings and ints and the characters are all EASCII codepoints. These should b…
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? |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
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. |
BetaWas this translation helpful?Give feedback.
All reactions
-
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 forxinrange(WORLD_WIDTH):foryinrange(WORLD_HEIGHT):ch=Chars[x][y]ifnotisinstance(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. importnumpyasnpimporttcod.camera# pip install tcod-camera, requires tcod>=14.0.0 to importdefBiomeMap(Chars,Colors,console:Console):# Assuming order="F" for console.world_chars=np.array(# Convert to int array. [[ord(ch)ifisinstance(ch,str)elsechforchinrow]forrowinChars],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 |
BetaWas this translation helpful?Give feedback.
All reactions
👍 2
-
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_*. |
BetaWas this translation helpful?Give feedback.
All reactions
-
|
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
-
Thanks very much, this is my code for console.rgb defBiomeMap(Chars,Colors,console:Console):forxinrange(WORLD_WIDTH):foryinrange(WORLD_HEIGHT):glyph=Chars[x][y]ifnotisinstance(glyph,int):glyph=ord(glyph)glyph=libtcod.tileset.CHARMAP_CP437[glyph]# Converts from EASCII to Unicode.console.rgb[x,y+SCREEN_HEIGHT//2-WORLD_HEIGHT//2]=glyph,Colors[x][y],libtcod.blacklibtcod.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 The results are incredible, that's what I wanted. Thanks very much@HexDecimal |
BetaWas this translation helpful?Give feedback.
All reactions
-
It's a good start, but your rendering routine will run slowly as long as it's in that nested for loop. |
BetaWas this translation helpful?Give feedback.
All reactions
-
Also note that the loop is using the wrong values for world/screen projection. Normally you'd iterate over the screen positions and project those into the world, doing otherwise will become very costly with larger world maps since every tile in the world is fetched. |
BetaWas this translation helpful?Give feedback.