package client.renderer;

import org.lwjgl.opengl.GL11;

import client.Client;
import client.gui.Font;
import client.gui.FontChar;
import common.log.Log;
import common.util.Color;
import common.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 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(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
        GlState.shadeModel(GL11.GL_SMOOTH);
        GlState.color(1.0f, 1.0f, 1.0f, 1.0f);
		Font.bindTexture();
        RenderBuffer rb = Tessellator.getBuffer();
        rb.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX_COLOR);
		int h = Font.HEIGHT;
		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 = Color.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.WIDTH * 16, Font.HEIGHT * 16, x, y, (ch & 0x0f) * Font.WIDTH + glyph.s, ((ch & 0xf0) >> 4) * Font.HEIGHT, glyph.u, h, ncolor, 0.0);
			x += u;
		}
        Tessellator.draw();
        GlState.shadeModel(GL11.GL_FLAT);
        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.HEIGHT;
		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.HEIGHT;
		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.HEIGHT;
		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.HEIGHT;
		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(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
        GlState.shadeModel(GL11.GL_SMOOTH);
        GlState.color(1.0f, 1.0f, 1.0f, 1.0f);
		Font.bindTexture();
        RenderBuffer rb = Tessellator.getBuffer();
        rb.begin(GL11.GL_QUADS, 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.HEIGHT;
					continue;
				}
				else if((ch >= Log.CHR_COLORS1 && ch <= Log.CHR_COLORE1) || (ch >= Log.CHR_COLORS2 && ch <= Log.CHR_COLORE2)) {
					bcolor = Color.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.WIDTH * 16, Font.HEIGHT * 16, x + 1, y + 1, (ch & 0x0f) * Font.WIDTH + glyph.s, ((ch & 0xf0) >> 4) * Font.HEIGHT, glyph.u, Font.HEIGHT, 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.HEIGHT;
				continue;
			}
			else if((ch >= Log.CHR_COLORS1 && ch <= Log.CHR_COLORE1) || (ch >= Log.CHR_COLORS2 && ch <= Log.CHR_COLORE2)) {
				ncolor = Color.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.WIDTH * 16, Font.HEIGHT * 16, x, y, (ch & 0x0f) * Font.WIDTH + glyph.s, ((ch & 0xf0) >> 4) * Font.HEIGHT, glyph.u, Font.HEIGHT, ncolor, -0.02);
			x += glyph.u + 3 - glyph.s;
		}
        Tessellator.draw();
        GlState.shadeModel(GL11.GL_FLAT);
        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.HEIGHT;
				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.HEIGHT);
	}
	
	
	public static void drawGradient(int x, int y, int w, int h, int top, int bottom)
    {
        GlState.disableTexture2D();
        GlState.enableBlend();
        GlState.disableAlpha();
        GlState.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
        GlState.shadeModel(GL11.GL_SMOOTH);
        RenderBuffer buf = Tessellator.getBuffer();
        buf.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_COLOR);
        buf.pos((double)(x + w), (double)y, 0.0).color(top).endVertex();
        buf.pos((double)x, (double)y, 0.0).color(top).endVertex();
        buf.pos((double)x, (double)(y + h), 0.0).color(bottom).endVertex();
        buf.pos((double)(x + w), (double)(y + h), 0.0).color(bottom).endVertex();
        Tessellator.draw();
        GlState.shadeModel(GL11.GL_FLAT);
        GlState.disableBlend();
        GlState.enableAlpha();
        GlState.enableTexture2D();
    }
	
