Author Topic: Fireworks effect  (Read 3579 times)

Crystal Noir

  • Guest
Fireworks effect
« on: 2010-Mar-04 »
Hi there  :happy:

I'm happy to start here with a small snippet. Here's a fireworks effect that use alpha blend and sprites :)

To start with you will need to download this picture :



Here's the code !

Code: (glbasic) [Select]


// Variables setup
GLOBAL MonSprite = 0
GLOBAL Vitesse = 0.1
GLOBAL parax
GLOBAL paray
GLOBAL paraz
GLOBAL coulr
GLOBAL coulg
GLOBAL coulb
GLOBAL angle1
GLOBAL angle2

GLOBAL switche = 0
GLOBAL nbfeu = 300

GLOBAL temps = 0
GLOBAL quit = 0
GLOBAL posx
GLOBAL posy

// Types
TYPE FeuArtifice
x
y
z
vy
xd
yd
zd
r
g
b
alpha
factaille
taillex
tailley
ENDTYPE

GLOBAL MaListe[] AS FeuArtifice


// Sprites
SETTRANSPARENCY RGB(0,0,0)
LOADSPRITE "flare2.png",MonSprite

// Main Loop

WHILE quit = 0
IF temps = 0
parax = RND(200) - 100
    paray = RND(200) - 100
    paraz = 200

    coulr = RND(255)
    coulg = RND(255)
    coulb = RND(255)

    IF switche = 0
    temps = 60
    ENDIF

    FOR i = 1 TO nbfeu
    NewFeu()
    NEXT
    ENDIF

AfficheFeu()

temps = temps - 1

IF KEY(01)
quit = 1
ENDIF

SHOWSCREEN
WEND


END



FUNCTION NewFeu:
GLOBAL Liste AS FeuArtifice

Angle1 = RND(360)
  Angle2 = RND(360)
  Vitesse = 0.1

Liste.x = parax
Liste.y = paray
Liste.z = paraz
Liste.r = coulr
Liste.g = coulg
Liste.b = coulb

Liste.xd = COS(Angle1) * COS(Angle2) * Vitesse
Liste.yd = COS(Angle1) * SIN(Angle2) * Vitesse
Liste.zd = SIN(Angle1) * Vitesse

Liste.alpha = 3
Liste.factaille = -1

DIMPUSH MaListe[],Liste

ENDFUNCTION


FUNCTION AfficheFeu:
FOREACH Liste IN MaListe[]
IF Liste.alpha > 0
Liste.x = Liste.x + Liste.xd * 10.0
    Liste.y = Liste.y + Liste.yd * 10.0
    Liste.z = Liste.z + Liste.zd * 10.0
    Liste.y = Liste.y + Liste.vy
    Liste.vy = Liste.vy + 0.02

    posx = (800/2) + ((Liste.x / Liste.z) * 350)
    posy = (600/2) + ((Liste.y / Liste.z) * 350)

Liste.alpha = Liste.alpha - 0.02

    ALPHAMODE Liste.alpha

IF Liste.factaille < 0
    Liste.taillex = ((139*10)/Liste.z)
    Liste.tailley = ((143*10)/Liste.z)
    Liste.factaille = Liste.factaille + 1
    ELSEIF Liste.taillex > 0 AND Liste.tailley  > 0
    Liste.taillex = Liste.taillex - Liste.factaille
    Liste.tailley = Liste.tailley - Liste.factaille
    Liste.factaille = Liste.factaille + 0.001
    ENDIF
    STARTPOLY MonSprite
    POLYVECTOR posx,posy,0,0,RGB(Liste.r,Liste.g,Liste.b)
    POLYVECTOR posx,posy+Liste.tailley,0,143,RGB(Liste.r,Liste.g,Liste.b)
    POLYVECTOR posx+Liste.taillex,posy+Liste.tailley,139,143,RGB(Liste.r,Liste.g,Liste.b)
    POLYVECTOR posx+Liste.taillex,posy,139,0,RGB(Liste.r,Liste.g,Liste.b)
    ENDPOLY

