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

Cache packed color on Sprite #7222

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
59 changes: 36 additions & 23 deletions gdx/src/com/badlogic/gdx/graphics/g2d/Sprite.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class Sprite extends TextureRegion {

final float[] vertices = new float[SPRITE_SIZE];
private final Color color = new Color(1, 1, 1, 1);
private float packedColor = Color.WHITE_FLOAT_BITS;
obigu marked this conversation as resolved.
Show resolved Hide resolved
private float x, y;
float width, height;
private float originX, originY;
Expand Down Expand Up @@ -338,45 +339,52 @@ public void translate (float xAmount, float yAmount) {
/** Sets the color used to tint this sprite. Default is {@link Color#WHITE}. */
public void setColor (Color tint) {
color.set(tint);
float color = tint.toFloatBits();
packedColor = tint.toFloatBits();
float[] vertices = this.vertices;
vertices[C1] = color;
vertices[C2] = color;
vertices[C3] = color;
vertices[C4] = color;
vertices[C1] = packedColor;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already store the packed color in the vertices, though using it (as we used to) is a bit hacky.

vertices[C2] = packedColor;
vertices[C3] = packedColor;
vertices[C4] = packedColor;
}

/** Sets the alpha portion of the color used to tint this sprite. */
public void setAlpha (float a) {
color.a = a;
float color = this.color.toFloatBits();
vertices[C1] = color;
vertices[C2] = color;
vertices[C3] = color;
vertices[C4] = color;
if (color.a != a) {
obigu marked this conversation as resolved.
Show resolved Hide resolved
color.a = a;
packedColor = color.toFloatBits();
vertices[C1] = packedColor;
vertices[C2] = packedColor;
vertices[C3] = packedColor;
vertices[C4] = packedColor;
}
}

/** @see #setColor(Color) */
public void setColor (float r, float g, float b, float a) {
color.set(r, g, b, a);
float color = this.color.toFloatBits();
packedColor = color.toFloatBits();
float[] vertices = this.vertices;
vertices[C1] = color;
vertices[C2] = color;
vertices[C3] = color;
vertices[C4] = color;
vertices[C1] = packedColor;
vertices[C2] = packedColor;
vertices[C3] = packedColor;
vertices[C4] = packedColor;
}

/** Sets the color of this sprite, expanding the alpha from 0-254 to 0-255.
/** Sets the packed color used to tint this sprite.
* @see #setColor(Color)
* @see Color#toFloatBits() */
public void setPackedColor (float packedColor) {
Color.abgr8888ToColor(color, packedColor);
float[] vertices = this.vertices;
vertices[C1] = packedColor;
vertices[C2] = packedColor;
vertices[C3] = packedColor;
vertices[C4] = packedColor;
// Handle 0f/-0f special case
if (packedColor != this.packedColor || (packedColor == 0f && this.packedColor == 0f
&& Float.floatToIntBits(packedColor) != Float.floatToIntBits(this.packedColor))) {
this.packedColor = packedColor;
Color.abgr8888ToColor(color, packedColor);
float[] vertices = this.vertices;
vertices[C1] = packedColor;
vertices[C2] = packedColor;
vertices[C3] = packedColor;
vertices[C4] = packedColor;
}
}

/** Sets the origin in relation to the sprite's position for scaling and rotation. */
Expand Down Expand Up @@ -632,6 +640,11 @@ public Color getColor () {
return color;
}

/** Returns the packed color of this sprite. */
public float getPackedColor () {
return packedColor;
}

public void setRegion (float u, float v, float u2, float v2) {
super.setRegion(u, v, u2, v2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public SpriteDrawable (SpriteDrawable drawable) {

public void draw (Batch batch, float x, float y, float width, float height) {
Color spriteColor = sprite.getColor();
float oldColor = spriteColor.toFloatBits();
float oldColor = sprite.getPackedColor();
sprite.setColor(spriteColor.mul(batch.getColor()));

sprite.setRotation(0);
Expand All @@ -56,7 +56,7 @@ public void draw (Batch batch, float x, float y, float originX, float originY, f
float scaleY, float rotation) {

Color spriteColor = sprite.getColor();
float oldColor = spriteColor.toFloatBits();
float oldColor = sprite.getPackedColor();
sprite.setColor(spriteColor.mul(batch.getColor()));

sprite.setOrigin(originX, originY);
Expand Down