Skip to content

Commit

Permalink
feat: added option for sprites to be imported as a separate texture (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
chqs-git authored Jan 15, 2025
1 parent 074370f commit 3ad6bbe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/assets/sprite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ export interface LoadSpriteOpt {
* Animation configuration.
*/
anims?: SpriteAnims;
/**
* If the sprite is a single image.
*/
singular?: boolean;
}

export type NineSlice = {
Expand Down Expand Up @@ -135,7 +139,7 @@ export class SpriteData {
data: ImageSource,
opt: LoadSpriteOpt = {},
): SpriteData {
const [tex, quad, packerId] = _k.assets.packer.add(data);
const [tex, quad, packerId] = opt.singular ? _k.assets.packer.add_single(data) : _k.assets.packer.add(data);
const frames = opt.frames
? opt.frames.map((f) =>
new Quad(
Expand Down
11 changes: 8 additions & 3 deletions src/gfx/classes/TexPacker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,21 @@ export default class TexPacker {
this.c2d = context2D;
}

// create a image with a single texture
add_single(img: ImageSource): [Texture, Quad, number] {
const tex = Texture.fromImage(this.gfx, img);
this.bigTextures.push(tex);
return [tex, new Quad(0, 0, 1, 1), 0];
}

add(img: ImageSource): [Texture, Quad, number] {
const paddedWidth = img.width + this.padding * 2;
const paddedHeight = img.height + this.padding * 2;

if (
paddedWidth > this.canvas.width || paddedHeight > this.canvas.height
) {
const tex = Texture.fromImage(this.gfx, img);
this.bigTextures.push(tex);
return [tex, new Quad(0, 0, 1, 1), 0];
this.add_single(img);
}

// next row
Expand Down

0 comments on commit 3ad6bbe

Please sign in to comment.