QB64 Phoenix Edition
A Simple Center Routine - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Prolific Programmers (https://qb64phoenix.com/forum/forumdisplay.php?fid=26)
+---- Forum: SMcNeill (https://qb64phoenix.com/forum/forumdisplay.php?fid=29)
+---- Thread: A Simple Center Routine (/showthread.php?tid=71)



A Simple Center Routine - SMcNeill - 04-20-2022

The title really says it all...

Code: (Select All)
SCREEN _NEWIMAGE(640, 480, 32)

Center 10, "A centered title", -1
'The 10 above is the line that we want to center our text on.
'"A centered title" is the text that we want centered.
'The -1 above says that we want to move the print positon to the next line down -- line 11 in this case.
Center 0, "by Steve", 0
'The 0 above says to print on the CURRENT LINE
'"by Steve" is simply the text we want centered.
'The 0 here says that we DON'T move the print position to the next line down.

'NOTE:  In this case, our print cursor is going to be at line 11, position 1 (like LOCATE 11,1) for our next PRINT statement.
COLOR _RGB32(255, 255, 0)
PRINT "<<<<<<<<<<<<<<<<"
'Notice where the yellow arrows  above printed on the screen -- it's the SAME line as "by Steve".
'If we wanted to move the cursor down to the next line automatically, we'd set that last parameter to anything other than 0.
'Because QB64 counts 0 as FALSE, all else as TRUE.

COLOR -1
Center 20, "Press <ANY KEY> to see this in SCREEN 0", 0
a$ = INPUT$(1)

SCREEN 0
Center 10, "A centered title", -1
Center 0, "by Steve", 0
Center 20, "Notice that the center command works fine in ALL screen modes?", 0



SUB Center (PrintLine AS INTEGER, text AS STRING, NewLine AS INTEGER)
IF PrintLine = 0 THEN
    y = CSRLIN
ELSE
    y = PrintLine
END IF
IF _PIXELSIZE <> 0 THEN Py = (y - 1) * _FONTHEIGHT ELSE Py = y
pw = _PRINTWIDTH(text)
w = _WIDTH
C = (w - pw) \ 2
_PRINTSTRING (C, Py), text
IF NewLine THEN LOCATE y + 1
END SUB

Note that we have a 3rd parameter for our Center routine, which is for the NEWLINE option.  Basically this is used to toggle between two modes of behavior:
0 -- don't move the print position location from what it previously existed.  (This allows us to center text wherever we want, without losing where we were printing before.)
Anything Else -- move the print position to the line directly below what we just centered, and to the left side of the screen, just as PRINT "something" would.

The rest should be easy enough to figure out.  Smile