Aligning text and graphics according to SCALE

Anything QL Software or Programming Related.
Post Reply
RWAP
RWAP Master
Posts: 2837
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Aligning text and graphics according to SCALE

Post by RWAP »

I often see comments (or get asked) about how you align cursor co-ordinates (eg for text) with graphics co-ordinates (which are based on the scale command).

I did this for Q-Route, and so thought that now the time is nigh to release the code for this common problem. There is some details about it in the SBASIC/SuperBASIC Reference Manual, but for Q-Route, I wrote my own commands - ATEXT, RFCURSOR, RFLINE and RFPOINT which all use the same co-ordinate system.

Code: Select all

100 REMark To start, some QL dependent variables
110 char_width0%=6:       REMark value for csize 0,0
120 char_width1=8.45:     REMark value for csize 1,0
130 char_height%=10:      REMark height in pixels
140 graf_scale=256:      REMark resolution used for graphics
150 ww%=448:             REMark window width
160 wh%=200:             REMark window height
170 wind_scalex=(1/ww%)*(graf_scale*(100/wh%*ww%/135.5))
180 wind_scaley=(1/wh%)*graf_scale
190 WINDOW #2,ww%,wh%,0,0
200 SCALE #2,graf_scale,0,0
205 tw=char_width0% : REMark which character width will we use for text?
210 DEFine PROCedure printat (x1,y1,s$)
220 atx=tw*(x1-1)
230 aty=(y1-1)*char_height%
240 ATEXT atx,aty,s$
250 END DEFine
260 :
270 DEFine PROCedure ATEXT(linex1,liney1,txt)
280 LOCal pos%
290 IF liney1<0 OR linex1>ww%:RETurn
300 IF liney1+char_height%>wh%:RETurn
310 txt$=txt
320 len_txt=LEN(txt$):IF len_txt=0:RETurn
330 IF linex1+len_txt*char_width0%<0:RETurn
340 txt_x%=linex1:pos%=1
350 pos%=pos%+(-linex1/char_width0%)*(linex1<0):IF pos%>1:txt_x%=0
360 RFCURSOR txt_x%,liney1
370 FOR txt%=pos% TO len_txt
380   IF txt_x%+char_width0%>ww%:EXIT txt%
390   PRINT#2,txt$(txt%);
400   txt_x%=txt_x%+char_width0%
410 END FOR txt%
420 END DEFine
430 :
440 DEFine PROCedure RFLINE(linex1,liney1,linex2,liney2)
450 LINE #2,linex1*wind_scalex,(wh%-liney1)*wind_scaley TO linex2*wind_scalex,(wh%-liney2)*wind_scaley
460 END DEFine
470 :
480 DEFine PROCedure RFCURSOR(linex1,liney1)
490 CURSOR #2,linex1*wind_scalex,(WINDTAB(2,5)-liney1)*wind_scaley,0,0
500 END DEFine
510 :
520 DEFine PROCedure RFPOINT(linex1,liney1)
530 POINT #2,linex1*wind_scalex,(WINDTAB(2,5)-liney1)*wind_scaley
540 END DEFine
If I have left anything out, I am sure someone will shout...


RWAP
RWAP Master
Posts: 2837
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Re: Aligning text and graphics according to SCALE

Post by RWAP »

Oops - forgot the WINDTAB function - this is a machine code function from my own programming toolkit, it takes two parameters

PRINT WINDTAB(channel, detail)

channel is the Channel Number
detail is the Detail Required -
2 = x co-ord of the top left hand corner of the window
3 = y co-ord of the top left hand corner of the window
4 = width of window in pixels
5 - height of window in pixels

Obviously this returns the details of a window created with
WINDOW #chan, width, height, x, y


User avatar
Mr_Navigator
QL Fanatic
Posts: 782
Joined: Mon Dec 13, 2010 11:17 pm
Location: UK, Essex
Contact:

Re: Aligning text and graphics according to SCALE

Post by Mr_Navigator »

So...
I have to ask, where does one obtain the machine code function?


-----------------------------------------------------------------------------------
QLick here for the Back 2 the QL Blog http://backtotheql.blogspot.co.uk/
RWAP
RWAP Master
Posts: 2837
Joined: Sun Nov 28, 2010 4:51 pm
Location: Stone, United Kingdom
Contact:

Re: Aligning text and graphics according to SCALE

Post by RWAP »

Ah - I am not giving away my development toolkit...

You don't need the machine code function really - you should know the size and position of the window, unless you allow the user to resize it!

Otherwise, use the Display Toolkit from Dilwyn's site ( http://www.dilwyn.me.uk/tk/display2.zip )- it has 4 built in functions:

Code: Select all

* FLIM_X and FLIM_Y return origin of largest window channel
* LET x = FLIM_X(#channel)
* LET y = FLIM_Y(#channel)
* FLIM_W and FLIM_H return limits of width and height respectively
* LET wide = FLIM_W(#channel)
* LET high = FLIM_H(#channel)


User avatar
Mr_Navigator
QL Fanatic
Posts: 782
Joined: Mon Dec 13, 2010 11:17 pm
Location: UK, Essex
Contact:

Re: Aligning text and graphics according to SCALE

Post by Mr_Navigator »

RWAP wrote:Ah - I am not giving away my development toolkit...
Hah!, that came across wrong RWAP, I should have phrased it better :)

Without looking at your code I thought none of it would would work without the machine code extension. I am reminded of the 'read twice speak once' scenario. apologies, no offence meant.


-----------------------------------------------------------------------------------
QLick here for the Back 2 the QL Blog http://backtotheql.blogspot.co.uk/
Post Reply