home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / OTSTARS.ZIP / stars3d.asm < prev    next >
Assembly Source File  |  1996-02-06  |  7KB  |  236 lines

  1. ; A 3d starfield in (t)asm
  2. ; By Vulture / Outlaw Triad
  3.  
  4. DOSSEG                ; Init program
  5. .MODEL SMALL
  6. .STACK 200h
  7. .386
  8. .DATA
  9. .CODE
  10. JUMPS
  11.  
  12. Label Credits Byte
  13.         db 13,10,"▄  ▄▄  ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄  ▄▄  ▄"
  14.         db 13,10,"                    - An Outlaw Triad Production (c) 1996 -",13,10
  15.         db 13,10,"                             Code∙∙∙∙∙∙∙∙∙∙Vulture" ,13,10,13,10
  16.         db 13,10,"                            -=≡ Outlaw Triad Is ≡=-",13,10
  17.         db 13,10,"         Vulture(code) ■ Dazl(artist) ■ Troop(sysop) ■ Xplorer(artist)",13,10
  18.         db 13,10,"▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄",13,10,"$"
  19.  
  20. Label Palette Byte
  21.         db  0,0,0       ; Base color black => R,G,B = 0,0,0
  22.         i=63
  23.         REPT 16         ; 16 grey shades (from bright to black)
  24.           db  i,i,i
  25.           i=i-4
  26.         ENDM
  27.  
  28. Star_3d STRUC           ; Format of star
  29.      X   DW   0         ; X-position of star
  30.      Y   DW   0         ; Y-position of star
  31.      Z   DW   0         ; Z-position of star
  32.      Old DW   0         ; Where to erase old star
  33. Star_3d ENDS
  34.  
  35. StarSize = 8            ; 4 wordz are 8 bytes
  36.  
  37. MaxStar equ 250         ; What would thiz be, eh? ;-)
  38.  
  39. Stars   Star_3d MaxStar DUP (?) ; Array to hold all star data
  40.  
  41. Speed   equ 1           ; Guess what?
  42.  
  43. Xoff    equ 160
  44. Yoff    equ 100         ; To center the star on the vga
  45. Zoff    equ 256         ; Z-value at start (distance, try and change this)
  46.  
  47. Seed    dw  ?           ; Random number seed (obtained from system clock)
  48.  
  49. ; =====================================
  50. ;  WaitVrt: Waits for vertical retrace
  51. ; =====================================
  52.  
  53. WaitVrt PROC NEAR                  ; Waits for vertical retrace
  54.     mov     dx,3dah                ; to avoid "snow"
  55. Vrt:
  56.     in      al,dx
  57.     test    al,1000b
  58.     jnz     Vrt
  59. NoVrt:
  60.     in      al,dx
  61.     test    al,1000b
  62.     jz      NoVrt
  63.     ret
  64. WaitVrt ENDP
  65.  
  66. ; =================================
  67. ;  Random: generates random number
  68. ;  Input : cx = range
  69. ;  Output: dx = random number
  70. ; =================================
  71.  
  72. Random PROC NEAR                   ; Reasonably random (can be improved)
  73.     mov     ax,Seed
  74.     add     ax,1234                ; Add a random number
  75.     xor     al,ah                  ; Shuffle around
  76.     rol     ah,1
  77.     add     ax,4321
  78.     ror     al,1
  79.     xor     ah,al
  80.     mov     Seed,ax
  81.     xor     dx,dx
  82.     div     cx
  83.     ret
  84. Random ENDP
  85.  
  86. ; =================================
  87. ;  InitStars: sets variables x,y,z
  88. ;             for all stars
  89. ; =================================
  90.  
  91. InitStars PROC NEAR
  92.     xor     si,si
  93.     mov     bx,MaxStar
  94. InitLoop:
  95.     mov     cx,320
  96.     call    Random
  97.     sub     dx,160
  98.     mov     [Stars.x+si],dx        ; Random X (-160..160)
  99.     mov     cx,200
  100.     call    Random
  101.     sub     dx,100
  102.     mov     [Stars.y+si],dx        ; Random Y (-100..100)
  103.     mov     [Stars.z+si],Zoff      ; Reset Z
  104.     add     si,StarSize
  105.     dec     bx
  106.     jnz     InitLoop
  107.     ret
  108. InitStars ENDP
  109.  
  110. ; =========================================
  111. ;  CreateStar: sets variables for new star
  112. ;  Input : si = pointer to current star
  113. ;  Output: variables set
  114. ; =========================================
  115.  
  116. CreateStar PROC NEAR
  117.     mov     cx,320
  118.     call    Random
  119.     sub     dx,160
  120.     mov     [Stars.x+si],dx        ; New random X (-160..160)
  121.     mov     cx,200
  122.     call    Random
  123.     sub     dx,100
  124.     mov     [Stars.y+si],dx        ; New random Y (-100..100)
  125.     mov     [Stars.z+si],Zoff      ; Reset Z
  126.     ret
  127. CreateStar ENDP
  128.  
  129. ; =====================================
  130. ;  CalcStars: updates all stars on vga
  131. ; =====================================
  132.  
  133. UpdateStars PROC NEAR
  134.     xor     si,si                  ; First star
  135.     mov     cx,MaxStar             ; Do all
  136. MainStarLoop:
  137.     push    cx
  138.  
  139.     cmp     [Stars+si].z,0         ; Reached z=0?
  140.     jbe     TermStar               ; If so, then terminate star
  141.  
  142.     mov     di,[Stars.Old+si]
  143.     mov     byte ptr es:[di],0     ; Delete old star
  144.  
  145. ; === Calc X pos ===
  146.     mov     ax,[Stars.x+si]
  147.     movsx   dx,ah
  148.     shl     ax,8                   ; ax = X * 256
  149.     mov     cx,[Stars.z+si]
  150.     idiv    cx                     ; ax = (X * 256) / Z
  151.     add     ax,Xoff                ; ax = ((X * 256) / Z) + Xoff
  152.     cmp     ax,320                 ; Rangecheck X
  153.     jae     TermStar
  154.     mov     di,ax
  155.  
  156. ; === Calc Y pos ===
  157.     mov     ax,[Stars.y+si]
  158.     movsx   dx,ah
  159.     shl     ax,8                   ; ax = Y * 256
  160.     idiv    cx                     ; ax = (Y * 256) / Z
  161.     add     ax,Yoff                ; ax = ((Y * 256) / Z) + Yoff
  162.     cmp     ax,200                 ; Rangecheck Y
  163.     jae     TermStar
  164.     mov     bx,320
  165.     imul    bx
  166.     add     di,ax
  167.  
  168. ; === Calc color ===               ; Z in range 0..255
  169.     mov     ax,[Stars.z+si]
  170.     mov     cx,16
  171.     idiv    cx
  172.  
  173. ; === Place dot & save vars ===
  174.     mov     byte ptr es:[di],al    ; Place star
  175.     mov     [Stars.Old+si],di      ; Save spot for erasure
  176.     sub     [Stars.z+si],Speed     ; Decrease Z (go towards viewer)
  177.     jmp     short ContinueStar     ; Do next
  178.  
  179. TermStar:
  180.     call    CreateStar             ; If z=0 or star<>range then create new 1
  181.  
  182. ContinueStar:
  183.     add     si,StarSize            ; Point to next star
  184.     pop     cx
  185.     loop    MainStarLoop           ; Loop until done
  186.  
  187.     ret
  188. UpdateStars ENDP
  189.  
  190. ; === Main Program ===
  191.  
  192. START:
  193.  
  194. ; === Set pointers ===
  195.     mov     ax,cs
  196.     mov     ds,ax                  ; Set ds
  197.     mov     ax,0a000h
  198.     mov     es,ax                  ; Set es
  199.  
  200. ; === Set random seed ===
  201.     xor     ah,ah
  202.     int     1ah
  203.     mov     [Seed],dx              ; Get random seed from clock
  204.  
  205. ; === Init vga ===
  206.     mov     ax,0013h
  207.     int     10h                    ; Vgamode 13h 320*200*256 (1 page)
  208.  
  209.     lea     si,Palette             ; Set palette
  210.     mov     dx,03c8h
  211.     xor     al,al
  212.     out     dx,al
  213.     inc     dx
  214.     mov     cx,17*3                ; 17 colors
  215.     rep     outsb
  216.  
  217. ; === Do starstuff ===
  218.     call    InitStars              ; Set all variables for stars
  219.  
  220. MainLoop:
  221.     call    UpdateStars            ; Next frame
  222.     call    WaitVrt                ; Wait for vertical retrace
  223.     in      al,60h                 ; Check for escape
  224.     cmp     al,1
  225.     jne     MainLoop
  226.  
  227. ; === Quit to DOS ===
  228.     mov     ax,0003h               ; Back to textmode
  229.     int     10h
  230.     lea     dx,Credits
  231.     mov     ah,9
  232.     int     21h                    ; Write string
  233.     mov     ax,4c00h               ; Return control to DOS
  234.     int     21h
  235.  
  236. END START