1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-01-12 10:20:28 +00:00

Add a palette class and use it for rendering

This commit is contained in:
Lignum 2017-05-05 15:24:29 +02:00
parent 09215daa03
commit 936a531cd5
No known key found for this signature in database
GPG Key ID: 0889206F5A8A700D
6 changed files with 146 additions and 35 deletions

View File

@ -8,6 +8,7 @@ package dan200.computercraft.client.gui;
import dan200.computercraft.core.terminal.TextBuffer;
import dan200.computercraft.shared.util.Colour;
import dan200.computercraft.shared.util.Palette;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
import net.minecraft.client.renderer.VertexBuffer;
@ -31,28 +32,37 @@ public class FixedWidthFontRenderer
m_textureManager = textureManager;
}
private void drawChar( VertexBuffer renderer, double x, double y, int index, int color )
private void drawChar( VertexBuffer renderer, double x, double y, int index, int color, Palette p )
{
int column = index % 16;
int row = index / 16;
Colour colour = Colour.values()[ 15 - color ];
renderer.pos( x, y, 0.0 ).tex( (double) (column * FONT_WIDTH) / 256.0, (double) (row * FONT_HEIGHT ) / 256.0 ).color( colour.getR(), colour.getG(), colour.getB(), 1.0f ).endVertex();
renderer.pos( x, y + FONT_HEIGHT, 0.0 ).tex( (double) (column * FONT_WIDTH) / 256.0, (double) ((row + 1) * FONT_HEIGHT) / 256.0 ).color( colour.getR(), colour.getG(), colour.getB(), 1.0f ).endVertex();
renderer.pos( x + FONT_WIDTH, y, 0.0 ).tex( (double) ((column + 1) * FONT_WIDTH) / 256.0, (double) (row * FONT_HEIGHT) / 256.0 ).color( colour.getR(), colour.getG(), colour.getB(), 1.0f ).endVertex();
renderer.pos( x + FONT_WIDTH, y, 0.0 ).tex( (double) ((column + 1) * FONT_WIDTH) / 256.0, (double) (row * FONT_HEIGHT) / 256.0 ).color( colour.getR(), colour.getG(), colour.getB(), 1.0f ).endVertex();
renderer.pos( x, y + FONT_HEIGHT, 0.0 ).tex( (double) (column * FONT_WIDTH) / 256.0, (double) ((row + 1) * FONT_HEIGHT) / 256.0 ).color( colour.getR(), colour.getG(), colour.getB(), 1.0f ).endVertex();
renderer.pos( x + FONT_WIDTH, y + FONT_HEIGHT, 0.0 ).tex( (double) ((column + 1) * FONT_WIDTH) / 256.0, (double) ((row + 1) * FONT_HEIGHT) / 256.0 ).color( colour.getR(), colour.getG(), colour.getB(), 1.0f ).endVertex();
float[] colour = p.getColour( 15 - color );
float r = colour[0];
float g = colour[1];
float b = colour[2];
renderer.pos( x, y, 0.0 ).tex( (double) (column * FONT_WIDTH) / 256.0, (double) (row * FONT_HEIGHT ) / 256.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x, y + FONT_HEIGHT, 0.0 ).tex( (double) (column * FONT_WIDTH) / 256.0, (double) ((row + 1) * FONT_HEIGHT) / 256.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x + FONT_WIDTH, y, 0.0 ).tex( (double) ((column + 1) * FONT_WIDTH) / 256.0, (double) (row * FONT_HEIGHT) / 256.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x + FONT_WIDTH, y, 0.0 ).tex( (double) ((column + 1) * FONT_WIDTH) / 256.0, (double) (row * FONT_HEIGHT) / 256.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x, y + FONT_HEIGHT, 0.0 ).tex( (double) (column * FONT_WIDTH) / 256.0, (double) ((row + 1) * FONT_HEIGHT) / 256.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x + FONT_WIDTH, y + FONT_HEIGHT, 0.0 ).tex( (double) ((column + 1) * FONT_WIDTH) / 256.0, (double) ((row + 1) * FONT_HEIGHT) / 256.0 ).color( r, g, b, 1.0f ).endVertex();
}
private void drawQuad( VertexBuffer renderer, double x, double y, int color, double width )
private void drawQuad( VertexBuffer renderer, double x, double y, int color, double width, Palette p )
{
Colour colour = Colour.values()[ 15 - color ];
renderer.pos( x, y, 0.0 ).color( colour.getR(), colour.getG(), colour.getB(), 1.0f ).endVertex();
renderer.pos( x, y + FONT_HEIGHT, 0.0 ).color( colour.getR(), colour.getG(), colour.getB(), 1.0f ).endVertex();
renderer.pos( x + width, y, 0.0 ).color( colour.getR(), colour.getG(), colour.getB(), 1.0f ).endVertex();
renderer.pos( x + width, y, 0.0 ).color( colour.getR(), colour.getG(), colour.getB(), 1.0f ).endVertex();
renderer.pos( x, y + FONT_HEIGHT, 0.0 ).color( colour.getR(), colour.getG(), colour.getB(), 1.0f ).endVertex();
renderer.pos( x + width, y + FONT_HEIGHT, 0.0 ).color( colour.getR(), colour.getG(), colour.getB(), 1.0f ).endVertex();
float[] colour = p.getColour( 15 - color );
float r = colour[0];
float g = colour[1];
float b = colour[2];
renderer.pos( x, y, 0.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x, y + FONT_HEIGHT, 0.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x + width, y, 0.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x + width, y, 0.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x, y + FONT_HEIGHT, 0.0 ).color( r, g, b, 1.0f ).endVertex();
renderer.pos( x + width, y + FONT_HEIGHT, 0.0 ).color( r, g, b, 1.0f ).endVertex();
}
private boolean isGreyScale( int colour )
@ -60,7 +70,7 @@ public class FixedWidthFontRenderer
return (colour == 0 || colour == 15 || colour == 7 || colour == 8);
}
public void drawStringBackgroundPart( int x, int y, TextBuffer backgroundColour, double leftMarginSize, double rightMarginSize, boolean greyScale )
public void drawStringBackgroundPart( int x, int y, TextBuffer backgroundColour, double leftMarginSize, double rightMarginSize, boolean greyScale, Palette p )
{
// Draw the quads
Tessellator tessellator = Tessellator.getInstance();
@ -73,7 +83,7 @@ public class FixedWidthFontRenderer
{
colour1 = 15;
}
drawQuad( renderer, x - leftMarginSize, y, colour1, leftMarginSize );
drawQuad( renderer, x - leftMarginSize, y, colour1, leftMarginSize, p );
}
if( rightMarginSize > 0.0 )
{
@ -82,7 +92,7 @@ public class FixedWidthFontRenderer
{
colour2 = 15;
}
drawQuad( renderer, x + backgroundColour.length() * FONT_WIDTH, y, colour2, rightMarginSize );
drawQuad( renderer, x + backgroundColour.length() * FONT_WIDTH, y, colour2, rightMarginSize, p );
}
for( int i = 0; i < backgroundColour.length(); i++ )
{
@ -91,14 +101,14 @@ public class FixedWidthFontRenderer
{
colour = 15;
}
drawQuad( renderer, x + i * FONT_WIDTH, y, colour, FONT_WIDTH );
drawQuad( renderer, x + i * FONT_WIDTH, y, colour, FONT_WIDTH, p );
}
GlStateManager.disableTexture2D();
tessellator.draw();
GlStateManager.enableTexture2D();
}
public void drawStringTextPart( int x, int y, TextBuffer s, TextBuffer textColour, boolean greyScale )
public void drawStringTextPart( int x, int y, TextBuffer s, TextBuffer textColour, boolean greyScale, Palette p )
{
// Draw the quads
Tessellator tessellator = Tessellator.getInstance();
@ -119,12 +129,12 @@ public class FixedWidthFontRenderer
{
index = (int)'?';
}
drawChar( renderer, x + i * FONT_WIDTH, y, index, colour );
drawChar( renderer, x + i * FONT_WIDTH, y, index, colour, p );
}
tessellator.draw();
}
public void drawString( TextBuffer s, int x, int y, TextBuffer textColour, TextBuffer backgroundColour, double leftMarginSize, double rightMarginSize, boolean greyScale )
public void drawString( TextBuffer s, int x, int y, TextBuffer textColour, TextBuffer backgroundColour, double leftMarginSize, double rightMarginSize, boolean greyScale, Palette p )
{
// Draw background
if( backgroundColour != null )
@ -133,7 +143,7 @@ public class FixedWidthFontRenderer
m_textureManager.bindTexture( background );
// Draw the quads
drawStringBackgroundPart( x, y, backgroundColour, leftMarginSize, rightMarginSize, greyScale );
drawStringBackgroundPart( x, y, backgroundColour, leftMarginSize, rightMarginSize, greyScale, p );
}
// Draw text
@ -143,7 +153,7 @@ public class FixedWidthFontRenderer
m_textureManager.bindTexture( font );
// Draw the quads
drawStringTextPart( x, y, s, textColour, greyScale );
drawStringTextPart( x, y, s, textColour, greyScale, p );
}
}

View File

@ -10,6 +10,7 @@ import dan200.computercraft.ComputerCraft;
import dan200.computercraft.core.terminal.TextBuffer;
import dan200.computercraft.shared.media.inventory.ContainerHeldItem;
import dan200.computercraft.shared.media.items.ItemPrintout;
import dan200.computercraft.shared.util.Palette;
import net.minecraft.client.gui.inventory.GuiContainer;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.util.ResourceLocation;
@ -204,7 +205,7 @@ public class GuiPrintout extends GuiContainer
int lineIdx = ItemPrintout.LINES_PER_PAGE * m_page + line;
if( lineIdx >= 0 && lineIdx < m_text.length )
{
fontRenderer.drawString( m_text[lineIdx], x, y, m_colours[lineIdx], null, 0, 0, false );
fontRenderer.drawString( m_text[lineIdx], x, y, m_colours[lineIdx], null, 0, 0, false, new Palette() );
}
y = y + FixedWidthFontRenderer.FONT_HEIGHT;
}

View File

@ -13,6 +13,7 @@ import dan200.computercraft.core.terminal.TextBuffer;
import dan200.computercraft.shared.computer.core.IComputer;
import dan200.computercraft.shared.computer.core.IComputerContainer;
import dan200.computercraft.shared.util.Colour;
import dan200.computercraft.shared.util.Palette;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiScreen;
import net.minecraft.client.renderer.GlStateManager;
@ -377,6 +378,8 @@ public class WidgetTerminal extends Widget
boolean greyscale = !computer.isColour();
synchronized( terminal )
{
Palette palette = terminal.getPalette();
// Get the data from the terminal first
// Unfortunately we have to keep the lock for the whole of drawing, so the text doesn't change under us.
FixedWidthFontRenderer fontRenderer = (FixedWidthFontRenderer)ComputerCraft.getFixedWidthFontRenderer();
@ -393,11 +396,11 @@ public class WidgetTerminal extends Widget
TextBuffer emptyLine = new TextBuffer( ' ', tw );
if( m_topMargin > 0 )
{
fontRenderer.drawString( emptyLine, x, startY, terminal.getTextColourLine( 0 ), terminal.getBackgroundColourLine( 0 ), m_leftMargin, m_rightMargin, greyscale );
fontRenderer.drawString( emptyLine, x, startY, terminal.getTextColourLine( 0 ), terminal.getBackgroundColourLine( 0 ), m_leftMargin, m_rightMargin, greyscale, palette );
}
if( m_bottomMargin > 0 )
{
fontRenderer.drawString( emptyLine, x, startY + 2 * m_bottomMargin + ( th - 1 ) * FixedWidthFontRenderer.FONT_HEIGHT, terminal.getTextColourLine( th - 1 ), terminal.getBackgroundColourLine( th - 1 ), m_leftMargin, m_rightMargin, greyscale );
fontRenderer.drawString( emptyLine, x, startY + 2 * m_bottomMargin + ( th - 1 ) * FixedWidthFontRenderer.FONT_HEIGHT, terminal.getTextColourLine( th - 1 ), terminal.getBackgroundColourLine( th - 1 ), m_leftMargin, m_rightMargin, greyscale, palette );
}
// Draw lines
@ -406,7 +409,7 @@ public class WidgetTerminal extends Widget
TextBuffer text = terminal.getLine(line);
TextBuffer colour = terminal.getTextColourLine( line );
TextBuffer backgroundColour = terminal.getBackgroundColourLine( line );
fontRenderer.drawString( text, x, y, colour, backgroundColour, m_leftMargin, m_rightMargin, greyscale );
fontRenderer.drawString( text, x, y, colour, backgroundColour, m_leftMargin, m_rightMargin, greyscale, palette );
y += FixedWidthFontRenderer.FONT_HEIGHT;
}
@ -421,7 +424,8 @@ public class WidgetTerminal extends Widget
startY + m_topMargin + FixedWidthFontRenderer.FONT_HEIGHT * ty,
cursorColour, null,
0, 0,
greyscale
greyscale,
palette
);
}
}

View File

@ -14,6 +14,7 @@ import dan200.computercraft.shared.common.ClientTerminal;
import dan200.computercraft.shared.peripheral.monitor.TileMonitor;
import dan200.computercraft.shared.util.Colour;
import dan200.computercraft.shared.util.DirectionUtil;
import dan200.computercraft.shared.util.Palette;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.GlStateManager;
import net.minecraft.client.renderer.Tessellator;
@ -48,6 +49,8 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
return;
}
Palette palette = origin.getTerminal().getTerminal().getPalette();
// Ensure each monitor is rendered only once
long renderFrame = ComputerCraft.getRenderFrame();
if( origin.m_lastRenderFrame == renderFrame )
@ -144,9 +147,9 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
{
GlStateManager.scale( 1.0, marginSquash, 1.0 );
GlStateManager.translate( 0.0, -marginYSize / marginSquash, 0.0 );
fontRenderer.drawStringBackgroundPart( 0, 0, terminal.getBackgroundColourLine( 0 ), marginXSize, marginXSize, greyscale );
fontRenderer.drawStringBackgroundPart( 0, 0, terminal.getBackgroundColourLine( 0 ), marginXSize, marginXSize, greyscale, palette );
GlStateManager.translate( 0.0, ( marginYSize + height * FixedWidthFontRenderer.FONT_HEIGHT ) / marginSquash, 0.0 );
fontRenderer.drawStringBackgroundPart( 0, 0, terminal.getBackgroundColourLine( height - 1 ), marginXSize, marginXSize, greyscale );
fontRenderer.drawStringBackgroundPart( 0, 0, terminal.getBackgroundColourLine( height - 1 ), marginXSize, marginXSize, greyscale, palette );
}
finally
{
@ -160,7 +163,8 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
0, FixedWidthFontRenderer.FONT_HEIGHT * y,
terminal.getBackgroundColourLine( y ),
marginXSize, marginXSize,
greyscale
greyscale,
palette
);
}
}
@ -186,7 +190,8 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
0, FixedWidthFontRenderer.FONT_HEIGHT * y,
terminal.getLine( y ),
terminal.getTextColourLine( y ),
greyscale
greyscale,
palette
);
}
}
@ -216,7 +221,8 @@ public class TileEntityMonitorRenderer extends TileEntitySpecialRenderer<TileMon
FixedWidthFontRenderer.FONT_HEIGHT * cursorY,
cursorColour, null,
0, 0,
greyscale
greyscale,
palette
);
}
}

View File

@ -5,6 +5,7 @@
*/
package dan200.computercraft.core.terminal;
import dan200.computercraft.shared.util.Palette;
import net.minecraft.nbt.NBTTagCompound;
public class Terminal
@ -24,6 +25,8 @@ public class Terminal
private TextBuffer m_textColour[];
private TextBuffer m_backgroundColour[];
private Palette m_palette;
private boolean m_changed;
public Terminal( int width, int height )
@ -49,6 +52,8 @@ public class Terminal
m_cursorBlink = false;
m_changed = false;
m_palette = new Palette();
}
public void reset()
@ -60,6 +65,7 @@ public class Terminal
m_cursorBlink = false;
clear();
m_changed = true;
m_palette.resetColours();
}
public int getWidth() {
@ -178,6 +184,11 @@ public class Terminal
return m_cursorBackgroundColour;
}
public Palette getPalette()
{
return m_palette;
}
public void blit( String text, String textColour, String backgroundColour )
{
int x = m_cursorX;

View File

@ -0,0 +1,79 @@
package dan200.computercraft.shared.util;
public class Palette
{
private static class PaletteColour
{
private float m_r, m_g, m_b;
public PaletteColour(float r, float g, float b)
{
m_r = r;
m_g = g;
m_b = b;
}
@Override
public boolean equals(Object o)
{
if(this == o) return true;
if(o == null || getClass() != o.getClass()) return false;
PaletteColour that = (PaletteColour) o;
if(Float.compare( that.m_r, m_r ) != 0) return false;
if(Float.compare( that.m_g, m_g ) != 0) return false;
return Float.compare( that.m_b, m_b ) == 0;
}
@Override
public int hashCode()
{
int result = (m_r != +0.0f ? Float.floatToIntBits( m_r ) : 0);
result = 31 * result + (m_g != +0.0f ? Float.floatToIntBits( m_g ) : 0);
result = 31 * result + (m_b != +0.0f ? Float.floatToIntBits( m_b ) : 0);
return result;
}
}
private static final int PALETTE_SIZE = 16;
private final PaletteColour[] colours = new PaletteColour[ PALETTE_SIZE ];
public Palette()
{
// Get the default palette
resetColours();
}
public void setColour(int i, float r, float g, float b)
{
if( i >= 0 && i < colours.length )
{
colours[ i ] = new PaletteColour( r, g, b );
}
}
public void setColour(int i, Colour colour)
{
setColour( i, colour.getR(), colour.getG(), colour.getB() );
}
public float[] getColour( int i )
{
if( i >= 0 && i < colours.length )
{
PaletteColour c = colours[ i ];
return new float[] { c.m_r, c.m_g, c.m_b };
}
return null;
}
public void resetColours()
{
for(int i = 0; i < Colour.values().length; ++i)
{
Colour c = Colour.values()[ i ];
colours[i] = new PaletteColour( c.getR(), c.getG(), c.getB() );
}
}
}