Author Topic: Plasma Effect V1  (Read 4334 times)

0 Members and 1 Guest are viewing this topic.

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Plasma Effect V1
« on: May 04, 2006 »
Here's a little Code Snippet of a Plasma which I learnt from Shockwaves BB Tutorials. With a few additions for a FB TinyPTC adaption from yours truely, with routines I developed whilst learning FreeBasic. And may prove useful to you in your demo coding quests.

Enjoy and Hope it helps.

Code: [Select]
'-------------------------------------
' Plasma Effect
' By Clyde Radcliffe May 2006.
' Adaption And Modifications from
' Shockwaves Fast Plasma BB Tutorial
' http://www.dbfinteractive.com/plasmatutorial.htm
'---------------------------------------------------

Option Static
Option Explicit

'-------------------------------------
' Includes.
'-------------------------------------
#Include Once "Tinyptc.bi"

'-------------------------------------
' Constants.
'-------------------------------------
Const XRES=320
Const YRES=240

Const PI = 3.141593

Const ARES=XRES*YRES

Const KEY_ESC=27
Const KEY_TAB= 9

'-------------------------------------
' Know what I mean 'Arry.
'-------------------------------------
Dim Shared ScreenBuffer( ARES-1 ) As Integer

Dim Shared RGBScreen( XRES, YRES )
Dim Shared PlasmaPal(1024)

Dim Shared sintable(2880) As Single
Dim Shared costable(2880) As Single

'-------------------------------------
' Meet the Globals.
'-------------------------------------
Dim Shared PlasmaAdd1, PlasmaDir1
Dim Shared PlasmaAdd2, PlasmaDir2

Dim Shared RunningTime As Double
Dim Shared PlasmaTimer As Double
Dim Shared PlasmaDuration

'-------------------------------------
' Sub / Function Initializing.
'-------------------------------------
Declare Sub CreatePal( ByVal RedInc As UByte=1, ByVal GrnInc As UByte=2, ByVal BluInc As Ubyte=3,_
  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â ByVal RedDec As Ubyte=3, ByVal GrnDec As Ubyte=2, ByVal BluDec As Ubyte=1 )
 

Declare Sub FeedPixels( Buffer(), Byval x As Integer, Byval y As Integer, Byval col As Integer)
Declare Sub InitializePlasma()
Declare Sub RunPlasma()
Declare Sub UpdatePlasma()

Declare Function Millisecs( Byval TimeVal As Double ) As Double

'-------------------------------------
' Running Order.
'-------------------------------------
InitializePlasma()
RunPlasma()
End


