Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(web): improve rendering performance of maps #60

Merged
merged 2 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions packages/spelunker-web/src/components/core/GameMap/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'leaflet/dist/leaflet.css';

import { Box } from '../';

import { drawBlp, loadBlp } from "../../../utils/blp";
import styles from './index.styl';

const CHUNK_SIZE = 33.3333;
Expand All @@ -32,13 +33,33 @@ const loadTileIndex = async (tileDirectory) => {
TILE_INDEX[tileDirectory] = tileIndex;
};

const loadTile = async (tileUrl, tile, done) => {
const blp = await loadBlp(tileUrl);

if (!blp) {
done(new Error('invalid blp'), tile);
return;
}

drawBlp(blp, tile);
done(undefined, tile);
};

class MinimapTileLayer extends TileLayer {
createTile(coords, done) {
const tile = document.createElement('canvas');
const tileUrl = this.getTileUrl(coords);
loadTile(tileUrl, tile, done);

return tile;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looked a bit odd to me at first glance, but I assume we need the done callback as Leaflet does not support async here? Might warrant a comment here at a later point as loadTile will be a lost promise (but still draw to the tile).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, Leaflet appears to still prefer callback style async vs Promises. I'll see about cleaning this up in the next PR.

}

getTileUrl({ x, y }) {
const tx = 32 + x;
const ty = 32 + y;

// TODO use real url
const unknownTileUrl = `${process.env.PIPELINE_URI}/files/textures/minimap/unknown_${tx}_${ty}`;
const unknownTileUrl = `${process.env.DATA_URI}/textures/minimap/unknown_${tx}_${ty}.blp`;

const tileDirectory = this._url;
if (!tileDirectory) {
Expand All @@ -55,7 +76,7 @@ class MinimapTileLayer extends TileLayer {
return unknownTileUrl;
}

return `${process.env.PIPELINE_URI}/files/textures/minimap/${contentPath}.png`;
return `${process.env.DATA_URI}/textures/minimap/${contentPath}`;
}
}

Expand Down
32 changes: 13 additions & 19 deletions packages/spelunker-web/src/components/images/GameImage/index.jsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,22 @@
import React, { useEffect, useRef } from 'react';
import { Blp, BLP_IMAGE_FORMAT } from '@wowserhq/format';
import classNames from 'classnames';

import { loadBlp, drawBlp } from '../../../utils/blp';
import { toPipelinePath } from '../../../utils/pipeline';

import styles from './index.styl';

const loadBlp = async (canvas, src) => {
const blpResponse = await fetch(src);
const blpData = await blpResponse.arrayBuffer();
const blp = new Blp();
blp.load(new Uint8Array(blpData));

const image = blp.getImage(0, BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
const imageData = new ImageData(new Uint8ClampedArray(image.data), image.width, image.height);

canvas.width = image.width;
canvas.height = image.height;

const context = canvas.getContext('2d');
context.putImageData(imageData, 0, 0);
};

const GameImageBackground = (props) => {
const canvasRef = useRef();

useEffect(() => {
const load = async (src, canvas) => {
const blp = await loadBlp(src);
drawBlp(blp, canvas);
};

if (canvasRef.current) {
loadBlp(canvasRef.current, props.src);
load(props.src, canvasRef.current);
}
}, [props.src]);

Expand All @@ -50,8 +39,13 @@ const GameImageElement = (props) => {
const canvasRef = useRef();

useEffect(() => {
const load = async (src, canvas) => {
const blp = await loadBlp(src);
drawBlp(blp, canvas);
};

if (canvasRef.current) {
loadBlp(canvasRef.current, props.src);
load(props.src, canvasRef.current);
}
}, [props.src]);

Expand Down
27 changes: 27 additions & 0 deletions packages/spelunker-web/src/utils/blp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Blp, BLP_IMAGE_FORMAT } from "@wowserhq/format";

const drawBlp = (blp, canvas, x = 0, y = 0) => {
const image = blp.getImage(0, BLP_IMAGE_FORMAT.IMAGE_RGBA8888);
const imageData = new ImageData(new Uint8ClampedArray(image.data), image.width, image.height);

canvas.width = image.width;
canvas.height = image.height;

const context = canvas.getContext('2d');
context.putImageData(imageData, x, y);
};

const loadBlp = async (src) => {
const blpResponse = await fetch(src);
if (!blpResponse.ok) {
return null;
}

const blpData = await blpResponse.arrayBuffer();
const blp = new Blp();
blp.load(new Uint8Array(blpData));

return blp;
};

export { loadBlp, drawBlp };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

Loading