From faa85491a40fea54d5b67a7da46e091220951bc0 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Tue, 3 Aug 2021 21:32:39 +0200 Subject: [PATCH] font loading --- .../minosoft/gui/rendering/RenderWindow.kt | 5 + .../minosoft/gui/rendering/font/CharData.kt | 30 ++++++ .../minosoft/gui/rendering/font/Font.kt | 33 +++++++ .../minosoft/gui/rendering/font/FontLoader.kt | 49 ++++++++++ .../font/provider/BitmapFontProvider.kt | 84 ++++++++++++++++ .../rendering/font/provider/FontProvider.kt | 19 ++++ .../font/provider/FontProviderFactory.kt | 22 +++++ .../provider/LegacyUnicodeFontProvider.kt | 98 +++++++++++++++++++ .../base/texture/texture/AbstractTexture.kt | 1 + .../base/texture/texture/MemoryTexture.kt | 1 + .../system/base/texture/texture/PNGTexture.kt | 1 + .../base/texture/texture/SpriteTexture.kt | 1 + .../opengl/texture/OpenGLTextureArray.kt | 2 + .../gui/rendering/textures/TextureUtil.kt | 35 +++++++ .../java/de/bixilon/minosoft/util/KUtil.kt | 9 +- .../bixilon/minosoft/util/nbt/tag/NBTUtil.kt | 2 +- 16 files changed, 390 insertions(+), 2 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/font/CharData.kt create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/font/Font.kt create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/font/FontLoader.kt create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/BitmapFontProvider.kt create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/FontProvider.kt create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/FontProviderFactory.kt create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/LegacyUnicodeFontProvider.kt create mode 100644 src/main/java/de/bixilon/minosoft/gui/rendering/textures/TextureUtil.kt diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt index fd7e6b75d..e912e751f 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/RenderWindow.kt @@ -20,6 +20,8 @@ import de.bixilon.minosoft.gui.rendering.block.WorldRenderer import de.bixilon.minosoft.gui.rendering.block.chunk.ChunkBorderRenderer import de.bixilon.minosoft.gui.rendering.block.outline.BlockOutlineRenderer import de.bixilon.minosoft.gui.rendering.entity.EntityHitBoxRenderer +import de.bixilon.minosoft.gui.rendering.font.Font +import de.bixilon.minosoft.gui.rendering.font.FontLoader import de.bixilon.minosoft.gui.rendering.hud.atlas.TextureLike import de.bixilon.minosoft.gui.rendering.hud.atlas.TextureLikeTexture import de.bixilon.minosoft.gui.rendering.input.key.RenderWindowInputHandler @@ -72,6 +74,7 @@ class RenderWindow( private val screenshotTaker = ScreenshotTaker(this) val tintColorCalculator = TintColorCalculator(connection.world) val textureManager = renderSystem.createTextureManager() + lateinit var font: Font val rendererMap: MutableMap = synchronizedMapOf() @@ -143,6 +146,7 @@ class RenderWindow( uvEnd = Vec2(1.0f, 1.0f), size = Vec2i(16, 16) ) + font = FontLoader.load(this) shaderManager.init() @@ -158,6 +162,7 @@ class RenderWindow( Log.log(LogMessageType.RENDERING_LOADING) { "Loading textures (${stopwatch.labTime()})..." } textureManager.staticTextures.load() + font.postInit() Log.log(LogMessageType.RENDERING_LOADING) { "Post loading renderer (${stopwatch.labTime()})..." } for (renderer in rendererMap.values) { diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/font/CharData.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/font/CharData.kt new file mode 100644 index 000000000..5f94d85ec --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/font/CharData.kt @@ -0,0 +1,30 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.font + +import de.bixilon.minosoft.gui.rendering.system.base.texture.texture.AbstractTexture +import glm_.vec2.Vec2 + +class CharData( + val char: Char, + val texture: AbstractTexture, + val width: Int, + var uvStart: Vec2, + var uvEnd: Vec2, +) { + fun postInit() { + uvStart = uvStart * texture.textureArrayUV + uvEnd = uvEnd * texture.textureArrayUV + } +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/font/Font.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/font/Font.kt new file mode 100644 index 000000000..30267fc00 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/font/Font.kt @@ -0,0 +1,33 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.font + +import de.bixilon.minosoft.gui.rendering.font.provider.FontProvider + +class Font( + val providers: MutableList, +) : FontProvider { + + + override fun postInit() { + for (provider in providers) { + provider.postInit() + } + } + + + companion object { + const val CHAR_HEIGHT = 8 + } +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/font/FontLoader.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/font/FontLoader.kt new file mode 100644 index 000000000..b7b1d6881 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/font/FontLoader.kt @@ -0,0 +1,49 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.font + +import de.bixilon.minosoft.data.registries.factory.DefaultFactory +import de.bixilon.minosoft.gui.rendering.RenderWindow +import de.bixilon.minosoft.gui.rendering.font.provider.BitmapFontProvider +import de.bixilon.minosoft.gui.rendering.font.provider.FontProvider +import de.bixilon.minosoft.gui.rendering.font.provider.FontProviderFactory +import de.bixilon.minosoft.gui.rendering.font.provider.LegacyUnicodeFontProvider +import de.bixilon.minosoft.util.KUtil.check +import de.bixilon.minosoft.util.KUtil.toResourceLocation +import de.bixilon.minosoft.util.nbt.tag.NBTUtil.listCast + +object FontLoader : DefaultFactory>( + BitmapFontProvider, + LegacyUnicodeFontProvider, + // ToDo: True type font +) { + private val FONT_INDEX = "font/default.json".toResourceLocation() + + + fun load(renderWindow: RenderWindow): Font { + val fontIndex = renderWindow.connection.assetsManager.readJsonAsset(FONT_INDEX) + + val providers: MutableList = mutableListOf() + + for (provider in fontIndex["providers"].listCast>()!!) { + val type = provider["type"].toResourceLocation() + providers += this[type].check { "Unknown font provider type $type" }.build(renderWindow, provider) + } + + return Font( + providers = providers, + ) + } + +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/BitmapFontProvider.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/BitmapFontProvider.kt new file mode 100644 index 000000000..9f1da5a39 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/BitmapFontProvider.kt @@ -0,0 +1,84 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.font.provider + +import de.bixilon.minosoft.data.registries.ResourceLocation +import de.bixilon.minosoft.gui.rendering.RenderWindow +import de.bixilon.minosoft.gui.rendering.font.CharData +import de.bixilon.minosoft.gui.rendering.textures.TextureUtil.texture +import de.bixilon.minosoft.util.KUtil.asList +import de.bixilon.minosoft.util.KUtil.toDouble +import de.bixilon.minosoft.util.KUtil.toInt +import de.bixilon.minosoft.util.KUtil.toResourceLocation +import de.bixilon.minosoft.util.KUtil.unsafeCast +import glm_.vec2.Vec2 + +class BitmapFontProvider( + private val renderWindow: RenderWindow, + data: Map, +) : FontProvider { + val height = data["height"]?.toInt() ?: 8 + val ascent = data["ascent"].toDouble() + private val chars: MutableMap = mutableMapOf() + + init { + val texture = renderWindow.textureManager.staticTextures.createTexture(data["file"].toResourceLocation().texture()) + texture.load(renderWindow.connection.assetsManager) + val pixel = Vec2(1.0f) / texture.size + for ((y, row) in data["chars"].asList().withIndex()) { + val yStart = pixel.y * y * height + val yEnd = pixel.y * (y + 1) * height + for ((x, char) in row.unsafeCast().toCharArray().withIndex()) { + val charXStart = 0 // ToDo: Calculate dynamically + val charXEnd = CHAR_WIDTH // ToDo: Calculate dynamically + + val xOffset = pixel.x * CHAR_WIDTH * x + + val uvStart = Vec2( + x = xOffset + (pixel.x * charXStart), + y = yStart, + ) + val uvEnd = Vec2( + x = xOffset + (pixel.x * charXEnd), + y = yEnd, + ) + + val charData = CharData( + char = char, + texture = texture, + width = charXEnd - charXStart, + uvStart = uvStart, + uvEnd = uvEnd, + ) + + chars[char] = charData + } + } + } + + override fun postInit() { + for (char in chars.values) { + char.postInit() + } + } + + companion object : FontProviderFactory { + private const val CHAR_WIDTH = 8 + override val RESOURCE_LOCATION: ResourceLocation = "minecraft:bitmap".toResourceLocation() + + override fun build(renderWindow: RenderWindow, data: Map): BitmapFontProvider { + return BitmapFontProvider(renderWindow, data) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/FontProvider.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/FontProvider.kt new file mode 100644 index 000000000..d50f4ea86 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/FontProvider.kt @@ -0,0 +1,19 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.font.provider + +interface FontProvider { + + fun postInit() +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/FontProviderFactory.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/FontProviderFactory.kt new file mode 100644 index 000000000..316afe4cd --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/FontProviderFactory.kt @@ -0,0 +1,22 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.font.provider + +import de.bixilon.minosoft.data.registries.CompanionResourceLocation +import de.bixilon.minosoft.gui.rendering.RenderWindow + + +interface FontProviderFactory : CompanionResourceLocation { + fun build(renderWindow: RenderWindow, data: Map): T +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/LegacyUnicodeFontProvider.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/LegacyUnicodeFontProvider.kt new file mode 100644 index 000000000..1d350bb56 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/font/provider/LegacyUnicodeFontProvider.kt @@ -0,0 +1,98 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.font.provider + +import de.bixilon.minosoft.data.registries.ResourceLocation +import de.bixilon.minosoft.gui.rendering.RenderWindow +import de.bixilon.minosoft.gui.rendering.font.CharData +import de.bixilon.minosoft.gui.rendering.font.Font +import de.bixilon.minosoft.gui.rendering.textures.TextureUtil.texture +import de.bixilon.minosoft.util.KUtil.toResourceLocation +import de.bixilon.minosoft.util.KUtil.unsafeCast +import glm_.vec2.Vec2 + +class LegacyUnicodeFontProvider( + private val renderWindow: RenderWindow, + data: Map, +) : FontProvider { + private val chars: Array = arrayOfNulls(1 shl Char.SIZE_BITS) + + init { + val template = data["template"].unsafeCast() + val sizes = renderWindow.connection.assetsManager.readAssetAsStream(data["sizes"].toResourceLocation()) + + var char = '\u0000' + for (page in 0 until UNICODE_PAGES) { + if (MISSING_UNICODE_PAGES.contains(page)) { + // This page somehow does not exist, but we are fine with it + // ToDo: Check if it really exist + sizes.skip(UNICODE_PAGE_SIZE.toLong()) // skip the sizes to not mess up + continue + } + val texture = renderWindow.textureManager.staticTextures.createTexture(template.format("%02x".format(page)).toResourceLocation().texture()) + for (y in 0 until UNICODE_PAGE_SIZE / CHAR_SIZE) { + val yStart = PIXEL.y * y * CHAR_SIZE + val yEnd = PIXEL.y * (y + 1) * CHAR_SIZE + for (x in 0 until UNICODE_PAGE_SIZE / CHAR_SIZE) { + val widthByte = sizes.read() + val charXStart = (widthByte shr 4) and 0x0F + val charXEnd = widthByte and 0x0F + + val xOffset = PIXEL.x * CHAR_SIZE * x + + val uvStart = Vec2( + x = xOffset + (PIXEL.x * charXStart), + y = yStart, + ) + + val uvEnd = Vec2( + x = xOffset + (PIXEL.x * charXEnd), + y = yEnd, + ) + + + val charData = CharData( + char = char, + texture = texture, + width = (charXEnd - charXStart) / (CHAR_SIZE / Font.CHAR_HEIGHT), + uvStart = uvStart, + uvEnd = uvEnd, + ) + + chars[(char++).code] = charData + } + } + } + } + + + override fun postInit() { + for (char in chars) { + char?.postInit() + } + } + + companion object : FontProviderFactory { + private val MISSING_UNICODE_PAGES = listOf(0x08, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF, 0xE0, 0xE1, 0xE2, 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xEB, 0xEC, 0xEE, 0xED, 0xEE, 0xEF, 0xF0, 0xF1, 0xF2, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8) + override val RESOURCE_LOCATION: ResourceLocation = "minecraft:legacy_unicode".toResourceLocation() + private const val UNICODE_PAGE_SIZE = 256 + private const val UNICODE_PAGES = 256 + private const val CHAR_SIZE = 16 + private val PIXEL = Vec2(1.0f) / UNICODE_PAGE_SIZE + + override fun build(renderWindow: RenderWindow, data: Map): LegacyUnicodeFontProvider { + return LegacyUnicodeFontProvider(renderWindow, data) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/AbstractTexture.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/AbstractTexture.kt index d78a08718..1209dd9c0 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/AbstractTexture.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/AbstractTexture.kt @@ -28,6 +28,7 @@ import java.nio.ByteBuffer interface AbstractTexture { val resourceLocation: ResourceLocation + var textureArrayUV: Vec2 var singlePixelSize: Vec2 val state: TextureStates val size: Vec2i diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/MemoryTexture.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/MemoryTexture.kt index ffb62b6dc..d133ad523 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/MemoryTexture.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/MemoryTexture.kt @@ -31,6 +31,7 @@ class MemoryTexture( override var properties: ImageProperties = ImageProperties(), generator: ((x: Int, y: Int) -> RGBColor)? = null, ) : AbstractTexture { + override lateinit var textureArrayUV: Vec2 override lateinit var singlePixelSize: Vec2 override var renderData: TextureRenderData? = null override var transparency: TextureTransparencies = TextureTransparencies.OPAQUE diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/PNGTexture.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/PNGTexture.kt index e5e5796d7..5da8b1e9e 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/PNGTexture.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/PNGTexture.kt @@ -29,6 +29,7 @@ import java.nio.ByteBuffer class PNGTexture(override val resourceLocation: ResourceLocation) : AbstractTexture { override var renderData: TextureRenderData? = null + override lateinit var textureArrayUV: Vec2 override lateinit var singlePixelSize: Vec2 override var state: TextureStates = TextureStates.DECLARED private set diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/SpriteTexture.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/SpriteTexture.kt index cf7964461..b15e0c260 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/SpriteTexture.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/base/texture/texture/SpriteTexture.kt @@ -25,6 +25,7 @@ import java.nio.ByteBuffer class SpriteTexture(private val original: AbstractTexture) : AbstractTexture { override val resourceLocation: ResourceLocation = original.resourceLocation + override var textureArrayUV: Vec2 by original::textureArrayUV override var singlePixelSize: Vec2 by original::singlePixelSize override var properties: ImageProperties by original::properties override var renderData: TextureRenderData? by original::renderData diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/texture/OpenGLTextureArray.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/texture/OpenGLTextureArray.kt index 043540a27..843c3568e 100644 --- a/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/texture/OpenGLTextureArray.kt +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/system/opengl/texture/OpenGLTextureArray.kt @@ -103,6 +103,7 @@ class OpenGLTextureArray( val uvEnd = Vec2(texture.size) / arrayResolution val singlePixelSize = Vec2(1.0f) / arrayResolution + val textureArrayUV = Vec2(texture.size) / arrayResolution if (texture is SpriteTexture) { val animationIndex = lastAnimationIndex++ @@ -121,6 +122,7 @@ class OpenGLTextureArray( texturesByResolution[arrayId] += texture texture.renderData = OpenGLTextureData(arrayId, lastTextureId[arrayId]++, uvEnd, -1) texture.singlePixelSize = singlePixelSize + texture.textureArrayUV = textureArrayUV } } diff --git a/src/main/java/de/bixilon/minosoft/gui/rendering/textures/TextureUtil.kt b/src/main/java/de/bixilon/minosoft/gui/rendering/textures/TextureUtil.kt new file mode 100644 index 000000000..3748e5813 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/gui/rendering/textures/TextureUtil.kt @@ -0,0 +1,35 @@ +/* + * Minosoft + * Copyright (C) 2021 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.gui.rendering.textures + +import de.bixilon.minosoft.data.registries.ResourceLocation +import de.bixilon.minosoft.util.KUtil.toResourceLocation + +object TextureUtil { + fun ResourceLocation.texture(): ResourceLocation { + var path = "" + + if (!this.path.startsWith("textures/")) { + path += "textures/" + } + path += this.path + + if (!path.contains(".")) { + // ending + path += ".png" + } + + return "$namespace:$path".toResourceLocation() + } +} diff --git a/src/main/java/de/bixilon/minosoft/util/KUtil.kt b/src/main/java/de/bixilon/minosoft/util/KUtil.kt index 8a16e5984..3f9aca0a3 100644 --- a/src/main/java/de/bixilon/minosoft/util/KUtil.kt +++ b/src/main/java/de/bixilon/minosoft/util/KUtil.kt @@ -73,7 +73,7 @@ object KUtil { return null } - fun Any.toResourceLocation(): ResourceLocation { + fun Any?.toResourceLocation(): ResourceLocation { return when (this) { is String -> ResourceLocation(this) is ResourceLocation -> this @@ -394,4 +394,11 @@ object KUtil { return ret.toMap() } + + fun T?.check(message: (() -> Any)? = null): T { + if (this == null) { + throw NullPointerException(message?.invoke()?.toString() ?: "Null check failed") + } + return this + } } diff --git a/src/main/java/de/bixilon/minosoft/util/nbt/tag/NBTUtil.kt b/src/main/java/de/bixilon/minosoft/util/nbt/tag/NBTUtil.kt index 852e5299a..29200b53e 100644 --- a/src/main/java/de/bixilon/minosoft/util/nbt/tag/NBTUtil.kt +++ b/src/main/java/de/bixilon/minosoft/util/nbt/tag/NBTUtil.kt @@ -44,7 +44,7 @@ object NBTUtil { return this.compoundCast()!! } - fun Any.listCast(): MutableList? { + fun Any?.listCast(): MutableList? { try { return this as MutableList } catch (ignored: ClassCastException) {