unit FontUnit;

interface

Uses
  OpenGL,
  MyUnit;

var
  //global
  FontTex:TextureImage;
  
procedure BuildFont;
procedure glPrint(x,y: GLint;text: string);
procedure DeleteFont;

implementation

Var
  //local
  Font:Cardinal;

procedure BuildFont;
var
  cx, cy,Xpart,Ypart: GLfloat;
  x,y,count:integer;
begin
  Font := glGenLists(128);
  glCallList(FontTex.TexId);
  count:=0;
  Xpart:=1/16;
  Ypart:=1/8;
  for y:=0 to 7 do
  for x:=0 to 15 do
    begin
    cy:=Ypart*y;
    cx:=Xpart*x;
    glNewList(Font + count,GL_COMPILE);
    count:=count+1;
      glBegin(GL_QUADS);
        glTexCoord2f(cx,1-cy-ypart);glVertex2i(0,0);
        glTexCoord2f(cx+Xpart ,1-cy-ypart);glVertex2i(16,0);
        glTexCoord2f(cx+xpart,1-cy);glVertex2i(16,16);
        glTexCoord2f(cx,1-cy);glVertex2i(0,16);
      glEnd;
      glTranslated(12,0,0);
    glEndList;
    end;
end;

{Procedure InitFont;
begin
  Buildfont;
end;}

procedure DeleteFont;
begin
  glDeleteLists(Font,128);
end;

procedure glPrint(x,y: GLint;text: string);
begin
  if text = '' then exit;
  glPushAttrib(GL_COLOR_BUFFER_BIT);
  glColor3f(1,1,1);
  glBlendFunc(GL_SRC_COLOR,GL_ONE);
  glCallList(FontTex.TexID);
  glDisable(GL_DEPTH_TEST);
  glEnable(GL_TEXTURE_2D);
  glMatrixMode(GL_PROJECTION);
  glPushMatrix;
  glLoadIdentity;
  glOrtho(0,WndWidth,WndHeight,0,-1,1);
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix;
  glLoadIdentity;
  glTranslated(x,y,0);
  glListBase(Font-32);
  glScalef(1,-1,1);
  glCallLists(length(text),GL_BYTE,Pchar(text));
  glMatrixMode(GL_PROJECTION);
  glPopMatrix;
  glMatrixMode(GL_MODELVIEW);
  glPopMatrix;
  glDisable(GL_TEXTURE_2D);
  glEnable(GL_DEPTH_TEST);
  glPopAttrib;
end;

end.
