add loadSprite

This commit is contained in:
Reimar 2025-10-15 13:21:02 +02:00
parent 74332ca1a0
commit 834afce5fc
2 changed files with 24 additions and 35 deletions

View File

@ -132,57 +132,42 @@ export class Gamelib {
ev.preventDefault(); ev.preventDefault();
} }
#loadSprite(width, height, name) { loadSprite(name, width, height) {
const spriteId = `${name}_${width}x${height}`; return new Promise((resolve) => {
if (this.spriteCache.has(spriteId)) {
return this.spriteCache.get(spriteId);
}
// start by caching an empty canvas
const sprite = new OffscreenCanvas(width, height); const sprite = new OffscreenCanvas(width, height);
this.spriteCache.set(spriteId, sprite);
const image = new Image(); const image = new Image();
image.src = this.assetProvider.url(name); image.src = this.assetProvider.url(name);
image.onload = () => { image.onload = () => {
const sprite = this.spriteCache.get(spriteId);
const spriteCx = sprite.getContext("2d"); const spriteCx = sprite.getContext("2d");
spriteCx.imageSmoothingEnabled = false; spriteCx.imageSmoothingEnabled = false;
spriteCx.drawImage(image, 0, 0, width, height); spriteCx.drawImage(image, 0, 0, width, height);
const invalidatedKeys = this.spriteCache.keys() resolve(sprite);
.filter((key) => key.startsWith(`${spriteId}r`));
for (const key of invalidatedKeys) {
this.spriteCache.delete(key);
}
}; };
});
return sprite;
} }
drawSprite(x, y, width, height, name) { drawSprite(x, y, sprite) {
const cx = this.cx; const cx = this.cx;
const sprite = this.#loadSprite(width, height, name);
cx.drawImage(sprite, x, y); cx.drawImage(sprite, x, y);
} }
drawSpriteRotated(x, y, width, height, name, angle) { drawSpriteRotated(x, y, sprite, angle) {
const cx = this.cx; const cx = this.cx;
const angleIncrement = Math.PI * 2 / 360; const angleIncrement = Math.PI * 2 / 360;
const angleNormalized = Math.floor((angle % (Math.PI * 2)) / angleIncrement) * const angleNormalized = Math.floor((angle % (Math.PI * 2)) / angleIncrement) *
angleIncrement; angleIncrement;
const rotatedSpriteId = `${name}_${width}x${height}r${angleNormalized}`; const rotatedSpriteId = `${sprite.url}_${sprite.width}x${sprite.height}r${angleNormalized}`;
if (this.spriteCache.has(rotatedSpriteId)) { if (this.spriteCache.has(rotatedSpriteId)) {
const sprite = this.spriteCache.get(rotatedSpriteId); const sprite = this.spriteCache.get(rotatedSpriteId);
cx.drawImage(sprite, x, y); cx.drawImage(sprite, x, y);
return; return;
} }
const sprite = this.#loadSprite(width, height, name);
const maxSize = Math.max(sprite.width, sprite.height); const maxSize = Math.max(sprite.width, sprite.height);
const newSprite = new OffscreenCanvas(maxSize, maxSize); const newSprite = new OffscreenCanvas(maxSize, maxSize);
const newSpriteCx = newSprite.getContext("2d"); const newSpriteCx = newSprite.getContext("2d");
@ -354,12 +339,16 @@ export class GamelibAdapter {
this.gamelib.mouseUpHandlers.set(button, handlerFunction); this.gamelib.mouseUpHandlers.set(button, handlerFunction);
} }
drawSprite(x, y, width, height, name) { loadSprite(name, width, height) {
this.gamelib.drawSprite(x, y, width, height, name); return this.gamelib.loadSprite(name, width, height);
} }
drawSpriteRotated(x, y, width, height, name, angle) { drawSprite(x, y, sprite) {
this.gamelib.drawSpriteRotated(x, y, width, height, name, angle); this.gamelib.drawSprite(x, y, sprite);
}
drawSpriteRotated(x, y, sprite, angle) {
this.gamelib.drawSpriteRotated(x, y, sprite, angle);
} }
rgb(red, green, blue) { rgb(red, green, blue) {

View File

@ -62,7 +62,7 @@ export class ProjectSaveHandler {
const code = items.find((x) => x.tag === "code"); const code = items.find((x) => x.tag === "code");
delete code.tag; delete code.tag;
this.assetEditor.importAssets( await this.assetEditor.importAssets(
assets.map(({ name, mime, content }) => { assets.map(({ name, mime, content }) => {
const file = new File([content], name, { type: mime }); const file = new File([content], name, { type: mime });