Skip to content

Commit

Permalink
Use individual tile drawing until building full parallax...
Browse files Browse the repository at this point in the history
...optimized
  • Loading branch information
AntumDeluge committed Jun 1, 2024
1 parent 0de470e commit eb9278e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/js/stendhal/landscape/ParallaxBackground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export class ParallaxBackground {
/** Tiled image to be drawn. */
private image?: HTMLImageElement;

private width = 0;
private height = 0;

private debugging = false;

/** Singleton instance. */
private static instance: ParallaxBackground;

Expand Down Expand Up @@ -54,6 +59,15 @@ export class ParallaxBackground {
* Map pixel height.
*/
setImage(name: string, width: number, height: number) {
if (!this.debugging) {
this.width = width;
this.height = height;
this.image = singletons.getSpriteStore().get(singletons.getPaths().parallax + "/" + name
+ ".png");
return;
}

// NOTE: map loading time is affected using this method
//this.image = singletons.getTileStore().getParallax(name, ParallaxBackground.SCROLL, width, height);
singletons.getTileStore().getParallaxPromise(name, ParallaxBackground.SCROLL, width, height)
.then(image => {
Expand All @@ -74,6 +88,19 @@ export class ParallaxBackground {
if (!this.image || !this.image.height) {
return;
}

if (!this.debugging) {
// NOTE: seams are visible when walking using this method
let dy = offsetY - ((offsetY * ParallaxBackground.SCROLL) % this.image.height);
for (dy; dy < this.height; dy += this.image.height) {
let dx = offsetX - ((offsetX * ParallaxBackground.SCROLL) % this.image.width);
for (dx; dx < this.width; dx += this.image.width) {
ctx.drawImage(this.image, dx, dy);
}
}
return;
}

const tileLeft = offsetX - (offsetX * ParallaxBackground.SCROLL);
const tileTop = offsetY - (offsetY * ParallaxBackground.SCROLL);
ctx.drawImage(this.image, tileLeft, tileTop);
Expand Down

0 comments on commit eb9278e

Please sign in to comment.