Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

How to replace the call to console_put_char_ex with the new API#126

AnsweredbyHexDecimal
Andres6936 asked this question inQ&A
Discussion options

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:

Sin nombre

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

You must be logged in to vote

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

Comment options

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 replaceconsole_put_char_ex with assignment to theConsole.rgb array. The most ideal case would be that you vectorize this code and remove the nested loop over the world positions, but that would requireChars to become a NumPy array as well.

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?

You must be logged in to vote
0 replies
Comment options

The linked repository is private.

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.

You must be logged in to vote
0 replies
Comment options

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 be changed to be all ints and Unicode codepoints. The following should work for both windows:

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.Colors is easy to convert, butChars should only hold numbers.
Thetcod-camera extension can also help with projecting the world arrays into the console.

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 changeChars andColors so that they're stored as Numpy arrays and don't need to be converted in this function.

You must be logged in to vote
0 replies
Answer selected byHexDecimal
Comment options

Thanks very much@HexDecimal, Work.

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_*.

You must be logged in to vote
4 replies
@HexDecimal
Comment options

console.rgb is the replacement ofconsole_put_char_ex, notconsole.print. When used correctlyconsole.rgb should run x20-x50 times faster then either as I described in my previous answer. This requires effective usage of Numpy to get this benefit.

@Andres6936
Comment options

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

@HexDecimal
Comment options

It's a good start, but your rendering routine will run slowly as long as it's in that nested for loop.console.rgb does not ignore out-of-bounds draws like the printing functions do, so for larger maps you'll need to check bounds or use the tcod-camera package as I've shown.

@HexDecimal
Comment options

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.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@Andres6936@HexDecimal

[8]ページ先頭

©2009-2025 Movatter.jp