1
0
mirror of https://github.com/tobspr/shapez.io.git synced 2024-10-27 20:34:29 +00:00

Improve color rendering performance

This commit is contained in:
tobspr 2020-09-22 10:50:36 +02:00
parent 0274597a1c
commit 5f2cf4e589
16 changed files with 846 additions and 681 deletions

File diff suppressed because it is too large Load Diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.3 MiB

After

Width:  |  Height:  |  Size: 1.3 MiB

File diff suppressed because it is too large Load Diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 279 KiB

After

Width:  |  Height:  |  Size: 279 KiB

File diff suppressed because it is too large Load Diff

Binary file not shown.

Before

Width:  |  Height:  |  Size: 705 KiB

After

Width:  |  Height:  |  Size: 697 KiB

View File

@ -197,7 +197,7 @@
<key>scaleMode</key> <key>scaleMode</key>
<enum type="ScaleMode">Smooth</enum> <enum type="ScaleMode">Smooth</enum>
<key>extrude</key> <key>extrude</key>
<uint>4</uint> <uint>3</uint>
<key>trimThreshold</key> <key>trimThreshold</key>
<uint>2</uint> <uint>2</uint>
<key>trimMargin</key> <key>trimMargin</key>
@ -507,6 +507,28 @@
<key>scale9FromFile</key> <key>scale9FromFile</key>
<false/> <false/>
</struct> </struct>
<key type="filename">sprites/colors/blue.png</key>
<key type="filename">sprites/colors/cyan.png</key>
<key type="filename">sprites/colors/green.png</key>
<key type="filename">sprites/colors/purple.png</key>
<key type="filename">sprites/colors/red.png</key>
<key type="filename">sprites/colors/uncolored.png</key>
<key type="filename">sprites/colors/white.png</key>
<key type="filename">sprites/colors/yellow.png</key>
<struct type="IndividualSpriteSettings">
<key>pivotPoint</key>
<point_f>0.5,0.5</point_f>
<key>spriteScale</key>
<double>1</double>
<key>scale9Enabled</key>
<false/>
<key>scale9Borders</key>
<rect>18,18,36,36</rect>
<key>scale9Paddings</key>
<rect>18,18,36,36</rect>
<key>scale9FromFile</key>
<false/>
</struct>
<key type="filename">sprites/debug/acceptor_slot.png</key> <key type="filename">sprites/debug/acceptor_slot.png</key>
<key type="filename">sprites/debug/ejector_slot.png</key> <key type="filename">sprites/debug/ejector_slot.png</key>
<key type="filename">sprites/misc/hub_direction_indicator.png</key> <key type="filename">sprites/misc/hub_direction_indicator.png</key>

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

View File

@ -1,122 +1,73 @@
import { globalConfig } from "../../core/config"; import { globalConfig } from "../../core/config";
import { smoothenDpi } from "../../core/dpi_manager"; import { DrawParameters } from "../../core/draw_parameters";
import { DrawParameters } from "../../core/draw_parameters"; import { Loader } from "../../core/loader";
import { types } from "../../savegame/serialization"; import { types } from "../../savegame/serialization";
import { BaseItem } from "../base_item"; import { BaseItem } from "../base_item";
import { enumColors, enumColorsToHexCode } from "../colors"; import { enumColors } from "../colors";
import { THEME } from "../theme"; import { THEME } from "../theme";
import { drawSpriteClipped } from "../../core/draw_utils";
export class ColorItem extends BaseItem {
export class ColorItem extends BaseItem { static getId() {
static getId() { return "color";
return "color"; }
}
static getSchema() {
static getSchema() { return types.enum(enumColors);
return types.enum(enumColors); }
}
serialize() {
serialize() { return this.color;
return this.color; }
}
deserialize(data) {
deserialize(data) { this.color = data;
this.color = data; }
}
/** @returns {"color"} **/
/** @returns {"color"} **/ getItemType() {
getItemType() { return "color";
return "color"; }
}
/**
/** * @param {BaseItem} other
* @param {BaseItem} other */
*/ equalsImpl(other) {
equalsImpl(other) { return this.color === /** @type {ColorItem} */ (other).color;
return this.color === /** @type {ColorItem} */ (other).color; }
}
/**
/** * @param {enumColors} color
* @param {enumColors} color */
*/ constructor(color) {
constructor(color) { super();
super(); this.color = color;
this.color = color; }
this.bufferGenerator = null;
} getBackgroundColorAsResource() {
return THEME.map.resources[this.color];
getBackgroundColorAsResource() { }
return THEME.map.resources[this.color];
} /**
* @param {number} x
/** * @param {number} y
* @param {number} x * @param {number} diameter
* @param {number} y * @param {DrawParameters} parameters
* @param {number} diameter */
* @param {DrawParameters} parameters drawItemCenteredClipped(x, y, parameters, diameter = globalConfig.defaultItemDiameter) {
*/ const realDiameter = diameter * 0.6;
drawItemCenteredImpl(x, y, parameters, diameter = globalConfig.defaultItemDiameter) { if (!this.cachedSprite) {
if (!this.bufferGenerator) { this.cachedSprite = Loader.getSprite("sprites/colors/" + this.color + ".png");
this.bufferGenerator = this.internalGenerateColorBuffer.bind(this); }
} this.cachedSprite.drawCachedCentered(parameters, x, y, realDiameter);
}
const realDiameter = diameter * 0.6; }
const dpi = smoothenDpi(globalConfig.shapesSharpness * parameters.zoomLevel);
const key = realDiameter + "/" + dpi + "/" + this.color; /**
const canvas = parameters.root.buffers.getForKey({ * Singleton instances
key: "coloritem", * @type {Object<enumColors, ColorItem>}
subKey: key, */
w: realDiameter, export const COLOR_ITEM_SINGLETONS = {};
h: realDiameter,
dpi, for (const color in enumColors) {
redrawMethod: this.bufferGenerator, COLOR_ITEM_SINGLETONS[color] = new ColorItem(color);
}); }
drawSpriteClipped({
parameters,
sprite: canvas,
x: x - realDiameter / 2,
y: y - realDiameter / 2,
w: realDiameter,
h: realDiameter,
originalW: realDiameter * dpi,
originalH: realDiameter * dpi,
});
}
/**
*
* @param {HTMLCanvasElement} canvas
* @param {CanvasRenderingContext2D} context
* @param {number} w
* @param {number} h
* @param {number} dpi
*/
internalGenerateColorBuffer(canvas, context, w, h, dpi) {
context.translate((w * dpi) / 2, (h * dpi) / 2);
context.scale((dpi * w) / 12, (dpi * h) / 12);
context.fillStyle = enumColorsToHexCode[this.color];
context.strokeStyle = THEME.items.outline;
context.lineWidth = 2 * THEME.items.outlineWidth;
context.beginCircle(2, -1, 3);
context.stroke();
context.fill();
context.beginCircle(-2, -1, 3);
context.stroke();
context.fill();
context.beginCircle(0, 2, 3);
context.closePath();
context.stroke();
context.fill();
}
}
/**
* Singleton instances
* @type {Object<enumColors, ColorItem>}
*/
export const COLOR_ITEM_SINGLETONS = {};
for (const color in enumColors) {
COLOR_ITEM_SINGLETONS[color] = new ColorItem(color);
}