improve texture loading
This commit is contained in:
@@ -110,11 +110,7 @@ window.Gui = class {
|
|||||||
stack.restore();
|
stack.restore();
|
||||||
}
|
}
|
||||||
|
|
||||||
static loadTexture(path, callback) {
|
static loadTexture(path) {
|
||||||
let img = new Image();
|
return document.textures[path];
|
||||||
img.src = "src/resources/" + path;
|
|
||||||
img.onload = callback;
|
|
||||||
return img;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -8,15 +8,14 @@ window.FontRenderer = class {
|
|||||||
constructor() {
|
constructor() {
|
||||||
this.charWidths = [];
|
this.charWidths = [];
|
||||||
|
|
||||||
let scope = this;
|
this.texture = Gui.loadTexture("font.png")
|
||||||
this.texture = Gui.loadTexture("font.png", function () {
|
|
||||||
let bitMap = scope.createBitMap(scope.texture);
|
let bitMap = this.createBitMap(this.texture);
|
||||||
|
|
||||||
// Calculate character width
|
// Calculate character width
|
||||||
for (let i = 0; i < 128; i++) {
|
for (let i = 0; i < 128; i++) {
|
||||||
scope.charWidths[i] = scope.calculateCharacterWidthAt(bitMap, i % FontRenderer.BITMAP_SIZE, Math.floor(i / FontRenderer.BITMAP_SIZE)) + 2;
|
this.charWidths[i] = this.calculateCharacterWidthAt(bitMap, i % FontRenderer.BITMAP_SIZE, Math.floor(i / FontRenderer.BITMAP_SIZE)) + 2;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
calculateCharacterWidthAt(bitMap, indexX, indexY) {
|
calculateCharacterWidthAt(bitMap, indexX, indexY) {
|
||||||
|
|||||||
@@ -37,10 +37,44 @@ function loadScripts(scripts) {
|
|||||||
}, Promise.resolve());
|
}, Promise.resolve());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Texture loader
|
||||||
|
function loadTexture(textures) {
|
||||||
|
let total = textures.length;
|
||||||
|
let index = 0;
|
||||||
|
|
||||||
|
document.textures = [];
|
||||||
|
|
||||||
|
return textures.reduce((currentPromise, texturePath) => {
|
||||||
|
return currentPromise.then(() => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
// Update status message
|
||||||
|
updatePreStatus("Loading texture... " + index + "/" + total);
|
||||||
|
|
||||||
|
// Load texture
|
||||||
|
let image = new Image();
|
||||||
|
image.src = "src/resources/" + texturePath;
|
||||||
|
image.onload = () => resolve();
|
||||||
|
document.textures[texturePath] = image;
|
||||||
|
|
||||||
|
index++;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}, Promise.resolve());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
function updatePreStatus(message) {
|
function updatePreStatus(message) {
|
||||||
document.getElementById("pre-status").innerText = message;
|
document.getElementById("pre-status").innerText = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load textures
|
||||||
|
loadTexture([
|
||||||
|
"font.png",
|
||||||
|
"gui.png",
|
||||||
|
"background.png",
|
||||||
|
"terrain.png",
|
||||||
|
"icons.png"
|
||||||
|
]).then(() => {
|
||||||
// Load scripts
|
// Load scripts
|
||||||
loadScripts([
|
loadScripts([
|
||||||
// Dependencies
|
// Dependencies
|
||||||
@@ -105,3 +139,4 @@ loadScripts([
|
|||||||
// Start Minecraft
|
// Start Minecraft
|
||||||
window.app = new Minecraft("canvas-container");
|
window.app = new Minecraft("canvas-container");
|
||||||
});
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user