ELSE
DELETE Liste

ENDIF
NEXT

ENDFUNCTION

« Last Edit: 2010-Mar-05 by Crystal Noir »

Offline Ian Price

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 4158
  • On the shoulders of giants.
    • View Profile
    • My Apps
Re: Fireworks effect
« Reply #1 on: 2010-Mar-05 »
That's lovely that is :)

Cheers.
I came. I saw. I played.

Offline Quentin

  • Prof. Inline
  • *****
  • Posts: 915
    • View Profile
Re: Fireworks effect
« Reply #2 on: 2010-Mar-05 »
nice, looks great

Offline Moru

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1787
    • View Profile
    • Homepage
Re: Fireworks effect
« Reply #3 on: 2010-Mar-06 »
Nice work, clear code! Sadly my computer seems a bit too slow to run it. Every time it creates a new firework after the second one, it pauses for a brief moment. I guess this is due to the many DIMPUSH that is needed at the same time during that frame so the animation misses a few frames.

A few french? words in the variables but easy to guess what they do anyway :-)

Crystal Noir

  • Guest
Re: Fireworks effect
« Reply #4 on: 2010-Mar-06 »
thx :)

The firework create 300 sprites each time with alpha blend. It may be a little slower, you can try to set 200 to NbFeu.

You can also put up the temps value to 100 instead of 60.

On my PC I don't have pause times but it depends of a lot of factors :)

Offline CrystalNoir

  • Mc. Print
  • *
  • Posts: 33
    • View Profile
Re: Fireworks effect
« Reply #5 on: 2011-Aug-30 »
Hi,

I've updated the code here to make it work with earlier version of GLBasic.

To simplify, I've made a zip file that contains the sources and medias.

Just extract the zip file and open the project, and compile it to launch.

Happy GLB !

[attachment deleted by admin]

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10745
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: Fireworks effect
« Reply #6 on: 2011-Aug-30 »
very good. I updated the code so it renders the whole firework in one batch (a lot faster on mobile phones).
Code: (glbasic) [Select]
FUNCTION AfficheFeu:
STARTPOLY MonSprite, 2
FOREACH Liste IN MaListe[]
IF Liste.alpha > 0
Liste.x = Liste.x + Liste.xd * 10.0
    Liste.y = Liste.y + Liste.yd * 10.0
    Liste.z = Liste.z + Liste.zd * 10.0
    Liste.y = Liste.y + Liste.vy
    Liste.vy = Liste.vy + 0.02

    posx = (800/2) + ((Liste.x / Liste.z) * 350)
    posy = (600/2) + ((Liste.y / Liste.z) * 350)

Liste.alpha = Liste.alpha - 0.02

    ALPHAMODE Liste.alpha

IF Liste.factaille < 0
    Liste.taillex = ((139*10)/Liste.z)
    Liste.tailley = ((143*10)/Liste.z)
    Liste.factaille = Liste.factaille + 1
    ELSEIF Liste.taillex > 0 AND Liste.tailley  > 0
    Liste.taillex = Liste.taillex - Liste.factaille
    Liste.tailley = Liste.tailley - Liste.factaille
    Liste.factaille = Liste.factaille + 0.001
    ENDIF
   
    POLYVECTOR posx,posy,0,0,RGB(Liste.r,Liste.g,Liste.b)
    POLYVECTOR posx,posy+Liste.tailley,0,143,RGB(Liste.r,Liste.g,Liste.b)
    POLYVECTOR posx+Liste.taillex,posy,139,0,RGB(Liste.r,Liste.g,Liste.b)
    POLYVECTOR posx+Liste.taillex,posy+Liste.tailley,139,143,RGB(Liste.r,Liste.g,Liste.b)
    POLYNEWSTRIP
ELSE
DELETE Liste

ENDIF
NEXT
ENDPOLY

ENDFUNCTION

Offline CrystalNoir

  • Mc. Print
  • *
  • Posts: 33
    • View Profile
Re: Fireworks effect
« Reply #7 on: 2011-Aug-30 »
Thank you :)