Skip to content

Commit

Permalink
Fix setLegend perf issue (another regex hotpath)
Browse files Browse the repository at this point in the history
  • Loading branch information
kognise committed Oct 7, 2022
1 parent fb31d01 commit 2427dfb
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
5 changes: 3 additions & 2 deletions engine/bitmap.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ export function bitmapTextToImageData(string) {

const colors = Object.fromEntries(palette);

const nonSpace = string.split("").filter(x => x !== " " && x !== "\n"); // \S regex was too slow
for (let i = 0; i < width*height; i++) {
const type = string.split("").filter(x => x.match(/\S/))[i] || ".";

const type = nonSpace[i] || ".";
if (!(type in colors)) {
const err = `in sprite string: no known color for char "${type}"`;
console.error(err + '\n' + string);
Expand Down
5 changes: 4 additions & 1 deletion engine/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { palette } from "../palette.js";
const spritesheetIndex = 0;
const texIndex = 1;
let backgroundTile = 0;
let backgroundKey = ".";
let gl, texLocation,
texResLocation,
spritesheetLocation,
Expand All @@ -14,7 +15,8 @@ const SPRITESHEET_TILE_COUNT = 64;

let _bitmaps;
export function setBackground(bg) {
backgroundTile = 1+_bitmaps.findIndex(f => f[0] == bg);
backgroundKey = bg;
backgroundTile = 1+_bitmaps.findIndex(f => f[0] == bg) || 0;
}

export function setBitmaps(bitmaps) {
Expand All @@ -36,6 +38,7 @@ export function setBitmaps(bitmaps) {
}
}
uploadImage(spritesheet, spritesheetIndex);
setBackground(backgroundKey);
}

export function init(canvas) {
Expand Down

0 comments on commit 2427dfb

Please sign in to comment.