Skip to content

Commit

Permalink
override pyglet text blending
Browse files Browse the repository at this point in the history
  • Loading branch information
eruvanos committed Nov 19, 2024
1 parent fe61ff7 commit 00bf67c
Showing 1 changed file with 63 additions and 1 deletion.
64 changes: 63 additions & 1 deletion arcade/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,67 @@
__all__ = ["load_font", "Text", "create_text_sprite", "draw_text"]


class ArcadeTextLayoutGroup(pyglet.text.layout.TextLayoutGroup):
"""Create a text layout rendering group.
Overrides pyglet blending handling to allow for additive blending.
Furthermore, it resets the blend function to the previous state.
"""

_prev_blend = None
_prev_blend_func = None

def set_state(self) -> None:
import pyglet.gl as gl
from ctypes import c_int, c_ubyte

self.program.use()
self.program["scissor"] = False

gl.glActiveTexture(gl.GL_TEXTURE0)
gl.glBindTexture(self.texture.target, self.texture.id)

blend = c_ubyte()
gl.glGetBooleanv(gl.GL_BLEND, blend)
self._prev_blend = bool(blend.value)

src_rgb = c_int()
dst_rgb = c_int()
src_alpha = c_int()
dst_alpha = c_int()
gl.glGetIntegerv(gl.GL_BLEND_SRC_RGB, src_rgb)
gl.glGetIntegerv(gl.GL_BLEND_DST_RGB, dst_rgb)
gl.glGetIntegerv(gl.GL_BLEND_SRC_ALPHA, src_alpha)
gl.glGetIntegerv(gl.GL_BLEND_DST_ALPHA, dst_alpha)

self._prev_blend_func = (src_rgb.value, dst_rgb.value, src_alpha.value, dst_alpha.value)

gl.glEnable(gl.GL_BLEND)
gl.glBlendFuncSeparate(
gl.GL_SRC_ALPHA,
gl.GL_ONE_MINUS_SRC_ALPHA,
gl.GL_ONE,
gl.GL_ONE,
)

def unset_state(self) -> None:
import pyglet.gl as gl

if not self._prev_blend:
gl.glDisable(gl.GL_BLEND)

gl.glBlendFuncSeparate(
self._prev_blend_func[0],
self._prev_blend_func[1],
self._prev_blend_func[2],
self._prev_blend_func[3],
)
self.program.stop()


pyglet.text.layout.TextLayout.group_class = ArcadeTextLayoutGroup


def load_font(path: str | Path) -> None:
"""
Load fonts in a file (usually .ttf) adding them to a global font registry.
Expand Down Expand Up @@ -253,7 +314,8 @@ def __init__(
bold=bold,
italic=italic,
multiline=multiline,
rotation=rotation, # type: ignore # pending https://github.com/pyglet/pyglet/issues/843
rotation=rotation,
# type: ignore # pending https://github.com/pyglet/pyglet/issues/843
batch=batch,
group=group,
**kwargs,
Expand Down

0 comments on commit 00bf67c

Please sign in to comment.