From e22cec01e6e7f36e6477f90db580875cfb73f624 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20N=C3=BCcke?= Date: Tue, 20 May 2014 00:12:55 +0200 Subject: [PATCH] Allow specifying a texture font's char size in the text file defining the characters in the texture. Also loading said text file via MC's resource system which should make it replaceable using resource packs. Probably. --- .../opencomputers/textures/font/chars.txt | 3 ++- .../renderer/MonospaceFontRenderer.scala | 20 +++++++++++++++---- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/main/resources/assets/opencomputers/textures/font/chars.txt b/src/main/resources/assets/opencomputers/textures/font/chars.txt index 65bd0a8fd..a5b66a8ba 100644 --- a/src/main/resources/assets/opencomputers/textures/font/chars.txt +++ b/src/main/resources/assets/opencomputers/textures/font/chars.txt @@ -1 +1,2 @@ -☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ \ No newline at end of file +☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■ +5 9 \ No newline at end of file diff --git a/src/main/scala/li/cil/oc/client/renderer/MonospaceFontRenderer.scala b/src/main/scala/li/cil/oc/client/renderer/MonospaceFontRenderer.scala index f10117297..b35c758ec 100644 --- a/src/main/scala/li/cil/oc/client/renderer/MonospaceFontRenderer.scala +++ b/src/main/scala/li/cil/oc/client/renderer/MonospaceFontRenderer.scala @@ -3,22 +3,34 @@ package li.cil.oc.client.renderer import li.cil.oc.client.Textures import li.cil.oc.util.{RenderState, PackedColor} import li.cil.oc.{OpenComputers, Settings} +import net.minecraft.client.Minecraft import net.minecraft.client.renderer.GLAllocation import net.minecraft.client.renderer.texture.TextureManager +import net.minecraft.util.ResourceLocation import org.lwjgl.opengl.GL11 import scala.io.Source object MonospaceFontRenderer { - private val chars = Source.fromInputStream(MonospaceFontRenderer.getClass.getResourceAsStream("/assets/" + Settings.resourceDomain + "/textures/font/chars.txt"))("UTF-8").mkString + val (chars, fontWidth, fontHeight) = try { + val lines = Source.fromInputStream(Minecraft.getMinecraft.getResourceManager.getResource(new ResourceLocation(Settings.resourceDomain, "/textures/font/chars.txt")).getInputStream)("UTF-8").getLines + val chars = lines.next() + val (w, h) = if (lines.hasNext) { + val size = lines.next().split(" ", 2) + (size(0).toInt, size(1).toInt) + } else (5, 9) + (chars, w, h) + } + catch { + case t: Throwable => + OpenComputers.log.warning("Failed reading font metdata, using defaults.") + ("""☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~⌂ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜ¢£¥₧ƒáíóúñѪº¿⌐¬½¼¡«»░▒▓│┤╡╢╖╕╣║╗╝╜╛┐└┴┬├─┼╞╟╚╔╩╦╠═╬╧╨╤╥╙╘╒╓╫╪┘┌█▄▌▐▀αßΓπΣσµτΦΘΩδ∞φε∩≡±≥≤⌠⌡÷≈°∙·√ⁿ²■""", 5, 9) + } private var instance: Option[Renderer] = None def init(textureManager: TextureManager) = this.synchronized( instance = instance.orElse(Some(new Renderer(textureManager)))) - val fontWidth = 5 - val fontHeight = 9 - def drawString(x: Int, y: Int, value: Array[Char], color: Array[Short], format: PackedColor.ColorFormat) = this.synchronized(instance match { case None => OpenComputers.log.warning("Trying to render string with uninitialized MonospaceFontRenderer.") case Some(renderer) => renderer.drawString(x, y, value, color, format)