home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / fakesrc.zip / PART6 / PLASMA.ASM < prev    next >
Assembly Source File  |  1993-09-26  |  9KB  |  244 lines

  1. ;=============================================================================
  2. ; plasma.asm - Plasma Clouds fractal calculation routine.
  3. ;                                                     File created: 9-26-93
  4. ; Copyright (c) 1993, Carlos Hasan                   Last modified: 9-26-93
  5. ;
  6. ; Description:
  7. ;  This file implements the plasma clouds routine using a recursive algorithm
  8. ;  optimized for use with VGA 320x200x256 graphics mode. There must be enough
  9. ;  stack space for this routine.
  10. ;
  11. ; Portability:
  12. ;  Requires Turbo Assembler 3.2 or better to be assembler.
  13. ;  Dependent on the IBM PC 286 or later microprocessor.
  14. ;=============================================================================
  15.  
  16.                 .model  small,pascal
  17.                 .286
  18.  
  19.                 global  DrawPlasma:proc
  20.  
  21. ;===================== Plasma equates ========================================
  22.  
  23. MAXWIDTH        equ     320                     ; virtual screen dimensions.
  24. MAXHEIGHT       equ     200
  25. MAXCOLOR        equ     255                     ; max colors for plasma.
  26. SHIFTVALUE      equ     2                       ; granularity shift.
  27.  
  28. ;====================== Plasma data ==========================================
  29.  
  30.                 .data
  31.  
  32. RandSeed        dw      ?                       ; random generator seed.
  33.  
  34. ;====================== Plasma routines ======================================
  35.  
  36.                 .code
  37.  
  38. ;-----------------------------------------------------------------------------
  39. ; Random - Returns a random number of 16 bits, modified version of the
  40. ;  ripped System unit random routine of the Borland Pascal 7.0.
  41. ; Out:
  42. ;  AX - random value.
  43. ; Modified:
  44. ;  AX, DX, Flags.
  45. ;-----------------------------------------------------------------------------
  46.  
  47. Random          proc near
  48.  
  49.                 mov     ax,[RandSeed]
  50.                 mov     dx,8405h
  51.                 mul     dx
  52.                 inc     ax
  53.                 mov     [RandSeed],ax
  54.                 ret
  55.  
  56. Random          endp
  57.  
  58.  
  59. ;-----------------------------------------------------------------------------
  60. ; GetPixel - get the pixel color at specified location.
  61. ; In:
  62. ;   ES    - virtual screen segment.
  63. ;   (X,Y) - pixel location.
  64. ; Out:
  65. ;   AL    - pixel color value.
  66. ; Modified:
  67. ;   BX, Flags.
  68. ;-----------------------------------------------------------------------------
  69.  
  70. GetPixel        macro   X,Y
  71.  
  72.                 mov     bx,Y
  73.                 imul    bx,MAXWIDTH
  74.                 add     bx,X
  75.                 mov     al,es:[bx]
  76.  
  77.                 endm
  78.  
  79. ;-----------------------------------------------------------------------------
  80. ; PutPixel - put the pixel color at specified location.
  81. ; In:
  82. ;   ES    - virtual screen segment.
  83. ;   (X,Y) - pixel location.
  84. ;   AL    - pixel value.
  85. ; Modified:
  86. ;   BX, Flags.
  87. ;-----------------------------------------------------------------------------
  88.  
  89. PutPixel        macro   X,Y
  90.  
  91.                 mov     bx,Y
  92.                 imul    bx,MAXWIDTH
  93.                 add     bx,X
  94.                 mov     es:[bx],al
  95.  
  96.                 endm
  97.  
  98.  
  99. ;----------------------------------------------------------------------------
  100. ; Adjust - adjust a new pixel value using two neighboring pixels.
  101. ; In:
  102. ;  (XA,YA) - first near pixel.
  103. ;  (XB,YB) - second near pixel.
  104. ;  (X,Y)   - new pixel.
  105. ; Out:
  106. ;  AX      - new pixel color value.
  107. ; Modified:
  108. ;  AX, BX, DX, SI, DI, Flags.
  109. ;----------------------------------------------------------------------------
  110.  
  111. Adjust          proc near XA:word,YA:word,X:word,Y:word,XB:word,YB:word
  112.  
  113.                 mov     si,[X]                  ; if already painted
  114.                 mov     di,[Y]                  ; the pixel, use this
  115.                 xor     ax,ax                   ; one instead of new
  116.                 GetPixel si,di                  ; calculation.
  117.                 test    ax,ax
  118.                 jne     AdjExit
  119.  
  120.                 call    Random                  ; get a random variation
  121.                 mov     bx,[XB]                 ; dependent of the pixels
  122.                 sub     bx,[XA]                 ; distance and granularity
  123.                 add     bx,[YB]                 ; shift factor.
  124.                 sub     bx,[YA]
  125.                 shl     bx,SHIFTVALUE
  126.                 imul    bx
  127.                 xor     ax,ax                   ; adds the average of the
  128.                 GetPixel [XA],[YA]              ; near pixels colors.
  129.                 add     dx,ax
  130.                 GetPixel [XB],[YB]
  131.                 add     ax,dx
  132.                 shr     ax,1
  133.                 cmp     ax,1                    ; check if new color is
  134.                 jge     ColorUp                 ; in the right range.
  135.                 mov     ax,1
  136. ColorUp:        cmp     ax,MAXCOLOR
  137.                 jle     ColorDn
  138.                 mov     ax,MAXCOLOR
  139. ColorDn:        PutPixel si,di                  ; paint pixel color.
  140. AdjExit:        ret
  141.  
  142. Adjust          endp
  143.  
  144.  
  145. ;-----------------------------------------------------------------------------
  146. ; SubDivide - main plasma routine that divides a region recursively.
  147. ; In:
  148. ;   (X1,Y1,X2,Y2) - screen plasma region.
  149. ; Modified:
  150. ;   AX, BX, CX, DX, Flags.
  151. ;-----------------------------------------------------------------------------
  152.  
  153. SubDivide       proc near X1:word,Y1:word,X2:word,Y2:word
  154.                 local   X:word,Y:word
  155.  
  156.                 mov     ax,[X2]                 ; test if this region
  157.                 sub     ax,[X1]                 ; needs a sub-division.
  158.                 cmp     ax,2
  159.                 jge     SubDivCont
  160.                 mov     ax,[Y2]
  161.                 sub     ax,[Y1]
  162.                 cmp     ax,2
  163.                 jge     SubDivCont
  164.                 jmp     SubDivExit
  165.  
  166. SubDivCont:     mov     ax,[X1]                 ; get the center position
  167.                 add     ax,[X2]                 ; of the region.
  168.                 rcr     ax,1
  169.                 mov     [X],ax
  170.                 mov     ax,[Y1]
  171.                 add     ax,[Y2]
  172.                 rcr     ax,1
  173.                 mov     [Y],ax
  174.  
  175.                 ; get the sum of the neighboring four pixel colors.
  176.                 xor     cx,cx
  177.                 call    Adjust, [X1],[Y1], [X],[Y1], [X2],[Y1]
  178.                 add     cx,ax
  179.                 call    Adjust, [X2],[Y1], [X2],[Y], [X2],[Y2]
  180.                 add     cx,ax
  181.                 call    Adjust, [X1],[Y2], [X],[Y2], [X2],[Y2]
  182.                 add     cx,ax
  183.                 call    Adjust, [X1],[Y1], [X1],[Y], [X1],[Y2]
  184.                 add     cx,ax
  185.  
  186.                 mov     si,[X]                  ; test if the center pixel
  187.                 mov     di,[Y]                  ; need to be calculated.
  188.                 GetPixel si,di
  189.                 test    al,al
  190.                 jne     SubDivNow
  191.                 mov     ax,cx                   ; yes, use the average of
  192.                 add     ax,2                    ; the neighboring pixels.
  193.                 shr     ax,2                    ; (don't allow color 0)
  194.                 PutPixel si,di
  195.  
  196.                 ; sub-divides the new four regions.
  197. SubDivNow:      call    SubDivide, [X1],[Y1], [X],[Y]
  198.                 call    SubDivide, [X],[Y1], [X2],[Y]
  199.                 call    SubDivide, [X],[Y], [X2],[Y2]
  200.                 call    SubDivide, [X1],[Y], [X],[Y2]
  201.  
  202. SubDivExit:     ret
  203.  
  204. SubDivide       endp
  205.  
  206. ;-----------------------------------------------------------------------------
  207. ; DrawPlasma - Main routine to draw plasma into a virtual screen.
  208. ; In:
  209. ;   (X1,Y1,X2,Y2) - Virtual screen Window location.
  210. ;   Seed          - Initial random seed for the plasma generator.
  211. ;   ScreenSeg     - Virtual screen segment.
  212. ;-----------------------------------------------------------------------------
  213.  
  214. DrawPlasma      proc    XStart:word,YStart:word,XStop:word,YStop:word, \
  215.                   Seed:word,ScreenSeg:word
  216.  
  217.                 mov     ax,[ScreenSeg]          ; setup virtual screen
  218.                 mov     es,ax                   ; segment,
  219.  
  220.                 mov     ax,[Seed]               ; and random calculation
  221.                 mov     [RandSeed],ax           ; seed.
  222.  
  223.                 call    Random                  ; set window corner pixels.
  224.                 or      al,1
  225.                 PutPixel [XStart],[YStart]
  226.  
  227.                 call    Random
  228.                 or      al,1
  229.                 PutPixel [XStop],[YStart]
  230.  
  231.                 call    Random
  232.                 or      al,1
  233.                 PutPixel [XStart],[YStop]
  234.  
  235.                 call    Random
  236.                 or      al,1
  237.                 PutPixel [XStop],[YStop]
  238.  
  239.                 call    SubDivide, [XStart],[YStart], [XStop],[YStop]
  240.                 ret
  241.  
  242. DrawPlasma      endp
  243.  
  244.                 end