Skip to content

Commit

Permalink
Remove RGBA.brightness()
Browse files Browse the repository at this point in the history
It is creating new instance of RGBA on each call which is really bad
for the JavaScript's GC
  • Loading branch information
rexim committed Jul 2, 2024
1 parent e5ed57d commit 4e3c26e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
27 changes: 12 additions & 15 deletions game.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 13 additions & 15 deletions game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ export class RGBA {
static cyan(): RGBA {
return new RGBA(0, 1, 1, 1);
}
brightness(factor: number): RGBA {
return new RGBA(factor*this.r, factor*this.g, factor*this.b, this.a);
}
toStyle(): string {
return `rgba(`
+`${Math.floor(this.r*255)}, `
Expand Down Expand Up @@ -392,13 +389,14 @@ function renderWallsToImageData(imageData: ImageData, player: Player, scene: Sce
const v = p.clone().sub(player.position);
const d = Vector2.angle(player.direction)
const stripHeight = imageData.height/v.dot(d);
const color = cell.brightness(v.dot(d));

const shadow = 1/v.dot(d)*2;
for (let dy = 0; dy < Math.ceil(stripHeight); ++dy) {
const y = Math.floor((imageData.height - stripHeight)*0.5) + dy;
const destP = (y*imageData.width + x)*4;
imageData.data[destP + 0] = color.r*255;
imageData.data[destP + 1] = color.g*255;
imageData.data[destP + 2] = color.b*255;
imageData.data[destP + 0] = cell.r*shadow*255;
imageData.data[destP + 1] = cell.g*shadow*255;
imageData.data[destP + 2] = cell.b*shadow*255;
}
} else if (cell instanceof ImageData) {
const v = p.clone().sub(player.position);
Expand Down Expand Up @@ -448,11 +446,11 @@ function renderCeilingIntoImageData(imageData: ImageData, player: Player) {
const t = t1.clone().lerp(t2, x/imageData.width);
const tile = sceneGetCeiling(t);
if (tile instanceof RGBA) {
const color = tile.brightness(Math.sqrt(player.position.sqrDistanceTo(t)));
const shadow = Math.sqrt(player.position.sqrDistanceTo(t));
const destP = (sz*imageData.width + x)*4;
imageData.data[destP + 0] = color.r*255;
imageData.data[destP + 1] = color.g*255;
imageData.data[destP + 2] = color.b*255;
imageData.data[destP + 0] = tile.r*shadow*255;
imageData.data[destP + 1] = tile.g*shadow*255;
imageData.data[destP + 2] = tile.b*shadow*255;
}
}
}
Expand All @@ -476,11 +474,11 @@ function renderFloorIntoImageData(imageData: ImageData, player: Player) {
const t = t1.clone().lerp(t2, x/imageData.width);
const tile = sceneGetFloor(t);
if (tile instanceof RGBA) {
const color = tile.brightness(Math.sqrt(player.position.sqrDistanceTo(t)));
const shadow = Math.sqrt(player.position.sqrDistanceTo(t));
const destP = (y*imageData.width + x)*4;
imageData.data[destP + 0] = color.r*255;
imageData.data[destP + 1] = color.g*255;
imageData.data[destP + 2] = color.b*255;
imageData.data[destP + 0] = tile.r*shadow*255;
imageData.data[destP + 1] = tile.g*shadow*255;
imageData.data[destP + 2] = tile.b*shadow*255;
}
}
}
Expand Down

0 comments on commit 4e3c26e

Please sign in to comment.