Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/main/scala/li/cil/oc/client/Proxy.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import li.cil.oc.client
import li.cil.oc.client.renderer.HighlightRenderer
import li.cil.oc.client.renderer.MFUTargetRenderer
import li.cil.oc.client.renderer.PetRenderer
import li.cil.oc.client.renderer.TextBufferRenderCache
import li.cil.oc.client.renderer.WirelessNetworkDebugRenderer
import li.cil.oc.client.renderer.block.BlockRenderer
import li.cil.oc.client.renderer.entity.DroneRenderer
Expand Down Expand Up @@ -98,6 +97,5 @@ private[oc] class Proxy extends CommonProxy {
FMLCommonHandler.instance.bus.register(HologramRenderer)
FMLCommonHandler.instance.bus.register(PetRenderer)
FMLCommonHandler.instance.bus.register(Sound)
FMLCommonHandler.instance.bus.register(TextBufferRenderCache)
}
}
Original file line number Diff line number Diff line change
@@ -1,108 +1,30 @@
package li.cil.oc.client.renderer

import java.util.concurrent.Callable
import java.util.concurrent.TimeUnit

import com.google.common.cache.CacheBuilder
import com.google.common.cache.RemovalListener
import com.google.common.cache.RemovalNotification
import cpw.mods.fml.common.eventhandler.SubscribeEvent
import cpw.mods.fml.common.gameevent.TickEvent.ClientTickEvent
import li.cil.oc.Settings
import li.cil.oc.client.renderer.font.TextBufferRenderData
import li.cil.oc.util.RenderState
import net.minecraft.client.renderer.GLAllocation
import net.minecraft.tileentity.TileEntity
import org.lwjgl.opengl.GL11

