Code archives/Graphics/Screen-centered text

This code has been declared by its author to be Public Domain code.

Download source code

Screen-centered text by BlitzSupport2011
Nothing fancy, but I had a need for this, so here it is...

Just pass it a string, specifying new lines using the "|" (pipe) character, eg. "Hello|world" will become:

Hello
world


You can specify a different 'splitter' character/string via the second parameter, eg. it'll work with the C-style "\n" if you pass that.
Function ScreenCenteredText (t:String, splitter:String = "|")

	' Turn t$ into an array of strings...

	Local line:String [] = t.Split (splitter)
	
	Local lineheight:Int = TextHeight (t)
	Local totalheight:Int = lineheight * line.length
	
	For Local loop:Int = 0 Until line.length

		DrawText line [loop],	GraphicsWidth () / 2 - TextWidth (line [loop]) / 2,..					' X
								GraphicsHeight () / 2 + (loop * lineheight) - (totalheight / 2)	' Y
	Next
	
End Function

Graphics 640, 480

Local msg:String = "Hello, here is some screen-centered text,|which is conveniently split onto several|lines using the ~qpipe~q character,|though you can specify which character to use!"

Repeat

	Cls
	
	ScreenCenteredText msg
	
	Flip
	
Until KeyHit (KEY_ESCAPE)

End

Comments

None.

Code Archives Forum