Skip to content

Commit

Permalink
Merge pull request #2177 from embeddedt/fix/animated-sprite-sync
Browse files Browse the repository at this point in the history
Keep times of animated sprites in sync
  • Loading branch information
IMS212 authored Dec 4, 2023
2 parents c08442b + 4901065 commit cf1e76b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package me.jellysquid.mods.sodium.mixin.features.textures.animations.tracking;

import net.minecraft.client.texture.SpriteContents;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

import java.util.List;

@Mixin(SpriteContents.Animation.class)
public interface SpriteContentsAnimationAccessor {
@Accessor("frames")
List<SpriteContents.AnimationFrame> getFrames();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package me.jellysquid.mods.sodium.mixin.features.textures.animations.tracking;

import net.minecraft.client.texture.SpriteContents;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.gen.Accessor;

@Mixin(SpriteContents.AnimationFrame.class)
public interface SpriteContentsAnimationFrameAccessor {
@Accessor("time")
int getTime();
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@
import me.jellysquid.mods.sodium.client.SodiumClientMod;
import me.jellysquid.mods.sodium.client.render.texture.SpriteContentsExtended;
import net.minecraft.client.texture.SpriteContents;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;

@Mixin(SpriteContents.AnimatorImpl.class)
public class SpriteContentsAnimatorImplMixin {
@Shadow
int currentTime;
@Shadow
@Final
SpriteContents.Animation animation;
@Shadow
int frame;

@Unique
private SpriteContents parent;

Expand All @@ -30,6 +42,12 @@ private void preTick(CallbackInfo ci) {
boolean onDemand = SodiumClientMod.options().performance.animateOnlyVisibleTextures;

if (onDemand && !parent.sodium$isActive()) {
this.currentTime++;
List<SpriteContents.AnimationFrame> frames = ((SpriteContentsAnimationAccessor)this.animation).getFrames();
if (this.currentTime >= ((SpriteContentsAnimationFrameAccessor)frames.get(this.frame)).getTime()) {
this.frame = (this.frame + 1) % frames.size();
this.currentTime = 0;
}
ci.cancel();
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/sodium.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
"features.textures.animations.tracking.DrawContextMixin",
"features.textures.animations.tracking.SpriteAtlasTextureMixin",
"features.textures.animations.tracking.SpriteBillboardParticleMixin",
"features.textures.animations.tracking.SpriteContentsAnimationAccessor",
"features.textures.animations.tracking.SpriteContentsAnimationFrameAccessor",
"features.textures.animations.tracking.SpriteContentsAnimatorImplMixin",
"features.textures.animations.tracking.SpriteContentsMixin",
"features.textures.animations.upload.SpriteContentsAccessor",
Expand Down

0 comments on commit cf1e76b

Please sign in to comment.