GLBasic forum

Codesnippets => Code Snippets => Topic started by: ampos on 2011-Mar-22

Title: R_PRINT (or relative_print or rotating_print)
Post by: ampos on 2011-Mar-22
I have a "problem" creating rotating (device orientation) applications, mainly text. The problem is not really to rotate the text, but to place it always in the same relative screen position regrdless of the device orientation.

So I have created a code to print text with a rotating angle (just 0º, 90º, 180º and 270º) and relative positions.

A few considerations:

-The font must be png+alpha (load my fonts in source to check)
-It uses font kerning. Not implemented (yet?) with fixed space/kerning.
-R_Fonts uses sprite id 1000+font id.
-Any font r_loaded is loaded also as standar font.
-My program "almost" work, at least with the supplied font. I have tested with other fonts, and although they work, results are not as accurate. But for me, it is enough.

In the supplied example, use LEFT & RIGHT cursor key to "rotate" screen.

X & Y coordinates are relative. If you use R_PRINT("Text",10,10,angle) it will be always printed in top-left 10,10 coordinate, independent of device rotation.

POSITIVE X (+X) MEANS RELATIVE TO LEFT EDGE, -X RELATIVE TO RIGHT EDGE, 0=CENTERED IN HORIZONTAL
POSITIVE X (+Y) MEANS RELATIVE TO TOP EDGE, -Y RELATIVE TO BOTTON EDGE, 0=CENTERED IN VERTICAL
X=0 & Y=0 CENTERED IN THE CENTER :)

So, R_PRINT ("Text",-10,1) will be printed with text finishing at 10 pixels of right border, even if you rotate the screen.

Hell, run the example and watch what I mean.

You can also "rotate" a pixel/coordinate, so you can draw your sprites in a rotating enviorment.

Commands:

R_LOADFONT: LOAD A FONT AS STD. FONT AND R_FONT
R_SETFONT: SET FONT NUMBER TO BE USED
R_PRINT: PRINT TEXT$ AT COORDS X,Y WITH ANGLE. COORDINATES ARE RELATIVE COORDS.
R_CONVERT: ROTATE A X,Y COORD. RESULTING COORDS ARE IN R_X & R_Y

v 1.0: http://www.ampostata.org/GLB/rPRINT.rar (http://www.ampostata.org/GLB/rPRINT.rar)

The code is in no way "clean", or perfect, but currently it works. I will update it. Also, if someone update it, let me know to update mine.
Title: Re: R_PRINT (or relative_print or rotating_print)
Post by: mentalthink on 2011-May-16
Wow, Ampos, it´s a very nice library.
Thanks
Kinds Regards,
Iván J

Title: Re: R_PRINT (or relative_print or rotating_print)
Post by: matchy on 2011-May-17
Hell, run the example...

Heavens above. That's a handy library.

Good one!  :good:
Title: Re: R_PRINT (or relative_print or rotating_print)
Post by: gigios on 2011-May-20
Very helpful functions.

Would it be possible to extend the function to handle 45-degree angles?
Title: Re: R_PRINT (or relative_print or rotating_print)
Post by: ampos on 2011-May-20
Not easily, as I really dont use sin/cos for printing...
Title: Re: R_PRINT (or relative_print or rotating_print)
Post by: gigios on 2011-May-20
Ok, no problem. Thanks for your answer.
Title: Re: R_PRINT (or relative_print or rotating_print)
Post by: Slydog on 2011-May-20
Quote
Would it be possible to extend the function to handle 45-degree angles?

Any angled rotation could only be done if ampos designed this using polyvectors.
It may be possible with rotated sprites too, just very awkward.
[Edit] It may not be too hard with sprites, you'd use the same technique as suggested below for polyvectors, but only need to keep track of one point instead of 4.

Using polyvectors its easy to snug two letters next to each other as they'd share two common vertices.
You'd start with a starting point on the screen and what part of the text string this point is touching.
Then to calculate the other points you'd need SIN and COS, and calculate the other three points for that character.
If you wanted to know what x,y position a point is 12 pixels over (char width) with a 30 degree rotation, you use something like:
Code: (glbasic) [Select]
new_x = cur_x + (COS(30) * 12)
new_y = cur_y + (SIN(30) * 12)
Then keep calculating the next character starting with the latest positions and using the next character's width in the calculation.

Or, do nothing and use ampos's routine as it, but draw the text to an off screen buffer to capture it into a new sprite.
Then you should be able to rotate that sprite to any angle and position. 

Damn, I should have started with the 2nd suggestion, it's much easier! ha