mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-15 11:45:42 +00:00
Use ByteBuffers in term.blit
This is about 5-6x faster than using a String as we don't need to allocate and copy multiple times. In the grand scheme of things, still vastly overshadowed by the Lua interpreter, but worth doing.
This commit is contained in:
parent
074793090d
commit
79467499e6
@ -14,6 +14,7 @@ import dan200.computercraft.shared.util.StringUtil;
|
|||||||
import org.apache.commons.lang3.ArrayUtils;
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A base class for all objects which interact with a terminal. Namely the {@link TermAPI} and monitors.
|
* A base class for all objects which interact with a terminal. Namely the {@link TermAPI} and monitors.
|
||||||
@ -283,9 +284,9 @@ public abstract class TermMethods
|
|||||||
* }</pre>
|
* }</pre>
|
||||||
*/
|
*/
|
||||||
@LuaFunction
|
@LuaFunction
|
||||||
public final void blit( String text, String textColour, String backgroundColour ) throws LuaException
|
public final void blit( ByteBuffer text, ByteBuffer textColour, ByteBuffer backgroundColour ) throws LuaException
|
||||||
{
|
{
|
||||||
if( textColour.length() != text.length() || backgroundColour.length() != text.length() )
|
if( textColour.remaining() != text.remaining() || backgroundColour.remaining() != text.remaining() )
|
||||||
{
|
{
|
||||||
throw new LuaException( "Arguments must be the same length" );
|
throw new LuaException( "Arguments must be the same length" );
|
||||||
}
|
}
|
||||||
@ -294,7 +295,7 @@ public abstract class TermMethods
|
|||||||
synchronized( terminal )
|
synchronized( terminal )
|
||||||
{
|
{
|
||||||
terminal.blit( text, textColour, backgroundColour );
|
terminal.blit( text, textColour, backgroundColour );
|
||||||
terminal.setCursorPos( terminal.getCursorX() + text.length(), terminal.getCursorY() );
|
terminal.setCursorPos( terminal.getCursorX() + text.remaining(), terminal.getCursorY() );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -11,6 +11,7 @@ import net.minecraft.nbt.CompoundNBT;
|
|||||||
import net.minecraft.network.PacketBuffer;
|
import net.minecraft.network.PacketBuffer;
|
||||||
|
|
||||||
import javax.annotation.Nonnull;
|
import javax.annotation.Nonnull;
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public class Terminal
|
public class Terminal
|
||||||
{
|
{
|
||||||
@ -191,7 +192,7 @@ public class Terminal
|
|||||||
return palette;
|
return palette;
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void blit( String text, String textColour, String backgroundColour )
|
public synchronized void blit( ByteBuffer text, ByteBuffer textColour, ByteBuffer backgroundColour )
|
||||||
{
|
{
|
||||||
int x = cursorX;
|
int x = cursorX;
|
||||||
int y = cursorY;
|
int y = cursorY;
|
||||||
|
@ -5,6 +5,8 @@
|
|||||||
*/
|
*/
|
||||||
package dan200.computercraft.core.terminal;
|
package dan200.computercraft.core.terminal;
|
||||||
|
|
||||||
|
import java.nio.ByteBuffer;
|
||||||
|
|
||||||
public class TextBuffer
|
public class TextBuffer
|
||||||
{
|
{
|
||||||
private final char[] text;
|
private final char[] text;
|
||||||
@ -42,6 +44,19 @@ public class TextBuffer
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void write( ByteBuffer text, int start )
|
||||||
|
{
|
||||||
|
int pos = start;
|
||||||
|
start = Math.max( start, 0 );
|
||||||
|
int length = text.remaining();
|
||||||
|
int end = Math.min( start + length, pos + length );
|
||||||
|
end = Math.min( end, this.text.length );
|
||||||
|
for( int i = start; i < end; i++ )
|
||||||
|
{
|
||||||
|
this.text[i] = (char) (text.get( i - pos ) & 0xFF);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void write( TextBuffer text )
|
public void write( TextBuffer text )
|
||||||
{
|
{
|
||||||
int end = Math.min( text.length(), this.text.length );
|
int end = Math.min( text.length(), this.text.length );
|
||||||
|
@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package dan200.computercraft.core.terminal;
|
package dan200.computercraft.core.terminal;
|
||||||
|
|
||||||
|
import dan200.computercraft.api.lua.LuaValues;
|
||||||
import dan200.computercraft.shared.util.Colour;
|
import dan200.computercraft.shared.util.Colour;
|
||||||
import dan200.computercraft.utils.CallCounter;
|
import dan200.computercraft.utils.CallCounter;
|
||||||
import io.netty.buffer.Unpooled;
|
import io.netty.buffer.Unpooled;
|
||||||
@ -293,7 +294,7 @@ class TerminalTest
|
|||||||
CallCounter callCounter = new CallCounter();
|
CallCounter callCounter = new CallCounter();
|
||||||
Terminal terminal = new Terminal( 4, 3, callCounter );
|
Terminal terminal = new Terminal( 4, 3, callCounter );
|
||||||
|
|
||||||
terminal.blit( "test", "1234", "abcd" );
|
blit( terminal, "test", "1234", "abcd" );
|
||||||
|
|
||||||
assertThat( terminal, allOf(
|
assertThat( terminal, allOf(
|
||||||
textMatches( new String[] {
|
textMatches( new String[] {
|
||||||
@ -322,7 +323,7 @@ class TerminalTest
|
|||||||
|
|
||||||
terminal.setCursorPos( 2, 1 );
|
terminal.setCursorPos( 2, 1 );
|
||||||
callCounter.reset();
|
callCounter.reset();
|
||||||
terminal.blit( "hi", "11", "ee" );
|
blit( terminal, "hi", "11", "ee" );
|
||||||
|
|
||||||
assertThat( terminal, allOf(
|
assertThat( terminal, allOf(
|
||||||
textMatches( new String[] {
|
textMatches( new String[] {
|
||||||
@ -354,13 +355,13 @@ class TerminalTest
|
|||||||
|
|
||||||
terminal.setCursorPos( 2, -5 );
|
terminal.setCursorPos( 2, -5 );
|
||||||
callCounter.reset();
|
callCounter.reset();
|
||||||
terminal.blit( "hi", "11", "ee" );
|
blit( terminal, "hi", "11", "ee" );
|
||||||
assertThat( terminal, old.matches() );
|
assertThat( terminal, old.matches() );
|
||||||
callCounter.assertNotCalled();
|
callCounter.assertNotCalled();
|
||||||
|
|
||||||
terminal.setCursorPos( 2, 5 );
|
terminal.setCursorPos( 2, 5 );
|
||||||
callCounter.reset();
|
callCounter.reset();
|
||||||
terminal.blit( "hi", "11", "ee" );
|
blit( terminal, "hi", "11", "ee" );
|
||||||
assertThat( terminal, old.matches() );
|
assertThat( terminal, old.matches() );
|
||||||
callCounter.assertNotCalled();
|
callCounter.assertNotCalled();
|
||||||
}
|
}
|
||||||
@ -584,7 +585,7 @@ class TerminalTest
|
|||||||
{
|
{
|
||||||
Terminal writeTerminal = new Terminal( 2, 1 );
|
Terminal writeTerminal = new Terminal( 2, 1 );
|
||||||
|
|
||||||
writeTerminal.blit( "hi", "11", "ee" );
|
blit( writeTerminal, "hi", "11", "ee" );
|
||||||
writeTerminal.setCursorPos( 2, 5 );
|
writeTerminal.setCursorPos( 2, 5 );
|
||||||
writeTerminal.setTextColour( 3 );
|
writeTerminal.setTextColour( 3 );
|
||||||
writeTerminal.setBackgroundColour( 5 );
|
writeTerminal.setBackgroundColour( 5 );
|
||||||
@ -614,7 +615,7 @@ class TerminalTest
|
|||||||
void testNbtRoundtrip()
|
void testNbtRoundtrip()
|
||||||
{
|
{
|
||||||
Terminal writeTerminal = new Terminal( 10, 5 );
|
Terminal writeTerminal = new Terminal( 10, 5 );
|
||||||
writeTerminal.blit( "hi", "11", "ee" );
|
blit( writeTerminal, "hi", "11", "ee" );
|
||||||
writeTerminal.setCursorPos( 2, 5 );
|
writeTerminal.setCursorPos( 2, 5 );
|
||||||
writeTerminal.setTextColour( 3 );
|
writeTerminal.setTextColour( 3 );
|
||||||
writeTerminal.setBackgroundColour( 5 );
|
writeTerminal.setBackgroundColour( 5 );
|
||||||
@ -687,6 +688,11 @@ class TerminalTest
|
|||||||
assertEquals( 5, Terminal.getColour( 'Z', Colour.LIME ) );
|
assertEquals( 5, Terminal.getColour( 'Z', Colour.LIME ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void blit( Terminal terminal, String text, String fg, String bg )
|
||||||
|
{
|
||||||
|
terminal.blit( LuaValues.encode( text ), LuaValues.encode( fg ), LuaValues.encode( bg ) );
|
||||||
|
}
|
||||||
|
|
||||||
private static final class TerminalBufferSnapshot
|
private static final class TerminalBufferSnapshot
|
||||||
{
|
{
|
||||||
final String[] textLines;
|
final String[] textLines;
|
||||||
|
Loading…
Reference in New Issue
Block a user