193f12425SAlexander von Gluck IV /*
28cd10c71STri-Edge AI * Copyright 2012-2013 Tri-Edge AI <triedgeai@gmail.com>
38e727810SJohn Scipione * Copyright 2014 Haiku, Inc. All rights reserved.
48e727810SJohn Scipione *
58e727810SJohn Scipione * Distributed under the terms of the MIT license.
68e727810SJohn Scipione *
78e727810SJohn Scipione * Authors:
88e727810SJohn Scipione * Tri-Edge AI
98e727810SJohn Scipione * John Scipione, jscipione@gmail.com
1093f12425SAlexander von Gluck IV */
1193f12425SAlexander von Gluck IV
1293f12425SAlexander von Gluck IV
138cd10c71STri-Edge AI #include "Particle.h"
148cd10c71STri-Edge AI
158cd10c71STri-Edge AI #include <stdlib.h>
1693f12425SAlexander von Gluck IV
1793f12425SAlexander von Gluck IV
1893f12425SAlexander von Gluck IV #define frand() ((float)rand() / (float)RAND_MAX)
1993f12425SAlexander von Gluck IV
208cd10c71STri-Edge AI
218e727810SJohn Scipione BObjectList<Particle>* Particle::list;
2293f12425SAlexander von Gluck IV
2393f12425SAlexander von Gluck IV
248e727810SJohn Scipione /*static*/ void
Initialize(int32 size,int32 shade)2593f12425SAlexander von Gluck IV Particle::Initialize(int32 size, int32 shade)
2693f12425SAlexander von Gluck IV {
278e727810SJohn Scipione list = new BObjectList<Particle>(2048);
288cd10c71STri-Edge AI
2993f12425SAlexander von Gluck IV for (int32 i = 0; i < size; i++) {
308cd10c71STri-Edge AI Particle* p = new Particle();
318e727810SJohn Scipione Particle::_FillParticle(p, size, shade);
328cd10c71STri-Edge AI list->AddItem(p);
3393f12425SAlexander von Gluck IV }
3493f12425SAlexander von Gluck IV }
3593f12425SAlexander von Gluck IV
3693f12425SAlexander von Gluck IV
378e727810SJohn Scipione /*static*/ void
AddParticles(int32 size,int32 shade)388e727810SJohn Scipione Particle::AddParticles(int32 size, int32 shade)
398e727810SJohn Scipione {
408e727810SJohn Scipione for (int32 i = list->CountItems(); i < size; i++) {
418e727810SJohn Scipione Particle* p = new Particle();
428e727810SJohn Scipione Particle::_FillParticle(p, size, shade);
438e727810SJohn Scipione list->AddItem(p);
448e727810SJohn Scipione }
458e727810SJohn Scipione }
468e727810SJohn Scipione
478e727810SJohn Scipione
488e727810SJohn Scipione /*static*/ void
RemoveParticles(int32 size,int32 shade)498e727810SJohn Scipione Particle::RemoveParticles(int32 size, int32 shade)
508e727810SJohn Scipione {
518e727810SJohn Scipione while (list->CountItems() > size)
528e727810SJohn Scipione delete list->RemoveItemAt(list->CountItems() - 1);
538e727810SJohn Scipione }
548e727810SJohn Scipione
558e727810SJohn Scipione
568e727810SJohn Scipione /*static*/ void
ColorParticles(int32 size,int32 shade)578e727810SJohn Scipione Particle::ColorParticles(int32 size, int32 shade)
588e727810SJohn Scipione {
598e727810SJohn Scipione for (int32 i = 0; i < size; i++) {
608e727810SJohn Scipione Particle* p = list->ItemAt(i);
618e727810SJohn Scipione Particle::_ColorParticle(p, size, shade);
628e727810SJohn Scipione }
638e727810SJohn Scipione }
648e727810SJohn Scipione
658e727810SJohn Scipione
668e727810SJohn Scipione /*static*/ void
Terminate()6793f12425SAlexander von Gluck IV Particle::Terminate()
6893f12425SAlexander von Gluck IV {
698cd10c71STri-Edge AI list->MakeEmpty();
708cd10c71STri-Edge AI delete list;
7193f12425SAlexander von Gluck IV }
7293f12425SAlexander von Gluck IV
7393f12425SAlexander von Gluck IV
748e727810SJohn Scipione /*static*/ void
Tick()758cd10c71STri-Edge AI Particle::Tick()
7693f12425SAlexander von Gluck IV {
778cd10c71STri-Edge AI for (int32 i = 0; i < list->CountItems(); i++) {
788e727810SJohn Scipione Particle* p = list->ItemAt(i);
798cd10c71STri-Edge AI p->_Logic();
808cd10c71STri-Edge AI p->_Render();
818cd10c71STri-Edge AI }
8293f12425SAlexander von Gluck IV }
8393f12425SAlexander von Gluck IV
8493f12425SAlexander von Gluck IV
8593f12425SAlexander von Gluck IV void
_Logic()868cd10c71STri-Edge AI Particle::_Logic()
8793f12425SAlexander von Gluck IV {
888cd10c71STri-Edge AI // Motion
8993f12425SAlexander von Gluck IV x += vx;
9093f12425SAlexander von Gluck IV y += vy;
9193f12425SAlexander von Gluck IV z += vz;
9293f12425SAlexander von Gluck IV r += vr;
9393f12425SAlexander von Gluck IV
948cd10c71STri-Edge AI // Friction
9593f12425SAlexander von Gluck IV vx *= 0.98f;
9693f12425SAlexander von Gluck IV vy *= 0.98f;
9793f12425SAlexander von Gluck IV vz *= 0.98f;
9893f12425SAlexander von Gluck IV vr *= 0.98f;
9993f12425SAlexander von Gluck IV }
10093f12425SAlexander von Gluck IV
10193f12425SAlexander von Gluck IV
10293f12425SAlexander von Gluck IV void
_Render() const1038cd10c71STri-Edge AI Particle::_Render() const
10493f12425SAlexander von Gluck IV {
10593f12425SAlexander von Gluck IV glPushMatrix();
10693f12425SAlexander von Gluck IV glTranslatef(x, y, z);
10793f12425SAlexander von Gluck IV glRotatef(r, 0.0f, 0.0f, 1.0f);
10893f12425SAlexander von Gluck IV glColor3f(red, green, blue);
1098cd10c71STri-Edge AI glBegin(GL_QUADS);
1108cd10c71STri-Edge AI glVertex3f(-0.25f, 0.25f, 0.0f);
1118cd10c71STri-Edge AI glVertex3f(-0.25f, -0.25f, 0.0f);
1128cd10c71STri-Edge AI glVertex3f( 0.25f, -0.25f, 0.0f);
1138cd10c71STri-Edge AI glVertex3f( 0.25f, 0.25f, 0.0f);
11493f12425SAlexander von Gluck IV glEnd();
11593f12425SAlexander von Gluck IV glPopMatrix();
11693f12425SAlexander von Gluck IV }
1178e727810SJohn Scipione
1188e727810SJohn Scipione
1198e727810SJohn Scipione /*static*/ void
_FillParticle(Particle * p,int32 size,int32 shade)1208e727810SJohn Scipione Particle::_FillParticle(Particle* p, int32 size, int32 shade)
1218e727810SJohn Scipione {
1228e727810SJohn Scipione p->x = frand() * 30.0f - 15.0f;
1238e727810SJohn Scipione p->y = frand() * 30.0f - 15.0f;
1248e727810SJohn Scipione p->z = frand() * 5.0f;
1258e727810SJohn Scipione p->r = frand() * 360.0f;
1268e727810SJohn Scipione
1278e727810SJohn Scipione p->vx = frand() - 0.5f;
1288e727810SJohn Scipione p->vy = frand() - 0.5f;
1298e727810SJohn Scipione p->vz = frand() - 0.5f;
1308e727810SJohn Scipione p->vr = (frand() - 0.5f) * 180.0f;
1318e727810SJohn Scipione
1328e727810SJohn Scipione Particle::_ColorParticle(p, size, shade);
1338e727810SJohn Scipione }
1348e727810SJohn Scipione
1358e727810SJohn Scipione
1368e727810SJohn Scipione /*static*/ void
_ColorParticle(Particle * p,int32 size,int32 shade)1378e727810SJohn Scipione Particle::_ColorParticle(Particle* p, int32 size, int32 shade)
1388e727810SJohn Scipione {
1398e727810SJohn Scipione switch(shade) {
1408e727810SJohn Scipione case 0:
1418e727810SJohn Scipione // Red
1428e727810SJohn Scipione p->red = 0.1f + frand() * 0.2f;
1438e727810SJohn Scipione p->green = 0.0f;
1448e727810SJohn Scipione p->blue = frand() * 0.05f;
1458e727810SJohn Scipione break;
1468e727810SJohn Scipione
1478e727810SJohn Scipione case 1:
1488e727810SJohn Scipione // Green
1498e727810SJohn Scipione p->red = 0;
1508e727810SJohn Scipione p->green = 0.1f + frand() * 0.2f;
1518e727810SJohn Scipione p->blue = frand() * 0.05f;
1528e727810SJohn Scipione break;
1538e727810SJohn Scipione
1548e727810SJohn Scipione case 2:
1558e727810SJohn Scipione default:
1568e727810SJohn Scipione // Blue
1578e727810SJohn Scipione p->red = 0;
1588e727810SJohn Scipione p->green = frand() * 0.05f;
1598e727810SJohn Scipione p->blue = 0.1f + frand() * 0.2f;
1608e727810SJohn Scipione break;
1618e727810SJohn Scipione
1628e727810SJohn Scipione case 3:
1638e727810SJohn Scipione // Orange
1648e727810SJohn Scipione p->red = 0.1f + frand() * 0.1f;
1658e727810SJohn Scipione p->green = 0.05f + frand() * 0.1f;
1668e727810SJohn Scipione p->blue = 0.0f;
1678e727810SJohn Scipione break;
1688e727810SJohn Scipione
1698e727810SJohn Scipione case 4:
1708e727810SJohn Scipione // Purple
1718e727810SJohn Scipione p->red = 0.1f + frand() * 0.2f;
1728e727810SJohn Scipione p->green = 0.0f;
1738e727810SJohn Scipione p->blue = 0.1f + frand() * 0.2f;
1748e727810SJohn Scipione break;
1758e727810SJohn Scipione
1768e727810SJohn Scipione case 5:
1778e727810SJohn Scipione // White
1788e727810SJohn Scipione p->red = p->green = p->blue = 0.1f + frand() * 0.2f;
179*2094bbb3SJohn Scipione break;
1808e727810SJohn Scipione
1818e727810SJohn Scipione case 6:
1828e727810SJohn Scipione // Rainbow
1838e727810SJohn Scipione p->red = 0.1f + frand() * 0.2f;
1848e727810SJohn Scipione p->green = 0.1f + frand() * 0.2f;
1858e727810SJohn Scipione p->blue = 0.1f + frand() * 0.2f;
1868e727810SJohn Scipione break;
1878e727810SJohn Scipione }
1888e727810SJohn Scipione }
189