sprite_numbers_horizon = 3
;!align 255,0,0 
;bitmask_x_char:
;!for COPY = 0 TO 31
;  !byte $80, $40, $20, $10, $08, $04, $02, $01
;!end

bitmask_delta_finder_lower:
  !byte $00, $01, $02, $03, $04, $05, $06, $07
  !byte $00, $ff, $fe, $fd, $fc, $fb, $fa, $f9

; we only have (3x3) Sprites values we should be smaller than one page
y_char_hi:
!for SPRITE_NUMBER = 0 TO sprite_numbers_horizon  
  !for SPRITE_LINE = 0 TO 20
    !byte >(sprite_logo_painting_area+((SPRITE_NUMBER*sprite_numbers_horizon)*64)+(SPRITE_LINE*3))
  !end        
!end
;speedup one cycle when pagebreak
;!align 255,0,0 
x_char:
!for SPRITE_NUMBER = 0 TO sprite_numbers_horizon  
  !for COLUMN = 0 to 2
    !for BYTE_NUMBER = 0 TO 7  
      !byte COLUMN+(SPRITE_NUMBER*64)
    !end  
  !end    
!end

;speedup one cycle when pagebreak
;!align 255,0,0 
y_char_lo:
!for SPRITE_NUMBER = 0 TO sprite_numbers_horizon  
  !for SPRITE_LINE = 0 TO 20
    !byte <(sprite_logo_painting_area+((SPRITE_NUMBER*sprite_numbers_horizon)*64)+(SPRITE_LINE*3))
  !end        
!end

;zp
table_lo = $fc
table_hi = $fd

plot_lo = $fe
plot_hi = $ff

x_1:
!byte 0
y_1:
!byte 0

x_2:
!byte 63
y_2:
!byte 63
 
;coords
 
draw_line
        lda x_1  ;check if both coordinates are the same
        cmp x_2
        bne +
        lda y_1
        cmp y_2
        bne +
        inc x_2 ;both are equal, so we make it one pixel wide
+        
;init
        ldx #$e8        ;inx
        lda y_2
        sta to_y+1
        sec
        sbc y_1
        bcs skip1
        eor #$ff
        adc #1
        ldx #$ca        ;dex - change direction
skip1
        sta d_y+1
        sta t_y_1+1
        sta t_y_2+1
        stx incx1
        stx incx2
 
        ldx #$c8        ;iny
        lda x_2
        sta to_x+1
        sec
        sbc x_1
        bcs skip2
        eor #$ff
        adc #1
        ldx #$88        ;dey - change direction
skip2
        stx incy1
        stx incy2
 
        ldy x_1
        ldx y_1
 
;loop
 
;start y in x-register
;start x in y-register
;delta x in a-register
 
d_y     cmp #0
        bcc steep
 
        sta t_x_1+1
        lsr
        sta errx+1
loopx
 ;       clc                 ;needed, as previous cmp could set carry. could be saved if we always count up and branch with bcc;
 ;       lda x_char,y
 ;       adc y_char_lo,x
 ;       sta plot_lo
 ;       lda y_char_hi,x
 ;       adc #$00
 ;       sta plot_hi
 ;       
 ;       clc
 ;       lda bitmask_x_char,y
 ;       ora (plot_lo),y
 ;       sta (plot_lo),y     ;Remember that the y_char_lo table in this example starts at $20 (which center hires mode plotting). If you lower the start of table to below $08 (say for multicolor purposes where x steps are in doubles), you will get high-byte issues when you $FE in the adc x_char with the sta (),y  
 ;
        clc
        lda y_char_hi,y
        sta plot_hi
        lda y_char_lo,y
        sta plot_lo

        sty .ytemp+1 ;remember y
        ldy x_char,x
        lda bitmask_x_char,x
        ora (plot_lo),y
        sta (plot_lo),y
.ytemp:    
        ldy #00
        
errx    lda #$00
        sec
t_y_1   sbc #0
        bcs skip3
 
        ;one might also swap cases (bcc here) and duplicate the loopend. saves more or less cycles as the subtract-case occurs more often than the add-case. Copying the whole loop to zeropage also save cycles as sta errx+1 is only 3 cycles then. (Bitbreaker)
 
t_x_1   adc #0
incx1   inx
skip3   sta errx+1
 
incy1   iny
to_x    cpy #0
        bne loopx
        rts
 
steep
        sta t_x_2+1
        lsr
        sta erry+1
loopy
        ;clc                 ;needed, as previous cmp could set carry. could be saved if we always count up and branch with bcc;
        ;lda x_char,y
        ;adc y_char_lo,x
        ;sta plot_lo
        ;lda y_char_hi,x
        ;adc #$00
        ;sta plot_hi
        ;
        ;clc
        ;lda bitmask_x_char,y
        ;ora (plot_lo),y
        ;sta (plot_lo),y
        clc
        lda y_char_hi,y
        sta plot_hi
        lda y_char_lo,y
        sta plot_lo

        sty .ytemp2+1 ;remember y
        ldy x_char,x
        lda bitmask_x_char,x
        ora (plot_lo),y
        sta (plot_lo),y        
.ytemp2:    
        ldy #00
        
erry    lda #$00
        sec
t_x_2   sbc #0
        bcs skip4
 
t_y_2   adc #0
incy2   iny
skip4   sta erry+1
 
incx2   inx
to_y    cpx #0
        bne loopy
        rts
        
current_step:
!byte 119
current_line:
!byte 30

set_draw:  
  lda #<((delta_xy+119*29)-1)
  sta table_lo
  lda #>((delta_xy+119*29)-1)
  sta table_hi  
  rts
  
draw_step:
  ; add/substract the diff to every point
  ; first point is rotated about, so no change
;  ldy #29
;-  
;  lda (table_lo),y
;  tax
;  lda bitmask_delta_finder_upper,x
;  clc
;  adc mesh_y,y
;  sta mesh_y,y
;  lda bitmask_delta_finder_lower,x
;  clc
;  adc mesh_x,y
;  sta mesh_x,y
;  dey
;  bne -

  ldy #29
-  
  lda (table_lo),y  
  lsr
  lsr
  lsr
  lsr
  tax
  lda bitmask_delta_finder_lower,x
  clc
  adc mesh_y,y
  sta mesh_y,y

  lda (table_lo),y  
  and #%00001111
  tax
  lda bitmask_delta_finder_lower,x
  clc
  adc mesh_x,y
  sta mesh_x,y
    
  dey
  bne -
  
  ; calc the next table starting point
  lda #29
  clc
  adc table_lo
  sta table_lo
  lda #$00
  adc table_hi
  sta table_hi
  clc
  
  ; draw all 30 lines
- 
  ldx current_line
  lda mesh_x-1,x
  sta x_1
  lda mesh_y-1,x
  sta y_1
  lda mesh_x,x
  sta x_2
  lda mesh_y,x
  sta y_2    
  
  jsr draw_line
  dec current_line
  bne -
  lda #30
  sta current_line
  
  ; are all 120 steps drawn -> restart from 0
  inc current_step
  lda current_step
  cmp #120
  bne + 
default_wait_time =*+1
  lda #$ff
  sta wait_worker
  lda #$00
  sta current_step
  lda #120
  sta default_wait_time
reset_draw:  
  lda #<(delta_xy-1)
  sta table_lo
  lda #>(delta_xy-1)
  sta table_hi  
+
  rts
