Advertisement
Legomany3448

8-Wide Font Routine

Nov 11th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; DrawString - INPUTS
  2. ; (Text_X)  = X position (pixels)
  3. ; (Text_Y)  = Y position (pixels)
  4. ; HL        = string
  5. ; OUTPUTS
  6. ; HL        = byte after string
  7. ; Destroys EVERYTHING else
  8. DrawString:
  9.     ; Save the X pos for when starting a new line
  10.     ld bc,(Text_X)
  11.     ld (Text_XBuf),bc
  12. DrawString_Loop:
  13.     ld a,(hl) ; Grab the next character of A
  14.     inc hl
  15.     or a ; Does A = 0?
  16.     ret z ; If so, return
  17.     push hl
  18.     cp 1 ; Does A = 1?
  19.     jr z,DrawString_NewLine ; If so, go to the next line
  20.     call DrawChar ; Draw the character
  21.     ld hl,(Text_X)
  22.     ld bc,Font_Width + Font_HSpacing
  23.     add hl,bc ; Move the X pos to the next character
  24.     ld (Text_X),hl
  25.     pop hl
  26.     jr DrawString_Loop ; Repeat for the next charactar!
  27. DrawString_NewLine:
  28.     ; Reset the X pos for the new line
  29. Text_XBuf: =$+1
  30.     ld bc,$000000
  31.     ld (Text_X),bc
  32.     ld hl,(Text_Y)
  33.     ld bc,Font_Height + Font_VSpacing
  34.     add hl,bc ; Add the font height + spacing to the Y pos
  35.     ld (Text_Y),hl
  36.     pop hl
  37.     jr DrawString_Loop ; Draw the next character
  38.  
  39. ; DrawChar - INPUTS
  40. ; A         = Character to be drawn
  41. ; (Text_X)  = X position (pixels)
  42. ; (Text_Y)  = Y position (pixels)
  43. ; Destroys EVERYTHING
  44. DrawChar:
  45.     ; First get the memory location of the character data
  46.     sbc hl,hl
  47.     ld l,a
  48.     ; Each character is 14 bytes total, so we will multiply by 14 (Font_CharBytes)
  49.     ld h,Font_Height
  50.     mlt hl
  51.     ; Add in the actual RAM location
  52. SMC_Font = $+1
  53.     ld bc,Font_CharData
  54.     add hl,bc
  55.     ex de,hl
  56.  
  57.     ; Now get the memory location of the top-left screen pixel
  58.     ; Multiply Y pos by screen height (160)
  59. Text_Y = $+1
  60.     ld hl,$000000
  61.     ld h,160
  62.     mlt hl ; x160
  63.     add hl,hl ; x320
  64.     ; Add in the X pos and vRAM location
  65. Text_X = $+1
  66.     ld bc,$000000
  67.     add hl,bc
  68.     ld bc,vRAM
  69.     add hl,bc
  70.  
  71.     ; DE is now pointing to the first byte of the character
  72.     ; HL is now pointing to the top left corner pixel where the character will be printed
  73.  
  74.     ld b,Font_Height ; Font height
  75. DrawChar_LoopV:
  76.         push bc
  77.             ld a,(de) ; Grab next byte of character
  78.             ld b,Font_Width ; Number of bits we actually care about
  79. DrawChar_LoopPixel:
  80. Text_BG = $+1
  81.                 ld (hl),BgColor
  82.                 rla
  83.                 jr nc,DrawChar_PixelOff
  84. Text_FG = $+1
  85.                 ld (hl),FgColor
  86. DrawChar_PixelOff:
  87.                 inc hl
  88.             djnz DrawChar_LoopPixel
  89.             inc de
  90.         ld bc,320-Font_Width ; 320 (screen width) minus char width
  91.         add hl,bc ; This will advance us the next row of pixels
  92.         pop bc
  93.     djnz DrawChar_LoopV
  94.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement