GLBasic forum

Codesnippets => Code Snippets => Topic started by: ampos on 2011-Oct-25

Title: zPrint (print with zoom, polysprites and much more!)
Post by: ampos on 2011-Oct-25
Using the "polyprint" code by AlienMenace, I have upgraded my current print function.

zprint: t$,x,y,font=100,centered=1,zoom=1,color=0xFFFFFF,underline=0,italic=0
Prints a text on screen with many optional parameters)
t$ text to be printed
x,y x&y coordinates of the text to be printed. Regardless of the zoom, the text is always printed starting at x,y coordinates.
font=100 (optional) font to be used. It has to be loaded previously as animated sprite using loadanim "name"... By default, it uses sprite id 100. I just discovered that the font has to be loaded also as font, or the kerning will fail and funny text will be printed.
centered=0/1 (optional) Choose 1 if you want the text centered on your X coordinate.
zoom (optional) the size of the text to be printed (.5=1/2 smaller, 2=2x times bigger,...)
color (optional) to change the color of the text, using the format RGB (red, green, blue). ON some mobile devices it can be slow or not work at all.
underline=0/1 (optional) Choose 1 if you want your text underlined.
italic (optional) Type any value to draw your text in italics. Although the value should be 1, you can change it to have a bigger or smaller angle.

Code (glbasic) Select
FUNCTION zprint: t$,x,y,font=100,centered=1,zoom=1,color=0xFFFFFF,underline=0,italic=0
LOCAL fx,fy,l,c$,tx,ty,dx,dy,fxz,fyz
LOCAL line_width=2 //width of the underline line in pixels at zoom 1 (100%)
LOCAL outline=2 //width of the outline in underline in pixels at zoom 1 (100%)
LOCAL und_pos=.9 //vertical position of underline (in % of Font Y size)
LOCAL ital_ang=.5 //inclination of font (in % of font X size)

GETSPRITESIZE font,fx,fy
fx=fx/16;fy=fy/16 //change to fy/8 if you dont use full charset fonts
        fxz=fx*zoom;fyz=fy*zoom

DEC x,(LEN(t$,1)*zoom*centered)/2

IF underline=1
STARTPOLY
POLYVECTOR x-(fxz/4)-(outline*zoom),y+(und_pos*fyz)-(outline*zoom),0,0,0    //remove "-(fxz/4)" if you dont want the underline to be larger than text
POLYVECTOR x-(fxz/4)-(outline*zoom),y+(und_pos*fyz)+((outline+line_width)*zoom),0,0,0
POLYVECTOR x-(fxz/4)+(outline*zoom)+(LEN(t$,1)+fx/2)*zoom,y+(und_pos*fyz)+((outline+line_width)*zoom),0,0,0
POLYVECTOR x-(fxz/4)+(outline*zoom)+(LEN(t$,1)+fx/2)*zoom,y+(und_pos*fyz)-(outline*zoom),0,0,0
ENDPOLY
STARTPOLY
POLYVECTOR x-(fxz/4),y+(und_pos*fyz),0,0,color
POLYVECTOR x-(fxz/4),y+(und_pos*fyz)+(line_width*zoom),0,0,color
POLYVECTOR x-(fxz/4)+(LEN(t$,1)+fx/2)*zoom,y+(und_pos*fyz)+(line_width*zoom),0,0,color
POLYVECTOR x-(fxz/4)+(LEN(t$,1)+fx/2)*zoom,y+(und_pos*fyz),0,0,color
ENDPOLY
ENDIF

        italic=italic*zoom*fx*ital_ang

FOR n=0 TO LEN(t$)-1
c$=MID$(t$,n,1)
l=ASC(c$)

ty=INTEGER(l/16)
tx=INTEGER(MOD(l,ty*16))
dx=tx*fx
dy=ty*fy

STARTPOLY font
POLYVECTOR x+italic,y,dx,dy,color
POLYVECTOR x,y+fyz,dx,dy+fy,color
POLYVECTOR x+fxz,y+fyz,dx+fx,dy+fy,color
POLYVECTOR x+fxz+italic,y,dx+fx,dy,color
ENDPOLY

INC x,LEN(c$,1)*zoom
NEXT
ENDFUNCTION


I should put the STARTPOLY and ENDPOLY commands outside the loop to make the draw faster, but I don't get it. If someone can fix it, I will update this post.

edit 1.- precalculates fxz=fx*zoom, and changed all (fx*zoom) for fx. Same for fy. Moved out of the loop the italic calculation.
Title: Re: zPrint (print with zoom, polysprites and much more!)
Post by: fuzzy70 on 2011-Oct-25
The way I speed up functions that draw etc in loops is to use types. So for example POLYVECTOR takes 5 parameters x%, y%, tx%, ty%, col%

Code (glbasic) Select

TYPE Poly
x%
y%
tx%
ty%
col%
END TYPE


then its just a case of looping through the type with FOREACH at the end of the function.

Just an idea  :x
Title: Re: zPrint (print with zoom, polysprites and much more!)
Post by: matchy on 2011-Oct-25
Cool function. If speed is a concern, perhaps there are too many clock cycle calculations that can be avoided in common calls. For example, zoom and outline might need constant changing. ie. An IF zoom = 1 all the time, don't multiply it again because IF is faster than *.

Also in general cases, even though this one caters for directs parameters, I like to clean the function code for just one parameter if possible. This way parameters can be set way before entering the function. ;)

Eg.:
Code (glbasic) Select

TYPE _fontprint
t$ = ""
x = 0
y = 0
font = 100
centered = 1
zoom= 1
color = 0xFFFFFF
underline = 0
italic = 0
fx = 0
fy = 0
line_width = 2 //width of the underline line in pixels at zoom 1 (100%)
outline = 2 //width of the outline in underline in pixels at zoom 1 (100%)
und_pos = 0.9 //vertical position of underline (in % of Font Y size)
ital_ang = 0.5 //inclination of font (in % of font X size)
ENDTYPE

FUNCTION zprint: word as _fontprint
GETSPRITESIZE word.font, word.fx, word.fy
...
ENDFUNCTION
Title: Re: zPrint (print with zoom, polysprites and much more!)
Post by: ampos on 2011-Oct-25
Matchy, althoug I get the idea, mine was to sustitute "native" glbasic funcions with new ones, using, when possible, the same sintaxis.
Title: Re: zPrint (print with zoom, polysprites and much more!)
Post by: ampos on 2011-Oct-25
Can someone fix the loop, making this to work:

Code (glbasic) Select
        STARTPOLY font
FOR n=0 TO LEN(t$)-1
c$=MID$(t$,n,1)
l=ASC(c$)

ty=INTEGER(l/16)
tx=INTEGER(MOD(l,ty*16))
dx=tx*fx
dy=ty*fy

POLYVECTOR x+italic,y,dx,dy,color
POLYVECTOR x,y+fyz,dx,dy+fy,color
POLYVECTOR x+fxz,y+fyz,dx+fx,dy+fy,color
POLYVECTOR x+fxz+italic,y,dx+fx,dy,color
                NEWPOLYVECTOR

INC x,LEN(c$,1)*zoom
NEXT
        ENDPOLY


I dont understand how this "newpolyvector" works... but it will make the polyprint rutine just a single call/draw by opengl...
Title: Re: zPrint (print with zoom, polysprites and much more!)
Post by: Kitty Hello on 2011-Oct-25

Can someone fix the loop, making this to work:

Code (glbasic) Select


        STARTPOLY font, 2 // 2=mode: Strips
        LOCAL n% = LEN(t$)-1 // tiny bit faster
FOR n=0 TO n%
c$=MID$(t$,n,1)
l=ASC(c$)

ty=ASR(l, 4) // tiny bit faster. Compiler might do that anyway...
tx=INTEGER(MOD(l,ty*16)) // I think there's a faster way for this - but have not thought about it
dx=tx*fx
dy=ty*fy

POLYVECTOR x+italic,       y,      dx,dy, color
POLYVECTOR x,                y+fyz,dx,dy+fy,color
POLYVECTOR x+fxz+italic,y       ,dx+fx,dy,color // had to swap Strip is like
POLYVECTOR x+fxz,         y+fyz,dx+fx,dy+fy,color
// strip works like:
// 1---3---5
// | /  | /   |
// 2---4---6

// with polynewgroup you can "restart" after 4 or 6 at a totally different position

                            // POLYNEWSTRIP // not required here - you have no space between the characters

INC x,LEN(c$,1)*zoom
NEXT
        ENDPOLY


Title: Re: zPrint (print with zoom, polysprites and much more!)
Post by: ampos on 2011-Oct-25
Tonite I will time my routine, change to your version and time it again, so we can know if it really (and how much) is faster (as should).
Title: Re: zPrint (print with zoom, polysprites and much more!)
Post by: ampos on 2011-Oct-25
Code (glbasic) Select
tx=INTEGER(MOD(l,ty*16)) // I think there's a faster way for this - but have not thought about it

perhaps something like

tx= l and (00001111)?
Title: Re: zPrint (print with zoom, polysprites and much more!)
Post by: Kitty Hello on 2011-Oct-25
no - ignore it. There's no other way I know of.
Title: Re: zPrint (print with zoom, polysprites and much more!)
Post by: matchy on 2011-Oct-25
Quote from: ampos on 2011-Oct-25
...outside the loop to make the draw faster...

Quote from: ampos on 2011-Oct-25
Matchy, althoug I get the idea, mine was to sustitute "native" glbasic funcions with new ones, using, when possible, the same sintaxis.

I'm just responding to the issue of (non-drawing) speed, not syntax.
Title: Re: zPrint (print with zoom, polysprites and much more!)
Post by: ampos on 2011-Oct-26
Kitty, the fan thing fails:



[attachment deleted by admin]
Title: Re: zPrint (print with zoom, polysprites and much more!)
Post by: Kitty Hello on 2011-Oct-26
ok, uncomment the polynewstrip line then.