Guest User

Untitled

a guest
Jun 14th, 2018
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'Fire particle Test
  2. ' NB: Bitmaps in DefData by matibee
  3. SuperStrict
  4.  
  5. SeedRnd MilliSecs()
  6.  
  7. Graphics 800, 600
  8.  
  9. AutoMidHandle(True)
  10.  
  11. RestoreData Cloud001_png
  12. Global smoke:TImage = DataToImage()
  13.  
  14. RestoreData Flame_png
  15. Global flame:TImage = DataToImage()
  16.  
  17. RestoreData Particle005_png
  18. Global ember:TImage = DataToImage()
  19.  
  20. Global intervalSmoke# = 4
  21. Global intervalSmokeCounter# = intervalSmoke
  22.  
  23. Global intervalEmber# = 20
  24. Global intervalEmberCounter# = intervalEmber
  25.  
  26. Global intervalClsColor# = 200
  27. Global intervalClsColorCounter% = intervalClsColor
  28.  
  29. Global fps_counter% = 0
  30. Global update_frequency% = 5
  31. Global fps_milli% = MilliSecs()
  32. Global fps%
  33.  
  34. SetClsColor (0, 0, 0)
  35.  
  36. Repeat
  37.     Update()
  38.     Render()
  39.     Delay(1)
  40. Until AppTerminate() Or KeyHit(KEY_ESCAPE)
  41.  
  42. Function Update()
  43.     If intervalClsColorCounter> 0
  44.         intervalClsColorCounter:-1
  45.         If intervalClsColorCounter <= 0
  46.             SetClsColor (Rand(255), Rand(255), Rand(255))
  47.             intervalClsColorCounter = intervalClsColor
  48.         EndIf
  49.     EndIf
  50.  
  51.     Local x# = 400 + Rand(-100,100)
  52.     Local y# = 550
  53.    
  54.     For Local i% = 0 To 1
  55.         CreateFlameParticle(x, y)
  56.     Next
  57.     CreateSmokeParticle(x, y - 140)
  58.     CreateEmberParticle(x, y - 150)
  59.  
  60.     TParticle.UpdateAll()
  61. End Function
  62.  
  63. Function Render()
  64.     Cls
  65.         TParticle.DrawAll(True)
  66.         TParticle.DrawAll(False)
  67.         SetColor 255, 255, 255
  68.         SetAlpha 1
  69.         SetBlend ALPHABLEND
  70.         SetScale 1, 1
  71.         SetRotation 0
  72.         Local tx%, ty% = 10
  73.         Local gap% = 15
  74.         DrawText "FPS:"+fps, tx, ty
  75.         ty:+gap
  76.         DrawText "No. of Particles = " + TParticle.list.Count(), tx, ty
  77.         ty:+gap
  78.         DrawText "Changing Cls Colour in = "+ intervalClsColorCounter, tx, ty
  79.        
  80.        
  81.         ' FPS counter
  82.         fps_counter:+1
  83.         If fps_counter = update_frequency
  84.              fps = 1000 / Float(((MilliSecs() - fps_milli)) / update_frequency)
  85.              fps_milli = MilliSecs()
  86.              fps_counter = 0
  87.         EndIf
  88.     Flip 1
  89. End Function
  90.  
  91.  
  92. Function CreateEmberParticle(x#, y#)
  93.     If intervalEmberCounter> 0
  94.         intervalEmberCounter:-1
  95.         If intervalEmberCounter<= 0
  96.             Local p:TParticle = TParticle.Create(ember, x, y)
  97.             p.SetRGB(255, 128, 0)
  98.             p.dy = Rnd(-2, -1)
  99.             p.rotationSpeed = Rnd(-.5, .5)
  100.             p.gravityY = 0.01
  101.             p.scale = Rnd(0.03, 0.05)
  102.             p.SetFade(10, 60, 40)
  103.             p.rotation = Rnd(360)
  104.             p.blend = LIGHTBLEND
  105.            
  106.             intervalEmberCounter = intervalEmber
  107.         EndIf
  108.     EndIf
  109. End Function
  110.  
  111. Function CreateSmokeParticle(x#, y#)
  112.     If intervalSmokeCounter > 0
  113.         intervalSmokeCounter:-1
  114.         If intervalSmokeCounter <= 0
  115.             Local p:TParticle = TParticle.Create(smoke, x, y)
  116.             p.SetRGB(128, 128, 128)
  117.             p.dy = Rnd(-1, 0)
  118.             p.rotationSpeed = Rnd(-.5, .5)
  119.             p.gravityY = -0.02
  120.             p.scale = 1 + Rnd(.25)
  121.             p.SetFade(80, 10, 80)
  122.             p.maxAlpha = .1
  123.             p.rotation = Rnd(360)
  124.             p.blend = ALPHABLEND
  125.            
  126.             intervalSmokeCounter = intervalSmoke
  127.         EndIf
  128.     EndIf
  129. End Function
  130.  
  131. Function CreateFlameParticle(x#, y#)
  132.     Local p:TParticle = TParticle.Create(flame, x, y)
  133.     p.SetRGB(255, 80, 0)
  134.     p.dy = Rnd(-1, 0)
  135.     p.rotationSpeed = Rnd(-1, 1)
  136.     p.gravityY = -0.02
  137.     p.scaleSpeed = -0.001
  138.     p.scale = .35
  139.     p.SetFade(30, 30, 35)
  140.     p.rotation = Rnd(360)
  141.     p.blend = LIGHTBLEND
  142. End Function
  143.  
  144. Type TParticle
  145.     Field x#, y#
  146.     Field dx#, dy#
  147.     Field gravityX#, gravityY#
  148.     Field rotation#
  149.     Field rotationSpeed#
  150.     Field scale# = 1
  151.     Field scaleSpeed#
  152.     Field alpha# = 1
  153.     Field maxAlpha# = 1
  154.     Field image:TImage
  155.     Field blend% = ALPHABLEND
  156.     Field r% = 255, g% = 255, b% = 255
  157.     Field fadeIn# = 0
  158.     Field fadeSustainCounter# = 0
  159.     Field fadeCounter# = 0 
  160.     Field fadeInLength# = 0
  161.     Field fadeLength#=0
  162.  
  163.     Global list:TList
  164.    
  165.     Function Create:TParticle(image:TImage, x#, y#)
  166.         If list = Null Then list = New TList
  167.         Local p:TParticle = New TParticle
  168.         p.image = image
  169.         p.x = x
  170.         p.y = y
  171.         list.AddLast p
  172.         Return p
  173.     End Function
  174.    
  175.     Function DrawAll(drawmask:Int = False)
  176.         If Not list Return
  177.         For Local p:TParticle = EachIn list
  178.             p.Draw(drawmask)
  179.         Next
  180.     End Function
  181.    
  182.     Function UpdateAll()
  183.         If Not list Return
  184.         For Local p:TParticle = EachIn list
  185.             p.Update()
  186.         Next
  187.     End Function
  188.    
  189.     Method SetFade(theFadeInLength%, theSustain%, theFadeLength%)  
  190.         If theFadeInLength > 0 Then
  191.             fadeIn = 1
  192.             alpha = 0
  193.         Else
  194.             fadeIn = 0
  195.         EndIf  
  196.         fadeInLength = theFadeInLength
  197.         fadeSustainCounter = theSustain
  198.         fadeLength = theFadeLength
  199.  
  200.         If fadeIn = 0 Then
  201.             fadeCounter = fadeLength
  202.         Else
  203.             fadeCounter = 0
  204.         EndIf
  205.     End Method 
  206.    
  207.     Method Update()
  208.         x :+ dx
  209.         y :+ dy
  210.         If gravityY <> 0
  211.             dy :+ gravityY
  212.         EndIf
  213.         If gravityX <> 0
  214.             dx :+ gravityX
  215.         EndIf
  216.         If rotationSpeed <> 0
  217.             rotation :+ rotationSpeed
  218.         EndIf
  219.         If scaleSpeed <> 0
  220.             scale :+ scaleSpeed
  221.             If scale < 0 Then scale = 0
  222.         EndIf
  223.  
  224.         If fadeIn
  225.             fadeCounter:+1
  226.             If fadeCounter >= fadeInLength
  227.                 fadeCounter = fadeLength
  228.                 fadeIn = 0
  229.                 alpha = maxAlpha
  230.             End If
  231.         Else If fadeSustainCounter > 0
  232.             fadeSustainCounter:-1
  233.             If fadeSustainCounter <= 0 Then
  234.                 fadeSustainCounter = 0
  235.                 fadeCounter = fadeLength
  236.             End If
  237.         Else If fadeCounter > 0 Then
  238.             fadeCounter:-1
  239.             If fadeCounter <= 0 Then
  240.                 alpha = 0
  241.                 Kill()
  242.             EndIf
  243.         EndIf
  244.     End Method
  245.    
  246.     Method Kill()
  247.         list.Remove(Self)
  248.     End Method
  249.    
  250.     Method Draw(drawmask:Int = False)
  251.         If fadeCounter > 0 Then
  252.             If fadeIn Then
  253.                 alpha = fadeCounter / fadeInLength
  254.                 If alpha > maxAlpha Then alpha = maxAlpha
  255.             Else
  256.                 alpha = fadeCounter / fadeLength
  257.                 If alpha > maxAlpha Then alpha = maxAlpha
  258.             EndIf
  259.         EndIf
  260.         SetAlpha alpha
  261.         SetScale scale, scale
  262.         SetRotation rotation
  263.         If drawmask Then
  264.             SetColor 0, 0, 0
  265.             SetBlend ALPHABLEND
  266.             DrawImage image, x, y
  267.         Else
  268.             SetColor r, g, b
  269.             SetBlend blend
  270.             DrawImage image, x, y
  271.         EndIf
  272.     End Method
  273.    
  274.     Method SetRGB(r%, g%, b%)
  275.         Self.r = r
  276.         Self.g = g
  277.         Self.b = b
  278.     End Method
  279.    
  280. End Type
  281.  
  282. 'graphics
  283.  
  284. Function DataToImage:TImage( flags:Int = -1 )
  285.     Local myhex$ = "0123456789ABCDEF"
  286.     Local w:Int, h:Int, line$
  287.     ReadData w
  288.     ReadData h
  289.     Local img:TImage = CreateImage( w, h, 1, flags )
  290.     Local tp:TPixmap = LockImage( img )
  291.     For Local y:Int = 0 To h - 1
  292.         ReadData line$
  293.         For Local x:Int = 0 To w - 1
  294.             Local word$ = Mid$( line$, x * 8 + 1, 8 )
  295.             Local pixel:Int = 0
  296.             Local shift:Int = 28
  297.             For Local i:Int = 1 To 8
  298.                 pixel :+ (Instr( myhex$, Mid$(word$, i, 1) ) - 1) Shl shift
  299.                 shift :- 4
  300.             Next
  301.             WritePixel( tp, x, y, pixel )
  302.         Next
  303.     Next
  304.     UnlockImage( img )
  305.     Return img
  306. End Function
Add Comment
Please, Sign In to add comment