migalib
Version:
MIGAlib - MInimal GAme LIBrary
107 lines (106 loc) • 4.94 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Renderer = void 0;
var rendererfactory_1 = require("./rendererfactory");
var texturemanager_1 = require("./texturemanager");
var splashscreen_1 = require("./splashscreen");
var RendererSingleton = /** @class */ (function () {
function RendererSingleton() {
}
RendererSingleton.prototype.init = function (displayContainer, renderResolution, fitToScreen, renderPixelated, textureBleedingFix) {
var _this = this;
if (textureBleedingFix === void 0) { textureBleedingFix = 0; }
var canvas = window.document.createElement("canvas");
canvas.id = RendererSingleton.MIGALIB_DISPLAY_ID;
if (renderResolution) {
canvas.width = renderResolution.width;
canvas.height = renderResolution.height;
}
else {
canvas.width = 384;
canvas.height = 216;
}
this.displayCanvas = canvas;
this.displayWidth = canvas.width;
this.displayHeight = canvas.height;
var domNodeToAttachTo = displayContainer || window.document.body;
domNodeToAttachTo.appendChild(canvas);
this.addCanvasStyles(fitToScreen, renderPixelated);
if (fitToScreen) {
this.resizeCanvas();
window.document.body.onresize = function () { return _this.resizeCanvas(); };
}
this.webGLRenderer = rendererfactory_1.RendererFactory.createWebGLRenderer(canvas, textureBleedingFix);
};
RendererSingleton.prototype.addCanvasStyles = function (fitToScreen, renderPixelated) {
var styleTag = document.createElement("style");
styleTag.innerHTML = "\n " + (renderPixelated
? "#" + RendererSingleton.MIGALIB_DISPLAY_ID + " {\n image-rendering: -moz-crisp-edges;\n \t\t image-rendering: -webkit-crisp-edges;\n \t\t image-rendering: pixelated;\n\t\t\t\timage-rendering: crisp-edges;\n }"
: "") + "\n\n " + (fitToScreen
? "\n html, body {\n\t\t\t padding: 0;\n\t\t\t margin: 0;\n\t\t\t height: 100%;\n\t\t\t width: 100%;\n\t\t }\n\n\t\t body {\n\t\t\t display: flex;\n\t\t\t justify-content: center;\n\t\t\t align-items: center;\n\t\t }\n \n "
: "") + "\n ";
document.body.appendChild(styleTag);
};
RendererSingleton.prototype.resizeCanvas = function () {
var canvas = document.querySelector("#" + RendererSingleton.MIGALIB_DISPLAY_ID);
var ratio = Math.min(window.innerWidth / canvas.width, window.innerHeight / canvas.height);
var result = {
width: canvas.width * ratio,
height: canvas.height * ratio,
};
canvas.style.width = result.width + "px";
canvas.style.height = result.height + "px";
splashscreen_1.SplashScreen.resizeSplashScreen();
};
RendererSingleton.prototype.loadTexture = function (name, imageSrc, scale) {
return texturemanager_1.TextureManager.loadTexture(name, imageSrc, this.webGLRenderer.g, scale);
};
RendererSingleton.prototype.drawBegin = function () {
this.webGLRenderer.cls();
};
RendererSingleton.prototype.setBackgroundColor = function (r, g, b) {
this.webGLRenderer.bkg(r / 255, g / 255, b / 255);
};
RendererSingleton.prototype.drawEnd = function () {
this.webGLRenderer.flush();
};
RendererSingleton.prototype.setTintColor = function (tintColor) {
this.webGLRenderer.col = tintColor;
};
RendererSingleton.prototype.draw = function (textureRegion, x, y, options) {
if (options === void 0) { options = {}; }
this.webGLRenderer.img(
// Texture
textureRegion.texture,
// Position X
options.originX !== undefined
? options.originX
: -textureRegion.halfWidth,
// Position Y
options.originY !== undefined
? options.originY
: -textureRegion.halfHeight,
// 0,
// Width
options.width !== undefined ? options.width : textureRegion.width,
// Height
options.height !== undefined ? options.height : textureRegion.height,
// Rotation
options.rotation !== undefined ? options.rotation : 0,
// Translation X
x,
// Translation Y
y,
// Scale X
options.scaleX !== undefined ? options.scaleX : 1,
// Scale Y
options.scaleY !== undefined ? options.scaleY : 1,
// UV0
textureRegion.u0, textureRegion.v0,
// UV1
textureRegion.u1, textureRegion.v1);
};
RendererSingleton.MIGALIB_DISPLAY_ID = "migalib-display";
return RendererSingleton;
}());
exports.Renderer = new RendererSingleton();