Advertisement
Zeda

Slender Render

Oct 2nd, 2015
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. max_render_distance=15
  2. slender_render:
  3. ;;HL points to the object list
  4.     ld a,(hl)   ;\
  5.     cp -1       ; |If the first item is the EOL, there is nothing to draw, so just RET
  6.     ret z       ;/
  7. checkObjLoop:
  8.     inc hl      ; |Get the y-coord
  9.     ld e,(hl)   ;/
  10.     inc hl      ; |Get the x-coord
  11.     ld d,(hl)   ;/
  12.     push hl
  13.     push af
  14.     call isObj_in_view  ;check if the object is in view
  15.     pop bc
  16.     jr c,checkObjloop
  17.     ex de,hl
  18.     ld hl,render_queue
  19.     inc (hl)
  20.     ld a,(hl)
  21.     add a,a
  22.     add a,(hl)
  23.     scf
  24.     adc a,l
  25.     ld l,a
  26.     jr nc,$+3
  27.     ld h,a
  28.     ld (hl),e   ;Save the on-screen Y coordinate
  29.     inc hl
  30.     ld (hl),d   ;save the on-screen X coordinate
  31.     inc hl
  32.     ld (hl),b   ;save the object ID
  33.     pop hl
  34.     inc hl      ;\
  35.     ld a,(hl)   ; |Check if next object is EOL, if not, reloop
  36.     cp -1       ;/
  37.     jp nz,checkObjLoop
  38. sortQueue:
  39. ;;These will be small lists, so bubble sort is sufficient
  40. ;;Sort furthest to closest
  41.     ld hl,render_queue
  42.     ld a,(hl)
  43.     or a
  44.     ret z
  45.     dec a
  46.     ret z
  47.     ld b,a
  48.     inc hl
  49. sortloop_outer:
  50.     ld a,(hl)
  51.     push hl
  52.     push bc
  53.     ld de,3
  54. sortloop_inner:
  55.     add hl,de
  56.     cp (hl)
  57.     jr nc,$+6
  58.     ld (max),hl
  59.     ld a,(hl)
  60.     djnz sortloop_inner
  61. ;swap first element with max element
  62.     pop bc
  63.     pop hl
  64. max=$+1
  65.     ld de,0
  66.     ld e,(hl) \ ld (hl),a \ ld a,e \ ld (de),a \ inc hl \ inc de
  67.     ld a,(de) \ ld e,(hl) \ ld (hl),a \ ld a,e \ ld (de),a \ inc hl \ inc de
  68.     ld a,(de) \ ld e,(hl) \ ld (hl),a \ ld a,e \ ld (de),a \ inc hl
  69.     djnz sortloop_outer
  70.     ret
  71. renderQueue:
  72. ;;draw the objects in the render queue
  73. ;;Objects are listed by their on-screen coordinate and ID
  74.     ;=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%
  75.     ;Need To Do
  76.     ;=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%=%
  77.     ret
  78. isObj_in_view:
  79. ;;Input: DE=(x,y) of the obj
  80. ;;Output: If the object is not in view, return c flag set.
  81. ;;      Otherwise c flag reset, (H,L) are the on-screen (x,y) coordinates of the object
  82.     ld hl,(playerY)
  83.     ld a,d \ sub h \ ld c,a
  84.     ld a,e \ sub l \ ld b,a ;c=x, b=y
  85.     call rotate
  86.     ld c,a
  87.     ld a,b
  88.     add a,a
  89.     ret c
  90.     cp max_render_distance*2
  91.     cpl
  92.     ret c       ;B is the Y
  93.     ld a,c
  94.     rla
  95.     rra
  96.     jr z,$+6 \ neg \ ld h,48*4
  97.     add a,a \ ret c
  98.     add a,a \ ret c
  99.     sub b
  100.     ccf
  101.     ret c
  102.     add a,b
  103. ;px =48+4r/s*3
  104. ;   =48+3a/c-h/4
  105.     ld d,0
  106.     add a,a \ cp b \ jr nc,$+5 \ sub b \ ld d,2
  107.     add a,a \ cp b \ jr nc,$+4 \ sub b \ inc d
  108.     add a,a \ sub b \ jr nc,$+3 \ add a,b \ rl e
  109.     add a,a \ sub b \ jr nc,$+3 \ add a,b \ rl e
  110.     add a,a \ sub b \ jr nc,$+3 \ add a,b \ rl e
  111.     add a,a \ sub b \ jr nc,$+3 \ add a,b \ rl e
  112.     add a,a \ sub b \ jr nc,$+3 \ add a,b \ rl e
  113.     add a,a \ sub b \ jr nc,$+3 \ add a,b \ rl e
  114.     add a,a \ sub b \ jr nc,$+3 \ add a,b \ rl e
  115.     add a,a \ sub b \ ld a,e \ rla \ cpl \ ld e,a
  116. ;px=(DE*3-H+48*4)/4
  117.     ld a,48*4 \ sub h
  118.     ld h,d
  119.     ld l,e
  120.     add hl,hl
  121.     add hl,de
  122.     add a,l
  123.     jr nc,$+4
  124.     inc h
  125.     rr h \ rra
  126.     rr h \ rra      ;px=a
  127.     ld h,a          ;px
  128. calc_py:
  129. #if max_render_distance==10
  130. ;py=64/10*B
  131. ;b<10
  132. ;py=6.4*B
  133.     ld a,b
  134.     add a,a
  135.     add a,b
  136.     add a,a
  137.     ld l,a
  138.     ld a,b
  139.     add a,a
  140.     add a,a
  141.     inc l \ sub 10 \ jr nc,$-2
  142.     dec l
  143. #endif
  144.  
  145. #if max_render_distance==15
  146. ;py=64/15*B
  147. ;b<15
  148. ;py=(4+4/15)*B
  149.     ld a,b
  150.     add a,a
  151.     add a,a
  152.     ld l,a
  153.     inc l \ sub 15 \ jr nc,$-2
  154.     dec l
  155. #endif
  156. #if max_render_distance==16
  157. ;py=64/16*B
  158. ;b<16
  159. ;py=4*B
  160.     ld a,b
  161.     add a,a
  162.     add a,a
  163.     ld l,a
  164. #endif
  165.  
  166. if calc_py==$
  167. .echo "this max_render_distance is not supported in computing py=64/max_render_distance*C"
  168. #endif
  169.     or a
  170.     ret
  171. rotate:
  172. ;;Input:
  173. ;;  B is the Y coordinate in respect to the user
  174. ;;  C is the X coorinate in respect to the user
  175. ;;Output:
  176. ;;  B = Y, rotated
  177. ;;  A = X, rotated
  178. ;;x=xcos+ysin   = C*(playervector+1)+B*(playervector)
  179. ;;y=ycos-xsin   =
  180.     ld a,(playervector) \ xor b         ;\
  181.     push af \ xor b \ ld h,b \ ld d,0   ; |
  182.     ld l,d \ and $7F \ res 7,h \ ld e,a ; |
  183.     sla h \ sla h \ jr nc,$+3 \ ld l,e  ; |
  184.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |ysin
  185.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  186.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  187.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  188.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  189.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  190.     sla l \ jr nc,$+3 \ inc h           ; |
  191.     pop af \ ld a,h \ jp p,$+5 \ neg    ; |
  192.     ld (ysin),a                         ;/
  193.     ld a,(playervector)
  194.     xor c
  195.     push af
  196.     ld h,c
  197.     ld l,d
  198.     sla h \ sla h \ jr nc,$+3 \ ld l,e  ;\
  199.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |xsin
  200.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  201.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  202.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  203.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  204.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  205.     sla l \ jr nc,$+3 \ inc h           ; |
  206.     pop af \ ld a,h \ jp p,$+5 \ neg    ; |
  207.     ld (xsin),a                         ;/
  208.     ld a,(playervector+1) \ xor b       ;\
  209.     push af \ xor b \ ld h,b \ ld d,0   ; |
  210.     ld l,d \ and $7F \ res 7,h \ ld e,a ; |
  211.     sla h \ sla h \ jr nc,$+3 \ ld l,e  ; |
  212.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |ycos
  213.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  214.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  215.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  216.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  217.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  218.     sla l \ jr nc,$+3 \ inc h           ; |
  219.     pop af \ ld a,h \ jp p,$+5 \ neg    ;/
  220. xsin=$+1
  221.     ld h,0
  222.     sub h
  223.     ld b,a      ;new Y
  224.     ld a,(playervector+1)
  225.     xor c
  226.     push af
  227.     ld h,c
  228.     ld l,d
  229.     sla h \ sla h \ jr nc,$+3 \ ld l,e  ;\
  230.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |xcos
  231.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  232.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  233.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  234.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  235.     add hl,hl \ jr nc,$+3 \ add hl,de   ; |
  236.     pop af \ ld a,h \ jp p,$+5 \ neg    ;/
  237.     sla l
  238. ysin=$+1
  239.     ld h,0
  240.     adc a,h
  241.     ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement