home *** CD-ROM | disk | FTP | other *** search
/ ticalc.org / ticalc_org_rev_b.iso / archives / 83 / asm / source / sprite.z80 < prev    next >
Encoding:
Text File  |  2001-07-01  |  8.8 KB  |  276 lines

  1. ;┌────────────────┬────────────────┬─────────────────────┬───────┬────────────┐
  2. ;│█████ Z80 ██████│  Sprite  demo  │█████████████████████│ movax │████████████│
  3. ;└────────────────┴────────────────┴─────────────────────┴───────┴────────────┘
  4.  
  5. ; Sprite example 1 v1.0
  6. ; Coded by Hannes Edfeldt in 1997
  7.  
  8. ; This little program demonstrates the basics of DRWSPR and CLRSPR.
  9.  
  10. ; Feel free to use these routines in your own productions as long as you give
  11. ; me some credit.
  12.  
  13. ; This file should of course be viewed in a DOS texteditor ;)
  14.  
  15. ; Hannes Edfeldt -+- movax@algonet.se -+- http://www.algonet.se/~movax
  16.  
  17.         .org     9327h
  18.  
  19. ;┌────────────────┬────────────────┬─────────────────────┬───────┬────────────┐
  20. ;│█████ Z80 ██████│      CODE      │█████████████████████│ movax │████████████│
  21. ;└────────────────┴────────────────┴─────────────────────┴───────┴────────────┘
  22.  
  23. START:
  24.         call    RINDOFF         ; Turn off runindicator
  25.         call    BUFCLR          ; Clear the graphbuf
  26.         call    BUFCOPY         ; Copy the graphbuf to the LCD
  27.         call    WAITKEY         ; Wait for a key
  28.  
  29.  
  30. ;████   Draw the sprite   ████
  31.  
  32.         ld      a,44            ; Load the x-coordinate in a
  33.         ld      e,28            ; Load the y-coordinate in e
  34.         ld      hl,ALIEN        ; Load the address to the sprite in hl
  35.         call    DRWSPR          ; Draw the sprite in the graphbuf
  36.         call    BUFCOPY         ; Copy the graphbuf to the LCD
  37.  
  38.         call    WAITKEY         ; Wait for a key
  39.  
  40.  
  41. ;████   Clear the sprite   ████
  42.  
  43.         ld      a,44            ; Load the x-coordinate in a
  44.         ld      e,28            ; Load the y-coordinate in e
  45.         ld      hl,ALIEN        ; Load the address to the sprite in hl
  46.         call    CLRSPR          ; Clear the sprite in the graphbuf
  47.         call    BUFCOPY         ; Copy the graphbuf to the LCD
  48.  
  49.         call    WAITKEY         ; Wait for a key
  50.  
  51.  
  52. ;████   Exit the program   ████
  53.  
  54.         call    CLRTSHD         ; Clear textshadow
  55.         call    GOHOME          ; Leave graphscreen and go to homescreen
  56.         call    BUFCLR          ; Clear the graphbuf
  57.         call    READKEY         ; Catch the clear press
  58.         call    HOMEUP          ; Place cursor at home
  59.         ret                     ; Exit program
  60.  
  61. ;┌────────────────┬────────────────┬─────────────────────┬───────┬────────────┐
  62. ;│█████ Z80 ██████│   PROCEDURES   │█████████████████████│ movax │████████████│
  63. ;└────────────────┴────────────────┴─────────────────────┴───────┴────────────┘
  64.  
  65. ;▄████████████▀ DRWSPR ▀███████████████████████████████████████████████████████
  66. ;┌────────────────────────────────────────────────────────────────────────────┐
  67. ;│ Draw 8x8 sprite ■ a=x, e=y, hl=sprite address                              │
  68. ;└────────────────────────────────────────────────────────────────────────────┘
  69. DRWSPR:
  70.  
  71.         push    hl              ; Save sprite address
  72.  
  73. ;████   Calculate the address in graphbuf   ████
  74.  
  75.         ld      hl,0            ; Do y*12
  76.         ld      d,0
  77.         add     hl,de
  78.         add     hl,de
  79.         add     hl,de
  80.         add     hl,hl
  81.         add     hl,hl
  82.  
  83.         ld      d,0             ; Do x/8
  84.         ld      e,a
  85.         srl     e
  86.         srl     e
  87.         srl     e
  88.         add     hl,de
  89.  
  90.         ld      de,8e29h
  91.         add     hl,de           ; Add address to graphbuf
  92.  
  93.         ld      b,00000111b     ; Get the remainder of x/8
  94.         and     b
  95.         cp      0               ; Is this sprite aligned to 8*n,y?
  96.         jp      z,ALIGN
  97.  
  98.  
  99. ;████   Non aligned sprite blit starts here   ████
  100.  
  101.         pop     ix              ; ix->sprite
  102.         ld      d,a             ; d=how many bits to shift each line
  103.  
  104.         ld      e,8             ; Line loop
  105. LILOP:  ld      b,(ix+0)        ; Get sprite data
  106.  
  107.         ld      c,0             ; Shift loop
  108.         push    de
  109. SHLOP:  srl     b
  110.         rr      c
  111.         dec     d
  112.         jp      nz,SHLOP
  113.         pop     de
  114.  
  115.         ld      a,b             ; Write line to graphbuf
  116.         or      (hl)
  117.         ld      (hl),a
  118.         inc     hl
  119.         ld      a,c
  120.         or      (hl)
  121.         ld      (hl),a
  122.  
  123.         ld      bc,11           ; Calculate next line address
  124.         add     hl,bc
  125.         inc     ix              ; Inc spritepointer
  126.  
  127.         dec     e
  128.         jp      nz,LILOP        ; Next line
  129.  
  130.         jp      DONE1
  131.  
  132.  
  133. ;████   Aligned sprite blit starts here   ████
  134.  
  135. ALIGN:                          ; Blit an aligned sprite to graphbuf
  136.         pop     de              ; de->sprite
  137.         ld      b,8
  138. ALOP1:  ld      a,(de)
  139.         or      (hl)            ; xor=erase/blit
  140.         ld      (hl),a
  141.         inc     de
  142.         push    bc
  143.         ld      bc,12
  144.         add     hl,bc
  145.         pop     bc
  146.         djnz    ALOP1
  147.  
  148. DONE1:
  149.         ret
  150. ;▄████████████▄ DRWSPR ▄███████████████████████████████████████████████████████
  151.  
  152.  
  153. ;▄████████████▀ CLRSPR ▀███████████████████████████████████████████████████████
  154. ;┌────────────────────────────────────────────────────────────────────────────┐
  155. ;│ Clear 8x8 sprite ■ a=x, e=y, hl=sprite address                             │
  156. ;└────────────────────────────────────────────────────────────────────────────┘
  157. CLRSPR:
  158.         push    hl              ; Save sprite address
  159.  
  160. ;████   Calculate the address in graphbuf   ████
  161.  
  162.         ld      hl,0            ; Do y*12
  163.         ld      d,0
  164.         add     hl,de
  165.         add     hl,de
  166.         add     hl,de
  167.         add     hl,hl
  168.         add     hl,hl
  169.  
  170.         ld      d,0             ; Do x/8
  171.         ld      e,a
  172.         srl     e
  173.         srl     e
  174.         srl     e
  175.         add     hl,de
  176.  
  177.         ld      de,8e29h
  178.         add     hl,de           ; Add address to graphbuf
  179.  
  180.         ld      b,00000111b     ; Get the remainder of x/8
  181.         and     b
  182.         cp      0               ; Is this sprite aligned to 8*n,y?
  183.         jp      z,ALIGN2
  184.  
  185.  
  186. ;████   Non aligned sprite erase starts here   ████
  187.  
  188.         pop     ix              ; ix->sprite
  189.         ld      d,a             ; d=how many bits to shift each line
  190.  
  191.         ld      e,8             ; Line loop
  192. LILOP2: ld      b,(ix+0)        ; Get sprite data
  193.  
  194.         ld      c,0             ; Shift loop
  195.         push    de
  196. SHLOP2: srl     b
  197.         rr      c
  198.         dec     d
  199.         jp      nz,SHLOP2
  200.         pop     de
  201.  
  202.         ld      a,b             ; Write line to graphbuf
  203.         cpl
  204.         and     (hl)
  205.         ld      (hl),a
  206.         inc     hl
  207.         ld      a,c
  208.         cpl
  209.         and     (hl)
  210.         ld      (hl),a
  211.  
  212.         ld      bc,11           ; Calculate next line address
  213.         add     hl,bc
  214.         inc     ix              ; Inc spritepointer
  215.  
  216.         dec     e
  217.         jp      nz,LILOP2       ; Next line
  218.  
  219.         jp      DONE5
  220.  
  221.  
  222. ;████   Aligned sprite erase starts here   ████
  223.  
  224. ALIGN2:                         ; Erase an aligned sprite in graphbuf
  225.         pop     de              ; de->sprite
  226.         ld      b,8
  227. ALOP2:  ld      a,(de)
  228.         cpl
  229.         and     (hl)
  230.         ld      (hl),a
  231.         inc     de
  232.         push    bc
  233.         ld      bc,12
  234.         add     hl,bc
  235.         pop     bc
  236.         djnz    ALOP2
  237.  
  238. DONE5:
  239.         ret
  240. ;▄████████████▄ CLRSPR ▄███████████████████████████████████████████████████████
  241.  
  242. ;┌────────────────┬────────────────┬─────────────────────┬───────┬────────────┐
  243. ;│█████ Z80 ██████│     EQUALS     │█████████████████████│ movax │████████████│
  244. ;└────────────────┴────────────────┴─────────────────────┴───────┴────────────┘
  245.  
  246. WAITKEY .equ     4CFEh  ; Wait for a key and read
  247. BUFCLR    .equ     515Bh    ; Clear the graph backup
  248. BUFCOPY .equ     5164h    ; Copy the graph backup to the screen
  249. RINDOFF .equ     4795h    ; Turn off runindicator
  250. PRINTHL .equ     4709h    ; Print HL in dec. on the screen
  251. OP2TOP1 .equ     41C2h    ; Move OP2 to OP1
  252. CONVOP1 .equ     4EFCh    ; Convert fp value in OP1 to a 2 byte hex
  253. READKEY .equ     4A18h    ; Read key and place it in OP2 as a fp value
  254. GOHOME    .equ     47A1h    ; Go to home screen (finish gfx program)
  255. CLRTSHD .equ     4765h    ; Clear text shadow
  256. HOMEUP    .equ     4775h    ; Place cursor at home
  257. STRING    .equ     470Dh    ; Print 0 terminated string to screen (hl->string)
  258.  
  259. ;┌────────────────┬────────────────┬─────────────────────┬───────┬────────────┐
  260. ;│█████ Z80 ██████│      DATA      │█████████████████████│ movax │████████████│
  261. ;└────────────────┴────────────────┴─────────────────────┴───────┴────────────┘
  262.  
  263. ALIEN   .db     00011000b
  264.         .db     01111110b
  265.         .db     11111111b
  266.         .db     10111101b
  267.         .db     10011001b
  268.         .db     11111111b
  269.         .db     01111110b
  270.         .db     00111100b
  271.  
  272. .end
  273. ;┌────────────────┬────────────────┬─────────────────────┬───────┬────────────┐
  274. ;│█████ Z80 ██████│  Sprite  demo  │█████████████████████│ movax │████████████│
  275. ;└────────────────┴────────────────┴─────────────────────┴───────┴────────────┘
  276.