object TextBufferRenderCache extends Callable[Int] with RemovalListener[TileEntity, Int] {
object TextBufferRenderCache {
val renderer =
if (Settings.get.fontRenderer == "texture") new font.StaticFontRenderer()
else new font.DynamicFontRenderer()

private val cache = com.google.common.cache.CacheBuilder.newBuilder().
expireAfterAccess(2, TimeUnit.SECONDS).
removalListener(this).
asInstanceOf[CacheBuilder[TextBufferRenderData, Int]].
build[TextBufferRenderData, Int]()

// To allow access in cache entry init.
private var currentBuffer: TextBufferRenderData = _

// ----------------------------------------------------------------------- //
// Rendering
// ----------------------------------------------------------------------- //

def render(buffer: TextBufferRenderData) {
currentBuffer = buffer
compileOrDraw(cache.get(currentBuffer, this))
}
RenderState.checkError(getClass.getName + ".render: entering (aka: wasntme)")

private def compileOrDraw(list: Int) = {
if (currentBuffer.dirty) {
RenderState.checkError(getClass.getName + ".compileOrDraw: entering (aka: wasntme)")

for (line <- currentBuffer.data.buffer) {
if (buffer.dirty) {
for (line <- buffer.data.buffer) {
renderer.generateChars(line)
}

val doCompile = !RenderState.compilingDisplayList
if (doCompile) {
currentBuffer.dirty = false
GL11.glNewList(list, GL11.GL_COMPILE_AND_EXECUTE)

RenderState.checkError(getClass.getName + ".compileOrDraw: glNewList")
}

renderer.drawBuffer(currentBuffer.data, currentBuffer.viewport._1, currentBuffer.viewport._2)

RenderState.checkError(getClass.getName + ".compileOrDraw: drawString")

if (doCompile) {
GL11.glEndList()

RenderState.checkError(getClass.getName + ".compileOrDraw: glEndList")

}

RenderState.checkError(getClass.getName + ".compileOrDraw: leaving")

true
buffer.dirty = false
}
else {
GL11.glCallList(list)

RenderState.checkError(getClass.getName + ".compileOrDraw: glCallList")
}
}

// ----------------------------------------------------------------------- //
// Cache
// ----------------------------------------------------------------------- //

def call = {
RenderState.checkError(getClass.getName + ".call: entering (aka: wasntme)")

val list = GLAllocation.generateDisplayLists(1)
currentBuffer.dirty = true // Force compilation.

RenderState.checkError(getClass.getName + ".call: leaving")
renderer.drawBuffer(buffer.data, buffer.viewport._1, buffer.viewport._2)

list
RenderState.checkError(getClass.getName + ".render: leaving")
}

def onRemoval(e: RemovalNotification[TileEntity, Int]) {
RenderState.checkError(getClass.getName + ".onRemoval: entering (aka: wasntme)")

GLAllocation.deleteDisplayLists(e.getValue)

RenderState.checkError(getClass.getName + ".onRemoval: leaving")
}

// ----------------------------------------------------------------------- //
// ITickHandler
// ----------------------------------------------------------------------- //

@SubscribeEvent
def onTick(e: ClientTickEvent) = cache.cleanUp()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import li.cil.oc.client.renderer.font.DynamicFontRenderer.CharTexture
import li.cil.oc.util.FontUtils
import li.cil.oc.util.RenderState
import net.minecraft.client.Minecraft
import net.minecraft.client.renderer.Tessellator
import net.minecraft.client.resources.IReloadableResourceManager
import net.minecraft.client.resources.IResourceManager
import net.minecraft.client.resources.IResourceManagerReloadListener
Expand Down Expand Up @@ -149,14 +150,11 @@ object DynamicFontRenderer {

class CharIcon(val texture: CharTexture, val w: Int, val h: Int, val u1: Double, val v1: Double, val u2: Double, val v2: Double) {
def draw(tx: Float, ty: Float) {
GL11.glTexCoord2d(u1, v2)
GL11.glVertex2f(tx, ty + h)
GL11.glTexCoord2d(u2, v2)
GL11.glVertex2f(tx + w, ty + h)
GL11.glTexCoord2d(u2, v1)
GL11.glVertex2f(tx + w, ty)
GL11.glTexCoord2d(u1, v1)
GL11.glVertex2f(tx, ty)
val t = Tessellator.instance
t.addVertexWithUV(tx, ty + h, 0, u1, v2)
t.addVertexWithUV(tx + w, ty + h, 0, u2, v2)
t.addVertexWithUV(tx + w, ty, 0, u2, v1)
t.addVertexWithUV(tx, ty, 0, u1, v1)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import li.cil.oc.OpenComputers
import li.cil.oc.Settings
import li.cil.oc.client.Textures
import net.minecraft.client.Minecraft
import net.minecraft.client.renderer.Tessellator
import net.minecraft.util.ResourceLocation
import org.lwjgl.opengl.GL11

import scala.io.Source

Expand Down Expand Up @@ -59,14 +59,11 @@ class StaticFontRenderer extends TextureFontRenderer {
val y = (index - 1) / cols
val u = x * uStep
val v = y * vStep
GL11.glTexCoord2d(u, v + vSize)
GL11.glVertex3d(tx - dw, ty + charHeight * s, 0)
GL11.glTexCoord2d(u + uSize, v + vSize)
GL11.glVertex3d(tx + charWidth * s, ty + charHeight * s, 0)
GL11.glTexCoord2d(u + uSize, v)
GL11.glVertex3d(tx + charWidth * s, ty - dh, 0)
GL11.glTexCoord2d(u, v)
GL11.glVertex3d(tx - dw, ty - dh, 0)
val t = Tessellator.instance
t.addVertexWithUV(tx - dw, ty + charHeight * s, 0, u, v + vSize)
t.addVertexWithUV(tx + charWidth * s, ty + charHeight * s, 0, u + uSize, v + vSize)
t.addVertexWithUV(tx + charWidth * s, ty - dh, 0, u + uSize, v)
t.addVertexWithUV(tx - dw, ty - dh, 0, u, v)
}

override protected def generateChar(char: Int) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package li.cil.oc.client.renderer.font

import li.cil.oc.Settings
import li.cil.oc.util.{ExtendedUnicodeHelper, PackedColor, RenderState, TextBuffer}
import net.minecraft.client.renderer.Tessellator
import org.lwjgl.opengl.GL11

/**
Expand Down Expand Up @@ -36,6 +37,7 @@ abstract class TextureFontRenderer {

def drawBuffer(buffer: TextBuffer, viewportWidth: Int, viewportHeight: Int) {
val format = buffer.format
val t = Tessellator.instance

GL11.glPushMatrix()
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS)
Expand All @@ -49,7 +51,7 @@ abstract class TextureFontRenderer {

// Background first. We try to merge adjacent backgrounds of the same
// color to reduce the number of quads we have to draw.
GL11.glBegin(GL11.GL_QUADS)
t.startDrawingQuads()
for (y <- 0 until (viewportHeight min buffer.height)) {
val color = buffer.color(y)
var cbg = 0x000000
Expand All @@ -66,7 +68,7 @@ abstract class TextureFontRenderer {
}
drawQuad(cbg, x, y, width)
}
GL11.glEnd()
t.draw()

RenderState.checkError(getClass.getName + ".drawBuffer: background")

Expand All @@ -84,7 +86,7 @@ abstract class TextureFontRenderer {
val ty = y * charHeight
for (i <- 0 until textureCount) {
bindTexture(i)
GL11.glBegin(GL11.GL_QUADS)
t.startDrawingQuads()
var cfg = -1
var tx = 0f
for (n <- 0 until viewportWidth) {
Expand All @@ -93,18 +95,15 @@ abstract class TextureFontRenderer {
// Check if color changed.
if (col != cfg) {
cfg = col
GL11.glColor3ub(
((cfg & 0xFF0000) >> 16).toByte,
((cfg & 0x00FF00) >> 8).toByte,
((cfg & 0x0000FF) >> 0).toByte)
t.setColorOpaque_I(cfg)
}
// Don't render whitespace.
if (ch != ' ') {
drawChar(tx, ty, ch)
}
tx += charWidth
}
GL11.glEnd()
t.draw()
}
}

Expand All @@ -116,19 +115,24 @@ abstract class TextureFontRenderer {
RenderState.checkError(getClass.getName + ".drawBuffer: leaving")
}

def drawString(s: String, x: Int, y: Int): Unit = {
def drawString(s: String, x: Int, y: Int): Unit = drawString(s, x, y, 0xFFFFFF)

def drawString(s: String, x: Int, y: Int, color: Int): Unit = {
val sLength = ExtendedUnicodeHelper.length(s)
val t = Tessellator.instance

GL11.glPushMatrix()
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS)

GL11.glTranslatef(x, y, 0)
GL11.glScalef(0.5f, 0.5f, 1)
GL11.glDepthMask(false)
GL11.glEnable(GL11.GL_TEXTURE_2D)

for (i <- 0 until textureCount) {
bindTexture(i)
GL11.glBegin(GL11.GL_QUADS)
t.startDrawingQuads()
t.setColorOpaque_I(color)
var tx = 0f
var cx = 0
for (n <- 0 until sLength) {
Expand All @@ -140,7 +144,7 @@ abstract class TextureFontRenderer {
tx += charWidth
cx = s.offsetByCodePoints(cx, 1)
}
GL11.glEnd()
t.draw()
}

GL11.glPopAttrib()
Expand All @@ -160,14 +164,15 @@ abstract class TextureFontRenderer {
protected def drawChar(tx: Float, ty: Float, char: Int): Unit

private def drawQuad(color: Int, x: Int, y: Int, width: Int) = if (color != 0 && width > 0) {
val t = Tessellator.instance
val x0 = x * charWidth
val x1 = (x + width) * charWidth
val y0 = y * charHeight
val y1 = (y + 1) * charHeight
GL11.glColor3ub(((color >> 16) & 0xFF).toByte, ((color >> 8) & 0xFF).toByte, (color & 0xFF).toByte)
GL11.glVertex3d(x0, y1, 0)
GL11.glVertex3d(x1, y1, 0)
GL11.glVertex3d(x1, y0, 0)
GL11.glVertex3d(x0, y0, 0)
t.setColorOpaque_I(color)
t.addVertex(x0, y1, 0)
t.addVertex(x1, y1, 0)
t.addVertex(x1, y0, 0)
t.addVertex(x0, y0, 0)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package li.cil.oc.client.renderer.markdown.segment
import li.cil.oc.client.renderer.TextBufferRenderCache
import li.cil.oc.client.renderer.markdown.MarkupFormat
import net.minecraft.client.gui.FontRenderer
import org.lwjgl.opengl.GL11

private[markdown] class CodeSegment(val parent: Segment, val text: String) extends BasicTextSegment {
override def render(x: Int, y: Int, indent: Int, maxWidth: Int, renderer: FontRenderer, mouseX: Int, mouseY: Int): Option[InteractiveSegment] = {
Expand All @@ -16,8 +15,7 @@ private[markdown] class CodeSegment(val parent: Segment, val text: String) exten
var numChars = maxChars(chars, maxWidth - indent, maxWidth - wrapIndent, renderer)
while (chars.length > 0) {
val part = chars.take(numChars)
GL11.glColor4f(0.75f, 0.8f, 1, 1)
TextBufferRenderCache.renderer.drawString(part, currentX, currentY)
TextBufferRenderCache.renderer.drawString(part, currentX, currentY, 0xBFCCFF)
currentX = x + wrapIndent
currentY += lineHeight(renderer)
chars = chars.drop(numChars).dropWhile(_.isWhitespace)
Expand Down
Loading