Dark Bit Factory & Gravity
PROGRAMMING => Freebasic => Topic started by: Hezad on November 03, 2008
-
Hey :)
Well today I wanted to try to code a simple smoke engine .. (thanks to fb.gfx_alpha_primitives :D) As always with my snippets, nothing really useful, but I guess it could give some ideas. For information, the graphics are made with predrawn circles (with kind of an ultra simple grey gradient) rendered with alpha (the older is a particule, the lesser is his alpha value)
''
''
'' Smoke Object
''
'' 2008 - Hezad
''
'' Contact : Hezad0 at Gmail.com
''
''
randomize timer
#include "fbgfx.bi"
Const Smoke_xRes = 640
Const Smoke_yRes = 480
Const SCR_MEM = Smoke_xRes*Smoke_yRes
Const DEF_UNIT_SIZE = 10
Const SCR_MEM_SIZED = SCR_MEM/(DEF_UNIT_SIZE*DEF_UNIT_SIZE)
Const MAX_PARTICLES = 5000
Const PARTICLE_LIFE = 700
Const X_SPREAD = 1
Screenres Smoke_xRes, Smoke_yRes,32,2,FB.GFX_ALPHA_PRIMITIVES
Dim shared as any ptr ParticleGFX
ParticleGFX = imageCreate(10,10)
'' preparing sprites
Circle ParticleGFX,(5,5),5,rgb(70,70,70),,,,f
Circle ParticleGFX,(5,5),4,rgb(110,110,110),,,,f
Circle ParticleGFX,(5,5),3,rgb(180,180,180),,,,f
Circle ParticleGFX,(5,5),2,rgb(250,250,250),,,,f
dim shared as single t0,GlobalT
Type vec2d
as single x,y
end type
Type pos2d
as integer x,y
end type
Dim shared as vec2d Wind
Dim shared as pos2d Mouse,oldMouse
Type ParticleType
as vec2d pos2d
as vec2d V
as Integer Life
End Type
Type SmokeType
as integer Nb_Particles
as ParticleType ptr Particle
as vec2d p0
Declare Constructor
Declare Destructor
Declare Sub Handle()
End Type
Constructor SmokeType
Particle = Callocate(MAX_PARTICLES,Sizeof(ParticleType))
p0.x = Smoke_xRes*.5
p0.y = Smoke_yRes*.5
Nb_Particles = 1
End Constructor
Destructor SmokeType
deallocate Particle
End Destructor
Sub SmokeType.Handle()
dim as single _Unit = 1/DEF_UNIT_SIZE
Dim as integer posField
for i as integer = 0 to Nb_Particles - 1
'' updates velocity
Particle[i].V.x = 100*(Wind.x+(-X_SPREAD+rnd*X_SPREAD*2))/GlobalT
Particle[i].V.y = -50/GlobalT
'' updates Life
Particle[i].Life += 1
'' updates position
Particle[i].pos2d.x += Particle[i].V.x
Particle[i].pos2d.y += Particle[i].V.y
'' Clip
If Particle[i].pos2d.x<0 or Particle[i].pos2d.x>Smoke_xRes or _
Particle[i].pos2d.y<0 or Particle[i].pos2d.y>Smoke_yRes or _
Particle[i].Life >= PARTICLE_LIFE-1 then
Particle[i].pos2d.x = p0.x
Particle[i].pos2d.y = p0.y
Particle[i].V.x = 0
Particle[i].V.y = 0
if Particle[i].Life >= PARTICLE_LIFE-1 then Particle[i].Life = 0
end if
Dim as single rap = Particle[i].Life/PARTICLE_LIFE
'' RENDER
if Particle[i].pos2d.x<> p0.x and Particle[i].pos2d.y <> p0.y then
Put(Particle[i].pos2d.x-2,Particle[i].pos2d.y-2),ParticleGFX,alpha,10-9*rap
Circle(Particle[i].pos2d.x+2,Particle[i].pos2d.y+2),2,rgba(50*rap,50*rap,100,10-9*rap),,,,f
end if
next
Nb_Particles += 2 : If Nb_Particles>MAX_PARTICLES then Nb_PArticles=MAX_PARTICLES
'' updates Wind
Wind.x = cos(sin(timer)+rnd)*.2
Wind.y = sin(timer)*.2
End Sub
Dim as SmokeType S
t0 = timer
Windowtitle "Smoke"
setmouse Smoke_xRes*.5,Smoke_yRes*.5,0,1
Do : screenlock : cls
GlobalT = 1/(Timer-t0)
t0 = timer
oldmouse = mouse
getmouse mouse.x,mouse.y
S.p0.x = mouse.x
S.p0.y = mouse.y
S.handle
Screenunlock : sleep 1,1
Loop until multikey(&h01)
setmouse ,,1,0
(Move your mouse ;) )
edit : added a destructor to the smoke udt to deallocate the particles
-
Nice one Hezad! Looks very good.
DrewPee
-
I'll really have to instal a more up to date version of Freebasic one of these days.
The screen shot looks really good Hezad :) Thanks for supplying the source code too, K+
-
Thats pretty cool mate! Welldone, and good one on showing some code using constructors. +k
Btw - Remember that compiler error that I got with FB.Alpha_Primitives in one of your earlier works; well I found the solution, and that is running the program with -lang FB
Cheers and welldone,
Clyde.
-
@DrewPee, shockwave, Clyde :
Thanks :) :cheers:
@Shockwave :
Hum perhaps it's my version which is outdated :p I'm running v0.18
@Clyde :
you don't use -lang fb by default ? Anyway I'm glad you found the solution :D
Oh and warning about constructors, I'm not sure all pointers are deallocated in the destructors, I should check that :)
edit :
Updated the source : added a destructor to the smoke udt to deallocate the particles