home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / fakesrc.zip / PART6 / LAND.ASM next >
Assembly Source File  |  1993-10-22  |  20KB  |  552 lines

  1. ;=============================================================================
  2. ; land.asm - Landscape fractal demostration.
  3. ;                                                    File created:  9/30/93
  4. ; Copyright (c) 1993, Carlos Hasan                  Last modified: 10/22/93
  5. ;
  6. ; Description:
  7. ;   This file implements a fractal 3D landscape rotation using the basic
  8. ;   plasma routines and 3D rotations. This uses the VGA 320x200x256 mode.
  9. ;
  10. ; Portability:
  11. ;  Requires Turbo Assembler 3.2 or better to be assembled.
  12. ;  Dependent on the IBM PC 286 and the VGA graphics card.
  13. ;
  14. ; Modifications:
  15. ;  10/22/93  - Startup Code. Now is an Standalone Program.
  16. ;=============================================================================
  17.  
  18.                 .model  small,pascal
  19.                 .286
  20.  
  21.                 dosseg                          ; used to link like
  22.                 .stack  1024                    ; an standalone program.
  23.  
  24.                 global  DrawPlasma:proc        ; in PLASMA.ASM file.
  25.                 global  LandScape:proc
  26.  
  27. ;===================== Demo equates and data =================================
  28.  
  29. TIMEOUT         equ     70 * 10                 ; about 10 sec for timeout.
  30. FADESPEED       equ     6                       ; fade speed.
  31. SCREENWIDTH     equ     320                     ; screen dimensions.
  32. SCREENHEIGHT    equ     200
  33. TRIGSHIFT       equ     6                       ; sin/cos shift factor.
  34. LANDSCAPEX      equ     320                     ; landscape grid dimensions.
  35. LANDSCAPEY      equ     200
  36. WATERLEVEL      equ     128                     ; plasma water level.
  37. PLASMASEED      equ     8945h                   ; plasma random seed.
  38. CENTERX         equ     160                     ; screen grid center
  39. CENTERY         equ     140                     ; position.
  40. GRIDX           equ     32                      ; screen grid dimensions
  41. GRIDY           equ     32                      ; and gaps length.
  42. GRIDDX          equ     6
  43. GRIDDY          equ     6
  44.  
  45.                 .data
  46.  
  47.                 include sincos.inc              ; sinus/cosinus table.
  48.  
  49. LandSeg         dw      ?                       ; landscape levels.
  50. Pixels          dw      GRIDX * GRIDY dup (?)   ; screen pixels locations.
  51. Palette         db      768 dup (?)             ; hold the color palette.
  52. FromPalette     db      768 dup (?)             ; fading-palettes.
  53. ToPalette       db      768 dup (?)
  54. Timer           dw      ?                       ; timer counter.
  55.  
  56. ;======================= Demo routines =======================================
  57.  
  58.                 .code
  59.  
  60. ;-----------------------------------------------------------------------------
  61. ; WaitVRT - Waits the VGA vertical retrace period.
  62. ;-----------------------------------------------------------------------------
  63.  
  64. WaitVRT         proc near
  65.  
  66.                 mov     dx,3DAh
  67. WaitVRT1:       in      al,dx                   ; wait the start of
  68.                 test    al,8                    ; vertical retrace.
  69.                 jz      WaitVRT1
  70. WaitVRT2:       in      al,dx                   ; wait the end of
  71.                 test    al,8                    ; vertical retrace.
  72.                 jnz     WaitVRT2
  73.                 ret
  74.  
  75. WaitVRT         endp
  76.  
  77. ;-----------------------------------------------------------------------------
  78. ; SetPalette - set the 256 entries of the VGA color palette.
  79. ; In:
  80. ;   DS:SI - Palette structure address.
  81. ;-----------------------------------------------------------------------------
  82.  
  83. SetPalette      proc near
  84.  
  85.                 mov     cx,768
  86.                 mov     dx,3C8h
  87.                 xor     al,al
  88.                 out     dx,al
  89.                 call    WaitVRT
  90.                 mov     dx,3C9h
  91.                 rep     outsb
  92.                 ret
  93.  
  94. SetPalette      endp
  95.  
  96. ;-----------------------------------------------------------------------------
  97. ; FadeTo - Fade palette effect.
  98. ; In:
  99. ;  FromPalette - Source palette.
  100. ;  ToPalette   - Destination palette.
  101. ;-----------------------------------------------------------------------------
  102.  
  103. FadeTo         proc near
  104.  
  105. FadeLoop:       lea     si,[FromPalette]
  106.                 call    SetPalette
  107.                 mov     cx,768
  108.                 xor     dx,dx
  109.                 xor     bx,bx
  110. FadeColor:      mov     al,[FromPalette+bx]
  111.                 mov     ah,[ToPalette+bx]
  112.                 cmp     al,ah
  113.                 je      SkipFade
  114.                 ja      FadeOut
  115. FadeIn:         add     al,FADESPEED
  116.                 cmp     al,ah
  117.                 jl      SetColor
  118.                 mov     al,ah
  119.                 jmp     SetColor
  120. FadeOut:        sub     al,FADESPEED
  121.                 cmp     al,ah
  122.                 jg      SetColor
  123.                 mov     al,ah
  124.                 jmp     SetColor
  125. SetColor:       mov     [FromPalette+bx],al
  126.                 inc     dx
  127. SkipFade:       inc     bx
  128.                 loop    FadeColor
  129.                 test    dx,dx
  130.                 jne     FadeLoop
  131.                 ret
  132.  
  133. FadeTo          endp
  134.  
  135. ;-----------------------------------------------------------------------------
  136. ; FlashIn - Flash fade-in effect.
  137. ; In:
  138. ;  Palette - Source palette.
  139. ;-----------------------------------------------------------------------------
  140.  
  141. FlashIn         proc near
  142.  
  143.                 mov     ax,ds
  144.                 mov     es,ax
  145.                 cld
  146.                 lea     di,[FromPalette]        ; fade from black to white.
  147.                 mov     cx,768
  148.                 mov     al,00h
  149.                 rep     stosb
  150.                 lea     di,[ToPalette]
  151.                 mov     cx,768
  152.                 mov     al,3Fh
  153.                 rep     stosb
  154.                 call    FadeTo
  155.                 lea     di,[FromPalette]        ; fade from white to
  156.                 mov     cx,768                  ; destination palette.
  157.                 mov     al,3Fh
  158.                 rep     stosb
  159.                 lea     si,[Palette]
  160.                 lea     di,[ToPalette]
  161.                 mov     cx,768
  162.                 rep     movsb
  163.                 call    FadeTo
  164.                 ret
  165.  
  166. FlashIn         endp
  167.  
  168. ;-----------------------------------------------------------------------------
  169. ; FlashOut - Flash fade-out effect.
  170. ; In:
  171. ;  Palette - Source palette.
  172. ;-----------------------------------------------------------------------------
  173.  
  174. FlashOut        proc near
  175.  
  176.                 mov     ax,ds
  177.                 mov     es,ax
  178.                 cld
  179.                 lea     di,[ToPalette]          ; fade from source palette
  180.                 mov     cx,768                  ; to white palette.
  181.                 mov     al,3Fh
  182.                 rep     stosb
  183.                 lea     si,[Palette]
  184.                 lea     di,[FromPalette]
  185.                 mov     cx,768
  186.                 rep     movsb
  187.                 call    FadeTo
  188.                 lea     di,[FromPalette]        ; fade from white to black.
  189.                 mov     cx,768
  190.                 mov     al,3Fh
  191.                 rep     stosb
  192.                 lea     di,[ToPalette]
  193.                 mov     cx,768
  194.                 mov     al,00h
  195.                 rep     stosb
  196.                 call    FadeTo
  197.                 ret
  198.  
  199. FlashOut        endp
  200.  
  201. ;-----------------------------------------------------------------------------
  202. ; GenLandscape - This routines generates the lanscape surface using the
  203. ;   plasma routines basically and checks that all the mountain levels
  204. ;   are in the right range.
  205. ; In:
  206. ;  LandSeg - Segment addressing for the landscape.
  207. ;-----------------------------------------------------------------------------
  208.  
  209. GenLandscape    proc near
  210.  
  211.                 mov     ax,[LandSeg]            ; clear landscape area.
  212.                 mov     es,ax
  213.                 xor     di,di
  214.                 xor     ax,ax
  215.                 mov     cx,LANDSCAPEX * LANDSCAPEY
  216.                 cld
  217.                 rep     stosb
  218.  
  219.                 ; draw initial plasma landscape.
  220.                 call    DrawPlasma,0,0,LANDSCAPEX-1,LANDSCAPEY-1, \
  221.                                                  PLASMASEED,[LandSeg]
  222.  
  223.                 ; adjust levels for the real landscape.
  224.  
  225.                 mov     ax,[LandSeg]
  226.                 mov     es,ax
  227.                 xor     di,di
  228.                 mov     cx,LANDSCAPEX * LANDSCAPEY
  229.                 cld
  230. AdjLoop:        mov     al,es:[di]
  231.                 sub     al,WATERLEVEL
  232.                 test    al,al
  233.                 jge     AdjLevel
  234.                 xor     al,al
  235. AdjLevel:       xor     ah,ah
  236.                 imul    ax,192
  237.                 inc     ah
  238.                 mov     es:[di],ah
  239.                 inc     di
  240.                 loop    AdjLoop
  241.                 ret
  242.  
  243. GenLandscape    endp
  244.  
  245.  
  246. ;-----------------------------------------------------------------------------
  247. ; DrawLandscape - Draws a region of the landscape on the screen starting
  248. ;   at the specified position and rotation angles. The clipping support
  249. ;   have been removed for speed.
  250. ; In:
  251. ;   (PosX,PosY) - Landscape starting position.
  252. ;   AngleY      - Inclination angle.
  253. ;   AngleZ      - Spin angle.
  254. ;-----------------------------------------------------------------------------
  255.  
  256. DrawLandscape   proc near PosX:word,PosY:word, AngleY:word,AngleZ:word
  257.                 local SinY:word,SinZ:word,CosY:word,CosZ:word, \
  258.                       YIAdd:word,YJAdd:word,ZIAdd:word,ZJAdd:word, \
  259.                       Y0:word,Z0:word,Y:word,Z:word
  260.  
  261.                 mov     bx,[AngleY]             ; compute rotation
  262.                 mov     al,[SinTable+bx]        ; parameters.
  263.                 cbw
  264.                 mov     [SinY],ax
  265.                 mov     al,[CosTable+bx]
  266.                 cbw
  267.                 mov     [CosY],ax
  268.  
  269.                 mov     bx,[AngleZ]
  270.                 mov     al,[SinTable+bx]
  271.                 cbw
  272.                 mov     [SinZ],ax
  273.                 mov     al,[CosTable+bx]
  274.                 cbw
  275.                 mov     [CosZ],ax
  276.  
  277.                 mov     ax,[SinZ]               ; yiadd = griddx * sinz
  278.                 imul    ax,GRIDDX
  279.                 mov     [YIAdd],ax
  280.                 mov     ax,[CosZ]               ; yjadd = griddy * cosz
  281.                 imul    ax,GRIDDY
  282.                 mov     [YJAdd],ax
  283.                 mov     ax,[CosZ]               ; ziadd = griddx * cosz * siny
  284.                 mov     dx,[SinY]
  285.                 imul    dx
  286.                 imul    ax,GRIDDX
  287.                 sar     ax,TRIGSHIFT
  288.                 mov     [ZIAdd],ax
  289.                 mov     ax,[SinZ]               ; zjadd = -griddy * sinz * siny
  290.                 mov     dx,[SinY]
  291.                 imul    dx
  292.                 imul    ax,-GRIDDY
  293.                 sar     ax,TRIGSHIFT
  294.                 mov     [ZJAdd],ax
  295.  
  296.                 mov     ax,[YIAdd]              ; compute starting grid
  297.                 imul    ax,-GRIDX/2             ; corner position.
  298.                 mov     dx,[YJAdd]
  299.                 imul    dx,-GRIDY/2
  300.                 add     ax,dx
  301.                 mov     [Y0],ax
  302.                 mov     ax,[ZIAdd]
  303.                 imul    ax,-GRIDX/2
  304.                 mov     dx,[ZJAdd]
  305.                 imul    dx,-GRIDY/2
  306.                 add     ax,dx
  307.                 mov     [Z0],ax
  308.  
  309.                 mov     ax,[LandSeg]            ; es = landscape segment.
  310.                 mov     es,ax
  311.  
  312.                 lea     si,[Pixels]             ; ds:si = pixels offsets.
  313.  
  314.                 mov     di,[PosY]               ; store offset
  315.                 imul    di,LANDSCAPEX           ; just for speed.
  316.                 add     di,[PosX]
  317.  
  318.                 mov     cx,GRIDX
  319. ForI:           push    cx
  320.                 mov     ax,[Y0]                 ; y = y0
  321.                 mov     [Y],ax
  322.                 mov     ax,[Z0]                 ; z = z0
  323.                 mov     [Z],ax
  324.  
  325.                 mov     cx,GRIDY
  326. ForJ:           push    es
  327.                 mov     ax,0A000h               ; clear old pixel.
  328.                 mov     es,ax
  329.                 mov     bx,[si]
  330.                 mov     byte ptr es:[bx],al
  331.                 pop     es
  332.  
  333. ; here the code must be hooked to support clipping, use an "illegal"
  334. ; offset to store pixels that are not drawed, and then the routine above
  335. ; must erase only "legal" pixels.
  336.  
  337.                 mov     bl,es:[di]              ; bl = landscape(posx,posy)
  338.                 mov     al,bl                   ; ax = -centery + (z + bl*cosy)
  339.                 xor     ah,ah
  340.                 imul    [CosY]
  341.                 add     ax,[Z]
  342.                 sar     ax,TRIGSHIFT
  343.                 sub     ax,CENTERY
  344.                 imul    ax,SCREENWIDTH
  345.                 mov     dx,[Y]                  ; dx = centery + y
  346.                 sar     dx,TRIGSHIFT
  347.                 add     dx,CENTERX
  348.                 sub     dx,ax
  349.                 mov     [si],dx                 ; store pixel offset.
  350.  
  351.                 push    es
  352.                 mov     ax,0A000h               ; paint new pixel.
  353.                 mov     es,ax
  354.                 xchg    dx,bx
  355.                 mov     es:[bx],dl
  356.                 pop     es
  357.  
  358.                 mov     ax,[YJAdd]              ; y = y + yjadd
  359.                 add     [Y],ax
  360.                 mov     ax,[ZJAdd]              ; z = z + zjadd
  361.                 add     [Z],ax
  362.                 inc     di                      ; posy = posy + 1
  363.                 add     si,2
  364.                 loop    ForJ
  365.  
  366.                 mov     ax,[YIAdd]              ; y0 = y0 + yiadd
  367.                 add     [Y0],ax
  368.                 mov     ax,[ZIAdd]              ; z0 = z0 + ziadd
  369.                 add     [Z0],ax
  370.                 add     di,LANDSCAPEX-GRIDY     ; posy = posy - gridy
  371.                 pop     cx                      ; posx = posx + 1
  372.                 loop    ForI
  373.                 ret
  374.  
  375. DrawLandscape   endp
  376.  
  377. ;-----------------------------------------------------------------------------
  378. ; LandScape - Main landscape demo routine. Because this routine was tested
  379. ;   using the Borland Pascal high-level interface, the allocation of the
  380. ;   memory is done by the caller.
  381. ; In:
  382. ;  DS     - Data segment.
  383. ;  MemSeg - Memory segment of 64K needed for storage.
  384. ;-----------------------------------------------------------------------------
  385.  
  386. LandScape       proc    MemSeg:word
  387.                 local   PosX:word,PosY:word,AngleY:word,AngleZ:word
  388.  
  389.                 mov     ax,0013h                ; set VGA 320x200x256 mode.
  390.                 int     10h
  391.  
  392.                 mov     ax,[MemSeg]             ; setup landscape
  393.                 mov     [LandSeg],ax            ; memory segment.
  394.  
  395.                 lea     di,[Palette]            ; set black palette.
  396.                 mov     ax,ds
  397.                 mov     es,ax
  398.                 mov     cx,768
  399.                 xor     ax,ax
  400.                 cld
  401.                 rep     stosb
  402.                 lea     si,[Palette]
  403.                 call    SetPalette
  404.  
  405. GenPalette:     lea     di,[Palette]            ; generate color palette.
  406.                 mov     ax,3F00h
  407.                 mov     [di+0],al               ; background.
  408.                 mov     [di+1],al
  409.                 mov     [di+2],al
  410.                 add     di,3
  411.                 mov     [di+0],al               ; blue. (water)
  412.                 mov     [di+1],al
  413.                 mov     [di+2],ah
  414.                 add     di,3
  415.                 mov     ax,0000h                ; green to brown.
  416.                 mov     bx,3F00h
  417.                 mov     dx,0000h
  418.                 mov     cx,61
  419. GP0:            mov     [di+0],ah
  420.                 mov     [di+1],bh
  421.                 mov     [di+2],dh
  422.                 add     di,3
  423.                 add     ax,0108h
  424.                 sub     bx,0086h
  425.                 add     dx,0000h
  426.                 loop    GP0
  427.                 mov     ax,3F00h                ; brown to white.
  428.                 mov     bx,2000h                ; (mountain)
  429.                 mov     dx,0000h
  430.                 mov     cx,48
  431. GP1:            mov     [di+0],ah
  432.                 mov     [di+1],bh
  433.                 mov     [di+2],dh
  434.                 add     di,3
  435.                 add     ax,0000h
  436.                 add     bx,00AAh
  437.                 add     dx,0150h
  438.                 loop    GP1
  439.                 mov     ax,3F00h                ; white. (snow)
  440.                 mov     bx,3F00h
  441.                 mov     dx,3F00h
  442.                 mov     cx,145
  443. GP2:            mov     [di+0],ah
  444.                 mov     [di+1],bh
  445.                 mov     [di+2],dh
  446.                 add     di,3
  447.                 add     ax,0000h
  448.                 add     bx,0000h
  449.                 add     dx,0000h
  450.                 loop    GP2
  451.  
  452.                 call    GenLandscape            ; creates the landscape.
  453.  
  454.                 mov     [AngleY],240            ; inclination angle.
  455.                 mov     [AngleZ],0              ; initial spin angle.
  456.                 mov     [PosX],0                ; starting landscape
  457.                 mov     [PosY],0                ; position.
  458.  
  459.                 ; pixels offset array could be cleaned but not necesary,
  460.                 ; because i am using the 64K of video memory ...
  461.                 ; i can't overwrite anything! :-)
  462.  
  463.                 ; draw hidden landscape and do flash-in effect.
  464.  
  465.                 call    DrawLandscape,[PosX],[PosY],[AngleY],[AngleZ]
  466.                 lea     si,[Palette]
  467.                 call    FlashIn
  468.  
  469.                 mov     [Timer],0               ; set timer counter.
  470.  
  471.                 mov     si,1                    ; starting landscape
  472.                 mov     di,1                    ; directions.
  473.  
  474. LandLoop:       call    WaitVRT                 ; wait vertical retrace.
  475.  
  476.                 push    si                      ; draw landscape.
  477.                 push    di
  478.                 call    DrawLandscape,[PosX],[PosY],[AngleY],[AngleZ]
  479.                 pop     di
  480.                 pop     si
  481.  
  482.                 inc     byte ptr [AngleZ]       ; increase spin angle.
  483.                 add     [PosX],si               ; advance landscape position.
  484.                 add     [PosY],di
  485. TestX:          cmp     [PosX],0                ; check for boundaries.
  486.                 jl      NegIncX
  487.                 cmp     [PosX],LANDSCAPEX-GRIDX
  488.                 jl      TestY
  489. NegIncX:        sub     [PosX],si
  490.                 neg     si
  491. TestY:          cmp     [PosY],0
  492.                 jl      NegIncY
  493.                 cmp     [PosY],LANDSCAPEY-GRIDY
  494.                 jl      CheckExit
  495. NegIncY:        sub     [PosY],di
  496.                 neg     di
  497.  
  498. CheckExit:      inc     [Timer]                 ; timeout?
  499.                 cmp     [Timer],TIMEOUT
  500.                 jae     ByeBye
  501.  
  502.                 mov     ah,1                    ; any key pressed?
  503.                 int     16h
  504.                 jz      LandLoop
  505.  
  506. ByeBye:         call    FlashOut                ; flash-out!
  507.  
  508.                 mov     ax,03h                  ; set 80x25x16 text mode.
  509.                 int     10h
  510.                 ret
  511.  
  512. LandScape       endp
  513.  
  514.  
  515. ;-----------------------------------------------------------------------------
  516. ; Start - Startup code called from DOS.
  517. ; In:
  518. ;   ES - Program Segment Prefix.
  519. ;-----------------------------------------------------------------------------
  520.  
  521. Start           proc
  522.  
  523.                 mov     ax,@Data
  524.                 mov     ds,ax
  525.  
  526.                 mov     ax,ss                   ; shrink memory block.
  527.                 mov     bx,es
  528.                 sub     ax,bx
  529.                 mov     bx,sp
  530.                 shr     bx,4
  531.                 inc     bx
  532.                 add     bx,ax
  533.                 mov     ah,4Ah
  534.                 int     21h
  535.  
  536.                 mov     ah,48h                  ; allocate 64000 bytes.
  537.                 mov     bx,4000
  538.                 int     21h
  539.                 jc      Exit
  540.                 push    ax
  541.                 call    LandScape,ax            ; do demostration.
  542.                 pop     es
  543.                 mov     ah,49h                  ; free 64000 bytes.
  544.                 int     21h
  545.  
  546. Exit:           mov     ax,4C00h                ; exit to DOS.
  547.                 int     21h
  548.  
  549. Start           endp
  550.  
  551.                 end     Start
  552.