BASIC

Author Topic: R_PRINT (or relative_print or rotating_print)  (Read 2645 times)

Offline ampos

  • Prof. Inline
  • *****
  • Posts: 1594
    • View Profile
    • AMpostata Website
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

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.
« Last Edit: 2011-May-17 by ampos »
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3366
  • Integrated Brain
    • View Profile
Re: R_PRINT (or relative_print or rotating_print)
« Reply #1 on: 2011-May-16 »
Wow, Ampos, it´s a very nice library.
Thanks
Kinds Regards,
Iván J


Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1543
    • View Profile
Re: R_PRINT (or relative_print or rotating_print)
« Reply #2 on: 2011-May-17 »
Hell, run the example...

Heavens above. That's a handy library.

Good one!  :good:

Offline gigios

  • Mc. Print
  • *
  • Posts: 36
    • View Profile
Re: R_PRINT (or relative_print or rotating_print)
« Reply #3 on: 2011-May-20 »
Very helpful functions.

Would it be possible to extend the function to handle 45-degree angles?

Offline ampos

  • Prof. Inline
  • *****
  • Posts: 1594
    • View Profile
    • AMpostata Website
Re: R_PRINT (or relative_print or rotating_print)
« Reply #4 on: 2011-May-20 »
Not easily, as I really dont use sin/cos for printing...
check my web and/or my blog :D
http://diniplay.blogspot.com (devblog)
http://www.ampostata.org
http://ampostata.blogspot.com
I own PC-Win, MacBook 13", iPhone 3G/3GS/4G and iPAC-WinCE

Offline gigios

  • Mc. Print
  • *
  • Posts: 36
    • View Profile
Re: R_PRINT (or relative_print or rotating_print)
« Reply #5 on: 2011-May-20 »
Ok, no problem. Thanks for your answer.

Offline Slydog

  • Prof. Inline
  • *****
  • Posts: 930
  • KodeSource
    • View Profile
    • KodeSource
Re: R_PRINT (or relative_print or rotating_print)
« Reply #6 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
« Last Edit: 2011-May-20 by Slydog »
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]