Skip to content

Commit 70863c3

Browse files
committed
Continue SDL3 port
All type issues resolved except for audio and event systems
1 parent 239ddbd commit 70863c3

22 files changed

+1172
-426
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@
319319
"OPENGL",
320320
"opensearch",
321321
"OPER",
322+
"OVERSCAN",
322323
"packbits",
323324
"PAGEDOWN",
324325
"pagerefs",

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ This project adheres to [Semantic Versioning](https://semver.org/) since version
1010

1111
- Switched to SDL3.
1212
This will cause several breaking changes such as the names of keyboard constants and other SDL enums.
13+
- `tcod.sdl.video.Window.grab` has been split into `.mouse_grab` and `.keyboard_grab` attributes.
14+
- `tcod.event.KeySym` single letter symbols are now all uppercase.
15+
- Relative mouse mode is set via `tcod.sdl.video.Window.relative_mouse_mode` instead of `tcod.sdl.mouse.set_relative_mode`.
16+
- `tcod.sdl.render.new_renderer`: Removed `software` and `target_textures` parameters, `vsync` takes `int`, `driver` takes `str` instead of `int`.
17+
- SDL renderer logical
18+
- `tcod.sdl.render.Renderer`: `integer_scaling` and `logical_size` are now set with `set_logical_presentation` method.
1319

1420
### Removed
1521

1622
- Support dropped for Python 3.8 and 3.9.
1723
- Removed `Joystick.get_current_power` due to SDL3 changes.
1824
- `WindowFlags.FULLSCREEN_DESKTOP` is now just `WindowFlags.FULLSCREEN`
25+
- `tcod.sdl.render.Renderer.integer_scaling` removed.
1926

2027
### Fixed
2128

build_libtcod.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33

44
from __future__ import annotations
55

6+
import ast
67
import contextlib
78
import glob
89
import os
910
import platform
1011
import re
12+
import subprocess
1113
import sys
1214
from collections.abc import Iterable, Iterator
1315
from pathlib import Path
@@ -208,7 +210,7 @@ def walk_sources(directory: str) -> Iterator[str]:
208210
extra_link_args.extend(GCC_CFLAGS[tdl_build])
209211

210212
ffi = FFI()
211-
sdl_cdef = build_sdl.get_cdef()
213+
sdl_cdef, sdl_strings = build_sdl.get_cdef()
212214
ffi.cdef(sdl_cdef)
213215
for include in includes:
214216
try:
@@ -415,6 +417,13 @@ def write_library_constants() -> None:
415417

416418
Path("tcod/event.py").write_text(event_py, encoding="utf-8")
417419

420+
with Path("tcod/sdl/constants.py").open("w", encoding="utf-8") as f:
421+
f.write('"""SDL private constants."""\n\n')
422+
for name, value in sdl_strings.items():
423+
f.write(f"{name} = {ast.literal_eval(value)!r}\n")
424+
425+
subprocess.run(["ruff", "format", "--silent", Path("tcod/sdl/constants.py")], check=True) # noqa: S603, S607
426+
418427

419428
def _fix_reserved_name(name: str) -> str:
420429
"""Add underscores to reserved Python keywords."""
@@ -440,6 +449,8 @@ def _type_from_names(names: list[str]) -> str:
440449
return "bool"
441450
if names[-1] in ("size_t", "int", "ptrdiff_t"):
442451
return "int"
452+
if names[-1] in ("float", "double"):
453+
return "float"
443454
return "Any"
444455

445456

@@ -570,6 +581,8 @@ def write_hints() -> None:
570581
"""
571582
+ cdef
572583
)
584+
for match in re.finditer(r"SDL_PIXELFORMAT_\w+", cdef):
585+
function_collector.variables.add(f"{match.group()}: int")
573586
cdef = re.sub(r"(typedef enum SDL_PixelFormat).*(SDL_PixelFormat;)", r"\1 \2", cdef, flags=re.DOTALL)
574587
cdef = cdef.replace("padding[...]", "padding[]")
575588
cdef = cdef.replace("...;} SDL_TouchFingerEvent;", "} SDL_TouchFingerEvent;")
@@ -606,6 +619,8 @@ def write_hints() -> None:
606619
)
607620
cdef = re.sub(r"""extern "Python" \{(.*?)\}""", r"\1", cdef, flags=re.DOTALL)
608621
function_collector.visit(c.parse(cdef))
622+
function_collector.variables.add("TCOD_ctx: Any")
623+
function_collector.variables.add("TCOD_COMPILEDVERSION: Final[int]")
609624

610625
# Write PYI file
611626
out_functions = """\n\n @staticmethod\n""".join(sorted(function_collector.functions))
@@ -628,6 +643,7 @@ class _lib:
628643
ffi: Any
629644
"""
630645
Path("tcod/_libtcod.pyi").write_text(pyi)
646+
subprocess.run(["ruff", "format", "--silent", Path("tcod/_libtcod.pyi")], check=True) # noqa: S603, S607
631647

632648

633649
if __name__ == "__main__":

build_sdl.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def on_directive_handle(
246246
"""
247247

248248

249-
def get_cdef() -> str:
249+
def get_cdef() -> tuple[str, dict[str, str]]:
250250
"""Return the parsed code of SDL for CFFI."""
251251
with TemporaryDirectory() as temp_dir:
252252
# Add a false SDL_oldnames.h to prevent old symbols from being collected
@@ -286,7 +286,7 @@ def get_cdef() -> str:
286286
)
287287
for name in FLEXIBLE_STRUCTS:
288288
sdl_cdef = sdl_cdef.replace(f"}} {name};", f"...;}} {name};")
289-
return sdl_cdef + EXTRA_CDEF
289+
return sdl_cdef + EXTRA_CDEF, parser.known_string_defines
290290

291291

292292
include_dirs: list[str] = []

examples/samples_tcod.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import tcod.sdl.render
3434
import tcod.tileset
3535
from tcod import libtcodpy
36-
from tcod.sdl.video import WindowFlags
3736

3837
if TYPE_CHECKING:
3938
from numpy.typing import NDArray
@@ -101,8 +100,8 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> None:
101100
elif event.sym == tcod.event.KeySym.RETURN and event.mod & tcod.event.Modifier.ALT:
102101
sdl_window = context.sdl_window
103102
if sdl_window:
104-
sdl_window.fullscreen = False if sdl_window.fullscreen else WindowFlags.FULLSCREEN_DESKTOP
105-
elif event.sym in (tcod.event.KeySym.PRINTSCREEN, tcod.event.KeySym.p):
103+
sdl_window.fullscreen = not sdl_window.fullscreen
104+
elif event.sym in (tcod.event.KeySym.PRINTSCREEN, tcod.event.KeySym.P):
106105
print("screenshot")
107106
if event.mod & tcod.event.Modifier.ALT:
108107
libtcodpy.console_save_apf(root_console, "samples.apf")
@@ -469,27 +468,27 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> None:
469468
if tcod.event.KeySym.N9 >= event.sym >= tcod.event.KeySym.N1:
470469
self.func = event.sym - tcod.event.KeySym.N1
471470
self.noise = self.get_noise()
472-
elif event.sym == tcod.event.KeySym.e:
471+
elif event.sym == tcod.event.KeySym.E:
473472
self.hurst += 0.1
474473
self.noise = self.get_noise()
475-
elif event.sym == tcod.event.KeySym.d:
474+
elif event.sym == tcod.event.KeySym.D:
476475
self.hurst -= 0.1
477476
self.noise = self.get_noise()
478-
elif event.sym == tcod.event.KeySym.r:
477+
elif event.sym == tcod.event.KeySym.R:
479478
self.lacunarity += 0.5
480479
self.noise = self.get_noise()
481-
elif event.sym == tcod.event.KeySym.f:
480+
elif event.sym == tcod.event.KeySym.F:
482481
self.lacunarity -= 0.5
483482
self.noise = self.get_noise()
484-
elif event.sym == tcod.event.KeySym.t:
483+
elif event.sym == tcod.event.KeySym.T:
485484
self.octaves += 0.5
486485
self.noise.octaves = self.octaves
487-
elif event.sym == tcod.event.KeySym.g:
486+
elif event.sym == tcod.event.KeySym.G:
488487
self.octaves -= 0.5
489488
self.noise.octaves = self.octaves
490-
elif event.sym == tcod.event.KeySym.y:
489+
elif event.sym == tcod.event.KeySym.Y:
491490
self.zoom += 0.2
492-
elif event.sym == tcod.event.KeySym.h:
491+
elif event.sym == tcod.event.KeySym.H:
493492
self.zoom -= 0.2
494493
else:
495494
super().ev_keydown(event)
@@ -648,10 +647,10 @@ def on_draw(self) -> None:
648647

649648
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
650649
MOVE_KEYS = { # noqa: N806
651-
tcod.event.KeySym.i: (0, -1),
652-
tcod.event.KeySym.j: (-1, 0),
653-
tcod.event.KeySym.k: (0, 1),
654-
tcod.event.KeySym.l: (1, 0),
650+
tcod.event.KeySym.I: (0, -1),
651+
tcod.event.KeySym.J: (-1, 0),
652+
tcod.event.KeySym.K: (0, 1),
653+
tcod.event.KeySym.L: (1, 0),
655654
}
656655
FOV_SELECT_KEYS = { # noqa: N806
657656
tcod.event.KeySym.MINUS: -1,
@@ -664,9 +663,9 @@ def ev_keydown(self, event: tcod.event.KeyDown) -> None:
664663
if self.walkable[self.player_x + x, self.player_y + y]:
665664
self.player_x += x
666665
self.player_y += y
667-
elif event.sym == tcod.event.KeySym.t:
666+
elif event.sym == tcod.event.KeySym.T:
668667
self.torch = not self.torch
669-
elif event.sym == tcod.event.KeySym.w:
668+
elif event.sym == tcod.event.KeySym.W:
670669
self.light_walls = not self.light_walls
671670
elif event.sym in FOV_SELECT_KEYS:
672671
self.algo_num += FOV_SELECT_KEYS[event.sym]
@@ -740,13 +739,13 @@ def on_draw(self) -> None:
740739

741740
def ev_keydown(self, event: tcod.event.KeyDown) -> None:
742741
"""Handle movement and UI."""
743-
if event.sym == tcod.event.KeySym.i and self.dest_y > 0: # destination move north
742+
if event.sym == tcod.event.KeySym.I and self.dest_y > 0: # destination move north
744743
self.dest_y -= 1
745-
elif event.sym == tcod.event.KeySym.k and self.dest_y < SAMPLE_SCREEN_HEIGHT - 1: # destination move south
744+
elif event.sym == tcod.event.KeySym.K and self.dest_y < SAMPLE_SCREEN_HEIGHT - 1: # destination move south
746745
self.dest_y += 1
747-
elif event.sym == tcod.event.KeySym.j and self.dest_x > 0: # destination move west
746+
elif event.sym == tcod.event.KeySym.J and self.dest_x > 0: # destination move west
748747
self.dest_x -= 1
749-
elif event.sym == tcod.event.KeySym.l and self.dest_x < SAMPLE_SCREEN_WIDTH - 1: # destination move east
748+
elif event.sym == tcod.event.KeySym.L and self.dest_x < SAMPLE_SCREEN_WIDTH - 1: # destination move east
750749
self.dest_x += 1
751750
elif event.sym == tcod.event.KeySym.TAB:
752751
self.using_astar = not self.using_astar
@@ -1334,9 +1333,9 @@ def init_context(renderer: int) -> None:
13341333
)
13351334
if context.sdl_renderer: # If this context supports SDL rendering.
13361335
# Start by setting the logical size so that window resizing doesn't break anything.
1337-
context.sdl_renderer.logical_size = (
1338-
tileset.tile_width * root_console.width,
1339-
tileset.tile_height * root_console.height,
1336+
context.sdl_renderer.set_logical_presentation(
1337+
resolution=(tileset.tile_width * root_console.width, tileset.tile_height * root_console.height),
1338+
mode=tcod.sdl.render.LogicalPresentation.STRETCH,
13401339
)
13411340
assert context.sdl_atlas
13421341
# Generate the console renderer and minimap.

examples/sdl-hello-world.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def main() -> None:
3131
"""Show hello world until the window is closed."""
3232
# Open an SDL window and renderer.
3333
window = tcod.sdl.video.new_window(720, 480, flags=tcod.sdl.video.WindowFlags.RESIZABLE)
34-
renderer = tcod.sdl.render.new_renderer(window, target_textures=True)
34+
renderer = tcod.sdl.render.new_renderer(window)
3535
# Render the text once, then reuse the texture.
3636
hello_world = render_text(renderer, "Hello World")
3737
hello_world.color_mod = (64, 255, 64) # Set the color when copied.

0 commit comments

Comments
 (0)