Skip to content

Commit 49847c9

Browse files
authored
Various blend state fixes (#2208)
* NinePatchTexture should support blending * blend_func -> prev_blend_func * SpriteList should restore old blend func * Surface.draw_sprite should use draw_sprite
1 parent 0f0d28b commit 49847c9

4 files changed

Lines changed: 19 additions & 7 deletions

File tree

arcade/gui/nine_patch.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,8 @@ def draw_rect(
197197
self,
198198
*,
199199
rect: arcade.types.Rect,
200-
pixelated: bool = False,
200+
pixelated: bool = True,
201+
blend: bool = True,
201202
**kwargs,
202203
):
203204
"""
@@ -212,6 +213,11 @@ def draw_rect(
212213
:param rect: Rectangle to draw the 9-patch texture in
213214
:param pixelated: Whether to draw with nearest neighbor interpolation
214215
"""
216+
if blend:
217+
self._ctx.enable_only(self._ctx.BLEND)
218+
else:
219+
self._ctx.disable(self._ctx.BLEND)
220+
215221
self.program.set_uniform_safe("texture_id", self._atlas.get_texture_id(self._texture))
216222
if pixelated:
217223
self._atlas.texture.filter = self._ctx.NEAREST, self._ctx.NEAREST
@@ -228,6 +234,9 @@ def draw_rect(
228234
self._atlas.texture.use(1)
229235
self._geometry.render(self._program, vertices=1)
230236

237+
if blend:
238+
self._ctx.disable(self._ctx.BLEND)
239+
231240
def _check_sizes(self):
232241
"""Raise a ValueError if any dimension is invalid."""
233242
# Sanity check values

arcade/gui/surface.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,12 @@ def draw_texture(
121121
else:
122122
arcade.draw_texture_rect(tex, LBWH(x, y, width, height), angle=angle, alpha=alpha)
123123

124-
def draw_sprite(self, x, y, width, height, sprite):
124+
def draw_sprite(self, x: float, y: float, width: float, height: float, sprite: arcade.Sprite):
125125
"""Draw a sprite to the surface"""
126126
sprite.position = x + width // 2, y + height // 2
127127
sprite.width = width
128128
sprite.height = height
129-
sprite.draw()
129+
arcade.draw_sprite(sprite)
130130

131131
@contextmanager
132132
def activate(self):
@@ -137,15 +137,15 @@ def activate(self):
137137
# Set viewport and projection
138138
self.limit(LBWH(0, 0, *self.size))
139139
# Set blend function
140-
blend_func = self.ctx.blend_func
140+
prev_blend_func = self.ctx.blend_func
141141

142142
try:
143143
self.ctx.blend_func = self.blend_func_render_into
144144
with self.fbo.activate():
145145
yield self
146146
finally:
147147
# Restore blend function.
148-
self.ctx.blend_func = blend_func
148+
self.ctx.blend_func = prev_blend_func
149149

150150
def limit(self, rect: Rect): # TODO track limit usage
151151
"""Reduces the draw area to the given rect"""

arcade/gui/widgets/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,8 @@ def on_update(self, dt):
671671
def do_render(self, surface: Surface):
672672
self.prepare_render(surface)
673673
surface.clear(color=TRANSPARENT_BLACK)
674-
surface.draw_sprite(0, 0, self.width, self.height, self._sprite)
674+
if self._sprite is not None:
675+
surface.draw_sprite(0, 0, self.width, self.height, self._sprite)
675676

676677

677678
class UILayout(UIWidget):

arcade/sprite_list/sprite_list.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,7 @@ def draw(
10411041
raise ValueError("Attempting to render without shader program.")
10421042
self._write_sprite_buffers_to_gpu()
10431043

1044+
prev_blend_func = self.ctx.blend_func
10441045
if self._blend:
10451046
self.ctx.enable(self.ctx.BLEND)
10461047
# Set custom blend function or revert to default
@@ -1088,7 +1089,8 @@ def draw(
10881089
# Leave global states to default
10891090
if self._blend:
10901091
self.ctx.disable(self.ctx.BLEND)
1091-
self.ctx.blend_func = self.ctx.BLEND_DEFAULT
1092+
if blend_function is not None:
1093+
self.ctx.blend_func = prev_blend_func
10921094

10931095
def draw_hit_boxes(self, color: RGBA255 = (0, 0, 0, 255), line_thickness: float = 1.0) -> None:
10941096
"""Draw all the hit boxes in this list"""

0 commit comments

Comments
 (0)