diff --git a/Test.java b/Test.java index 7faf09c..cc013fd 100644 --- a/Test.java +++ b/Test.java @@ -41,6 +41,7 @@ public class Test { term.setChar(new Vec2i(0, 0), 0xC9, Color.GREEN); term.setChar(new Vec2i(9, 9), 0xC9, Color.GREEN); term.setChar(new Vec2i(10, 10), 0xC9, Color.GREEN); + term.drawHalfString(new Vec2i(11, 11), "AHello world. This is a test.", Color.GREEN); while(!window.shouldClose()) { window.tick(); diff --git a/com/danitheskunk/skunkworks/gfx/BaseRenderContext.java b/com/danitheskunk/skunkworks/gfx/BaseRenderContext.java index 9d5842a..3c46da3 100644 --- a/com/danitheskunk/skunkworks/gfx/BaseRenderContext.java +++ b/com/danitheskunk/skunkworks/gfx/BaseRenderContext.java @@ -1,8 +1,8 @@ package com.danitheskunk.skunkworks.gfx; -import com.danitheskunk.skunkworks.gfx.font.IFont; import com.danitheskunk.skunkworks.Recti; import com.danitheskunk.skunkworks.Vec2i; +import com.danitheskunk.skunkworks.gfx.font.IFont; import com.danitheskunk.skunkworks.gfx.vt.Terminal; import java.io.UnsupportedEncodingException; @@ -60,8 +60,8 @@ abstract public class BaseRenderContext implements IRenderContext { drawTexture(pixelPos, fullFont.getTexture(0xdb), bgColor); if(isHalfWidth) { - drawTexture(pixelPos, fullFont.getTexture(terminal.getLeftHalfChar(charPos)), fgColor); - drawTexture(Vec2i.add(pixelPos, halfCharSize), fullFont.getTexture(terminal.getRightHalfChar(charPos)), fgColor); + drawTexture(pixelPos, halfFont.getTexture(terminal.getLeftHalfChar(charPos)), fgColor); + drawTexture(new Vec2i(pixelPos.getX() + halfCharSize.getX(), pixelPos.getY()), halfFont.getTexture(terminal.getRightHalfChar(charPos)), fgColor); } else { drawTexture(pixelPos, fullFont.getTexture(terminal.getChar(charPos)), fgColor); } diff --git a/com/danitheskunk/skunkworks/gfx/vt/Terminal.java b/com/danitheskunk/skunkworks/gfx/vt/Terminal.java index 970f410..16043aa 100644 --- a/com/danitheskunk/skunkworks/gfx/vt/Terminal.java +++ b/com/danitheskunk/skunkworks/gfx/vt/Terminal.java @@ -4,6 +4,9 @@ import com.danitheskunk.skunkworks.Vec2i; import com.danitheskunk.skunkworks.gfx.Color; import com.danitheskunk.skunkworks.gfx.font.IFont; +import java.io.UnsupportedEncodingException; +import java.nio.ByteBuffer; +import java.nio.IntBuffer; import java.util.ArrayList; import java.util.List; @@ -150,6 +153,74 @@ public class Terminal { return Vec2i.mul(size, fullCharSize); } + public void drawString(Vec2i pos, String str) { + drawString(pos, str, null); + } + + public void drawString(Vec2i pos, String str, Color foregroundColor) { + drawString(pos, str, foregroundColor, null); + } + + public void drawString(Vec2i pos, String str, Color foregroundColor, Color backgroundColor) { + IntBuffer buf; + + try { + buf = ByteBuffer.wrap(str.getBytes("UTF-32")).asIntBuffer(); + } catch(UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + + for(int i = 0; i < str.length(); ++i) { + var ch = buf.get(i); + var cpos = new Vec2i(pos.getX() + i, pos.getY()); + if(ch > 0 && ch < 256) { + setChar(cpos, ch); + } + if(foregroundColor != null) { + setForegroundColor(cpos, foregroundColor); + } + if(backgroundColor != null) { + setBackgroundColor(cpos, backgroundColor); + } + } + } + + public void drawHalfString(Vec2i pos, String str) { + drawHalfString(pos, str, null); + } + + public void drawHalfString(Vec2i pos, String str, Color foregroundColor) { + drawHalfString(pos, str, foregroundColor, null); + } + + public void drawHalfString(Vec2i pos, String str, Color foregroundColor, Color backgroundColor) { + IntBuffer buf; + + try { + buf = ByteBuffer.wrap(str.getBytes("UTF-32")).asIntBuffer(); + } catch(UnsupportedEncodingException e) { + throw new RuntimeException(e); + } + + for(int i = 0; i < str.length(); i += 2) { + var ch1 = buf.get(i); + var ch2 = i + 1 == str.length() ? 0 : buf.get(i + 1); + var cpos = new Vec2i(pos.getX() + i / 2, pos.getY()); + + if(ch1 < 0 || ch1 >= 256) ch1 = 0; + if(ch2 < 0 || ch2 >= 256) ch2 = 0; + + setHalfChars(cpos, ch1, ch2); + + if(foregroundColor != null) { + setForegroundColor(cpos, foregroundColor); + } + if(backgroundColor != null) { + setBackgroundColor(cpos, backgroundColor); + } + } + } + private class Cell { int fullChar; int secondChar;