1919import numpy as np
2020import tcod
2121import tcod .render
22+ import tcod .sdl .render
2223from numpy .typing import NDArray
2324
2425if not sys .warnoptions :
@@ -50,6 +51,8 @@ def get_data(path: str) -> str:
5051# Mutable global names.
5152context : tcod .context .Context
5253tileset : tcod .tileset .Tileset
54+ console_render : tcod .render .SDLConsoleRender # Optional SDL renderer.
55+ sample_minimap : tcod .sdl .render .Texture # Optional minimap texture.
5356root_console = tcod .Console (80 , 50 , order = "F" )
5457sample_console = tcod .console .Console (SAMPLE_SCREEN_WIDTH , SAMPLE_SCREEN_HEIGHT , order = "F" )
5558cur_sample = 0 # Current selected sample.
@@ -68,7 +71,7 @@ def on_draw(self) -> None:
6871 pass
6972
7073 def ev_keydown (self , event : tcod .event .KeyDown ) -> None :
71- global cur_sample , context
74+ global cur_sample
7275 if event .sym == tcod .event .K_DOWN :
7376 cur_sample = (cur_sample + 1 ) % len (SAMPLES )
7477 SAMPLES [cur_sample ].on_enter ()
@@ -91,8 +94,7 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> None:
9194 raise SystemExit ()
9295 elif event .sym in RENDERER_KEYS :
9396 # Swap the active context for one with a different renderer.
94- context .close ()
95- context = init_context (RENDERER_KEYS [event .sym ])
97+ init_context (RENDERER_KEYS [event .sym ])
9698
9799 def ev_quit (self , event : tcod .event .Quit ) -> None :
98100 raise SystemExit ()
@@ -541,7 +543,7 @@ def __init__(self) -> None:
541543 self .player_y = 10
542544 self .torch = False
543545 self .light_walls = True
544- self .algo_num = 0
546+ self .algo_num = tcod . FOV_SYMMETRIC_SHADOWCAST
545547 self .noise = tcod .noise .Noise (1 ) # 1D noise for the torch flickering.
546548
547549 map_shape = (SAMPLE_SCREEN_WIDTH , SAMPLE_SCREEN_HEIGHT )
@@ -582,7 +584,7 @@ def on_draw(self) -> None:
582584 self .draw_ui ()
583585 sample_console .print (self .player_x , self .player_y , "@" )
584586 # Draw windows.
585- sample_console .tiles_rgb ["ch" ][SAMPLE_MAP == "=" ] = tcod . CHAR_DHLINE
587+ sample_console .tiles_rgb ["ch" ][SAMPLE_MAP == "=" ] = 0x2550 # BOX DRAWINGS DOUBLE HORIZONTAL
586588 sample_console .tiles_rgb ["fg" ][SAMPLE_MAP == "=" ] = BLACK
587589
588590 # Get a 2D boolean array of visible cells.
@@ -1394,34 +1396,48 @@ def on_draw(self) -> None:
13941396)
13951397
13961398
1397- def init_context (renderer : int ) -> tcod . context . Context :
1398- """Return a new context with common parameters set.
1399+ def init_context (renderer : int ) -> None :
1400+ """Setup or reset a global context with common parameters set.
13991401
14001402 This function exists to more easily switch between renderers.
14011403 """
1404+ global context , console_render , sample_minimap
1405+ if "context" in globals ():
1406+ context .close ()
14021407 libtcod_version = "%i.%i.%i" % (
14031408 tcod .lib .TCOD_MAJOR_VERSION ,
14041409 tcod .lib .TCOD_MINOR_VERSION ,
14051410 tcod .lib .TCOD_PATCHLEVEL ,
14061411 )
1407- return tcod .context .new (
1412+ context = tcod .context .new (
14081413 columns = root_console .width ,
14091414 rows = root_console .height ,
14101415 title = f"python-tcod samples" f" (python-tcod { tcod .__version__ } , libtcod { libtcod_version } )" ,
14111416 renderer = renderer ,
14121417 vsync = False , # VSync turned off since this is for benchmarking.
14131418 tileset = tileset ,
14141419 )
1420+ if context .sdl_renderer : # If this context supports SDL rendering.
1421+ # Start by setting the logical size so that window resizing doesn't break anything.
1422+ context .sdl_renderer .logical_size = (
1423+ tileset .tile_width * root_console .width ,
1424+ tileset .tile_height * root_console .height ,
1425+ )
1426+ assert context .sdl_atlas
1427+ # Generate the console renderer and minimap.
1428+ console_render = tcod .render .SDLConsoleRender (context .sdl_atlas )
1429+ sample_minimap = context .sdl_renderer .new_texture (
1430+ SAMPLE_SCREEN_WIDTH ,
1431+ SAMPLE_SCREEN_HEIGHT ,
1432+ format = tcod .lib .SDL_PIXELFORMAT_RGB24 ,
1433+ access = tcod .sdl .render .TextureAccess .STREAMING , # Updated every frame.
1434+ )
14151435
14161436
14171437def main () -> None :
14181438 global context , tileset
14191439 tileset = tcod .tileset .load_tilesheet (FONT , 32 , 8 , tcod .tileset .CHARMAP_TCOD )
1420- context = init_context (tcod .RENDERER_SDL2 )
1421- sdl_renderer = context .sdl_renderer
1422- assert sdl_renderer
1423- atlas = tcod .render .SDLTilesetAtlas (sdl_renderer , tileset )
1424- console_render = tcod .render .SDLConsoleRender (atlas )
1440+ init_context (tcod .RENDERER_SDL2 )
14251441 try :
14261442 SAMPLES [cur_sample ].on_enter ()
14271443
@@ -1434,9 +1450,25 @@ def main() -> None:
14341450 SAMPLES [cur_sample ].on_draw ()
14351451 sample_console .blit (root_console , SAMPLE_SCREEN_X , SAMPLE_SCREEN_Y )
14361452 draw_stats ()
1437- # context.present(root_console)
1438- sdl_renderer .copy (console_render .render (root_console ))
1439- sdl_renderer .present ()
1453+ if context .sdl_renderer :
1454+ # SDL renderer support, upload the sample console background to a minimap texture.
1455+ sample_minimap .update (sample_console .rgb .T ["bg" ])
1456+ # Render the root_console normally, this is the drawing step of context.present without presenting.
1457+ context .sdl_renderer .copy (console_render .render (root_console ))
1458+ # Render the minimap to the screen.
1459+ context .sdl_renderer .copy (
1460+ sample_minimap ,
1461+ dest = (
1462+ tileset .tile_width * 24 ,
1463+ tileset .tile_height * 36 ,
1464+ SAMPLE_SCREEN_WIDTH * 3 ,
1465+ SAMPLE_SCREEN_HEIGHT * 3 ,
1466+ ),
1467+ )
1468+ context .sdl_renderer .present ()
1469+ else : # No SDL renderer, just use plain context rendering.
1470+ context .present (root_console )
1471+
14401472 handle_time ()
14411473 handle_events ()
14421474 finally :
0 commit comments