buffer renderer
This commit is contained in:
@@ -5,13 +5,16 @@ import dev.architectury.event.events.client.ClientLifecycleEvent
|
||||
import dev.architectury.event.events.common.LifecycleEvent
|
||||
import dev.architectury.registry.client.gui.MenuScreenRegistry
|
||||
import dev.architectury.registry.registries.RegistrarManager
|
||||
import net.minecraft.resources.Identifier
|
||||
import org.neoflock.neocomputers.block.Blocks
|
||||
import org.neoflock.neocomputers.entity.BlockEntities
|
||||
import org.neoflock.neocomputers.gui.buffer.BufferRenderer
|
||||
import org.neoflock.neocomputers.gui.menu.Menus
|
||||
import org.neoflock.neocomputers.gui.screen.ScreenScreen
|
||||
import org.neoflock.neocomputers.item.Items
|
||||
import org.neoflock.neocomputers.item.Tabs
|
||||
import org.neoflock.neocomputers.network.Networking
|
||||
import org.neoflock.neocomputers.utils.FontProvider
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
import java.util.function.Supplier
|
||||
@@ -37,6 +40,20 @@ object NeoComputers {
|
||||
MenuScreenRegistry.registerScreenFactory(Menus.SCREEN_MENU.get(), ::ScreenScreen)
|
||||
}
|
||||
|
||||
ClientLifecycleEvent.CLIENT_STARTED.register {
|
||||
FontProvider.load(Identifier.fromNamespaceAndPath("neocomputers", "font/unscii.hex"))
|
||||
|
||||
var buffer: ArrayList<BufferRenderer.GPUChar> = arrayListOf(BufferRenderer.GPUChar('h'), BufferRenderer.GPUChar('a'), BufferRenderer.GPUChar('i'))
|
||||
for (i in 0..<(400-3)) {
|
||||
buffer.add(BufferRenderer.GPUChar(' '))
|
||||
}
|
||||
var bufferRenderer = BufferRenderer(20, 20, Identifier.fromNamespaceAndPath(MODID, "screen/test"), buffer)
|
||||
bufferRenderer.drawBuffer()
|
||||
bufferRenderer.dump()
|
||||
bufferRenderer.clean()
|
||||
|
||||
}
|
||||
|
||||
val logA = Networking.LoggerNode("LogA")
|
||||
val logB = Networking.LoggerNode("LogB")
|
||||
val batteryA = Networking.DebugBatteryNode(0.0, 10000.0)
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package org.neoflock.neocomputers.gui.buffer;
|
||||
|
||||
import com.mojang.blaze3d.platform.NativeImage
|
||||
import net.minecraft.client.Minecraft
|
||||
import net.minecraft.client.renderer.texture.DynamicTexture
|
||||
import net.minecraft.client.renderer.texture.TextureManager
|
||||
import net.minecraft.resources.Identifier
|
||||
import org.neoflock.neocomputers.NeoComputers
|
||||
import org.neoflock.neocomputers.utils.FontProvider
|
||||
import java.io.File
|
||||
import kotlin.experimental.and
|
||||
import kotlin.experimental.xor
|
||||
|
||||
class BufferRenderer(width: Int, height: Int, id: Identifier, buffer: MutableList<GPUChar>) { // TODO: NN buffer
|
||||
val CHARW = 8
|
||||
val CHARH = 16
|
||||
|
||||
private var width: Int = width;
|
||||
private var height: Int = height;
|
||||
private var id: Identifier = id;
|
||||
private var buffer: MutableList<GPUChar> = buffer;
|
||||
|
||||
private var texwidth: Int = width*CHARW;
|
||||
private var texheight: Int = height*CHARH;
|
||||
private var image: NativeImage = NativeImage(texwidth, texheight, true); // idk what the boolean is
|
||||
private var tex: DynamicTexture = DynamicTexture({id.path}, image)
|
||||
|
||||
fun dump() {
|
||||
image.writeToFile(File("/home/mewhenthe/code/NeoComputers/dump.png"))
|
||||
NeoComputers.LOGGER.info("DUMPED!!!")
|
||||
}
|
||||
|
||||
fun drawGlyph(x: Int, y: Int, c: Char, fg: Int) {
|
||||
var glyph: ArrayList<Byte> = FontProvider.map[c]!!
|
||||
|
||||
for (j in 0..<CHARH) {
|
||||
for (i in 0..<CHARW) {
|
||||
// var pixel = ((glyph[j] and ((1 shl (CHARW - i - 1)).toByte())).toInt()) ushr (CHARW - i - 1) // retardation
|
||||
var pixel = (glyph[j] and (0b10000000 ushr i).toByte()).toInt()
|
||||
if (pixel > 0) image.setPixelABGR(x+i, y+j, (0xFF000000+fg).toInt())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun drawBuffer() {
|
||||
for (i in 0..<width) {
|
||||
for (j in 0..<height) {
|
||||
var char: GPUChar = get(i, j)
|
||||
var x = i*CHARW
|
||||
var y = j*CHARH
|
||||
image.fillRect(x, y, CHARW, CHARH, (0xFF000000+char.bg).toInt())
|
||||
if (char.c != ' ' && char.c != '\u0000') drawGlyph(x, y, char.c, char.fg)
|
||||
}
|
||||
}
|
||||
tex.upload()
|
||||
}
|
||||
|
||||
fun get(x: Int, y: Int) = buffer[y*width+x]
|
||||
fun set(x: Int, y: Int, c: GPUChar) {
|
||||
buffer[y*width+x] = c
|
||||
}
|
||||
|
||||
fun register() { // i would love to do this in the constructor but kotlin quirks blahblah
|
||||
Minecraft.getInstance().textureManager.register(this.id, tex) // also idk how to unregister this
|
||||
}
|
||||
|
||||
fun clean() {
|
||||
image.close()
|
||||
tex.close()
|
||||
Minecraft.getInstance().textureManager.release(this.id)
|
||||
}
|
||||
|
||||
data class GPUChar(val c: Char, val fg: Int =0xFFFFFF, val bg: Int = 0)
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
package org.neoflock.neocomputers.gui.screen;
|
||||
|
||||
import com.mojang.blaze3d.vertex.BufferBuilder
|
||||
import com.mojang.blaze3d.vertex.DefaultVertexFormat
|
||||
import com.mojang.blaze3d.vertex.Tesselator
|
||||
import com.mojang.blaze3d.vertex.VertexFormat
|
||||
import net.minecraft.client.gui.GuiGraphics
|
||||
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen
|
||||
import net.minecraft.network.chat.Component
|
||||
@@ -10,8 +12,7 @@ import org.neoflock.neocomputers.gui.menu.ScreenMenu
|
||||
|
||||
class ScreenScreen(abstractContainerMenu: ScreenMenu, inventory: Inventory, component: Component) : AbstractContainerScreen<ScreenMenu>(abstractContainerMenu, inventory, component) {
|
||||
override fun renderBg(guiGraphics: GuiGraphics, f: Float, i: Int, j: Int) {}
|
||||
|
||||
override fun getTitle(): Component {
|
||||
return Component.literal("Mango Vlud!")
|
||||
override fun render(graphics: GuiGraphics, mouseX: Int, mouseY: Int, something: Float) {
|
||||
super.render(graphics, mouseX, mouseY, something)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +0,0 @@
|
||||
package org.neoflock.neocomputers.util
|
||||
|
||||
fun interface TieredSupplier<T> {
|
||||
fun get(tier: Int): T
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package org.neoflock.neocomputers.utils;
|
||||
|
||||
import net.minecraft.client.Minecraft
|
||||
import net.minecraft.resources.Identifier
|
||||
import net.minecraft.server.packs.resources.Resource
|
||||
import net.minecraft.server.packs.resources.ResourceManager
|
||||
import org.neoflock.neocomputers.NeoComputers
|
||||
import java.nio.charset.StandardCharsets
|
||||
|
||||
object FontProvider {
|
||||
val map: MutableMap<Char, ArrayList<Byte>> = mutableMapOf();
|
||||
|
||||
fun load(loc: Identifier) { // TODO: optimize, this can totally be optimized
|
||||
var man: ResourceManager = Minecraft.getInstance().resourceManager
|
||||
var resource: Resource = man.getResourceOrThrow(loc)
|
||||
var stream = resource.open()
|
||||
while (stream.available() > 0) {
|
||||
var key = Integer.parseInt(String(stream.readNBytes(5), StandardCharsets.UTF_8), 16).toChar()
|
||||
stream.skip(1)
|
||||
var bytes: ArrayList<Byte> = ArrayList<Byte>();
|
||||
while (true) { // shut up will you
|
||||
var b1 = stream.read()
|
||||
if (b1 == 10) break // 10 is line break
|
||||
var b2 = stream.read()
|
||||
var value: Byte = Integer.parseInt(arrayOf(b1.toChar(), b2.toChar()).joinToString(""), 16).toByte()
|
||||
bytes.add(value)
|
||||
}
|
||||
map[key] = bytes
|
||||
}
|
||||
NeoComputers.LOGGER.info("[FontProvider] Loaded font!");
|
||||
}
|
||||
}
|
||||
64347
src/main/resources/assets/neocomputers/font/unscii.hex
Normal file
64347
src/main/resources/assets/neocomputers/font/unscii.hex
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user