J'ai commencé à développer un système de particules en 2D (sans OGRE3D).
J'aurais besoin de vos précieux conseils pour savoir ce que je peux ajouter/modifier.
EDIT:
Cette partie de mon code fonctionne très mal sur mac : elle assombrie les images au lieu d'en filtrer les couleurs.
Si quelqu'un pouvait m'aider à résoudre ce problème.
Code : Tout sélectionner
FilteredImage(i) = CopyImage(img, #PB_Any)
StartDrawing(ImageOutput(FilteredImage(i)))
DrawingMode(#PB_2DDrawing_AllChannels)
For y = 0 To ImageHeight(FilteredImage(i)) - 1
For x = 0 To ImageWidth(FilteredImage(i)) - 1
col = Point(x,y)
r = (Red(col) * Red(colorfilter)) / 255
g = (Green(col) * Green(colorfilter)) / 255
b = (Blue(col) * Blue(colorfilter)) / 255
a = (Alpha(col) * Alpha(colorfilter)) / 255
Plot(x, y, RGBA(r, g, b, a))
Next
Next
StopDrawing()
Code : Tout sélectionner
;
; Particules 2D, par Retro-Bruno, 20/11/2013
;
#MaximumEmittersCount = 1000
#MaximumParticlesCountByEmitter = 1000
Global Dim Emitter2d.f(17, #MaximumEmittersCount) ; x, y, w, h, image, rate, count, maxlife, startwidth, startheight, growspeed, colorfilter, active( 1 / 0 )
Global Dim Particles2D(4, #MaximumParticlesCountByEmitter, #MaximumEmittersCount) ; x, y, life (de 1 à n, ou 0 pour inactif), angle
Global Dim FilteredImage(#MaximumEmittersCount)
; Créer si possible un nouvel émetteur de particules et retourne son numémo, sinon, retourne -1 (erreur)
; Ne pas créer de particules dans un block startdrawing/stopdrawing !
Procedure CreateParticlesEmitter2D(x, y, w, h, img, rate, count, maxlife, startwidth, startheight, growspeed.f, colorfilter, rayspeed.f = 0, roundspeed.f = 0, dirangle1 = 0, dirangle2 = 359)
; Image utilisée par l'émétteur
If IsImage(img) = 0
ProcedureReturn -1
EndIf
; x, y, w, h = Zone d'émission
; rate = Vitesse d'apparition des nouvelles particules
; count = Nombre maximum de particules qui peuvent exister en même temps
; maxlife = Durée de vie maximum d'une particule
; startwidth et startheight = taille de départ, ou '0' pour la taille originale de l'image
; growspeed = vitesse de croissance ou de décroissance si ce nombre est négatif
; colorfilter, si différent de 0, applique un filtre de couleur et une transparence alpha (colorfilter = RGBA(r,g,b,a))
; rayspeed est la vitesse à laquelle s'éloignent les particules du centre ( >= 0)
; roundspeed est la vitesse de gravitation autour du centre (positive, négative ou nulle)
; dirangle1 et dirangle2 sont les angles d'ouverture du champs de propagation des particules (par défaut de 0 à 359)
If w <= 0 Or h <= 0 Or rate <= 0 Or count <= 0 Or maxlife <= 0 Or startwidth < 0 Or startheight < 0 Or rayspeed < 0
ProcedureReturn -1
EndIf
If startwidth = 0
startwidth = ImageWidth(img)
EndIf
If startheight = 0
startheight = ImageHeight(img)
EndIf
For i = 1 To #MaximumEmittersCount
If Emitter2D(17, i) = 0
Emitter2D(17, i) = 1
Emitter2D(1, i) = x
Emitter2D(2, i) = y
Emitter2D(3, i) = w
Emitter2D(4, i) = h
Emitter2D(5, i) = img
Emitter2D(6, i) = rate
Emitter2D(7, i) = count
Emitter2D(8, i) = maxlife
Emitter2D(9, i) = startwidth
Emitter2D(10, i) = startheight
Emitter2D(11, i) = growspeed
Emitter2D(12, i) = colorfilter
Emitter2D(13, i) = rayspeed
Emitter2D(14, i) = roundspeed
a1 = Mod(dirangle1, 360)
a2 = Mod(dirangle2, 360)
Emitter2D(15, i) = a1
Emitter2D(16, i) = a2
FilteredImage(i) = CopyImage(img, #PB_Any)
StartDrawing(ImageOutput(FilteredImage(i)))
DrawingMode(#PB_2DDrawing_AllChannels)
For y = 0 To ImageHeight(FilteredImage(i)) - 1
For x = 0 To ImageWidth(FilteredImage(i)) - 1
col = Point(x,y)
r = (Red(col) * Red(colorfilter)) / 255
g = (Green(col) * Green(colorfilter)) / 255
b = (Blue(col) * Blue(colorfilter)) / 255
a = (Alpha(col) * Alpha(colorfilter)) / 255
Plot(x, y, RGBA(r, g, b, a))
Next
Next
StopDrawing()
For j = 1 To #MaximumParticlesCountByEmitter
Particles2D(3, j, i) = 0
Next
ProcedureReturn i
EndIf
Next
ProcedureReturn -1
EndProcedure
; Procedurede de mise à jour et d'affichage des particules
Procedure Update2DParticles()
RandomSeed(ElapsedMilliseconds())
; Creation, vie et destruction des particules
For i = 1 To #MaximumEmittersCount
If Emitter2D(17, i) = 1
rate = Emitter2D(6, i)
x1 = Emitter2D(1, i)
y1 = Emitter2D(2, i)
x2 = x1 + Emitter2D(3, i) - 1
y2 = y1 + Emitter2D(4, i) - 1
; Mise à jour de la vie des particule + création et destruction
count = Emitter2D(7, i)
For j = 1 To #MaximumParticlesCountByEmitter
If Particles2D(3, j, i) > 0
Particles2D(3, j, i) + 1
If Particles2D(3, j, i) = Emitter2D(8, i)
Particles2D(3, j, i) = 0
EndIf
EndIf
If Particles2D(3, j, i) = 0 And rate > 0
Particles2D(3, j, i) = 1
vx = 0
If x1 < 0
vx = Abs(x1)
EndIf
If x2 + vx < 0
vx + Abs(x2 + vx)
EndIf
vy = 0
If y1 < 0
vy = Abs(y1)
EndIf
If y2 + vy < 0
vy + Abs(y2 + vy)
EndIf
Particles2D(1, j, i) = Random(x2 + vx, x1 + vx) - vx
Particles2D(2, j, i) = Random(y2 + vy, y1 + vy) - vy
If Emitter2D(15, i) < 0 Or Emitter2D(16, i) < 0
Emitter2d(15, i) + 360
Emitter2d(16, i) + 360
EndIf
Particles2D(4, j, i) = Mod(Random(Emitter2D(16, i),Emitter2D(15, i)), 360)
rate - 1
EndIf
; Affichage des particules
If Particles2D(3, j, i) > 0
w = Emitter2D(9, i) + (Emitter2D(11, i) * Particles2D(3, j, i))
h = Emitter2D(10, i) + (Emitter2D(11, i) * Particles2D(3, j, i))
If w <= 0 Or h <= 0
Particles2D(3, j, i) = 0
Else
If count > 0
x = Particles2D(1, j, i) - Int(w / 2)
y = Particles2D(2, j, i) - Int(h / 2)
tmpimg = CopyImage(FilteredImage(i), #PB_Any)
ResizeImage(tmpimg, w, h, #PB_Image_Raw)
roundspeed = Mod(Emitter2D(14, i) * Particles2D(3, j, i), 360)
rayspeed = Emitter2D(13, i) * Particles2D(3, j, i)
angle = Mod(roundspeed + Particles2D(4, j, i), 360)
DrawAlphaImage(ImageID(tmpimg), x + (rayspeed * Cos(Radian(angle))), y + (rayspeed * Sin(Radian(angle))))
FreeImage(tmpimg)
count - 1
EndIf
EndIf
EndIf
Next
EndIf
Next
EndProcedure
Procedure MoveEmitter2d(n, x, y)
Emitter2D(1, n) = x
Emitter2D(2, n) = y
EndProcedure
Procedure FreeEmitter2d(n)
Emitter2d(17,n) = 0
EndProcedure
Code : Tout sélectionner
IncludeFile "particules2d.pb"
#FPS = 30
If Not(InitSprite())
End
EndIf
If Not(InitMouse())
End
EndIf
If Not(OpenScreen(800,600,32,"Test Particules 2D"))
End
EndIf
image = CreateImage(#PB_Any,128,128,32,#PB_Image_Transparent)
StartDrawing(ImageOutput(image))
DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Gradient)
CircularGradient(80,48,46)
FrontColor(RGBA(255,255,255,255))
BackColor(RGBA(127,127,127,255))
Circle(64,64,63)
StopDrawing()
image2 = CreateImage(#PB_Any,128,128,32,#PB_Image_Transparent)
StartDrawing(ImageOutput(image2))
DrawingMode(#PB_2DDrawing_AlphaBlend | #PB_2DDrawing_Gradient)
CircularGradient(80,48,46)
FrontColor(RGBA(255,64,0,255))
BackColor(RGBA(255,255,0,255))
Circle(64,64,63)
StopDrawing()
screen = CreateSprite(#PB_Any, 800, 600, #PB_Sprite_AlphaBlending)
angle = 0
angle2 = 0
pe1 = CreateParticlesEmitter2D(400 + (100 * Cos(Radian(angle))), 295 + (100 * Sin(Radian(angle))), 1, 10, image, 4, 128, 24, 8, 4, 1.0, RGBA(0,128,0,20), 7, 0, 260, 280)
If pe1 = 0
CloseScreen()
MessageRequester("Erreur","Particules non créées !")
End
EndIf
pe2 = CreateParticlesEmitter2D(440, 320, 10, 1, image, 4, 128, 20, 1, 1, 0.5, RGBA(0,128,255,20), 7, 5, -10, 10)
If pe2 = 0
CloseScreen()
MessageRequester("Erreur","Particules non créées !")
End
EndIf
pe3 = CreateParticlesEmitter2D(360, 320, 10, 1, image, 4, 128, 20, 1, 1, 0.5, RGBA(0,128,255,20), 7, -5, 170, 190)
If pe3 = 0
CloseScreen()
MessageRequester("Erreur","Particules non créées !")
End
EndIf
pe4 = CreateParticlesEmitter2D(395, 295, 10, 10, image2, 8, 256, 16, 4, 1, 4, RGBA(255,255,255,70), 10, 0, 250, 290)
If pe4 = 0
CloseScreen()
MessageRequester("Erreur","Particules non créées !")
End
EndIf
sortie = 0
While sortie = 0
t = ElapsedMilliseconds()
StartDrawing(SpriteOutput(screen))
Box(0,0,800,600,RGBA(0,0,0,255))
Update2DParticles()
angle2 + 10
angle + Int(10.00 * Sin(Radian(angle2))) - 5
angle = Mod(angle, 360)
angle2 = Mod(angle2, 360)
MoveEmitter2d(pe1, 400 + (100 * Cos(Radian(angle))), 295 + (100 * Sin(Radian(angle))))
DrawText(10,10,"LeftClick to finish...",RGB(255,255,255))
StopDrawing()
DisplaySprite(screen,0,0)
FlipBuffers()
While ElapsedMilliseconds() - t < 1000 / #FPS
ExamineMouse()
If MouseButton(#PB_MouseButton_Left)
sortie = 1
EndIf
Wend
ExamineMouse()
If MouseButton(#PB_MouseButton_Left)
sortie = 1
EndIf
Wend
FreeEmitter2d(pe1)
FreeEmitter2d(pe2)
FreeEmitter2d(pe3)
FreeEmitter2d(pe4)
CloseScreen()
End