~~~~~~~
First things first I want to say I learned/stole/modified alot of this stuff from LuMo ( http://lumo.at.tt )
Okay, We need to find a font to download: I found some good fonts here
http://lumo2000.lu.funpic.de/?action=fonts
(careful some of the pics are bigger then 512 so won't work)
I chose the font called "bmf02_007.png"
It is a font that contains charactor that are exactly 8 pixels high and 8 pixels wide for each charactor. So we need to tell this to our program
Code: Select all
font = Image.load("bmf02_007.png")
LetterPixelsWide = 8
LetterPixelsTall = 8
Code: Select all
InputString = [[ !"#$%&'()* ,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ]]
Code: Select all
function char2font(char_input)
IndexNumber = 0
for i = 1, string.len(InputString) do
if string.sub(InputString, i,i) == char_input then
return IndexNumber
end -- end if
IndexNumber = IndexNumber + LetterPixelsWide
end -- end if
return 0
end
function blitChar(target, char, posx, posy)
target:blit(posx,posy, font,char2font(char),0,LetterPixelsWide,LetterPixelsTall,true)
end
function blitString(myTarget, possx, possy, thestring)
incX = 0
for w = 1, string.len(thestring) do
blitChar(myTarget, string.upper(string.sub(thestring, w,w)), possx + incX, possy)
incX = incX + LetterPixelsWide
end -- end for
end
*special note: You see the string.upper() command? that because our font set only has uppercase letters, if it would have had lowercase as well as uppercase we could get rid of that function, likewise if our font set only had lowercase letters then we would want to change that to string.lower()
To get our new font to print we can now use the command:
Code: Select all
blitString(screen, 10, 10, "Hello World")
10, 10 is the x and y cordinates where this is going to print
"Hello World" is what we are going to print
Now lets test everything out:
Code: Select all
-- Global Variables
font = Image.load("bmf02_007.png")
LetterPixelsWide = 8
LetterPixelsTall = 8
InputString = [[ !"#$%&'()* ,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ]]
--Functions
function char2font(char_input)
IndexNumber = 0
for i = 1, string.len(InputString) do
if string.sub(InputString, i,i) == char_input then
return IndexNumber
end -- end if
IndexNumber = IndexNumber + LetterPixelsWide
end -- end if
return 0
end -- char2font
function blitChar(target, char, posx, posy)
target:blit(posx,posy, font,char2font(char),0,LetterPixelsWide,LetterPixelsTall,true)
end -- blitChar
function blitString(myTarget, possx, possy, thestring)
incX = 0
for w = 1, string.len(thestring) do
blitChar(myTarget, string.upper(string.sub(thestring, w,w)), possx + incX, possy)
incX = incX + LetterPixelsWide
end -- end for
end -- blitString
--main program
blitString(screen, 10, 10, "Hello World")
screen:flip()
screen.waitVblankStart(600)
all we need to change is four little lines:
Code: Select all
font = Image.load("bmf02_003.png")
LetterPixelsWide = 8
LetterPixelsTall = 15
InputString = [[ () ,/12456:@ABCDEFGHIJKLMNOPQRSTUVWXYZ]]
We could also change font mid program, just redefine the varables:
Code: Select all
-- Global Variables
font = Image.load("bmf02_007.png")
LetterPixelsWide = 8
LetterPixelsTall = 8
InputString = [[ !"#$%&'()* ,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ]]
--Functions
function char2font(char_input)
IndexNumber = 0
for i = 1, string.len(InputString) do
if string.sub(InputString, i,i) == char_input then
return IndexNumber
end -- end if
IndexNumber = IndexNumber + LetterPixelsWide
end -- end if
return 0
end -- char2font
function blitChar(target, char, posx, posy)
target:blit(posx,posy, font,char2font(char),0,LetterPixelsWide,LetterPixelsTall,true)
end -- blitChar
function blitString(myTarget, possx, possy, thestring)
incX = 0
for w = 1, string.len(thestring) do
blitChar(myTarget, string.upper(string.sub(thestring, w,w)), possx + incX, possy)
incX = incX + LetterPixelsWide
end -- end for
end -- blitString
--main program
blitString(screen, 10, 10, "Hello World")
font = Image.load("bmf02_003.png")
LetterPixelsWide = 8
LetterPixelsTall = 15
InputString = [[ () ,/12456:@ABCDEFGHIJKLMNOPQRSTUVWXYZ]]
blitString(screen, 10, 100, "Hello World")
screen:flip()
screen.waitVblankStart(600)