improve texture loading

This commit is contained in:
LabyStudio
2022-02-12 22:17:15 +01:00
parent d77a7dae0c
commit bb2d830c84
3 changed files with 105 additions and 75 deletions
+2 -6
View File
@@ -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")
// 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;
}
});
let bitMap = this.createBitMap(this.texture);
// Calculate character width
for (let i = 0; i < 128; i++) {
this.charWidths[i] = this.calculateCharacterWidthAt(bitMap, i % FontRenderer.BITMAP_SIZE, Math.floor(i / FontRenderer.BITMAP_SIZE)) + 2;
}
}
calculateCharacterWidthAt(bitMap, indexX, indexY) {
+96 -61
View File
@@ -37,71 +37,106 @@ 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 scripts
loadScripts([
// Dependencies
"libraries/three.min.js",
"libraries/stats.min.js",
// Minecraft Source
"src/js/net/minecraft/util/EnumBlockFace.js",
"src/js/net/minecraft/util/Timer.js",
"src/js/net/minecraft/util/Random.js",
"src/js/net/minecraft/util/EnumBlockFace.js",
"src/js/net/minecraft/util/EnumSkyBlock.js",
"src/js/net/minecraft/util/MetadataChunkBlock.js",
"src/js/net/minecraft/util/Vector3.js",
"src/js/net/minecraft/util/MovingObjectPosition.js",
"src/js/net/minecraft/util/MathHelper.js",
"src/js/net/minecraft/util/BoundingBox.js",
"src/js/net/minecraft/util/Keyboard.js",
"src/js/net/minecraft/client/gui/Gui.js",
"src/js/net/minecraft/client/gui/GuiScreen.js",
"src/js/net/minecraft/client/gui/widgets/GuiButton.js",
"src/js/net/minecraft/client/gui/widgets/GuiKeyButton.js",
"src/js/net/minecraft/client/gui/IngameOverlay.js",
"src/js/net/minecraft/client/gui/screens/GuiLoadingScreen.js",
"src/js/net/minecraft/client/gui/screens/GuiControls.js",
"src/js/net/minecraft/client/gui/screens/GuiIngameMenu.js",
"src/js/net/minecraft/client/GameWindow.js",
"src/js/net/minecraft/client/world/block/Block.js",
"src/js/net/minecraft/client/world/block/BlockStone.js",
"src/js/net/minecraft/client/world/block/BlockGrass.js",
"src/js/net/minecraft/client/world/block/BlockDirt.js",
"src/js/net/minecraft/client/world/block/BlockLog.js",
"src/js/net/minecraft/client/world/block/BlockLeave.js",
"src/js/net/minecraft/client/world/block/BlockWater.js",
"src/js/net/minecraft/client/world/block/BlockSand.js",
"src/js/net/minecraft/client/world/ChunkSection.js",
"src/js/net/minecraft/client/world/Chunk.js",
"src/js/net/minecraft/client/world/World.js",
"src/js/net/minecraft/client/world/generator/NoiseGenerator.js",
"src/js/net/minecraft/client/world/generator/noise/NoiseGeneratorPerlin.js",
"src/js/net/minecraft/client/world/generator/noise/NoiseGeneratorOctaves.js",
"src/js/net/minecraft/client/world/generator/noise/NoiseGeneratorCombined.js",
"src/js/net/minecraft/client/world/generator/WorldGenerator.js",
"src/js/net/minecraft/client/entity/Player.js",
"src/js/net/minecraft/client/inventory/Inventory.js",
"src/js/net/minecraft/client/GameSettings.js",
"src/js/net/minecraft/client/Minecraft.js",
"src/js/net/minecraft/client/render/isometric/IsometricRenderer.js",
"src/js/net/minecraft/client/render/isometric/Point.js",
"src/js/net/minecraft/client/render/isometric/TextCoord.js",
"src/js/net/minecraft/client/render/isometric/Triangle.js",
"src/js/net/minecraft/client/render/gui/FontRenderer.js",
"src/js/net/minecraft/client/render/gui/ScreenRenderer.js",
"src/js/net/minecraft/client/render/gui/ItemRenderer.js",
"src/js/net/minecraft/client/render/Tessellator.js",
"src/js/net/minecraft/client/render/WorldRenderer.js",
"src/js/net/minecraft/client/render/BlockRenderer.js"
// Load textures
loadTexture([
"font.png",
"gui.png",
"background.png",
"terrain.png",
"icons.png"
]).then(() => {
// Remove pre status
document.getElementById("pre-status").remove();
// Load scripts
loadScripts([
// Dependencies
"libraries/three.min.js",
"libraries/stats.min.js",
// Start Minecraft
window.app = new Minecraft("canvas-container");
// Minecraft Source
"src/js/net/minecraft/util/EnumBlockFace.js",
"src/js/net/minecraft/util/Timer.js",
"src/js/net/minecraft/util/Random.js",
"src/js/net/minecraft/util/EnumBlockFace.js",
"src/js/net/minecraft/util/EnumSkyBlock.js",
"src/js/net/minecraft/util/MetadataChunkBlock.js",
"src/js/net/minecraft/util/Vector3.js",
"src/js/net/minecraft/util/MovingObjectPosition.js",
"src/js/net/minecraft/util/MathHelper.js",
"src/js/net/minecraft/util/BoundingBox.js",
"src/js/net/minecraft/util/Keyboard.js",
"src/js/net/minecraft/client/gui/Gui.js",
"src/js/net/minecraft/client/gui/GuiScreen.js",
"src/js/net/minecraft/client/gui/widgets/GuiButton.js",
"src/js/net/minecraft/client/gui/widgets/GuiKeyButton.js",
"src/js/net/minecraft/client/gui/IngameOverlay.js",
"src/js/net/minecraft/client/gui/screens/GuiLoadingScreen.js",
"src/js/net/minecraft/client/gui/screens/GuiControls.js",
"src/js/net/minecraft/client/gui/screens/GuiIngameMenu.js",
"src/js/net/minecraft/client/GameWindow.js",
"src/js/net/minecraft/client/world/block/Block.js",
"src/js/net/minecraft/client/world/block/BlockStone.js",
"src/js/net/minecraft/client/world/block/BlockGrass.js",
"src/js/net/minecraft/client/world/block/BlockDirt.js",
"src/js/net/minecraft/client/world/block/BlockLog.js",
"src/js/net/minecraft/client/world/block/BlockLeave.js",
"src/js/net/minecraft/client/world/block/BlockWater.js",
"src/js/net/minecraft/client/world/block/BlockSand.js",
"src/js/net/minecraft/client/world/ChunkSection.js",
"src/js/net/minecraft/client/world/Chunk.js",
"src/js/net/minecraft/client/world/World.js",
"src/js/net/minecraft/client/world/generator/NoiseGenerator.js",
"src/js/net/minecraft/client/world/generator/noise/NoiseGeneratorPerlin.js",
"src/js/net/minecraft/client/world/generator/noise/NoiseGeneratorOctaves.js",
"src/js/net/minecraft/client/world/generator/noise/NoiseGeneratorCombined.js",
"src/js/net/minecraft/client/world/generator/WorldGenerator.js",
"src/js/net/minecraft/client/entity/Player.js",
"src/js/net/minecraft/client/inventory/Inventory.js",
"src/js/net/minecraft/client/GameSettings.js",
"src/js/net/minecraft/client/Minecraft.js",
"src/js/net/minecraft/client/render/isometric/IsometricRenderer.js",
"src/js/net/minecraft/client/render/isometric/Point.js",
"src/js/net/minecraft/client/render/isometric/TextCoord.js",
"src/js/net/minecraft/client/render/isometric/Triangle.js",
"src/js/net/minecraft/client/render/gui/FontRenderer.js",
"src/js/net/minecraft/client/render/gui/ScreenRenderer.js",
"src/js/net/minecraft/client/render/gui/ItemRenderer.js",
"src/js/net/minecraft/client/render/Tessellator.js",
"src/js/net/minecraft/client/render/WorldRenderer.js",
"src/js/net/minecraft/client/render/BlockRenderer.js"
]).then(() => {
// Remove pre status
document.getElementById("pre-status").remove();
// Start Minecraft
window.app = new Minecraft("canvas-container");
});
});