Sub CreatePal( ByVal RedInc As UByte=1, ByVal GrnInc As UByte=2, ByVal BluInc As Ubyte=3,_
  Â  Â  Â  Â  Â  Â  Â ByVal RedDec As Ubyte=3, ByVal GrnDec As Ubyte=2, ByVal BluDec As Ubyte=1 )
  Â  
  Â  Dim A, Red, Grn, Blu
  Â  Dim RedDir, GrnDir, BluDir
  

  Â  For A=1 To 1024
  Â  Â  Â  
  Â  Â  Â  If RedDir=0 And A>512 Then
  Â  Â  Â  
  Â  Â  Â  Â  Â  Red=Red+RedInc
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  If Red>255 Then
  Â  Â  Â  Â  Â  Â  Â  Red  Â  Â = 255
  Â  Â  Â  Â  Â  Â  Â  RedDir  = 1
  Â  Â  Â  Â  Â  End If
  Â  Â  Â  Â  Â  
  Â  Â  Â  End If


  Â  Â  Â  If GrnDir=0 Then 'And A>512 Then
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  Grn=Grn+GrnInc
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  If Grn>255 Then
  Â  Â  Â  Â  Â  Â  Â  Grn  Â  Â = 255
  Â  Â  Â  Â  Â  Â  Â  GrnDir  = 1
  Â  Â  Â  Â  Â  End If
  Â  Â  Â  
  Â  Â  Â  End IF

 
  Â  Â  Â  If BluDir=0 Then
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  Blu=Blu+BluInc
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  If Blu>255 Then
  Â  Â  Â  Â  Â  Â  Â  Blu  Â  Â = 255
  Â  Â  Â  Â  Â  Â  Â  BluDir  = 1
  Â  Â  Â  Â  Â  End If
  Â  Â  Â  
  Â  Â  Â  End If
  Â  Â  Â  
  Â  Â  Â  
  Â  Â  Â  If RedDir=1 Then
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  Red=Red-RedDec
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  If Red<0 Then
  Â  Â  Â  Â  Â  Â  Â  Red  Â  Â = 0
  Â  Â  Â  Â  Â  Â  Â  RedDir  = 0
  Â  Â  Â  Â  Â  End If
  Â  Â  Â  
  Â  Â  Â  End If


  Â  Â  Â  If GrnDir=1 Then
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  Grn=Grn-GrnDec
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  If Grn<0 Then
  Â  Â  Â  Â  Â  Â  Â  Grn  Â  Â = 0
  Â  Â  Â  Â  Â  Â  Â  GrnDir  = 0
  Â  Â  Â  Â  Â  End If
  Â  Â  Â  Â  Â  
  Â  Â  Â  End If

  Â  Â  Â  
  Â  Â  Â  If BluDir=1 Then
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  Blu=Blu-BluDec
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  If Blu<0 Then
  Â  Â  Â  Â  Â  Â  Â  Blu  Â  Â = 0
  Â  Â  Â  Â  Â  Â  Â  BluDir  = 0
  Â  Â  Â  Â  Â  End If
  Â  Â  Â  
  Â  Â  Â  End If

  Â  Â  Â  '
  Â  Â  Â  ' Calculate colour and store.
  Â  Â  Â  '
  Â  Â  Â  PlasmaPal(A)=( Red Shl 16) Or ( Grn Shl 8) Or ( Blu Shl 0 )

  Â  Next
  Â  
End Sub


Sub FeedPixels( Buffer(), Byval x As Integer, Byval y As Integer, Byval col As Integer)
  Â  Â  Â  
  Â  Â  Â  If x>0 and x<XRES-1 and y>0 and y<YRES-1 then
  Â  Â  Â  
  Â  Â  Â  Â  Â  Buffer(y * XRES + x) = col
  Â  Â  Â  
  Â  Â  Â  End If
  Â  Â  Â  
End Sub


Sub InitializePlasma()
  Â  
  Â  '
  Â  ' Setup Screen.
  Â  '
  Â  If( ptc_open( "Cool Plasma V1", XRES, YRES ) = 0 ) Then
End -1Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  
End if
  Â  
  Â  Randomize Timer()
  Â  
  Â  Dim a

  Â  For a=1 To 2880

  Â  Â  Â  sintable(a) = Sin((a * PI/180))
  Â  Â  Â  costable(a) = Cos((a * PI/180))

  Â  Next
  Â  
  Â  CreatePal( Int(Rnd*5)+1, Int(Rnd*5)+1, Int(Rnd*5)+1,_
  Â  Â  Â  Â  Â  Â  Â Int(Rnd*5)+1, Int(Rnd*5)+1, Int(Rnd*5)+1 )
  Â  
  Â  PlasmaDir1= Int(Rnd*5)+2
  Â  PlasmaDir2= Int(Rnd*5)+2
  Â  
End Sub


