#include "Font.h" Font::Font() { texID=0; srcBlend = GL_SRC_ALPHA; dstBlend = GL_ONE_MINUS_SRC_ALPHA; scale.x = 1; scale.y = 1; TextColor[0]=1; TextColor[1]=1; TextColor[2]=1; bShadow = true; mAlign = ALIGN_CENTER; sine =0; bSineWave = false; } Font::~Font() { if(texID) glDeleteTextures(1,&texID); } bool Font::LoadFont(char* name, char* shadowname) { if(!GenerateGlyphs(name)) return false; GenerateCoords(); if(!UTIL_GL::Image::LoadImage(shadowname)) return false; UTIL_GL::TextureFilter(GL_LINEAR,GL_LINEAR); UTIL_GL::TextureWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); return true; } bool Font::GenerateGlyphs(char* name) { ILuint ImageName; ilOriginFunc(IL_ORIGIN_UPPER_LEFT); ilGenImages(1, &ImageName); ilBindImage(ImageName); if(!ilLoadImage(name)) { gLog.OutPut("Failed to load fontset: "); gLog.OutPut(name); gLog.OutPut("!\n"); ilDeleteImages(1, &ImageName); return false; } gLog.OutPut("Image Loaded:"); gLog.OutPut(name); gLog.OutPut(".\n"); unsigned int width=ilGetInteger(IL_IMAGE_WIDTH), height=ilGetInteger(IL_IMAGE_HEIGHT); unsigned char* data = ilGetData(); GenerateOffsets(data,width,height); glGenTextures(1,&texID); UTIL_GL::BindTexture(GL_TEXTURE_2D,texID); UTIL_GL::TextureFilter(GL_LINEAR,GL_LINEAR); UTIL_GL::TextureWrap(GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,width,height,0,GL_RGBA,GL_UNSIGNED_BYTE,data); ilDeleteImages(1, &ImageName); return true; } void Font::GenerateOffsets(unsigned char* data, int w, int h) { int x,y,pixcount,ox,oy; int width = w/16; int height = h/16; for(ox=0;ox0) { foundpixel = true; break; } } if(foundpixel) pixcount++; } mGlyphs[(ox/width)+((oy/height)*16)].width = pixcount; } } } void Font::GenerateCoords() { float u=0,v=0; int x,y; for(x=0;x<16;x++) { v=0; for(y=0;y<16;y++) { mGlyphs[x+(y*16)].tU = u; mGlyphs[x+(y*16)].tV = v; v+=(1.0f/16.0f); } u+=(1.0f/16.0f); } } void Font::SetBlendMode(unsigned int src, unsigned int dst) { srcBlend = src; dstBlend = dst; } void Font::SetScale(float width, float height) { scale.x = width; scale.y = height; } void Font::EnableShadow() { bShadow = true; } void Font::DisableShadow() { bShadow = false; } void Font::SetColor(float r, float g, float b, float a) { TextColor[0]=r; TextColor[1]=g; TextColor[2]=b; TextColor[3]=a; } void Font::SetAlignment(int align) { mAlign = align; } float Font::GetStringLength(char* txt) { float length=0; int l = (int)strlen(txt),n; for(n=0;n