unit TexFont;
interface
uses opengl,types3d;

procedure ChrBeg(Ch: Char; var R: TTexCoord);
procedure WantWriteNow(ID: GLuint);
procedure Txt(S: String; X,Y,SizX,SizY: GLfloat);


implementation

procedure ChrBeg(Ch: Char; var R: TTexCoord);
begin
 R.U:=-1;
 if UpCase(Ch) in ['A'..'Z'] then begin
  R.U:=1/26*(Ord(UpCase(Ch))-65);
  R.V:=0;
 end else begin
  R.V:=0.5;
  if Ch in ['1'..'9'] then R.U:=1/26*(Ord(Ch)-49);
  if Ch='0' then R.U:=1/26*9;
  if Ch='.' then R.U:=1/26*10;
  if Ch=',' then R.U:=1/26*11;
  if Ch=':' then R.U:=1/26*12;
  if Ch='!' then R.U:=1/26*13;
  if Ch='?' then R.U:=1/26*14;
  if Ch='-' then R.U:=1/26*15;
  if Ch='(' then R.U:=1/26*16;
  if Ch=')' then R.U:=1/26*17;
  if Ch=';' then R.U:=1/26*18;
  if (Ch='/') or (Ch='\') or (Ch='|')
            then R.U:=1/26*19;
  if Ch='_' then R.U:=1/26*20;
  if Ch='=' then R.U:=1/26*21;
  if Ch='+' then R.U:=1/26*22;
  if Ch='#' then R.U:=1/26*23;
  if Ch=' ' then R.U:=1/26*25;
 end;
 if R.U=-1 then R.U:=1/26*24;
end;

procedure WantWriteNow(ID: GLuint);
begin
 glEnable(GL_TEXTURE_2D);glBindTexture(GL_TEXTURE_2D,ID);
 glEnable(GL_BLEND);glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
end;

procedure Txt(S: String; X,Y,SizX,SizY: GLfloat);
var C: GLint; Co: TTexCoord;
begin
 glBegin(GL_QUADS);
   For C:= 1 to Length(S) do begin
     ChrBeg(S[C],Co);
     glTexCoord2f(   Co.U     ,Co.V      ); glVertex2f(X+ (C-1)*SizX,Y+ 0*SizY);
     glTexCoord2f(   Co.U+1/26,Co.V      ); glVertex2f(X+ (C  )*SizX,Y+ 0*SizY);
     glTexCoord2f(   Co.U+1/26,Co.V+0.49 ); glVertex2f(X+ (C  )*SizX,Y+ 1*SizY);
     glTexCoord2f(   Co.U     ,Co.V+0.49 ); glVertex2f(X+ (C-1)*SizX,Y+ 1*SizY);
   end;
 glEnd();
end;

end.
