animated gifs correctly hotsync

This commit is contained in:
John Alanbrook 2024-08-22 14:41:01 -05:00
parent 285a227691
commit b31069fe91
3 changed files with 49 additions and 13 deletions

View file

@ -67,6 +67,19 @@ var sprite = {
advance(); advance();
}, },
tex_sync() {
if (this.anim) this.stop();
this.rect = fullrect;
var anim = SpriteAnim.make(this.path);
this.update_dimensions();
this.sync();
if (!anim) return;
this.anim = anim;
this.play();
this.pos = this.dimensions().scale(this.anchor);
},
stop() { stop() {
this.del_anim?.(); this.del_anim?.();
}, },
@ -77,23 +90,15 @@ var sprite = {
return; return;
} }
if (p === this.path) return; if (p === this.path) return;
this._p = p; this._p = p;
this.del_anim?.(); this.del_anim?.();
this.texture = game.texture(p); this.texture = game.texture(p);
this.diffuse = this.texture; this.diffuse = this.texture;
this.rect = fullrect;
var anim = SpriteAnim.make(p); this.tex_sync();
this.update_dimensions();
this.sync();
if (!anim) return;
this.anim = anim;
this.play();
this.pos = this.dimensions().scale(this.anchor);
}, },
get path() { get path() {
return this._p; return this._p;
@ -200,7 +205,6 @@ Object.mixin(os.make_seg2d(), {
var animcache = {}; var animcache = {};
var SpriteAnim = {}; var SpriteAnim = {};
SpriteAnim.make = function(path) { SpriteAnim.make = function(path) {
if (path in animcache) return animcache[path];
var anim; var anim;
if (io.exists(path.set_ext(".ase"))) if (io.exists(path.set_ext(".ase")))
anim = SpriteAnim.aseprite(path.set_ext(".ase")); anim = SpriteAnim.aseprite(path.set_ext(".ase"));

View file

@ -182,9 +182,16 @@ game.tex_hotreload = function()
{ {
for (var path in game.texture.cache) { for (var path in game.texture.cache) {
if (io.mod(path) > game.texture.time_cache[path]) { if (io.mod(path) > game.texture.time_cache[path]) {
say(`HOT SWAPPING IMAGE ${path}`);
var tex = game.texture.cache[path]; var tex = game.texture.cache[path];
game.texture.time_cache[path] = io.mod(path); game.texture.time_cache[path] = io.mod(path);
os.texture_swap(path, game.texture.cache[path]); os.texture_swap(path, game.texture.cache[path]);
for (var sprite of Object.values(allsprites)) {
if (sprite.texture == tex) {
say('syncing a sprite ...');
sprite.tex_sync();
}
}
} }
} }
} }

View file

@ -527,6 +527,30 @@ render.init = function() {
render.sprites = function render_sprites(gridsize = 1) render.sprites = function render_sprites(gridsize = 1)
{ {
// When y sorting, draw layer is firstly important followed by the gameobject position
profile.frame("y bucketing");
var sps = Object.values(allsprites);
var buckets = {};
for (var sprite of sps) {
var layer = (sprite.gameobject.drawlayer*10000)-sprite.gameobject.pos.y;
buckets[layer] ??= {};
if (buckets[layer][sprite.path])
buckets[layer][sprite.path].push(sprite);
else
buckets[layer][sprite.path] = [sprite];
}
profile.endframe();
profile.frame("y sorting");
buckets = Object.entries(buckets).sort((a,b) => {
var na = Number(a[0]);
var nb = Number(b[0]);
if (na < nb) return -1;
return 1;
});
profile.endframe();
/* /*
profile.frame("bucketing"); profile.frame("bucketing");
var sps = Object.values(allsprites); var sps = Object.values(allsprites);
@ -544,7 +568,7 @@ render.sprites = function render_sprites(gridsize = 1)
} }
profile.endframe(); profile.endframe();
*/
profile.frame("sorting"); profile.frame("sorting");
var sprite_buckets = component.sprite_buckets(); var sprite_buckets = component.sprite_buckets();
@ -556,6 +580,7 @@ render.sprites = function render_sprites(gridsize = 1)
return 1; return 1;
}); });
profile.endframe(); profile.endframe();
*/
profile.frame("drawing"); profile.frame("drawing");
render.use_shader(spritessboshader); render.use_shader(spritessboshader);