Skip to content

Commit eccad15

Browse files
committed
Fix mouse tile coords in samples when context.present is skipped.
1 parent ba4ac69 commit eccad15

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

examples/samples_tcod.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
#!/usr/bin/env python3
2-
"""
3-
This code demonstrates various usages of python-tcod.
4-
"""
2+
"""This code demonstrates various usages of python-tcod."""
53
# To the extent possible under law, the libtcod maintainers have waived all
64
# copyright and related or neighboring rights to these samples.
75
# https://creativecommons.org/publicdomain/zero/1.0/
@@ -1412,7 +1410,7 @@ def init_context(renderer: int) -> None:
14121410
context = tcod.context.new(
14131411
columns=root_console.width,
14141412
rows=root_console.height,
1415-
title=f"python-tcod samples" f" (python-tcod {tcod.__version__}, libtcod {libtcod_version})",
1413+
title=f"python-tcod samples (python-tcod {tcod.__version__}, libtcod {libtcod_version})",
14161414
renderer=renderer,
14171415
vsync=False, # VSync turned off since this is for benchmarking.
14181416
tileset=tileset,
@@ -1488,7 +1486,19 @@ def handle_time() -> None:
14881486

14891487
def handle_events() -> None:
14901488
for event in tcod.event.get():
1491-
context.convert_event(event)
1489+
if context.sdl_renderer:
1490+
# Manual handing of tile coordinates since context.present is skipped.
1491+
if isinstance(event, (tcod.event.MouseState, tcod.event.MouseMotion)):
1492+
event.tile = tcod.event.Point(event.pixel.x // tileset.tile_width, event.pixel.y // tileset.tile_height)
1493+
if isinstance(event, tcod.event.MouseMotion):
1494+
prev_tile = (
1495+
(event.pixel[0] - event.pixel_motion[0]) // tileset.tile_width,
1496+
(event.pixel[1] - event.pixel_motion[1]) // tileset.tile_height,
1497+
)
1498+
event.tile_motion = tcod.event.Point(event.tile[0] - prev_tile[0], event.tile[1] - prev_tile[1])
1499+
else:
1500+
context.convert_event(event)
1501+
14921502
SAMPLES[cur_sample].dispatch(event)
14931503
if isinstance(event, tcod.event.Quit):
14941504
raise SystemExit()

0 commit comments

Comments
 (0)