Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review performance impact of Sprite.scale changes #2324

Open
einarf opened this issue Jul 28, 2024 · 1 comment
Open

Review performance impact of Sprite.scale changes #2324

einarf opened this issue Jul 28, 2024 · 1 comment
Labels
performance Speed / Optimizations
Milestone

Comments

@einarf
Copy link
Member

einarf commented Jul 28, 2024

This might just be about reverting #2021

The vector sprite perf testing showed 2.x cost. We should quickly review the cost of the sprite scale changes before we release.

@property
def scale(self) -> Vec2:
"""Get or set the x & y scale of the sprite as a pair of values.
You may set it to either a single value or a pair of values:
.. list-table::
:header-rows: 0
* - Single value
- ``sprite.scale = 2.0``
* - Tuple or :py:class:`~pyglet,math.Vec2`
- ``sprite.scale = (1.0, 3.0)``
The two-channel version is useful for making health bars and
other indicators.
.. note:: Returns a :py:class:`pyglet.math.Vec2` for
compatibility.
Arcade versions lower than 3,0 used one or both of the following
for scale:
* A single :py:class:`float` on versions <= 2.6
* A ``scale_xy`` property and exposing only the x component
on some intermediate dev releases
Although scale is internally stored as a :py:class:`tuple`, we
return a :py:class:`pyglet.math.Vec2` to allow the in-place
operators to work in addition to setting values directly:
* Old-style (``sprite.scale *= 2.0``)
* New-style (``sprite.scale *= 2.0, 2.0``)
.. note:: Negative scale values are supported.
This applies to both single-axis and dual-axis.
Negatives will flip & mirror the sprite, but the
with will use :py:func:`abs` to report total width
and height instead of negatives.
"""
return Vec2(*self._scale)
@scale.setter
def scale(self, new_scale: Point2 | AsFloat):
if isinstance(new_scale, (float, int)):
scale_x = new_scale
scale_y = new_scale
else: # Treat it as some sort of iterable or sequence
# Don't abstract this. Keep it here since it's a hot code path
try:
scale_x, scale_y = new_scale # type / length implicit check
except ValueError:
raise ValueError(
"scale must be a tuple-like object which unpacks to exactly 2 coordinates"
)
except TypeError:
raise TypeError(
"scale must be a tuple-like object which unpacks to exactly 2 coordinates"
)
new_scale = scale_x, scale_y
if new_scale == self._scale:
return
self._hit_box.scale = new_scale
tex_width, tex_height = self._texture.size
self._scale = new_scale
self._width = tex_width * scale_x
self._height = tex_height * scale_y
self.update_spatial_hash()
for sprite_list in self.sprite_lists:
sprite_list._update_size(self)

We might just have to revert these changes for now and bake them into the new experimental sprites.
See : #2184

@einarf einarf added this to the 3.0 mandatory milestone Jul 28, 2024
@einarf einarf added the performance Speed / Optimizations label Aug 3, 2024
@einarf
Copy link
Member Author

einarf commented Aug 3, 2024

Considering scale returns a Vec2 this should be part of the vector sprites later in 3.1/3.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Speed / Optimizations
Projects
None yet
Development

No branches or pull requests

1 participant