Skip to content

Commit c6967a8

Browse files
committed
More libtcodpy deprecation.
Update samples to work with namespaces a little better. Add missing SDL mouse feature.
1 parent 9cb25f7 commit c6967a8

File tree

5 files changed

+164
-110
lines changed

5 files changed

+164
-110
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,17 @@ This project adheres to [Semantic Versioning](https://semver.org/) since version
66
## [Unreleased]
77
### Added
88
- Added PathLike support to more libtcodpy functions.
9+
- New `tcod.sdl.mouse.show` function for querying or setting mouse visibility.
10+
11+
### Deprecated
12+
- Deprecated the libtcodpy functions for images and noise generators.
913

1014
### Removed
1115
- `tcod.console_set_custom_font` can no longer take bytes.
1216

17+
### Fixed
18+
- Fix `tcod.sdl.mouse.warp_in_window` function.
19+
1320
## [15.0.3] - 2023-05-25
1421
### Deprecated
1522
- Deprecated all libtcod color constants. Replace these with your own manually defined colors.

examples/samples_tcod.py

Lines changed: 57 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
from numpy.typing import NDArray
1919

2020
import tcod
21+
import tcod.constants
22+
import tcod.event
23+
import tcod.libtcodpy
24+
import tcod.noise
2125
import tcod.render
26+
import tcod.sdl.mouse
2227
import tcod.sdl.render
2328

2429
# ruff: noqa: S311
@@ -48,7 +53,7 @@
4853
tileset: tcod.tileset.Tileset
4954
console_render: tcod.render.SDLConsoleRender # Optional SDL renderer.
5055
sample_minimap: tcod.sdl.render.Texture # Optional minimap texture.
51-
root_console = tcod.Console(80, 50, order="F")
56+
root_console = tcod.console.Console(80, 50, order="F")
5257
sample_console = tcod.console.Console(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT, order="F")
5358
cur_sample = 0 # Current selected sample.
5459
frame_times = [time.perf_counter()]
@@ -191,7 +196,7 @@ def __init__(self) -> None:
191196
"You can render to an offscreen console and blit in on another " "one, simulating alpha transparency.",
192197
fg=WHITE,
193198
bg=None,
194-
alignment=tcod.CENTER,
199+
alignment=tcod.constants.CENTER,
195200
)
196201

197202
def on_enter(self) -> None:
@@ -245,8 +250,8 @@ class LineDrawingSample(Sample):
245250

246251
def __init__(self) -> None:
247252
self.name = "Line drawing"
248-
self.mk_flag = tcod.BKGND_SET
249-
self.bk_flag = tcod.BKGND_SET
253+
self.mk_flag = tcod.constants.BKGND_SET
254+
self.bk_flag = tcod.constants.BKGND_SET
250255

251256
self.bk = tcod.console.Console(sample_console.width, sample_console.height, order="F")
252257
# initialize the colored background
@@ -291,7 +296,7 @@ def on_draw(self) -> None:
291296
yd = int(sample_console.height // 2 - sin_angle * sample_console.width // 2)
292297
# draw the line
293298
# in python the easiest way is to use the line iterator
294-
for x, y in tcod.line_iter(xo, yo, xd, yd):
299+
for x, y in tcod.los.bresenham((xo, yo), (xd, yd)).tolist():
295300
if 0 <= x < sample_console.width and 0 <= y < sample_console.height:
296301
tcod.console_set_char_background(sample_console, x, y, LIGHT_BLUE, self.bk_flag)
297302
sample_console.print(
@@ -359,10 +364,10 @@ def __init__(self) -> None:
359364
self.dy = 0.0
360365
self.octaves = 4.0
361366
self.zoom = 3.0
362-
self.hurst = tcod.NOISE_DEFAULT_HURST
363-
self.lacunarity = tcod.NOISE_DEFAULT_LACUNARITY
367+
self.hurst = tcod.libtcodpy.NOISE_DEFAULT_HURST
368+
self.lacunarity = tcod.libtcodpy.NOISE_DEFAULT_LACUNARITY
364369
self.noise = self.get_noise()
365-
self.img = tcod.image_new(SAMPLE_SCREEN_WIDTH * 2, SAMPLE_SCREEN_HEIGHT * 2)
370+
self.img = tcod.image.Image(SAMPLE_SCREEN_WIDTH * 2, SAMPLE_SCREEN_HEIGHT * 2)
366371

367372
@property
368373
def algorithm(self) -> int:
@@ -537,7 +542,7 @@ def __init__(self) -> None:
537542
self.player_y = 10
538543
self.torch = False
539544
self.light_walls = True
540-
self.algo_num = tcod.FOV_SYMMETRIC_SHADOWCAST
545+
self.algo_num = tcod.constants.FOV_SYMMETRIC_SHADOWCAST
541546
self.noise = tcod.noise.Noise(1) # 1D noise for the torch flickering.
542547

543548
map_shape = (SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT)
@@ -668,17 +673,19 @@ def __init__(self) -> None:
668673
self.busy = 0.0
669674
self.oldchar = " "
670675

671-
self.map = tcod.map_new(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT)
676+
self.map = tcod.map.Map(SAMPLE_SCREEN_WIDTH, SAMPLE_SCREEN_HEIGHT)
672677
for y in range(SAMPLE_SCREEN_HEIGHT):
673678
for x in range(SAMPLE_SCREEN_WIDTH):
674679
if SAMPLE_MAP[x, y] == " ":
675680
# ground
676-
tcod.map_set_properties(self.map, x, y, True, True)
681+
self.map.walkable[y, x] = True
682+
self.map.transparent[y, x] = True
677683
elif SAMPLE_MAP[x, y] == "=":
678684
# window
679-
tcod.map_set_properties(self.map, x, y, True, False)
680-
self.path = tcod.path_new_using_map(self.map)
681-
self.dijkstra = tcod.dijkstra_new(self.map)
685+
self.map.walkable[y, x] = False
686+
self.map.transparent[y, x] = True
687+
self.path = tcod.path.AStar(self.map)
688+
self.dijkstra = tcod.path.Dijkstra(self.map)
682689

683690
def on_enter(self) -> None:
684691
# we draw the foreground only the first time.
@@ -901,43 +908,41 @@ def traverse_node(bsp_map: NDArray[np.bool_], node: tcod.bsp.BSP) -> None:
901908
left, right = node.children
902909
node.x = min(left.x, right.x)
903910
node.y = min(left.y, right.y)
904-
node.w = max(left.x + left.w, right.x + right.w) - node.x
905-
node.h = max(left.y + left.h, right.y + right.h) - node.y
911+
node.width = max(left.x + left.width, right.x + right.width) - node.x
912+
node.height = max(left.y + left.height, right.y + right.height) - node.y
906913
# create a corridor between the two lower nodes
907914
if node.horizontal:
908915
# vertical corridor
909-
if left.x + left.w - 1 < right.x or right.x + right.w - 1 < left.x:
916+
if left.x + left.width - 1 < right.x or right.x + right.width - 1 < left.x:
910917
# no overlapping zone. we need a Z shaped corridor
911-
x1 = random.randint(left.x, left.x + left.w - 1)
912-
x2 = random.randint(right.x, right.x + right.w - 1)
913-
y = random.randint(left.y + left.h, right.y)
918+
x1 = random.randint(left.x, left.x + left.width - 1)
919+
x2 = random.randint(right.x, right.x + right.width - 1)
920+
y = random.randint(left.y + left.height, right.y)
914921
vline_up(bsp_map, x1, y - 1)
915922
hline(bsp_map, x1, y, x2)
916923
vline_down(bsp_map, x2, y + 1)
917924
else:
918925
# straight vertical corridor
919926
min_x = max(left.x, right.x)
920-
max_x = min(left.x + left.w - 1, right.x + right.w - 1)
927+
max_x = min(left.x + left.width - 1, right.x + right.width - 1)
921928
x = random.randint(min_x, max_x)
922929
vline_down(bsp_map, x, right.y)
923930
vline_up(bsp_map, x, right.y - 1)
931+
elif left.y + left.height - 1 < right.y or right.y + right.height - 1 < left.y: # horizontal corridor
932+
# no overlapping zone. we need a Z shaped corridor
933+
y1 = random.randint(left.y, left.y + left.height - 1)
934+
y2 = random.randint(right.y, right.y + right.height - 1)
935+
x = random.randint(left.x + left.width, right.x)
936+
hline_left(bsp_map, x - 1, y1)
937+
vline(bsp_map, x, y1, y2)
938+
hline_right(bsp_map, x + 1, y2)
924939
else:
925-
# horizontal corridor
926-
if left.y + left.h - 1 < right.y or right.y + right.h - 1 < left.y:
927-
# no overlapping zone. we need a Z shaped corridor
928-
y1 = random.randint(left.y, left.y + left.h - 1)
929-
y2 = random.randint(right.y, right.y + right.h - 1)
930-
x = random.randint(left.x + left.w, right.x)
931-
hline_left(bsp_map, x - 1, y1)
932-
vline(bsp_map, x, y1, y2)
933-
hline_right(bsp_map, x + 1, y2)
934-
else:
935-
# straight horizontal corridor
936-
min_y = max(left.y, right.y)
937-
max_y = min(left.y + left.h - 1, right.y + right.h - 1)
938-
y = random.randint(min_y, max_y)
939-
hline_left(bsp_map, right.x - 1, y)
940-
hline_right(bsp_map, right.x, y)
940+
# straight horizontal corridor
941+
min_y = max(left.y, right.y)
942+
max_y = min(left.y + left.height - 1, right.y + right.height - 1)
943+
y = random.randint(min_y, max_y)
944+
hline_left(bsp_map, right.x - 1, y)
945+
hline_right(bsp_map, right.x, y)
941946

942947

943948
class BSPSample(Sample):
@@ -1027,9 +1032,9 @@ class ImageSample(Sample):
10271032
def __init__(self) -> None:
10281033
self.name = "Image toolkit"
10291034

1030-
self.img = tcod.image_load(DATA_DIR / "img/skull.png")
1035+
self.img = tcod.image.Image.from_file(DATA_DIR / "img/skull.png")
10311036
self.img.set_key_color(BLACK)
1032-
self.circle = tcod.image_load(DATA_DIR / "img/circle.png")
1037+
self.circle = tcod.image.Image.from_file(DATA_DIR / "img/circle.png")
10331038

10341039
def on_draw(self) -> None:
10351040
sample_console.clear()
@@ -1066,8 +1071,10 @@ def __init__(self) -> None:
10661071
self.log: list[str] = []
10671072

10681073
def on_enter(self) -> None:
1069-
tcod.mouse_move(320, 200)
1070-
tcod.mouse_show_cursor(True)
1074+
sdl_window = context.sdl_window
1075+
if sdl_window:
1076+
tcod.sdl.mouse.warp_in_window(sdl_window, 320, 200)
1077+
tcod.sdl.mouse.show(True)
10711078

10721079
def ev_mousemotion(self, event: tcod.event.MouseMotion) -> None:
10731080
self.motion = event
@@ -1123,9 +1130,9 @@ def on_draw(self) -> None:
11231130

11241131
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
11251132
if event.sym == tcod.event.KeySym.N1:
1126-
tcod.mouse_show_cursor(False)
1133+
tcod.sdl.mouse.show(False)
11271134
elif event.sym == tcod.event.KeySym.N2:
1128-
tcod.mouse_show_cursor(True)
1135+
tcod.sdl.mouse.show(True)
11291136
else:
11301137
super().ev_keydown(event)
11311138

@@ -1215,7 +1222,7 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> None:
12151222
xc = xc - HALF_W
12161223
yc = yc - HALF_H
12171224

1218-
noise2d = tcod.noise_new(2, 0.5, 2.0)
1225+
noise2d = tcod.noise.Noise(2, hurst=0.5, lacunarity=2.0)
12191226
if numpy_available: # the texture starts empty
12201227
texture = np.zeros((RES_U, RES_V))
12211228

@@ -1359,11 +1366,11 @@ def on_draw(self) -> None:
13591366
#############################################
13601367

13611368
RENDERER_KEYS = {
1362-
tcod.event.KeySym.F1: tcod.RENDERER_GLSL,
1363-
tcod.event.KeySym.F2: tcod.RENDERER_OPENGL,
1364-
tcod.event.KeySym.F3: tcod.RENDERER_SDL,
1365-
tcod.event.KeySym.F4: tcod.RENDERER_SDL2,
1366-
tcod.event.KeySym.F5: tcod.RENDERER_OPENGL2,
1369+
tcod.event.KeySym.F1: tcod.constants.RENDERER_GLSL,
1370+
tcod.event.KeySym.F2: tcod.constants.RENDERER_OPENGL,
1371+
tcod.event.KeySym.F3: tcod.constants.RENDERER_SDL,
1372+
tcod.event.KeySym.F4: tcod.constants.RENDERER_SDL2,
1373+
tcod.event.KeySym.F5: tcod.constants.RENDERER_OPENGL2,
13671374
}
13681375

13691376
RENDERER_NAMES = (
@@ -1406,7 +1413,6 @@ def init_context(renderer: int) -> None:
14061413
columns=root_console.width,
14071414
rows=root_console.height,
14081415
title=f"python-tcod samples (python-tcod {tcod.__version__}, libtcod {libtcod_version})",
1409-
renderer=renderer,
14101416
vsync=False, # VSync turned off since this is for benchmarking.
14111417
tileset=tileset,
14121418
)
@@ -1430,7 +1436,7 @@ def init_context(renderer: int) -> None:
14301436
def main() -> None:
14311437
global context, tileset
14321438
tileset = tcod.tileset.load_tilesheet(FONT, 32, 8, tcod.tileset.CHARMAP_TCOD)
1433-
init_context(tcod.RENDERER_SDL2)
1439+
init_context(tcod.constants.RENDERER_SDL2)
14341440
try:
14351441
SAMPLES[cur_sample].on_enter()
14361442

tcod/image.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ def from_array(cls, array: ArrayLike) -> Image:
6565
image_array[...] = array
6666
return image
6767

68+
@classmethod
69+
def from_file(cls, path: str | PathLike[str]) -> Image:
70+
path = Path(path).resolve(strict=True)
71+
return cls._from_cdata(ffi.gc(lib.TCOD_image_load(bytes(path)), lib.TCOD_image_delete))
72+
6873
def clear(self, color: tuple[int, int, int]) -> None:
6974
"""Fill this entire Image with color.
7075

0 commit comments

Comments
 (0)