Draw string with alignment

Post your FreeBASIC tips and tricks here. Please don’t post your code without including an explanation.
Posts: 2149
Joined: May 24, 2007 22:10
Location: The Netherlands

Draw string with alignment

Postby badidea » Oct 12, 2019 23:58

I needed a way to align an axis text label to a graph, so I wrote this odd piece of code.
printxy is like draw string but with an alignment parameter. Also taking into account the font size (8x8, 8x14, 8x16).

Code: Select all

'this UDT is not really essential, code can be rewritten without it
type int2d
   as integer x, y
   declare constructor
   declare constructor(x as integer, y as integer)
   declare operator cast () as string
end type

constructor int2d
end constructor

constructor int2d(x as integer, y as integer)
   this.x = x : this.y = y
end constructor


'get width (x) & height (y) of screen characters
function getFontSize() as int2d
   dim as int2d screenSize
   screeninfo screenSize.x, screenSize.y
   dim as long twid = width()
   return int2d(screenSize.x \ loWord(twid), screenSize.y \ hiWord(twid))
end function

enum h_align_enum
   H_ALIGN_L 'left
   H_ALIGN_C 'center
   H_ALIGN_R 'right
end enum

enum v_align_enum
   V_ALIGN_T 'top
   V_ALIGN_C 'center
   V_ALIGN_B 'bottom
end enum

'draw string with h/v-alignment
sub printxy(position as int2d, text as string, align as int2d, c as ulong)
   dim as integer x = position.x
   dim as integer y = position.y
   'get screen area size of text string
   dim as int2d textSize = getFontSize()
   textSize.x *= len(text)
   select case align.x
      case H_ALIGN_L 'nothing
      case H_ALIGN_R : x -= textSize.x
      case H_ALIGN_C : x -= textSize.x \ 2
   end select
   select case align.y
      case V_ALIGN_T 'nothing
      case V_ALIGN_B : y -= textSize.y
      case V_ALIGN_C : y -= textSize.y \ 2
   end select
   draw string (x, y), text, c
end sub


const as integer SCRN_W = 800
const as integer SCRN_H = 600
const as integer CHAR_W = 8
const as integer CHAR_H = 14 '8, 14, 16

'lazy RGB macro
#define hRGB(rgb_) ((((((rgb_) and &hF00) shl 4) or ((rgb_) and &hF00)) shl 8) _
   or (((((rgb_) and &h0F0) shl 4) or ((rgb_) and &h0F0)) shl 4) _
   or ((((rgb_) and &h00F) shl 4) or ((rgb_) and &h00F)))

'set screen and character size
screenres(SCRN_W, SCRN_H, 32)

'display text with anchor
for h_align as h_align_enum = H_ALIGN_L to H_ALIGN_R
   for v_align as v_align_enum = V_ALIGN_T to V_ALIGN_B
      dim as integer x = 300 + h_align * 100
      dim as integer y = 200 + v_align * 100
      dim as int2d alignMode = int2d(h_align, v_align)
      pset(x, y), hRGB(&hFF0)
      circle(x, y), 5, hRGB(&hF00)
      printxy(int2d(x, y), "Test123", alignMode, hRGB(&h0F0))

printxy(int2d(0, 0), "Top left corner", int2d(H_ALIGN_L, V_ALIGN_T), hRGB(&h0FF))
printxy(int2d(SCRN_W - 1, 0), "Top right corner", int2d(H_ALIGN_R, V_ALIGN_T), hRGB(&h0FF))
printxy(int2d(0, SCRN_H - 1), "Bottom left corner", int2d(H_ALIGN_L, V_ALIGN_B), hRGB(&h0FF))
printxy(int2d(SCRN_W - 1, SCRN_H - 1), "Bottom right corner", int2d(H_ALIGN_R, V_ALIGN_B), hRGB(&h0FF))

printxy(int2d(SCRN_W \ 2, 0), "Top edge", int2d(H_ALIGN_C, V_ALIGN_T), hRGB(&hF0F))
printxy(int2d(SCRN_W \ 2, SCRN_H - 1), "Botton edge", int2d(H_ALIGN_C, V_ALIGN_B), hRGB(&hF0F))
printxy(int2d(0, SCRN_H \ 2), "Left egde", int2d(H_ALIGN_L, V_ALIGN_C), hRGB(&hF0F))
printxy(int2d(SCRN_W - 1, SCRN_H \ 2), "Right edge", int2d(H_ALIGN_R, V_ALIGN_C), hRGB(&hF0F))


Return to “Tips and Tricks”

Who is online

Users browsing this forum: No registered users and 3 guests