📄 cfont.h
字号:
//////////////////////////////////////////////////////////////////
// Code by HiperBlaster, based on NeHe's
///////////////////////////////////////////////////////////////////
// TODO : *Brutal* Optimization and some nice effects
//
class CFont
{
public:
void Init(char *file,int PixelSep); //load the texture
void Display(char *Text,float scale,int x,int y,int set); //display
~CFont(); //free mem
private:
void LoadFontTex(char *filename);
GLuint FontTex; //font texture
GLuint base; //display list
}Standart;
//////////////////////////////////////////////////////////////////////
void CFont::Init(char *file,int PixelSep=10)
{
LoadFontTex(file); //load the font using alpha channel
float cx;
float cy;
base=glGenLists(256);
for (int loop=0; loop<256; loop++)
{
cx=float(loop%16)/16.0f;
cy=float(loop/16)/16.0f;
glNewList(base+loop,GL_COMPILE);
glBindTexture(GL_TEXTURE_2D, FontTex); //New | Normal Texture
glBegin(GL_QUADS); //New
glTexCoord2f(cx,1-cy-0.0625f); //New
glVertex2i(0,0); //New
glTexCoord2f(cx+0.0625f,1-cy-0.0625f); //New
glVertex2i(16,0); //New
glTexCoord2f(cx+0.0625f,1-cy); //New
glVertex2i(16,16); //New
glTexCoord2f(cx,1-cy); //New
glVertex2i(0,16); //New
glEnd(); //New
//glEnable(GL_DEPTH_TEST); //New
//glDisable(GL_BLEND); //New
glTranslated(PixelSep,0,0);
glEndList();
}
}
///////////////////////////////////////////////////////////////
CFont::~CFont()
{
glDeleteLists(base,256);
glDeleteTextures(1,&FontTex);
// glDeleteTextures(1,&FontMask);
}
////////////////////////////////////////////////////////////////
//GLvoid glPrint(GLint x, GLint y, char *string, int set) // Where The Printing Happens
void CFont::Display(char *string,float scale,int x,int y,int set)
{
if (set>1)
{
set=1;
}
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER,0.3f);
glDisable(GL_DEPTH_TEST); // Disables Depth Testing
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPushMatrix(); // Store The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
glOrtho(0,640,0,480,-100,100); // Set Up An Ortho Screen
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glPushMatrix(); // Store The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
glTranslated(x,y,0); // Position The Text (0,0 - Bottom Left)
glScalef(scale,scale,1);
glListBase(base-32+(128*set)); // Choose The Font Set (0 or 1)
glCallLists(strlen(string),GL_BYTE,string); // Write The Text To The Screen
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPopMatrix(); // Restore The Old Projection Matrix
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glPopMatrix(); // Restore The Old Projection Matrix
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDisable(GL_ALPHA_TEST);
}
/////////////////////////////////////////////////////////////////////
void CFont::LoadFontTex(char *filename)
{
FIBITMAP *Image;
Image = FreeImage_LoadBMP(filename);
FreeImage_ConvertTo24Bits(Image); //for safety
unsigned char *data;
data = FreeImage_GetBits(Image); //get a pointer to the data
////////////////////////////////////////////
//this is slow , should be asm :(
//convert BGR to RGB for compatibility
unsigned int a,b; //temp location
unsigned int loop=0;
while(loop!=(FreeImage_GetWidth(Image)*FreeImage_GetHeight(Image))*3)
{
a=data[loop]; //store B
b=data[loop+2]; //store R
data[loop]=b; //put R in the first byte
data[loop+2]=a; //and B in the last
loop+=3;
}
/////////////////////////////////////////////
//create the alpha channel
unsigned char *aTexture = (unsigned char*)malloc((FreeImage_GetWidth(Image)*FreeImage_GetHeight(Image))*4);
loop=0;
int aloop=0;
while(loop!=FreeImage_GetWidth(Image)*FreeImage_GetHeight(Image)*3)
{
aTexture[aloop]=data[loop];
aTexture[aloop+1]=data[loop+1];
aTexture[aloop+2]=data[loop+2];
//the comparation
if(aTexture[aloop]+aTexture[aloop+1]+aTexture[aloop+2]==0)aTexture[aloop+3]=0;
else aTexture[aloop+3]=255;
loop+=3;
aloop+=4;
}
/////////////////////////////////
//create the gl texture
glGenTextures(1,&FontTex);
glBindTexture(GL_TEXTURE_2D,FontTex);
//linear
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 4, FreeImage_GetWidth(Image) , FreeImage_GetHeight(Image), 0, GL_RGBA, GL_UNSIGNED_BYTE, aTexture);
free(aTexture);
FreeImage_Free(Image); //frees up the memory used, it's now on gl
//free(data);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -