Skip to content

Commit ee8cde1

Browse files
committed
Add support for creating plain SDL windows and renderers.
1 parent 1cf53b4 commit ee8cde1

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@
220220
"pcpp",
221221
"PILCROW",
222222
"pilmode",
223+
"PRESENTVSYNC",
223224
"PRINTF",
224225
"printn",
225226
"pycall",

tcod/sdl/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,11 @@ def _check(result: int) -> int:
3535
return result
3636

3737

38+
def _check_p(result: Any) -> Any:
39+
"""Check if an SDL function returned NULL, and raise an exception if it did."""
40+
if not result:
41+
raise RuntimeError(_get_error())
42+
return result
43+
44+
3845
lib.SDL_LogSetOutputFunction(lib._sdl_log_output_function, ffi.NULL)

tcod/sdl/render.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
import numpy as np
66
from numpy.typing import NDArray
77

8+
import tcod.sdl.video
89
from tcod.loader import ffi, lib
9-
from tcod.sdl import _check
10+
from tcod.sdl import _check, _check_p
1011

1112

1213
class Texture:
@@ -161,3 +162,34 @@ def upload_texture(
161162
lib.SDL_UpdateTexture(texture.p, ffi.NULL, ffi.cast("const void*", pixels.ctypes.data), pixels.strides[0])
162163
)
163164
return texture
165+
166+
167+
def new_renderer(
168+
window: tcod.sdl.video.Window,
169+
*,
170+
driver: Optional[int] = None,
171+
software: bool = False,
172+
vsync: bool = True,
173+
target_textures: bool = False,
174+
) -> Renderer:
175+
"""Initialize and return a new SDL Renderer.
176+
177+
Example::
178+
179+
# Start by creating a window.
180+
sdl_window = tcod.sdl.video.new_window(640, 480)
181+
# Create a renderer with target texture support.
182+
sdl_renderer = tcod.sdl.render.new_renderer(sdl_window, target_textures=True)
183+
184+
.. seealso::
185+
:func:`tcod.sdl.video.new_window`
186+
"""
187+
driver = driver if driver is not None else -1
188+
flags = 0
189+
if vsync:
190+
flags |= int(lib.SDL_RENDERER_PRESENTVSYNC)
191+
if target_textures:
192+
flags |= int(lib.SDL_RENDERER_TARGETTEXTURE)
193+
flags |= int(lib.SDL_RENDERER_SOFTWARE) if software else int(lib.SDL_RENDERER_ACCELERATED)
194+
renderer_p = _check_p(ffi.gc(lib.SDL_CreateRenderer(window.p, driver, flags), lib.SDL_DestroyRenderer))
195+
return Renderer(renderer_p)

tcod/sdl/video.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
"""
66
from __future__ import annotations
77

8-
from typing import Any, Tuple
8+
import sys
9+
from typing import Any, Optional, Tuple
910

1011
import numpy as np
1112
from numpy.typing import ArrayLike, NDArray
1213

1314
from tcod.loader import ffi, lib
15+
from tcod.sdl import _check_p
1416

1517
__all__ = ("Window",)
1618

@@ -124,6 +126,33 @@ def title(self, value: str) -> None:
124126
lib.SDL_SetWindowtitle(self.p, value.encode("utf-8"))
125127

126128

129+
def new_window(
130+
width: int,
131+
height: int,
132+
*,
133+
x: Optional[int] = None,
134+
y: Optional[int] = None,
135+
title: Optional[str] = None,
136+
flags: int = 0,
137+
) -> Window:
138+
"""Initialize and return a new SDL Window.
139+
140+
Example::
141+
142+
# Create a new resizable window with a custom title.
143+
window = tcod.sdl.video.new_window(640, 480, title="Title bar text", flags=tcod.lib.SDL_WINDOW_RESIZABLE)
144+
145+
.. seealso::
146+
:func:`tcod.sdl.render.new_renderer`
147+
"""
148+
x = x if x is not None else int(lib.SDL_WINDOWPOS_UNDEFINED)
149+
y = y if y is not None else int(lib.SDL_WINDOWPOS_UNDEFINED)
150+
if title is None:
151+
title = sys.argv[0]
152+
window_p = ffi.gc(lib.SDL_CreateWindow(title.encode("utf-8"), x, y, width, height, flags), lib.SDL_DestroyWindow)
153+
return Window(_check_p(window_p))
154+
155+
127156
def _get_active_window() -> Window:
128157
"""Return the SDL2 window current managed by libtcod.
129158

0 commit comments

Comments
 (0)