//	public static void draw4Gradient(int x, int y, int w, int h, int topleft, int topright, int btmleft, int btmright)
//    {
//        GlState.disableTexture2D();
//        GlState.enableBlend();
//        GlState.disableAlpha();
//        GlState.tryBlendFuncSeparate(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
//        GlState.shadeModel(GL11.GL_SMOOTH);
//        RenderBuffer buf = Tessellator.getBuffer();
//        buf.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_COLOR);
//        buf.pos((double)(x + w), (double)y, 0.0).color(topright).endVertex();
//        buf.pos((double)x, (double)y, 0.0).color(topleft).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(GL11.GL_FLAT);
//        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(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA, GL11.GL_ONE, GL11.GL_ZERO);
        GlState.shadeModel(GL11.GL_SMOOTH);
        GlState.color(color);
        rb.begin(GL11.GL_QUADS, 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(GL11.GL_FLAT);
        GlState.enableTexture2D();
        GlState.disableBlend();
        GlState.enableAlpha();
        GlState.color(1.0f, 1.0f, 1.0f, 1.0f);
    }
    
	public static void drawRectBorder(int x, int y, int w, int h, int color, int border, int topleft, int btmright) {
		int corner = Util.mixColor(topleft, btmright);
		drawRect(x, y, w, h, border);
		drawRect(x + 1, y + 1, w - 3, 1, topleft);
		drawRect(x + 1, y + 2, 1, h - 3, topleft);
		drawRect(x + w - 2, y + 1, 1, 1, corner);
		drawRect(x + 1, y + h - 2, 1, 1, corner);
		drawRect(x + w - 2, y + 2, 1, h - 3, btmright);
		drawRect(x + 2, y + h - 2, w - 4, 1, btmright);
		drawRect(x + 2, y + 2, w - 4, h - 4, color);
	}
	
	public static void drawGradientBorder(int x, int y, int w, int h, int top, int bottom, int border, int topleft, int btmright) {
		int corner = Util.mixColor(topleft, btmright);
		drawRect(x, y, w, h, border);
		drawRect(x + 1, y + 1, w - 3, 1, topleft);
		drawRect(x + 1, y + 2, 1, h - 3, topleft);
		drawRect(x + w - 2, y + 1, 1, 1, corner);
		drawRect(x + 1, y + h - 2, 1, 1, corner);
		drawRect(x + w - 2, y + 2, 1, h - 3, btmright);
		drawRect(x + 2, y + h - 2, w - 4, 1, btmright);
		drawGradient(x + 2, y + 2, w - 4, h - 4, top, bottom);
	}
	
	public static void drawRect(int x, int y, int w, int h, int color, int topleft, int btmright) {
		int corner = Util.mixColor(topleft, btmright);
		drawRect(x, y, w - 1, 1, topleft);
		drawRect(x, y + 1, 1, h - 1, topleft);
		drawRect(x + w - 1, y, 1, 1, corner);
		drawRect(x, y + h - 1, 1, 1, corner);
		drawRect(x + w - 1, y + 1, 1, h - 1, btmright);
		drawRect(x + 1, y + h - 1, w - 2, 1, btmright);
		drawRect(x + 1, y + 1, w - 2, h - 2, color);
	}
	
	public static void drawDoubleRect(int x, int y, int w, int h, int color, int topleft, int btmright) {
		int corner = Util.mixColor(topleft, btmright);
		drawRect(x, y, w - 2, 2, topleft);
		drawRect(x, y + 2, 2, h - 2, topleft);
		drawRect(x + w - 2, y, 2, 2, corner);
		drawRect(x, y + h - 2, 2, 2, corner);
		drawRect(x + w - 2, y + 2, 2, h - 2, btmright);
		drawRect(x + 2, y + h - 2, w - 4, 2, btmright);
		drawRect(x + 2, y + 2, w - 4, h - 4, color);
	}
	
	public static void drawGradient(int x, int y, int w, int h, int top, int bottom, int topleft, int btmright) {
		int corner = Util.mixColor(topleft, btmright);
		drawRect(x, y, w - 1, 1, topleft);
		drawRect(x, y + 1, 1, h - 1, topleft);
		drawRect(x + w - 1, y, 1, 1, corner);
		drawRect(x, y + h - 1, 1, 1, corner);
		drawRect(x + w - 1, y + 1, 1, h - 1, btmright);
		drawRect(x + 1, y + h - 1, w - 2, 1, btmright);
		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 + 2, back);
		drawText(str, x + 2, y + 1, 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 drawScaled(Client gm, String texture) {
		drawScaled(gm, texture, 0, 0, gm.fbX, gm.fbY, 0xffffffff);
	}

	public static void drawScaled(Client gm, String texture, int x, int y, int width, int height, int color) {
		GlState.enableTexture2D();
		GlState.disableLighting();
		GlState.disableFog();
		RenderBuffer buf = Tessellator.getBuffer();
		gm.getTextureManager().bindTexture(texture);
		GlState.color(color);
		double scale = 32.0;
		buf.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
		buf.pos((double)x, (double)y + (double)height, 0.0D).tex(0.0D, (double)height / scale).endVertex();
		buf.pos((double)x + (double)width, (double)y + (double)height, 0.0D).tex((double)width / scale, (double)height / scale).endVertex();
		buf.pos((double)x + (double)width, (double)y, 0.0D).tex((double)width / scale, 0.0).endVertex();
		buf.pos((double)x, (double)y, 0.0D).tex(0.0D, 0.0).endVertex();
		Tessellator.draw();
		GlState.disableTexture2D();
		GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
	}
    
  public static void drawTexturedRect(Client gm, String texture, int texWidth, int texHeight, int x, int y, int u, int v, int width, int height)
  {
		GlState.enableTexture2D();
//		GlState.disableFog();
		RenderBuffer buf = Tessellator.getBuffer();
		gm.getTextureManager().bindTexture(texture);
//		GlState.color(color);
	      float xs = 1.0f / (float)texWidth; // 0.00390625F;
	      float ys = 1.0f / (float)texHeight; // 0.00390625F;
	      buf.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
	      buf.pos((double)(x + 0), (double)(y + height), 0.0).tex((double)((float)(u + 0) * xs), (double)((float)(v + height) * ys)).endVertex();
	      buf.pos((double)(x + width), (double)(y + height), 0.0).tex((double)((float)(u + width) * xs), (double)((float)(v + height) * ys)).endVertex();
	      buf.pos((double)(x + width), (double)(y + 0), 0.0).tex((double)((float)(u + width) * xs), (double)((float)(v + 0) * ys)).endVertex();
	      buf.pos((double)(x + 0), (double)(y + 0), 0.0).tex((double)((float)(u + 0) * xs), (double)((float)(v + 0) * ys)).endVertex();
//		double scale = 32.0;
//		buf.begin(GL11.GL_QUADS, DefaultVertexFormats.POSITION_TEX);
//		buf.pos((double)x, (double)y + (double)height, 0.0D).tex(0.0D, (double)height / scale).endVertex();
//		buf.pos((double)x + (double)width, (double)y + (double)height, 0.0D).tex((double)width / scale, (double)height / scale).endVertex();
//		buf.pos((double)x + (double)width, (double)y, 0.0D).tex((double)width / scale, 0.0).endVertex();
//		buf.pos((double)x, (double)y, 0.0D).tex(0.0D, 0.0).endVertex();
		Tessellator.draw();
//		GlState.disableTexture2D();
		GlState.color(1.0F, 1.0F, 1.0F, 1.0F);
  }
}
