home *** CD-ROM | disk | FTP | other *** search
/ ticalc.org / ticalc_org_rev_b.iso / archives / 85 / asm / source / routines / nasr.asm < prev    next >
Encoding:
Assembly Source File  |  2001-07-01  |  13.9 KB  |  553 lines

  1. ;***********************************************
  2. ;       NASR WARP - by Jingyang Xu             * 
  3. ;   (br00416@bingsuns.cc.binghamton.edu)       * 
  4. ; Include this header if you wish to use       *
  5. ;       the subroutines please                 *
  6. ;***********************************************
  7.  
  8. ******************
  9. *Revision History*
  10. ******************
  11. 1.0 - First version - scanned sprite pixel-by-pixel and PTON
  12. 1.5 - 70% speed increase - only use FINDPIXEL once
  13. 1.9 - 100% speed increase from 1.0 - optimized routines
  14. 2.0a - first attempt at byte-shifting, didn't work
  15. 2.0b - swapped b and c registers, first working version of 
  16. byte-shifter - 200% increase of speed over 1.0
  17. 2.1 - fixed periodic cutting off of the right side of sprite
  18. WARP - fixed extra blank pixels "region of influence" around 
  19. sprite (I hope I don't get sued by IBM)
  20. WARP fix - replaced fake NASR 2.1 with real one.
  21. WARP fix 2 - fixed WARP clipping problem, added fixed 
  22. transparent version of NASR, fixed hellosprite
  23. WARP addon 1 - Erasure built into NASRWARP
  24.  
  25. *******
  26. *Usage*
  27. *******
  28. Included with this file are NASR (Non-Aligned Sprite Routine) 
  29. 2.1 and WARP and TRANS. NASR 2.1 and WARP will write a non-transparent
  30. sprite to the screen, to erase, a blank sprite of the same size must be 
  31. defined*. 2.1 is included because it is a bit faster and 
  32. smaller than WARP, but it will overwrite up to 8 blank pixels to the
  33. right and left of each line of the sprite. So if you are using 
  34. big sprites or don't mind the overwriting, use 2.1 instead of 
  35. WARP. NASRTRANS is a working transparent version of NASR; 
  36. Erasure should be done by reading the area that the sprite is 
  37. drawn over.
  38.  
  39. --------------------------
  40. * this version of NASR allows you to erase using the same sprite, but only with NASR WARP.
  41.                                                                           
  42. If you have a sprite that is x wide and y high then:
  43.  
  44. a=smallest integer greater than or equal to x/8
  45. b=a*8-x+1
  46. c=y
  47.  
  48. Define a sprite like this:
  49.  
  50. Sprite:
  51.        .db     a,b,c  <- 3 bytes
  52.        .db     %xxxxxxxx,%xxxxxxxx,%xxxxxxxx,... <- first line of sprite
  53.        .db     %xxxxxxxx,%xxxxxxxx,%xxxxxxxx,... <- 2nd line
  54.        .db     ....                              <- other lines like above
  55.  
  56. Each of the x's is a 1 or a 0, depending on if you want the 
  57. pixel on or off. If a sprite have a length that's not a 
  58. multiple of 8, then just leave the unused bits of each line 
  59. to be 0 (they can be anything you want, just fill them in)
  60. Example - the following sprite is a "hello" (the 0's have been 
  61. changed to a "." to make the sprite stand out)
  62.  
  63. Hello_Sprite:
  64.         .db     3,6,5
  65.         .db     %1.1.111., %1...1..., %111.....
  66.         .db     %1.1.1..., %1...1..., %1.1.....
  67.         .db     %111.111., %1...1..., %1.1.....
  68.         .db     %1.1.1..., %1...1..., %1.1.....
  69.         .db     %1.1.111., %111.111., %111.....
  70.  
  71. Input - (x,y) coordinates from bottom left corner of screen 
  72. are passed in b and c respectively, these define the upper 
  73. left corner of the sprite. The address of the sprite must be 
  74. in hl.
  75. So if you want to blit the sprite to 2,55 on the screen, 
  76. you'd do this:                  
  77.         ld      b,2
  78.         ld      c,55
  79.         ld      hl,Hello_Sprite
  80.         ld      de,(PROGRAM_ADDR)
  81.         add     hl,de
  82.         CALL_(NASRWARP)
  83. *******
  84. *Setup*
  85. *******
  86. You must define three (four for NASRWARP) variables in Text Memory:
  87.  
  88. TempLine = $80df
  89. TempLine is a storage buffer for the each line of the sprite 
  90. before it is blitted to the screen. The number of bytes you 
  91. allot to TempLine determines the maximum width of the sprite. 
  92. In this setup, 8 bytes is allotted to TempLine, therefore the 
  93. maximum width is 64 bytes.
  94.  
  95. PicCoor  = $80e7    
  96. Two byte space needed by the program to store on-screen 
  97. coordinates
  98.  
  99. offset   = $80e9
  100. One byte space to store c
  101.  
  102. NASR_Status = $80ea (NASRWARP only, do not use with NASR 2.1 or NASR TRANS)
  103. One byte space: load a zero in this prior to calling NASRWARP to have it 
  104. erase your sprite, load anything else in to have it draw your sprite.
  105.  
  106. **********
  107. *Routines*
  108. **********
  109.  
  110. NASRWARP:    
  111.         push    bc
  112.         ex      de,hl
  113.         ROM_CALL(FIND_PIXEL)
  114.         ld      bc,$fc00
  115.         add     hl,bc
  116.         ld      (PicCoor),hl
  117.         ex      de,hl
  118.         ld      b,$ff
  119. Patch:
  120.         rla
  121.         inc     b
  122.         jr      nc,Patch
  123.         ld      a,(hl)
  124.         ld      c,a      
  125.         inc     hl
  126.         ld      a,(hl)
  127.         ld      (offset),a
  128.         ld      a,b   
  129.         ld      e,c
  130.         cp      (hl)
  131.         jr      c,DS1
  132.         inc     e
  133. DS1:
  134.         inc     hl
  135.         ld      a,(hl)
  136.         ld      d,a
  137.         inc     hl
  138. YLoop:            
  139.         push    bc 
  140.         push    de
  141.         ld      b,8
  142.         ex      de,hl
  143.         ld      hl,TempLine
  144. InitTempLineLoop:
  145.         ld      (hl),$ff
  146.         inc     hl
  147.         djnz    InitTempLineLoop
  148.         ex      de,hl
  149.     ld    a,(NASR_Status)
  150.     cp    0
  151.     jr    z,NASRErase
  152. NASRDraw:
  153.         ld      b,0
  154.         ld      de,TempLine
  155.         ldir       
  156.     jr    NASRDrawFin
  157. NASRErase:
  158.     ld    b,c
  159.     ld    hl,TempLine
  160. LoadEmpty:
  161.     ld    (hl),0
  162.     inc    hl
  163.     djnz    LoadEmpty
  164. NASRDrawFin:
  165.         pop     de
  166.         pop     bc
  167.         push    hl
  168.         push    bc         
  169.         push    de
  170.         ld      d,b
  171.         ld      a,b
  172.         cp      0
  173.         jr      z,skipshift
  174. SpriLoop1:
  175.         ld      a,b    
  176.         ld      HL,TempLine
  177.         ld      b,e
  178.         scf
  179. SpriLoop2:        
  180.         rr      (HL)
  181.         inc     HL
  182.         djnz    SpriLoop2
  183.         ld      b,a
  184.         djnz    SpriLoop1        
  185.         ld      b,d
  186. skipshift:
  187.         ld      a,$ff           ;fill accumulator
  188.         inc     b
  189. mask1:
  190.         srl     a               ;make the mask
  191.         djnz    mask1           ;something like
  192.         scf                     ;00001111 ->
  193.         rl      a                        
  194.         ld      hl,(PicCoor)    ;implement the mask
  195.         or      (hl)            ;this preserve the 0 bits
  196.         ld      hl,TempLine   ;become xxxx1111
  197.         and     (hl)            ;when anded, become
  198.         ld      (hl),a          ;xxxxyyyy
  199.         ld      b,d             ;retrieve b
  200.         ld      a,(offset)
  201.         dec     a
  202.         sub     b
  203.         jr      nc,skip
  204.         sub     248
  205. skip:
  206.         ld      b,a
  207.         inc     b
  208.         ld      a,$ff
  209. mask2:               
  210.         sla     a
  211.         djnz    mask2
  212.         scf
  213.         rr      a
  214.         dec     e
  215.         ld      hl,(PicCoor)
  216.         ld      d,0
  217.         add     hl,de
  218.         or      (hl)
  219.         ld      hl,TempLine
  220.         add     hl,de
  221.         and     (hl)
  222.         ld      (hl),a
  223.         inc     e
  224.         ld      c,e
  225.         ld      hl,(PicCoor)
  226.         ld      de,16
  227.         push    hl
  228.         add     hl,de
  229.         ld      (PicCoor),hl
  230.         pop     hl
  231.         ld      de,TempLine
  232.         ex      de,hl
  233.         ld      b,0
  234.         ldir      
  235.         pop     de
  236.         pop     bc
  237.         pop     hl
  238.         dec     d
  239.         jr      nz,YLoop
  240.         pop     bc
  241.         ret
  242.  
  243. ;**************************************************8
  244.  
  245. NASR2_1:    
  246.         push    bc
  247.         ex      de,hl
  248.         ROM_CALL(FIND_PIXEL)
  249.         ld      bc,$fc00
  250.         add     hl,bc
  251.         ld      (PicCoor),hl
  252.         ex      de,hl
  253.         ld      b,$ff
  254. Patch:
  255.         rla
  256.         inc     b
  257.         jr      nc,Patch
  258.         ld      a,(hl)
  259.         ld      c,a      
  260.         inc     hl
  261.         ld      a,b   
  262.         ld      e,c
  263.         cp      (hl)
  264.         jr      c,DS1
  265.         inc     e
  266. DS1:
  267.         inc     hl
  268.         ld      a,(hl)
  269.         ld      d,a
  270.         inc     hl
  271. YLoop:            
  272.         push    bc 
  273.         push    de
  274.         ld      b,8
  275.         ex      de,hl
  276.         ld      hl,TempLine
  277. InitTempLineLoop:
  278.         ld      (hl),0
  279.         inc     hl
  280.         djnz    InitTempLineLoop
  281.         ex      de,hl
  282.         ld      b,0
  283.         ld      de,TempLine
  284.         ldir       
  285.         pop     de
  286.         pop     bc
  287.         ld      a,b
  288.         cp      0 
  289.         CALL_NZ(ShiftLine)   
  290.         push    bc
  291.         push    hl
  292.         push    de
  293.         ld      c,e
  294.         ld      hl,(PicCoor)
  295.         ld      de,16
  296.         push    hl
  297.         add     hl,de
  298.         ld      (PicCoor),hl
  299.         pop     hl
  300.         ld      de,TempLine
  301.         ex      de,hl
  302.         ld      b,0
  303.         ldir      
  304.         pop     de
  305.         pop     hl
  306.         pop     bc
  307.         dec     d
  308.         jr      nz,YLoop
  309.         pop     bc
  310.         ret
  311.  
  312.  
  313. ShiftLine:
  314.         push    hl
  315.         push    bc 
  316. SpriLoop1:
  317.         ld      a,b    
  318.         ld      HL,TempLine
  319.         ld      b,e
  320.         and     a
  321. SpriLoop2:        
  322.         rr      (HL)
  323.         inc     HL
  324.         djnz    SpriLoop2
  325.         ld      b,a
  326.         djnz    SpriLoop1        
  327.         pop     bc
  328.         pop     hl
  329.         ret                                         
  330.  
  331. ;**********************************************************
  332.  
  333. NASRTRANS:    
  334.         push    bc
  335.         ex      de,hl
  336.         ROM_CALL(FIND_PIXEL)
  337.         ld      bc,$fc00
  338.         add     hl,bc
  339.         ld      (PicCoor),hl
  340.         ex      de,hl
  341.         ld      b,$ff
  342. Patch:
  343.         rla
  344.         inc     b
  345.         jr      nc,Patch
  346.         ld      c,(hl)
  347.         inc     hl
  348.         ld      a,b   
  349.         ld      e,c
  350.         cp      (hl)
  351.         jr      c,DS1
  352.         inc     e
  353. DS1:
  354.         inc     hl
  355.         ld      a,(hl)
  356.         ld      d,a
  357.         inc     hl
  358. YLoop:            
  359.         push    bc 
  360.         push    de
  361.         ld      b,8
  362.         ex      de,hl
  363.         ld      hl,TempLine
  364. InitTempLineLoop:
  365.         ld      (hl),0
  366.         inc     hl
  367.         djnz    InitTempLineLoop
  368.         ex      de,hl
  369.         ld      b,0
  370.         ld      de,TempLine
  371.         ldir       
  372.         pop     de
  373.         pop     bc
  374.         ld      a,b
  375.         cp      0 
  376.         CALL_NZ(ShiftLine)   
  377.         push    bc
  378.         push    hl
  379.         push    de
  380.         ld      b,e
  381.         ld      hl,(PicCoor)
  382.         ld      de,16
  383.         push    hl
  384.         add     hl,de
  385.         ld      (PicCoor),hl
  386.         pop     hl
  387.         ld      de,TempLine
  388.         ex      de,hl
  389. LoadSpriteLoop:
  390.         ld      a,(hl)
  391.         ex      de,hl
  392.         or      (hl)
  393.         ld      (hl),a
  394.         ex      de,hl
  395.         inc     de
  396.         inc     hl
  397.         djnz    LoadSpriteLoop
  398.         pop     de
  399.         pop     hl
  400.         pop     bc
  401.         dec     d
  402.         jr      nz,YLoop
  403.         pop     bc
  404.         ret
  405.  
  406.  
  407. ShiftLine:
  408.         push    hl
  409.         push    bc 
  410. SpriLoop1:
  411.         ld      a,b    
  412.         ld      HL,TempLine
  413.         ld      b,e
  414.         and     a
  415. SpriLoop2:        
  416.         rr      (HL)
  417.         inc     HL
  418.         djnz    SpriLoop2
  419.         ld      b,a
  420.         djnz    SpriLoop1        
  421.         pop     bc
  422.         pop     hl
  423.         ret
  424.  
  425. ;****************************************************************
  426.  
  427. NASRWARPErase:    
  428.         push    bc
  429.         ex      de,hl
  430.         ROM_CALL(FIND_PIXEL)
  431.         ld      bc,$fc00
  432.         add     hl,bc
  433.         ld      (PicCoor),hl
  434.         ex      de,hl
  435.         ld      b,$ff
  436. Patch:
  437.         rla
  438.         inc     b
  439.         jr      nc,Patch
  440.         ld      a,(hl)
  441.         ld      c,a      
  442.         inc     hl
  443.         ld      a,(hl)
  444.         ld      (offset),a
  445.         ld      a,b   
  446.         ld      e,c
  447.         cp      (hl)
  448.         jr      c,DS1
  449.         inc     e
  450. DS1:
  451.         inc     hl
  452.         ld      a,(hl)
  453.         ld      d,a
  454.         inc     hl
  455. YLoop:            
  456.         push    bc 
  457.         push    de
  458.         ld      b,8
  459.         ex      de,hl
  460.         ld      hl,TempLine
  461. InitTempLineLoop:
  462.         ld      (hl),$ff
  463.         inc     hl
  464.         djnz    InitTempLineLoop
  465.         ex      de,hl
  466.         ld      b,c    
  467.         ld      hl,TempLine
  468. LoadEmpty:
  469.         ld      (hl),0
  470.         inc     hl   
  471.         inc     de
  472.         djnz    LoadEmpty
  473.         ex      de,hl
  474.         pop     de
  475.         pop     bc
  476.         push    hl
  477.         push    bc         
  478.         push    de
  479.         ld      d,b
  480.         ld      a,b
  481.         cp      0
  482.         jr      z,skipshift
  483. SpriLoop1:
  484.         ld      a,b    
  485.         ld      HL,TempLine
  486.         ld      b,e
  487.         scf
  488. SpriLoop2:        
  489.         rr      (HL)
  490.         inc     HL
  491.         djnz    SpriLoop2
  492.         ld      b,a
  493.         djnz    SpriLoop1        
  494.         ld      b,d
  495. skipshift:
  496.         ld      a,$ff           ;fill accumulator
  497.         inc     b
  498. mask1:
  499.         srl     a               ;make the mask
  500.         djnz    mask1           ;something like
  501.         scf                     ;00001111 ->
  502.         rl      a                        
  503.         ld      hl,(PicCoor)    ;implement the mask
  504.         or      (hl)            ;this preserve the 0 bits
  505.         ld      hl,TempLine   ;become xxxx1111
  506.         and     (hl)            ;when anded, become
  507.         ld      (hl),a          ;xxxxyyyy
  508.         ld      b,d             ;retrieve b
  509.         ld      a,(offset)
  510.         dec     a
  511.         sub     b
  512.         jr      nc,skip
  513.         sub     248
  514. skip:
  515.         ld      b,a
  516.         inc     b
  517.         ld      a,$ff
  518. mask2:               
  519.         sla     a
  520.         djnz    mask2
  521.         scf
  522.         rr      a
  523.         dec     e
  524.         ld      hl,(PicCoor)
  525.         ld      d,0
  526.         add     hl,de
  527.         or      (hl)
  528.         ld      hl,TempLine
  529.         add     hl,de
  530.         and     (hl)
  531.         ld      (hl),a
  532.         inc     e
  533.         ld      c,e
  534.         ld      hl,(PicCoor)
  535.         ld      de,16
  536.         push    hl
  537.         add     hl,de
  538.         ld      (PicCoor),hl
  539.         pop     hl
  540.         ld      de,TempLine
  541.         ex      de,hl
  542.         ld      b,0
  543.         ldir      
  544.         pop     de
  545.         pop     bc
  546.         pop     hl
  547.         dec     d
  548.         jr      nz,YLoop
  549.         pop     bc
  550.         ret
  551.  
  552. ; Enjoy!
  553.