improve texture loading
This commit is contained in:
@@ -110,11 +110,7 @@ window.Gui = class {
|
||||
stack.restore();
|
||||
}
|
||||
|
||||
static loadTexture(path, callback) {
|
||||
let img = new Image();
|
||||
img.src = "src/resources/" + path;
|
||||
img.onload = callback;
|
||||
return img;
|
||||
static loadTexture(path) {
|
||||
return document.textures[path];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -8,15 +8,14 @@ window.FontRenderer = class {
|
||||
constructor() {
|
||||
this.charWidths = [];
|
||||
|
||||
let scope = this;
|
||||
this.texture = Gui.loadTexture("font.png", function () {
|
||||
let bitMap = scope.createBitMap(scope.texture);
|
||||
this.texture = Gui.loadTexture("font.png")
|
||||
|
||||
let bitMap = this.createBitMap(this.texture);
|
||||
|
||||
// Calculate character width
|
||||
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) {
|
||||
|
||||
@@ -37,10 +37,44 @@ function loadScripts(scripts) {
|
||||
}, 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) {
|
||||
document.getElementById("pre-status").innerText = message;
|
||||
}
|
||||
|
||||
// Load textures
|
||||
loadTexture([
|
||||
"font.png",
|
||||
"gui.png",
|
||||
"background.png",
|
||||
"terrain.png",
|
||||
"icons.png"
|
||||
]).then(() => {
|
||||
// Load scripts
|
||||
loadScripts([
|
||||
// Dependencies
|
||||
@@ -105,3 +139,4 @@ loadScripts([
|
||||
// Start Minecraft
|
||||
window.app = new Minecraft("canvas-container");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user