Sub RunPlasma()
  Â  
  Â  Dim Key As String
  Â  
  Â  PlasmaTimer  Â  Â = Millisecs( Timer() )
  Â  PlasmaDuration  = 4000
  Â  
  Â  While Key<>Chr(KEY_ESC)
  Â  Â  Â  
  Â  Â  Â  RunningTime = Timer()
  Â  Â  Â  
  Â  Â  Â  UpdatePlasma()
  Â  Â  Â  
  Â  Â  Â  Ptc_Update @ScreenBuffer(0)
  Â  Â  Â  
  Â  Â  Â  If Key=Chr(KEY_TAB) Then
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  CreatePal( Int(Rnd*5)+1, Int(Rnd*5)+1, Int(Rnd*5)+1,_
  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â Int(Rnd*5)+1, Int(Rnd*5)+1, Int(Rnd*5)+1 )
  Â  Â  Â  Â  Â  
  Â  Â  Â  End If
  Â  Â  Â  
  Â  Â  Â  
  Â  Â  Â  If (PlasmaTimer + PlasmaDuration) <=Millisecs( RunningTime ) then
  Â  Â  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  Â  Â  PlasmaTimer = Millisecs( RunningTime )
  Â  Â  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  Â  Â  CreatePal( Int(Rnd*5)+1, Int(Rnd*5)+1, Int(Rnd*5)+1,_
  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â Int(Rnd*5)+1, Int(Rnd*5)+1, Int(Rnd*5)+1 )
  Â  Â  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  Â  Â  PlasmaDir1=Int(Rnd*5)+2
  Â  Â  Â  Â  Â  Â  Â  PlasmaDir2=Int(Rnd*5)+2
  Â  Â  Â  Â  Â  Â  Â  
  Â  Â  Â  End If
  Â  Â  Â  
  Â  Â  Â  Key=Inkey()

  Â  Wend
  Â  
End Sub


Sub UpdatePlasma()
  Â  
  Â  Dim x,y
  Â  Dim cv1,cv2,ca1,ca2  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â ' Used in calculations.
  Â  
  Â  PlasmaAdd1=PlasmaAdd1+PlasmaDir1  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  ' These additions are
  Â  PlasmaAdd2=PlasmaAdd2+PlasmaDir2  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  ' For different speeds X+Y.

  Â  If PlasmaAdd1>360 Then PlasmaAdd1=PlasmaAdd1-360
  Â  If PlasmaAdd2>360 Then PlasmaAdd2=PlasmaAdd2-360

  Â  ca1 = 50+49*costable( PlasmaAdd1 )  Â  Â  Â  Â  Â  Â  Â  Â  Â ' You can play with these vars.
  Â  ca2 = 50+49*sintable( PlasmaAdd2 )  Â  Â  Â  Â  Â  Â  Â  Â  Â ' Strange things may happen though.

  Â  '
  Â  ' Calculate the plasma and store it, it's stored in case
  Â  ' you want to do any pixel wise effects in the same screen
  Â  ' array so it only needs to be drawn once.
  Â  '
  Â  For y = 1 To YRES - 1
  Â  Â  Â  
  Â  Â  Â  cv1 = 205+Int(ca1*sintable( PlasmaAdd2+y ))
  Â  Â  Â  
  Â  Â  Â  For x = 1 To XRES - 1
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  cv2 = 400+Int(ca2*costable( PlasmaAdd1+x ))
  Â  Â  Â  Â  Â  
  Â  Â  Â  Â  Â  RGBScreen(x,y) = cv1+cv2
  Â  
  Â  Â  Â  Next
  Â  
  Â  Next

  Â  '-------------------------------------------------
  Â  ' Feed Pixels To ScreenBuffer()
  Â  '-------------------------------------------------
  Â  For y = 1 To YRES - 1
  Â  Â  Â  For x = 1 To XRES - 1

  Â  Â  Â  Â  Â  FeedPixels( ScreenBuffer(), x, y, PlasmaPal( RGBScreen( x, y )))

  Â  Â  Â  Next
  Â  Next

End Sub


Function Millisecs( ByVal TimeVal As Double ) As Double
  Â  
  Â  Return ( TimeVal * 1000.00 )
  Â  Â  Â  
End Function

Cheers and all ze best,
Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: Plasma Effect V1
« Reply #1 on: May 04, 2006 »
Neat.  You may want to read my tutorial on how to make plasmas too. It discusses the way I make my plasmas. :*)

http://www.petesqbsite.com/sections/zines/qbcm/issues/4-1/default.html#plasmas

Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Plasma Effect V1
« Reply #2 on: May 04, 2006 »
Cheers Dude :)
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Plasma Effect V1
« Reply #3 on: May 04, 2006 »
Jeez, that ran too fast for my eyes!!  :o
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Re: Plasma Effect V1
« Reply #4 on: May 04, 2006 »
Looks pretty nice in 640 x 480 res too.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won: