Skip to content

Commit 5fd8236

Browse files
perf: only refresh texture once after a batch add (#1044)
1 parent 38e4ebd commit 5fd8236

File tree

5 files changed

+18
-3
lines changed

5 files changed

+18
-3
lines changed

src/assets/font.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ export function makeFont(
136136
),
137137
);
138138
}
139+
_k.assets.packer.refreshIfPending();
139140

140141
return {
141142
map,

src/assets/sprite.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export class SpriteData {
164164
? (src => _k.assets.packer.addSingle(src))
165165
: (src => _k.assets.packer.add(src)),
166166
);
167+
_k.assets.packer.refreshIfPending();
167168
return new SpriteData(frames, opt.anims, opt.slice9);
168169
}
169170

@@ -183,11 +184,13 @@ export class SpriteData {
183184
opt.slice9,
184185
);
185186
}
186-
return new SpriteData(
187+
const sd = new SpriteData(
187188
frames.map(frame => _k.assets.packer.add(data, frame)),
188189
opt.anims,
189190
opt.slice9,
190191
);
192+
_k.assets.packer.refreshIfPending();
193+
return sd;
191194
}
192195
}
193196

src/assets/spriteAtlas.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export function loadSpriteAtlas(
9090
_k.assets.sprites.addLoaded(name, spr);
9191
map[name] = spr;
9292
}
93+
_k.assets.packer.refreshIfPending();
9394
return map;
9495
}),
9596
);

src/gfx/TexPacker.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ export class TexPacker {
3838
this._newTexture();
3939
}
4040

41+
private _hasPendingRefresh = false;
4142
private _newTexture(): TexMap {
43+
this.refreshIfPending();
4244
const el = document.createElement("canvas");
4345
el.width = this._w;
4446
el.height = this._h;
@@ -51,6 +53,12 @@ export class TexPacker {
5153
this._texToEntry.set(tex, this._curMap = { tex, el, ctx });
5254
return this._curMap;
5355
}
56+
refreshIfPending() {
57+
if (this._hasPendingRefresh) {
58+
this._curMap.tex.update(this._curMap.el);
59+
this._hasPendingRefresh = false;
60+
}
61+
}
5462

5563
// create a image with a single texture
5664
addSingle(img: ImageSource): Frame {
@@ -186,7 +194,7 @@ export class TexPacker {
186194
imgHeight,
187195
);
188196

189-
curTex.update(curEl);
197+
this._hasPendingRefresh = true;
190198

191199
this._used.set(this._last, {
192200
rect: rectToAdd,

src/gfx/formatText.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { BitmapFontData, GfxFont } from "../assets/bitmapFont";
33
import { FontData, resolveFont } from "../assets/font";
44
import { DEF_TEXT_CACHE_SIZE } from "../constants/general";
55
import { Color } from "../math/color";
6-
import { Quad, vec2 } from "../math/math";
6+
import { vec2 } from "../math/math";
77
import { _k } from "../shared";
88
import type { Outline } from "../types";
99
import { runes } from "../utils/runes";
@@ -451,6 +451,8 @@ export function formatText(opt: DrawTextOpt): FormattedText {
451451
th += thisLineHeight;
452452
}
453453

454+
_k.assets.packer.refreshIfPending();
455+
454456
return {
455457
width: tw,
456458
height: th,

0 commit comments

Comments
 (0)