ZX BASIC:HRprint.bas

From BorielWiki
Jump to: navigation, search

The High Resolution printing routine allows standard sized characters to be positioned anywhere on the screen.

Usage

There is a an example program that uses this at the end of the page.

 
HRPrint(x,y,character,attribute,OVER)

Where

  • x is the x value in pixel co-ordinates
  • y is the y value in pixel co-ordinates
  • character is the memory address of the UDG style bytes for the character being printed.
To print standard characters, use the address of the character from the ROM table.
  • attribute is the attribute byte value (As you'd get from the ATTR function)
  • over is 0 or 1, and chooses whether to print in OVER 0 or OVER 1 mode.


Prints the character to the screen at the given pixel co-ordinates.

NOTE: The ZX Spectrum's attribute system is encoded into the hardware as a 32 character grid. HRPrint does its best, but changing the paper/bright/flash colour from the background is likely to look imperfect as the attribute blocks cannot line up well with the character printed unless it overlaps - as a result, the colour of attribute squares nearby is likely to be changed.


CODE

 
SUB HRPrint (x AS UBYTE, y AS UBYTE, char AS UINTEGER, attribute AS UBYTE, overprint AS UBYTE)
'High res Printing, based on code produced, with thanks, by Turkwel over on the WOS boards.
'Brought to ZX Basic by Britlion, June 2010.
 
ASM
      ld a,(IX+13) ; Get overprint value
      AND a ; zero?
      JR Z,HRP_No_Over
      LD a,182
        JP HRP_Change_Code
HRP_No_Over:
        XOR A ; faster than LD a,0
HRP_Change_Code:     
        LD (HRPOver1),a
        LD (HRPOver2),a
       
        ld b,(IX+7)
       ld c,(IX+5)
      
      push BC ; SAVE our co-ordinates.
      
;print_char:   
        ld  d,(IX+09)
        inc d
        dec d
        jr z, HRPrint_From_Charset
        ld e,(IX+08)
        jp HR_Print
HRPrint_From_Charset:       
        ld  de,(23606)
      ld  h,0
      ld  l,(IX+8) ; character
      add  hl,hl
      add  hl,hl
      add  hl,hl
      add  hl,de
      
HR_Print:
 
      call HRPat   
;convert the Y AND X pixel values TO the correct Screen Address  - Address in DE
      ld a,8
;set counter TO 8 - Bytes of Character Data TO put down
HRPrint0:
       push af
;save off Counter
      ld a,b
      cp 192
      jr c,HRprint1
       pop af
      jp HRPrintEnd
;don't print character if  > 191 - off the bottom of the screen - restore AF and exit Print routine
;[this can be REMoved if you are keeping tight control of your Y values]
HRprint1:
       push hl
      push de
      push de
;save off Address of Character Data, Screen Address, Screen Address
      ld a,c
      AND 7
      ld d,a
;get lowest 3 bits of Screen address
      ld e,255
;set up E with the Mask TO use - 11111111b = All On
      ld a,(hl)
      jr z,HRprint3
;get a BYTE of Character Data TO put down - but ignore the following Mask shifting
;if the the X value is on an actual Character boundary i.e. there's no need to shift anything
HRprint2:
       rrca
      srl e
      dec d
      jp nz,HRprint2
;Rotate the Character Data BYTE D times - AND Shift the Mask BYTE AS well, forcing Zeroes into the
;Left hand side. The Mask will be used TO split the Rotated Character Data OVER a Character boundary
HRprint3:
       pop hl
;POP one of the Screen Addresses (formerly in DE) into HL
      ld d,a
      ld a,e
      AND d
HRPOver1:      OR (hl)
      ld (hl),a
;take the Rotated Character Data, mask it with the Mask BYTE AND the OR it with what's already on the Screen,
;this takes care of the first part of the BYTE
;[REMove the OR (HL) if you just want a straight write rather than a merge]
      inc l
      ld a,l
      AND 31
      jr z,HRprint4
;IncREMent the Screen Address and check to see if it's at the end of a line,
;if so THEN there's no need to put down the second part of the Byte
      ld a,e
      cpl
      AND d
HRPOver2:      OR (hl)
      ld (hl),a
;Similar TO the first BYTE, we need TO Invert the mask with a CPL so we can put down the second part of the BYTE
;in the NEXT Character location
;[again, REMove the OR (HL) if you just want a straight write rather than a merge]
HRprint4:
       pop de
      inc d
      inc b
;get the Screen Address back into DE, incREMent the MSB so it points the the Address immediately below
;it AND IncREMent the Y value in B as well
      ld a,b
      AND 7
      call z,HRPat
;now check IF the Y value has gone OVER a Character Boundary i.e. we will need TO recalculate the Screen
;Address IF we've jumped from one Character Line to another - messy but necessary especially for lines 7 and 15
      pop hl
      inc hl
;get the Address of the Character Data back AND incREMent it ready for the next byte of data
      pop af
      dec a
      jp nz,HRPrint0
;get the Counter value back, decREMent it and go back for another write if we haven't reached the end yet
      jp HRPrintAttributes
 
       
;HRPAT is a subroutine TO convert pixel values into an absolute screen address
;On Entry - B = Y Value C = X Value   On EXIT - DE = Screen Address
HRPat:
      ld a,b
      srl a
      srl a
      srl a
      ld e,a
      AND 24
      OR 64
      ld d,a
      ld a,b
      AND 7
      add a,d
      ld d,a
      ld a,e
      AND  7
      rrca
      rrca
      rrca
      ld e,a
      ld a,c
      srl a
      srl a
       srl a
       add a,e
      ld  e,a
      ret
 
HRPrintAttributes:
        POP BC ; recover our X-Y co-ordinates.
       ld d,0
      ld a,(IX+11) ; attribute
      AND a
      jr z, HRPrintEnd  ; IF attribute=0, THEN we don't do attributes.
      ld e,a ; pass TO e
;transfer Attribute BYTE TO e FOR easier use
      ld a,b
      cp 192
      jr nc, HRPrintEnd
;check Y position AND EXIT IF off bottom of screen
      push bc
;save off Y AND X values FOR later
      AND 248
      ld h,22
      ld l,a
      add hl,hl
      add hl,hl
      srl c
      srl c
      srl c
      ld b,d
      add hl,bc
;calculate the correct Attribute Address FOR the Y\X values
      ld (hl),e
;set the Attribute - this is ALWAYS set no matter what the valid Y\X values used
      pop bc
;get the Y AND X values back into BC
      ;call print_attribute2
;call the subroutine TO see IF an adjacent Horizontal Attribute needs TO be set
print_attributes1:
       ld a,c
      cp 248
      jr nc,endPrintAttributes1
;check TO see IF we are AT Horizontal character 31 - IF so THEN no need TO set adjacent Horizontal Attribute
      AND 7
      jr z, endPrintAttributes1
;and don't set the adjacent Horizontal Attribute if there's no need to
      inc l
      ld (hl),e
      dec l
;incREMent the Attribute address - set the adjacent horizontal Attribute - then set the Attribute Address back
endPrintAttributes1:                             
 
 
;
      ld a,b
      cp 184
      jr nc, HRPrintEnd
;check TO see IF we are AT Vertical character 23 - IF so THEN no need TO set adjacent Vertical Attribute & EXIT routine
      AND 7
      jr z, HRPrintEnd
;and don't set the adjacent Vertical Attribute if there's no need to & Exit routine
      ld a,l
      add a,32
      ld l,a
      ld a,d
      adc a,h
      ld h,a
      ld (hl),e
;set the Attribute address TO the line below  - AND set the adjacent Vertical Attribute
;
;drop through now into adjacent Horizontal Attribute subroutine - all RETs will now EXIT the routine completely
;
HRPrintAttribute2:   ld a,c
      cp 248
      jr nc, HRPrintEnd
;check TO see IF we are AT Horizontal character 31 - IF so THEN no need TO set adjacent Horizontal Attribute
      AND 7
      jr z, HRPrintEnd
;and don't set the adjacent Horizontal Attribute if there's no need to
      inc l
      ld (hl),e
      dec l
;incREMent the Attribute address - set the adjacent horizontal Attribute - then set the Attribute Address back
      ;ret                       
 
HRPrintEnd:
      
END ASM
END SUB


Usage Example

Here is a short program that demonstrates the HRPrint routine in use - in this case to animate a sprite. Four calls to the routine put the four characters of the sprite onto the screen, and similarly erase it.

 
CLS
 
DIM x,y AS UBYTE
DIM xd,yd AS BYTE
x=100
y=10
xd=1
yd=1
 
DO
PAUSE 2
HRPrint(x,y,32,56,0): REM Erase with a space (CHR$ 32)
HRPrint(x+8,y,32,56,0)
HRPrint(x,y+8,32,56,0)
HRPrint(x+8,y+8,32,56,0)
 
x=x+xd
y=y+yd
 
HRPrint(x,y,@gentle,76,0)
HRPrint(x+8,y,@gentle+8,76,1)
HRPrint(x,y+8,@gentle+16,76,0)
HRPrint(x+8,y+8,@gentle+24,76,1)
 
IF x<=0 OR x>=247 THEN xd=-xd : END IF
IF y<=0 OR y>=184 THEN yd=-yd : END IF
 
LOOP
END
 
gentle:
ASM
defb 15,15,15,15,15,15,13,15
defb 240,144,208,208,240,240,176,240
defb 15,14,63,0,0,12,26,30
defb 176,112,252,0,0,24,104,120
END ASM
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox
Translate
Google AdSense