tcr/java/src/game/renderer/Drawing.java
2025-03-19 21:54:09 +01:00

568 lines
17 KiB
Java

package game.renderer;
import game.Game;
import game.color.TextColor;
import game.gui.Font;
import game.gui.FontChar;
import game.log.Log;
import game.util.Util;
public abstract class Drawing {
public static class Vec2i {
public final int xpos;
public final int ypos;
private Vec2i(int x, int y) {
this.xpos = x;
this.ypos = y;
}
}
public static class Offset extends Vec2i {
public final int offset;
private Offset(int off, int x, int y) {
super(x, y);
this.offset = off;
}
}
// public static void drawTexturedModalRect(int tw, int th, int x, int y, int textureX, int textureY, int width, int height)
// {
// float xs = 1.0f / (float)tw; // 0.00390625F;
// float ys = 1.0f / (float)th; // 0.00390625F;
// RenderBuffer rb = Tessellator.getBuffer();
// rb.begin(7, DefaultVertexFormats.POSITION_TEX);
// rb.pos((double)(x + 0), (double)(y + height), 0.0).tex((double)((float)(textureX + 0) * xs), (double)((float)(textureY + height) * ys)).endVertex();
// rb.pos((double)(x + width), (double)(y + height), 0.0).tex((double)((float)(textureX + width) * xs), (double)((float)(textureY + height) * ys)).endVertex();
// rb.pos((double)(x + width), (double)(y + 0), 0.0).tex((double)((float)(textureX + width) * xs), (double)((float)(textureY + 0) * ys)).endVertex();
// rb.pos((double)(x + 0), (double)(y + 0), 0.0).tex((double)((float)(textureX + 0) * xs), (double)((float)(textureY + 0) * ys)).endVertex();
// Tessellator.draw();
// }
public static void txt_draw(int x, int y, int x1, int y1, int x2, int y2, int color, String str) {
GlState.enableTexture2D();
GlState.enableBlend();
GlState.disableAlpha();
GlState.tryBlendFuncSeparate(770, 771, 1, 0);
GlState.shadeModel(7425);
GlState.color(1.0f, 1.0f, 1.0f, 1.0f);
Font.bindTexture();
RenderBuffer rb = Tessellator.getBuffer();
rb.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
int h = Font.YGLYPH;
int tx, ty, u, v;
FontChar glyph;
char ch;
int ncolor = color;
for(int z = 0; z < str.length(); z++) {
ch = str.charAt(z);
if(ch == Log.CHR_NLN) {
x = x1;
y += h;
continue;
}
else if((ch >= Log.CHR_COLORS1 && ch <= Log.CHR_COLORE1) || (ch >= Log.CHR_COLORS2 && ch <= Log.CHR_COLORE2)) {
ncolor = TextColor.getColor(ch) | (color & 0xff000000);
continue;
}
else if(ch == Log.CHR_CRESET) {
ncolor = color;
continue;
}
if(ch >= 256)
ch = Log.CHR_UNK;
glyph = Font.SIZES[ch];
if(glyph.u == 0 && glyph.v != 0)
continue;
else if(glyph.u == 0)
glyph = Font.SIZES[Log.CHR_UNK];
u = glyph.u + 3 - glyph.s;
v = h;
tx = ((x + u) > x2) ? x1 : x;
ty = ((x + u) > x2) ? y + h : y;
x = tx;
y = ty;
if(x < x1 || y < y1 || x > x2 || y > y2) {
break;
}
putGlyph(rb, Font.XGLYPH * 16, Font.YGLYPH * 16, x, y, (ch & 0x0f) * Font.XGLYPH + glyph.s, ((ch & 0xf0) >> 4) * Font.YGLYPH, glyph.u, h, ncolor, 0.0);
x += u;
}
Tessellator.draw();
GlState.shadeModel(7424);
GlState.disableBlend();
GlState.enableAlpha();
GlState.enableTexture2D();
}
public static void txt_overlay(int start, int end, int x, int y, int x1, int y1, int x2, int y2, int back, String str) {
int h = Font.YGLYPH;
int tx, ty, u, v;
FontChar glyph;
char ch;
int pos = 0;
int lpos = 0;
// ShaderContext shd = Shader.TEXT.use();
// shd.setVec2("font_size", (float)font.xglyph, (float)font.yglyph);
// shd.setInt("flags", 0);
// shd.setColor("color", 0x00000000);
// shd.setColor("back", back);
// shd.setInt("ch_index", Log.CHR_SPC & 0xff);
// GlState.bindTexture(font.textures[Log.CHR_SPC >> 8]);
while(true) {
pos = lpos;
if(lpos >= str.length())
break;
ch = str.charAt(lpos++);
if(ch == Log.CHR_NLN) {
x = x1;
y += h;
continue;
}
else if(ch < Log.CHR_SPC) {
continue;
}
if(ch >= 256)
ch = Log.CHR_UNK;
glyph = Font.SIZES[ch];
if(glyph.u == 0 && glyph.v != 0)
continue;
else if(glyph.u == 0)
glyph = Font.SIZES[Log.CHR_UNK];
u = glyph.u + 3 - glyph.s;
v = h;
tx = ((x + u) > x2) ? x1 : x;
ty = ((x + u) > x2) ? y + h : y;
x = tx;
y = ty;
if(x < x1 || y < y1 || x > x2 || y > y2) {
break;
}
if(pos >= start && pos < end) {
// shd.setVec2("offset", (float)x, (float)y);
// shd.setVec2("size", (float)u, (float)v);
// shd.setVec4("glyph", (float)glyph.s, 0.0f, (float)glyph.u, (float)h);
// shd.draw();
drawRect(x, y, u, v, back);
}
x += u;
}
// shd.discard();
}
public static Vec2i txt_size(int x, int y, int x1, int y1, int x2, int y2, String str) {
int h = Font.YGLYPH;
int ix = x;
int iy = y;
int tx, ty, u, v;
FontChar glyph;
char ch;
for(int z = 0; z < str.length(); z++) {
ch = str.charAt(z);
if(ch == Log.CHR_NLN) {
x = x1;
y += h;
continue;
}
else if(ch < Log.CHR_SPC) {
continue;
}
if(ch >= 256)
ch = Log.CHR_UNK;
glyph = Font.SIZES[ch];
if(glyph.u == 0 && glyph.v != 0)
continue;
else if(glyph.u == 0)
glyph = Font.SIZES[Log.CHR_UNK];
u = glyph.u + 3 - glyph.s;
v = h;
tx = ((x + u) > x2) ? x1 : x;
ty = ((x + u) > x2) ? y + h : y;
x = tx;
y = ty;
if(x < x1 || y < y1 || x > x2 || y > y2) {
break;
}
x += u;
}
return new Vec2i(x - ix, y - iy);
}
public static Vec2i txt_coord(int offset, int x, int y, int x1, int y1, int x2, int y2, String str) {
int h = Font.YGLYPH;
int tx, ty, u, v;
FontChar glyph;
char ch;
int pos = 0;
int lpos = 0;
while(true) {
pos = lpos;
if(lpos >= str.length())
break;
ch = str.charAt(lpos++);
if(pos == offset) {
return new Vec2i(x, y);
}
if(ch == Log.CHR_NLN) {
x = x1;
y += h;
continue;
}
else if(ch < Log.CHR_SPC) {
continue;
}
if(ch >= 256)
ch = Log.CHR_UNK;
glyph = Font.SIZES[ch];
if(glyph.u == 0 && glyph.v != 0)
continue;
else if(glyph.u == 0)
glyph = Font.SIZES[Log.CHR_UNK];
u = glyph.u + 3 - glyph.s;
v = h;
tx = ((x + u) > x2) ? x1 : x;
ty = ((x + u) > x2) ? y + h : y;
x = tx;
y = ty;
if(x < x1 || y < y1 || x > x2 || y > y2) {
break;
}
x += u;
}
return new Vec2i(x, y);
}
public static Offset txt_offset(int ox, int oy, int x, int y, int x1, int y1, int x2, int y2, String str) {
int h = Font.YGLYPH;
int tx, ty, u, v;
FontChar glyph;
char ch;
int pos = 0;
int lpos = 0;
if(oy < y) {
return null;
}
ox = ox < x1 ? x1 : ox;
while(true) {
pos = lpos;
if(lpos >= str.length())
break;
ch = str.charAt(lpos++);
if(ch == Log.CHR_NLN) {
if(ox >= x && oy >= y && oy < (y + h)) {
return new Offset(pos, x, y);
}
x = x1;
y += h;
continue;
}
else if(ch < Log.CHR_SPC) {
continue;
}
if(ch >= 256)
ch = Log.CHR_UNK;
glyph = Font.SIZES[ch];
if(glyph.u == 0 && glyph.v != 0)
continue;
else if(glyph.u == 0)
glyph = Font.SIZES[Log.CHR_UNK];
u = glyph.u + 3 - glyph.s;
v = h;
tx = ((x + u) > x2) ? x1 : x;
ty = ((x + u) > x2) ? y + h : y;
if(ty > y && ox >= x && oy >= y && oy < (y + h)) {
return new Offset(pos, tx, ty);
}
x = tx;
y = ty;
if(x < x1 || y < y1 || x > x2 || y > y2) {
pos = lpos;
break;
}
if(ox >= x && oy >= y && ox < (x + u) && oy < (y + h)) {
return new Offset(pos, x, y);
}
x += u;
}
return new Offset(pos, x, y);
}
private static void putGlyph(RenderBuffer rb, int tw, int th, int x, int y, int textureX, int textureY, int width, int height, int color, double depth)
{
float xs = 1.0f / (float)tw;
float ys = 1.0f / (float)th;
rb.pos((double)(x + 0), (double)(y + height), depth).tex((double)((float)(textureX + 0) * xs), (double)((float)(textureY + height) * ys)).color(color).endVertex();
rb.pos((double)(x + width), (double)(y + height), depth).tex((double)((float)(textureX + width) * xs), (double)((float)(textureY + height) * ys)).color(color).endVertex();
rb.pos((double)(x + width), (double)(y + 0), depth).tex((double)((float)(textureX + width) * xs), (double)((float)(textureY + 0) * ys)).color(color).endVertex();
rb.pos((double)(x + 0), (double)(y + 0), depth).tex((double)((float)(textureX + 0) * xs), (double)((float)(textureY + 0) * ys)).color(color).endVertex();
}
public static void drawText(String str, int x, int y, int color, boolean shadow) {
GlState.enableTexture2D();
GlState.enableBlend();
GlState.disableAlpha();
GlState.tryBlendFuncSeparate(770, 771, 1, 0);
GlState.shadeModel(7425);
GlState.color(1.0f, 1.0f, 1.0f, 1.0f);
Font.bindTexture();
RenderBuffer rb = Tessellator.getBuffer();
rb.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
int ox = x;
int ncolor = color;
FontChar glyph;
char ch;
if(shadow) {
int oy = y;
int scolor = Util.mixColor(color, color & 0xff000000);
scolor = Util.mixColor(scolor, scolor & 0xff000000);
int bcolor = scolor;
for(int z = 0; z < str.length(); z++) {
ch = str.charAt(z);
if(ch == Log.CHR_NLN) {
x = ox;
y += Font.YGLYPH;
continue;
}
else if((ch >= Log.CHR_COLORS1 && ch <= Log.CHR_COLORE1) || (ch >= Log.CHR_COLORS2 && ch <= Log.CHR_COLORE2)) {
bcolor = TextColor.getShadow(ch) | (scolor & 0xff000000);
continue;
}
else if(ch == Log.CHR_CRESET) {
bcolor = scolor;
continue;
}
if(ch >= 256)
ch = Log.CHR_UNK;
glyph = Font.SIZES[ch];
if(glyph.u == 0 && glyph.v != 0)
continue;
else if(glyph.u == 0)
glyph = Font.SIZES[Log.CHR_UNK];
putGlyph(rb, Font.XGLYPH * 16, Font.YGLYPH * 16, x + 1, y + 1, (ch & 0x0f) * Font.XGLYPH + glyph.s, ((ch & 0xf0) >> 4) * Font.YGLYPH, glyph.u, Font.YGLYPH, bcolor, -0.01);
x += glyph.u + 3 - glyph.s;
}
x = ox;
y = oy;
}
for(int z = 0; z < str.length(); z++) {
ch = str.charAt(z);
if(ch == Log.CHR_NLN) {
x = ox;
y += Font.YGLYPH;
continue;
}
else if((ch >= Log.CHR_COLORS1 && ch <= Log.CHR_COLORE1) || (ch >= Log.CHR_COLORS2 && ch <= Log.CHR_COLORE2)) {
ncolor = TextColor.getColor(ch) | (color & 0xff000000);
continue;
}
else if(ch == Log.CHR_CRESET) {
ncolor = color;
continue;
}
if(ch >= 256)
ch = Log.CHR_UNK;
glyph = Font.SIZES[ch];
if(glyph.u == 0 && glyph.v != 0)
continue;
else if(glyph.u == 0)
glyph = Font.SIZES[Log.CHR_UNK];
putGlyph(rb, Font.XGLYPH * 16, Font.YGLYPH * 16, x, y, (ch & 0x0f) * Font.XGLYPH + glyph.s, ((ch & 0xf0) >> 4) * Font.YGLYPH, glyph.u, Font.YGLYPH, ncolor, -0.02);
x += glyph.u + 3 - glyph.s;
}
Tessellator.draw();
GlState.shadeModel(7424);
GlState.disableBlend();
GlState.enableAlpha();
GlState.enableTexture2D();
}
public static Vec2i getSize(String str) {
int x = 0;
int y = 0;
int ix = 0;
FontChar glyph;
char ch;
for(int z = 0; z < str.length(); z++) {
ch = str.charAt(z);
if(ch == Log.CHR_NLN) {
ix = x > ix ? x : ix;
x = 0;
y += Font.YGLYPH;
continue;
}
else if(ch < Log.CHR_SPC) {
continue;
}
if(ch >= 256)
ch = Log.CHR_UNK;
glyph = Font.SIZES[ch];
if(glyph.u == 0 && glyph.v != 0)
continue;
else if(glyph.u == 0)
glyph = Font.SIZES[Log.CHR_UNK];
x += glyph.u + 3 - glyph.s;
ix = x > ix ? x : ix;
}
return new Vec2i(ix - 3, y + Font.YGLYPH);
}
public static void drawGradient(int x, int y, int w, int h, int ctop, int cbottom)
{
GlState.disableTexture2D();
GlState.enableBlend();
GlState.disableAlpha();
GlState.tryBlendFuncSeparate(770, 771, 1, 0);
GlState.shadeModel(7425);
RenderBuffer buf = Tessellator.getBuffer();
buf.begin(7, DefaultVertexFormats.POSITION_COLOR);
buf.pos((double)(x + w), (double)y, 0.0).color(ctop).endVertex();
buf.pos((double)x, (double)y, 0.0).color(ctop).endVertex();
buf.pos((double)x, (double)(y + h), 0.0).color(cbottom).endVertex();
buf.pos((double)(x + w), (double)(y + h), 0.0).color(cbottom).endVertex();
Tessellator.draw();
GlState.shadeModel(7424);
GlState.disableBlend();
GlState.enableAlpha();
GlState.enableTexture2D();
}
public static void draw4Gradient(int x, int y, int w, int h, int lopleft, int topright, int btmleft, int btmright)
{
GlState.disableTexture2D();
GlState.enableBlend();
GlState.disableAlpha();
GlState.tryBlendFuncSeparate(770, 771, 1, 0);
GlState.shadeModel(7425);
RenderBuffer buf = Tessellator.getBuffer();
buf.begin(7, DefaultVertexFormats.POSITION_COLOR);
buf.pos((double)(x + w), (double)y, 0.0).color(topright).endVertex();
buf.pos((double)x, (double)y, 0.0).color(lopleft).endVertex();
buf.pos((double)x, (double)(y + h), 0.0).color(btmleft).endVertex();
buf.pos((double)(x + w), (double)(y + h), 0.0).color(btmright).endVertex();
Tessellator.draw();
GlState.shadeModel(7424);
GlState.disableBlend();
GlState.enableAlpha();
GlState.enableTexture2D();
}
public static void drawRect(int x, int y, int w, int h, int color)
{
RenderBuffer rb = Tessellator.getBuffer();
GlState.enableBlend();
GlState.disableAlpha();
GlState.disableTexture2D();
GlState.tryBlendFuncSeparate(770, 771, 1, 0);
GlState.shadeModel(7425);
GlState.color(color);
rb.begin(7, DefaultVertexFormats.POSITION);
rb.pos((double)x, (double)(y + h), 0.0D).endVertex();
rb.pos((double)(x + w), (double)(y + h), 0.0D).endVertex();
rb.pos((double)(x + w), (double)y, 0.0D).endVertex();
rb.pos((double)x, (double)y, 0.0D).endVertex();
Tessellator.draw();
GlState.shadeModel(7424);
GlState.enableTexture2D();
GlState.disableBlend();
GlState.enableAlpha();
GlState.color(1.0f, 1.0f, 1.0f, 1.0f);
}
public static void drawBordered(int x, int y, int w, int h, int color, int border) {
drawRect(x, y, w, h, border);
drawRect(x + 1, y + 1, w - 2, h - 2, color);
}
public static void drawRect2GradBorder(int x, int y, int w, int h, int color, int border, int lopleft, int topright, int btmleft, int btmright) {
drawRect(x, y, w, h, border);
draw4Gradient(x + 1, y + 1, w - 2, h - 2, lopleft, topright, btmleft, btmright);
drawRect(x + 2, y + 2, w - 4, h - 4, color);
}
public static void drawGradient2GradBorder(int x, int y, int w, int h, int top, int bottom, int border, int lopleft, int topright, int btmleft, int btmright) {
drawRect(x, y, w, h, border);
draw4Gradient(x + 1, y + 1, w - 2, h - 2, lopleft, topright, btmleft, btmright);
drawGradient(x + 2, y + 2, w - 4, h - 4, top, bottom);
}
public static void drawGradient2Border(int x, int y, int w, int h, int top, int bottom, int b_top, int b_bottom) {
drawGradient(x, y, w, h, b_top, b_bottom);
drawGradient(x + 1, y + 1, w - 2, h - 2, top, bottom);
}
public static int getWidth(String str) {
return getSize(str).xpos;
}
public static int getBoxWidth(String str) {
return getSize(str).xpos + 4;
}
public static void drawTextbox(String str, int x, int y, int back) {
Vec2i size = getSize(str);
drawRect(x, y, size.xpos + 4, size.ypos, back);
drawText(str, x + 2, y, 0xffffffff);
}
public static void drawTextboxRight(String str, int x, int y, int back) {
drawTextbox(str, x - getBoxWidth(str), y, back);
}
public static void drawTextboxCentered(String str, int x, int y, int back) {
drawTextbox(str, x - getBoxWidth(str) / 2, y, back);
}
public static void drawText(String str, int x, int y, int color) {
drawText(str, x, y, color, true);
}
public static void drawTextCenteredN(String str, int x, int y, int color) {
Vec2i size = getSize(str);
drawText(str, x - size.xpos / 2, y, color, false);
}
public static void drawTextCentered(String str, int x, int y, int color) {
Vec2i size = getSize(str);
drawText(str, x - size.xpos / 2, y, color);
}
public static void drawTextRight(String str, int x, int y, int color) {
Vec2i size = getSize(str);
drawText(str, x - size.xpos, y, color);
}
public static void drawTextUpward(String str, int x, int y, int color) {
Vec2i size = getSize(str);
drawText(str, x - size.xpos / 2, y - size.ypos, color);
}
public static void drawScaledBackground(Game gm, String texture) {
drawScaledBackground(gm, texture, 0, 0, gm.fb_x, gm.fb_y);
}
public static void drawScaledBackground(Game gm, String texture, double x, double y, double width, double height) {
GlState.enableTexture2D();
GlState.disableLighting();
GlState.disableFog();
RenderBuffer buf = Tessellator.getBuffer();
gm.getTextureManager().bindTexture(texture);
GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
double scale = 32.0;
buf.begin(7, DefaultVertexFormats.POSITION_TEX_COLOR);
buf.pos(x, y + height, 0.0D).tex(0.0D, height / scale)
.color(64, 64, 64, 255).endVertex();
buf.pos(x + width, y + height, 0.0D)
.tex(width / scale, height / scale)
.color(64, 64, 64, 255).endVertex();
buf.pos(x + width, y, 0.0D).tex(width / scale, 0.0)
.color(64, 64, 64, 255).endVertex();
buf.pos(x, y, 0.0D).tex(0.0D, 0.0)
.color(64, 64, 64, 255).endVertex();
Tessellator.draw();
GlState.disableTexture2D();
}
}