GLBasic forum

Codesnippets => 2D-snippets => Topic started by: backspace on 2013-Jan-08

Title: _PRINT A clean scalable print type
Post by: backspace on 2013-Jan-08
This little project was inspired when I discovered that the print does not zoom very well. This solution uses a large font file, and allows you to zoom it smaller for different sizes.

The left alignment needs a little work, but I'll leave that to the experts to play with. Note that this does not use the built in PRINT command, but duplicates what it does.

Code (glbasic) Select

TYPE _PRINTX
chr []
fontID
base
border = 2   //font border if used
fw = 64 + self.border    //font width
fh = 72 + self.border    //font height
count = 15

FUNCTION Init: font$
LOCAL i, j, c=-1, x,y
self.fontID = RND(1000)+1000
self.base = RND(10000)+2000
LOADSPRITE font$, self.fontID
FOR j = 0 TO 15  //change this to 31 to include high ascii values
FOR i = 0 TO 15
c = c + 1
x = i * self.fw
y = j * self.fh
CLEARSCREEN
DRAWSPRITE self.fontID, -x,-y
GRABSPRITE self.base + c, 0,0, self.fw, self.fh 
DIMPUSH self.chr[], self.base + c
NEXT
NEXT
CLEARSCREEN
ENDFUNCTION


FUNCTION _print: text$, x, y, scale#
LOCAL i,c, s$
FOR i = 0 TO LEN(text$)-1
s$ = MID$(text$,i,1)
c = ASC(s$)
ZOOMSPRITE self.chr[c], x + (i * self.fw *scale#), y, scale#, scale#
NEXT
ENDFUNCTION

ENDTYPE



LOCAL font1 AS _PRINTX   //define your own font object

font1.Init("arial72.png")  //initialize the font here

font1._print ("AbCdEfG 123", 0, 0, 0.2) 
font1._print ("Hello world", 0, 20, 0.2)
font1._print ("!@#$%^&*()_+", 0, 40, 0.3)
font1._print ("Is it a bird, is it a plane? No it's Super-GLBasic", 0 ,60, 0.18)
font1._print ("Eat your food to grow", 0, 80, 0.4)
font1._print ("even bigger", 0, 110, 0.6)
font1._print ("and bigger", 0, 140, 0.8)
font1._print ("Yum !", 30, 200, 1.5)
font1._print ("...oooOOOoooooo...", 20, 300, 0.5)

SHOWSCREEN
MOUSEWAIT


[attachment deleted by admin]
Title: Re: _PRINT A clean scalable print type
Post by: mentalthink on 2013-Jan-08
Thanks very interesting... and usefully...
Title: Re: _PRINT A clean scalable print type
Post by: kanonet on 2013-Jan-08
Nice work, but a few thoughts:
-normally i like to have everything inside a type, but here it makes things to long, the print command should ge a function outside of the type.
-without kerning its not to useful.

Btw. do you know Moru's scalable font systems? He did a great work with them, years ago. Too bad his website is down and i dont have a copy of it.
Title: Re: _PRINT A clean scalable print type
Post by: Moru on 2013-Jan-08
Aw I totally forgot about putting that page up again.

It's not all my work though, just modified Peejays code to use colours, faster rendering on tiny computers and auto-kerning.
Original can be found here for the kerning code, centering and justifying text and so on:
http://www.glbasic.com/forum/index.php?topic=1521.0

Haven't used it for anything useful yet though, really a sin.
The attached file should be the last update I did to it about three years ago.

[attachment deleted by admin]
Title: Re: _PRINT A clean scalable print type
Post by: backspace on 2013-Jan-09
There is a large difference in the project when a novice like myself attempts something, against when someone with a lot of experience does it. But I'll get there one day. 
Thank you for the link, The code gives me something to aspire to.  :nw:
Title: Re: _PRINT A clean scalable print type
Post by: Schranz0r on 2013-Jan-09
Nice work!
Title: Re: _PRINT A clean scalable print type
Post by: Hemlos on 2013-Jan-09
Nice font objects  :good:
One note, as it were, you have a possibility(although not likely), you might end up with an ID collision.
You can use the GENSPRITE command to eliminate such an event.