Advertisement
Zeda

FloodFill

Jun 14th, 2014
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Flood:
  2. ;Inputs:
  3. ;     HL points to the buffer (cannot be at saveSScreen)
  4. ;     BC=(x,y)
  5. ;Needs these RAM addresses:
  6. ;     TempWord1
  7. ;     TempWord2
  8. ;     TempWord3
  9.  
  10.      ld hl,(TempWord1)
  11.      push bc
  12.      ld de,86ECh
  13.      xor a
  14.      ld bc,768
  15.        ld (de),a
  16.        inc de
  17.        dec c
  18.        jr nz,$-3
  19.        dec b
  20.        jr nz,$-6
  21.      pop bc
  22.      push bc
  23.      call GetPixelLoc
  24.      pop bc
  25.      ret nc
  26.      ld de,(TempWord1)
  27.      add hl,de
  28.      and (hl)
  29.      ret nz
  30. FloodFill:
  31.      ld de,1   ;Direction
  32. ;DE is the direction (x,y)
  33. ;BC is the current coordinates
  34. FloodFillLoop:
  35.      push bc
  36.      call GetPixelLoc
  37.      jr nc,ChangeDirection
  38.      push hl
  39.      push af
  40.      ld bc,(TempWord1)
  41.      add hl,bc
  42.      and (hl)
  43.      ld (TempWord3),hl
  44.      pop hl
  45.      ld a,h
  46.      pop hl
  47.      jr nz,ChangeDirection
  48.  
  49.      ld bc,86ECh
  50.      add hl,bc
  51.      ld b,a
  52.      or (hl)
  53.      ld (hl),a
  54.      ld a,b
  55.  
  56.  
  57.  
  58. ;     ld bc,86ECh
  59. ;     add hl,bc
  60. ;     or (hl)
  61. ;     ld (hl),a
  62.  
  63. ;     pop hl
  64. ;     ld bc,(TempWord1)
  65. ;     add hl,bc
  66. ;     pop af
  67. ;     ld b,a
  68. ;     and (hl)
  69. ;     ld a,b
  70. ;     jr nz,ChangeDirection
  71.  
  72.      ld hl,(TempWord3)
  73.      pop bc
  74.      ld (TempWord2),bc
  75.      or (hl)
  76.      ld (hl),a
  77. FloodFillLoop0:
  78.      ld a,d
  79.      add a,b
  80.      ld b,a
  81.      ld a,e
  82.      add a,c
  83.      ld c,a
  84.      jp FloodFillLoop
  85. ChangeDirection:
  86.      pop bc
  87.      ld bc,(TempWord2)
  88.      ld a,d
  89.      or a
  90.      jr nz,UpDown
  91.        call CheckLeftRight
  92.        jp z,FloodFillLoop0
  93.        jr NextPath
  94. UpDown:
  95.      call CheckUpDown
  96.      jp z,FloodFillLoop0
  97. NextPath:
  98.      ld hl,86ECh
  99.      xor a
  100.      ld d,a
  101.      ld e,a
  102.      ld c,64
  103.      ld b,12
  104. PathFind:
  105.        cp (hl)
  106.        jr z,$+15
  107.          push bc
  108.          push de
  109.          push hl
  110.          ld a,(hl)
  111.          call PathFound
  112.          pop hl
  113.          jr z,PathSelected
  114.          xor a
  115.          pop de
  116.          pop bc
  117.        inc hl
  118.        inc d
  119.        djnz PathFind
  120.        ld d,0
  121.        inc e
  122.        dec c
  123.        jr nz,PathFind-2
  124.      jp BufferToLCD
  125. PathSelected:
  126.      pop hl
  127.      pop hl
  128.      jp FloodFillLoop0
  129. PathFound:
  130.      ld c,e
  131.      ld b,d
  132.      sla b
  133.      sla b
  134.      sla b
  135.      dec b
  136.      ld a,(hl)
  137. FindXVal:
  138.        inc b
  139.        add a,a
  140.        jr nc,$-2
  141.      ld (TempWord2),a
  142.      ld (TempWord3),bc
  143. ;Now BC has the adjusted coordinates
  144.      call CheckLeftRight
  145.      ret z
  146.      call CheckUpDown
  147.      ret z
  148.      call GetPixelLoc
  149.      ld bc,86ECh
  150.      add hl,bc
  151.      cpl
  152.      and (hl)
  153.      ld (hl),a
  154.      ld a,(TempWord2)
  155.      ld bc,(TempWord3)
  156.      or a
  157.      jr nz,FindXVal
  158.      inc a
  159.      ret
  160. CheckLeftRight:
  161.      ld de,$FF00
  162.      push bc
  163.      dec b
  164.      call GetPixelLoc
  165.      jr nc,$+11
  166.        ld bc,(TempWord1)
  167.        add hl,bc
  168.        and (hl)
  169.        jr nz,$+4
  170.        pop bc
  171.        ret z
  172.      pop bc
  173.      ld d,1
  174.      push bc
  175.      inc b
  176.      call GetPixelLoc
  177.      jr nc,$+11
  178.        ld bc,(TempWord1)
  179.        add hl,bc
  180.        and (hl)
  181.        jr nz,$+4
  182.        pop bc
  183.        ret z
  184.      inc b
  185.      pop bc
  186.      ret
  187. CheckUpDown:
  188.      ld de,$00FF
  189.      push bc
  190.      dec c
  191.      call GetPixelLoc
  192.      jr nc,$+11
  193.        ld bc,(TempWord1)
  194.        add hl,bc
  195.        and (hl)
  196.        jr nz,$+4
  197.        pop bc
  198.        ret z
  199.      pop bc
  200.      ld e,1
  201.      push bc
  202.      inc c
  203.      call GetPixelLoc
  204.      jr nc,$+11
  205.        ld bc,(TempWord1)
  206.        add hl,bc
  207.        and (hl)
  208.        jr nz,$+4
  209.        pop bc
  210.        ret z
  211.      inc c
  212.      pop bc
  213.      ret
  214. GetPixelLoc:
  215. ;Input:
  216. ;     b is X
  217. ;     c is y
  218. ;Output:
  219. ;     HL points to byte
  220. ;     A is the mask
  221. ;     nc if not computed, c if computed
  222.      ld a,c
  223.      cp 64 \ ret nc
  224.      ld a,b
  225.      cp 96 \ ret nc
  226.      ld l,c
  227.      ld h,0
  228.      ld b,h
  229.      add hl,hl
  230.      add hl,bc
  231.      add hl,hl
  232.      add hl,hl
  233.      ld b,a
  234.      rrca \ rrca \ rrca
  235.      and 0Fh
  236.      ld c,a
  237.      ld a,b
  238.      ld b,0
  239.      add hl,bc
  240.      and 7
  241.      ld b,a
  242.      inc b
  243.      ld a,1
  244.        rrca
  245.        djnz $-1
  246.      scf
  247.      ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement