Skip to content

Commit c0a890c

Browse files
committed
Fix remaining issues with rendering.
1 parent 957e4b7 commit c0a890c

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

tcod/render.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ def render(self, console: tcod.console.Console) -> tcod.sdl.render.Texture:
4848
format=int(lib.SDL_PIXELFORMAT_RGBA32),
4949
access=int(lib.SDL_TEXTUREACCESS_TARGET),
5050
)
51-
_check(
52-
lib.TCOD_sdl2_render_texture(
53-
self._atlas.p, console.console_c, self._cache_console.console_c, self._texture.p
51+
52+
with self._renderer.set_render_target(self._texture):
53+
_check(
54+
lib.TCOD_sdl2_render_texture(
55+
self._atlas.p, console.console_c, self._cache_console.console_c, self._texture.p
56+
)
5457
)
55-
)
5658
return self._texture

tcod/sdl/render.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,20 @@ def rgb_mod(self, rgb: Tuple[int, int, int]) -> None:
8282
_check(lib.SDL_SetTextureColorMod(self.p, rgb[0], rgb[1], rgb[2]))
8383

8484

85+
class _RestoreTargetContext:
86+
"""A context manager which tracks the current render target and restores it on exiting."""
87+
88+
def __init__(self, renderer: Renderer) -> None:
89+
self.renderer = renderer
90+
self.old_texture_p = lib.SDL_GetRenderTarget(renderer.p)
91+
92+
def __enter__(self) -> None:
93+
pass
94+
95+
def __exit__(self, *_: Any) -> None:
96+
_check(lib.SDL_SetRenderTarget(self.renderer.p, self.old_texture_p))
97+
98+
8599
class Renderer:
86100
def __init__(self, sdl_renderer_p: Any) -> None:
87101
if ffi.typeof(sdl_renderer_p) is not ffi.typeof("struct SDL_Renderer*"):
@@ -105,6 +119,10 @@ def copy(
105119
dest_ = ffi.NULL if dest is None else ffi.new("SDL_Rect*", dest)
106120
_check(lib.SDL_RenderCopy(self.p, texture.p, source_, dest_))
107121

122+
def present(self) -> None:
123+
"""Present the currently rendered image to the screen."""
124+
lib.SDL_RenderPresent(self.p)
125+
108126
def new_texture(
109127
self, width: int, height: int, *, format: Optional[int] = None, access: Optional[int] = None
110128
) -> Texture:
@@ -113,11 +131,15 @@ def new_texture(
113131
format = 0
114132
if access is None:
115133
access = int(lib.SDL_TEXTUREACCESS_STATIC)
116-
format = int(lib.SDL_PIXELFORMAT_RGBA32)
117-
access = int(lib.SDL_TEXTUREACCESS_STATIC)
118134
texture_p = ffi.gc(lib.SDL_CreateTexture(self.p, format, access, width, height), lib.SDL_DestroyTexture)
119135
return Texture(texture_p, self.p)
120136

137+
def set_render_target(self, texture: Texture) -> _RestoreTargetContext:
138+
"""Change the render target to `texture`, returns a context that will restore the original target when exited."""
139+
restore = _RestoreTargetContext(self)
140+
_check(lib.SDL_SetRenderTarget(self.p, texture.p))
141+
return restore
142+
121143
def upload_texture(
122144
self, pixels: NDArray[Any], *, format: Optional[int] = None, access: Optional[int] = None
123145
) -> Texture:

0 commit comments

Comments
 (0)