Font Unit

unit FontUnit;

interface

uses
  sdl,
  sdlutils,

  GraphicsUnit;

const
  CharSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?.,:;"' + #39 + #92 + '/|+-=_@#$%^&*()' + #123 + #125 + '[]<>';

type
  TFont = class(TObject)
    FontData: PSDL_Surface;
    FontColor: Cardinal;
    CharWidth, CharHeight: Integer;
    
    constructor LoadFromFile(Width, Height, Color: Cardinal; FileName: String);
    destructor Free;
    procedure DrawText(Dest: PSDL_Surface; X, Y: Integer; Text: String);
    function GetTextWidth(Text: String): Cardinal;
  end;


implementation


function GetCharNum(ch: Char): Integer;
begin
     Result := -1;
     if (ch < #32) or
        (ch = #96) or
        (ch > #125) then
        Exit;

     case (ch) of
       'A' : Result := 1;
       'B' : Result := 2;
       'C' : Result := 3;
       'D' : Result := 4;
       'E' : Result := 5;
       'F' : Result := 6;
       'G' : Result := 7;
       'H' : Result := 8;
       'I' : Result := 9;
       'J' : Result := 10;
       'K' : Result := 11;
       'L' : Result := 12;
       'M' : Result := 13;
       'N' : Result := 14;
       'O' : Result := 15;
       'P' : Result := 16;
       'Q' : Result := 17;
       'R' : Result := 18;
       'S' : Result := 19;
       'T' : Result := 20;
       'U' : Result := 21;
       'V' : Result := 22;
       'W' : Result := 23;
       'X' : Result := 24;
       'Y' : Result := 25;
       'Z' : Result := 26;
       'a' : Result := 27;
       'b' : Result := 28;
       'c' : Result := 29;
       'd' : Result := 30;
       'e' : Result := 31;
       'f' : Result := 32;
       'g' : Result := 33;
       'h' : Result := 34;
       'i' : Result := 35;
       'j' : Result := 36;
       'k' : Result := 37;
       'l' : Result := 38;
       'm' : Result := 39;
       'n' : Result := 40;
       'o' : Result := 41;
       'p' : Result := 42;
       'q' : Result := 43;
       'r' : Result := 44;
       's' : Result := 45;
       't' : Result := 46;
       'u' : Result := 47;
       'v' : Result := 48;
       'w' : Result := 49;
       'x' : Result := 50;
       'y' : Result := 51;
       'z' : Result := 52;
       '0' : Result := 53;
       '1' : Result := 54;
       '2' : Result := 55;
       '3' : Result := 56;
       '4' : Result := 57;
       '5' : Result := 58;
       '6' : Result := 59;
       '7' : Result := 60;
       '8' : Result := 61;
       '9' : Result := 62;
       '!' : Result := 63;
       '?' : Result := 64;
       '.' : Result := 65;
       ',' : Result := 66;
       ':' : Result := 67;
       ';' : Result := 68;
       '"' : Result := 69;
       #39 : Result := 70;
       #92 : Result := 71;
       '/' : Result := 72;
       '|' : Result := 73;
       '+' : Result := 74;
       '-' : Result := 75;
       '=' : Result := 76;
       '_' : Result := 77;
       '@' : Result := 78;
       '#' : Result := 79;
       '$' : Result := 80;
       '%' : Result := 81;
       '^' : Result := 82;
       '&' : Result := 83;
       '*' : Result := 84;
       '(' : Result := 85;
       ')' : Result := 86;
      #123 : Result := 87;
      #125 : Result := 88;
       '[' : Result := 89;
       ']' : Result := 90;
       '<' : Result := 91;
       '>' : Result := 92;
     end;
end;


constructor TFont.LoadFromFile(Width, Height, Color: Cardinal; FileName: String);
begin
     CharWidth := Width;
     CharHeight := Height;
     FontColor := Color;
     FontData := LoadImage(FileName, True);
end;
destructor TFont.Free;
begin
     SDL_FreeSurface(FontData);
end;
procedure TFont.DrawText(Dest: PSDL_Surface; X, Y: Integer; Text: String);
var
  i: Integer;
  xChar: Integer;
  SrcRect, DestRect: TSDL_Rect;
begin
     if (Length(Text) > 0) then
     begin
          for i := 0 to Length(Text) - 1 do
          begin
               // Find Character tile
               xChar := GetCharNum(Text[i + 1]) - 1;

               SrcRect  := SDLRect(xChar * CharWidth, 0, CharWidth, CharHeight);
               DestRect := SDLRect(X + i * CharWidth, Y, CharWidth, CharHeight);
               // Draw Letter
               SDL_BlitSurface(FontData, @SrcRect, Dest, @DestRect);
          end;
     end;
end;
function TFont.GetTextWidth(Text: String): Cardinal;
begin
     Result := Length(Text) * CharWidth;
end;

end.

Programming - a skill for life!

Part 4 of republished guest tutorial by Jason McMillen