C++

Miscellaneous Forums/General Discussion/C++

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here's a question:

What if I wanted to rewrite my particle system in C++? Obviously, I'd stay away from drawing routines in C++ (let bmax handle that) but would doing the math and stuff be quicker in C++? How hard would it be to do this?

Here's my particle system, anyone can use it, I don't care:


Private
Global ParticleList:TList = CreateList()


Rem
bbdoc: Particle object type
about: Handles particles
EndRem
Type TParticle
	Field x:Float,y:Float,a:Float = 1,r:Int
	Field ox:Int,oy:Int
	Field img:TImage
	Field dx:Float,dy:Float, da:Float
	Field maxdistance:Int
	Field speed:Float
	Field dir:Int
	Field fade:Int
	Field z:Int
	
	Method Create:TParticle(_x:Int, _y:Int, _img:TImage, _dir:Int = 0, _speed:Float = 0, frames:Int = 1, _fade:Int = False, _autorot:Int = False, _z:Int = 0)
		ListAddLast(ParticleList,Self)
		z = _z
		x = _x
		y = _y
		ox = _x
		oy = _y
		img = _img
		speed = _speed
		dir = _dir
		dx = Sin(dir) * speed
		dy = -Cos(dir) * speed
		da = 1.0 / frames
		maxdistance = frames
		If dir And _autorot
			r = -dir - 180
		End If

		If _fade
			fade = True
		End If
	End Method
	
	Method Update:TParticle()
		x:+dx
		y:+dy
		If fade a:-da
		SetRotation(r)
		SetAlpha(a)
		DrawImage img,x,y
		SetRotation(0)
		SetAlpha(1)
		
		maxdistance:- 1
		If Not maxdistance Then
			Destroy()
		End If
		
		If x > GraphicsWidth() Or x < 0 - ImageWidth(img) Or y > GraphicsHeight() Or y < 0 - ImageHeight(img)
			Destroy()
		End If
	End Method
	
	
	Method SetPAlpha(alpha:Float)
		a = alpha
	End Method
	
	Method SetPRotation(rot:Int)
		r = rot
	End Method
	
	Method Destroy()
		ParticleList.Remove(Self)
	End Method
	
	Method AddX(_x:Int)
		x:+ _x
	End Method
	
	Method AddY(_y:Int)
		y:+ _y
	End Method
	
	Method SetDirection(_dir:Int)
		dir = _dir
	End Method
	
	Method SetSpeed(_speed:Float)
		speed = _speed
	End Method
	
End Type


Public


Rem
bbdoc: ParticleExplosion creator
about: Creates a particle explosion at X and Y with Image with a certain N of particles, that lasts a certain amount of Frames and can have a SpeedMultiplyer, and a Z location(to specify where rendered)
returns: Nothing
EndRem
Function ParticleExplosion(_x:Float, _y:Float, image:TImage, n:Int, frames:Int, speed_multiplyer:Float = 0, ar:Int = False, _z:Int = 0)
	Local speed:Float
	
	For Local i:Int = 1 To n
		Local dir:Int = Rand(0, 359)
		
		If speed_multiplyer
			speed = Rnd(0.08*speed_multiplyer, 1.5*speed_multiplyer)
		Else
			speed = Rnd(0.08, 1.5)
		End If
		
		Local part:TParticle = New TParticle.Create(_x, _y, image, dir, speed, frames - 10 + Rand((frames*1.40)), True, ar, _z)
	Next
	
End Function

Rem
bbdoc: Particle creator
about: Creates a particle at said point with said attributes
returns: Nothing
EndRem
Function EmitParticle(_x:Float, _y:Float, image:TImage, frames:Int, fade:Int = True, dir:Int = 0, speed:Float = 0, ar:Int = False, _z:Int = 0)

	Local part:TParticle = New TParticle.Create(_x, _y, image, dir, speed, frames, fade, ar, _z)

End Function

Rem
bbdoc: Particle count function
about: Returns the particle count
returns: The particle count
End Rem
Function ParticleCount:Int()
	Return ParticleList.Count()
End Function

Rem
bbdoc: Clear particles function
about: Clears all particles
returns: Nothing
End Rem
Function ClearParticles()
	ParticleList.Clear()
End Function

Rem
bbdoc: Clear particlesZ function
about: Clears all particles on the given Z level.
returns: Nothing
End Rem
Function ClearParticlesZ(z:Int = 0)
	For Local delp:TParticle = EachIn ParticleList
		If delp.z = z
			ParticleList.Remove(delp)
		End If
	Next
End Function

Rem
bbdoc: Particle update routine
about: Updates particles at said Z level, you can add a optional force routine for things such as gravity.
returns: Nothing
EndRem
Function UpdateParticlesZ(z:Int = 0, forcex:Float = 0, forcey:Float = 0)	
	For Local UpdateP:TParticle = EachIn ParticleList
		If UpdateP.z = z
			UpdateP.Update()
			If forcex UpdateP.x:+forcex
			If forcey UpdateP.y:+forcey
		End If
	Next
End Function