.... No reply?
Ok, I'm starting to explain now.
Using array in PascalYou need to use array to store each of the font pictures copied from the loaded image. To do it, first load the font bitmap (or image), then perform some calculations and copy the rectangular size of the single character from the font image and store it in a array.
The array to store the font pictures is declared like this:
var
MyFonts: array[1..something] of PSDL_Texture;
Note:
something is the characters count in your font image.
But as I remember, you ever said you haven't learn array from your school, so I will tried to avoid the using of array.
Read image from file and save it to memory bufferI guess you already know how to do it. I won't explain this topic.
Using graphics editing software to generate a font bitmapThis is not a programming thing, so I skip the process about how I draw the font image using Gimp. You can simply use the image I provide.
In the font image provided, I use:
- 30 pixels width for each character
- 30 pixels height for each character
- I intentionally draw the rectangle so you can have the idea of the size
- It consist only: a..z, A..Z, 0..9
Some useful links (for beginners only):
https://en.wikipedia.org/wiki/ASCIIhttp://wiki.freepascal.org/Modhttp://www.freepascal.org/docs-html/rtl/system/length.htmlhttp://www.freepascal.org/docs-html/rtl/system/ord.htmlhttp://wiki.freepascal.org/DivAs usual, I do not provide working codes. You have to learn from my code and write your own. And it is not optimized and has not tested. This code provided here is to give you the idea how to use font image.
I do not use SDL, so any SDL experts please correct my code if I write something wrong.
procedure WriteText(R: PSDL_Renderer; X, Y: Integer; const S: string);
var
i: Integer;
CurrentChar: Char;
FontPos: Integer;
SourceRect, DestinationRect: PSDL_Rect;
begin
for i := 1 to Length(S) do begin
// Get the font position of the character
FontPos := 0;
CurrentChar := S[i];
if (CurrentChar >= 'a') and (CurrentChar <= 'z') then
FontPos := ord(C) - 96; // ASCII value of 'a' is 97
if (CurrentChar >= 'A') and (CurrentChar <= 'Z') then
FontPos := ord(C) - 64 + 26; // ASCII value of 'A' is 65
if (CurrentChar >= '0') and (CurrentChar <= '9') then
FontPos := ord(C) - 47 + 52; // ASCII value of '0' is 48
// Render it if the character can be found in the font image
if (FontPos > 0) then begin
with SourceRect do begin
y := (FontPos div 10) * 30; // 30 is the height of the font
x := ((FontPos-1) mod 10) * 30; // 30 is the width of the font
w := 30;
h := 30;
end;
with DestinationRect do begin
x := i * 30;
w := 30;
h := 30;
end;
DestinationRect.y := Y;
SDL_RenderCopy(R, LoadedFontTexture, SourceRect, DestinationRect);
end;
end;
end;
Further improvement:
- You can add more characters, symbols, etc
- The font in this font image looks ugly, you should decrease its width
- You should remove the rectangles (horizontal and vertical lines)
- Variable-width fonts usually will look better, but to draw it more things are needed to be performed
- It can be optimized for speed