1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
/* sparkle2d.cc
manage 2D sparkles (in 2D mode, of course)
Copyright (C) 2000 Mathias Broxvall
Yannick Perret
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "sparkle2d.h"
#include "glHelp.h"
/* structure for */
typedef struct _glitter {
float pos[3];
float speed[3];
float color[4];
float size;
float age;
struct _glitter *next, *prev;
} Glitter;
// initialize sparkle module
Sparkle2D::Sparkle2D() {
// just be sure that this field is empty
this->sparkle_first = NULL;
}
// destroy
Sparkle2D::~Sparkle2D() {
// remove all remaining sparkle
clear();
}
// remove all entries
void Sparkle2D::clear() {
Sparkle *tmp = sparkle_first;
while (tmp != NULL) {
Sparkle *tmp2 = tmp->next;
delete tmp;
tmp = tmp2;
}
sparkle_first = NULL;
}
// remove a particular sparkle from the list
void Sparkle2D::remove_sparkle(Sparkle *sparkle) {
if (sparkle == NULL) return;
if (sparkle->next != NULL) sparkle->next->prev = sparkle->prev;
if (sparkle->prev != NULL)
sparkle->prev->next = sparkle->next;
else
sparkle_first = sparkle->next;
delete sparkle;
}
/*
* create and insert a glitter
*/
Sparkle *Sparkle2D::create_and_insert() {
Sparkle *tmp = new Sparkle;
if (tmp == NULL) { return (NULL); }
tmp->next = sparkle_first;
tmp->prev = NULL;
if (sparkle_first != NULL) { sparkle_first->prev = tmp; }
sparkle_first = tmp;
return (tmp);
}
/*
* add a new sparkle
*/
int Sparkle2D::add(float px, float py, float vx, float vy, float ttl, float size, float r,
float g, float b, float a) {
Sparkle *sparkle;
if ((sparkle = create_and_insert()) == NULL) return (0);
sparkle->pos[0] = px;
sparkle->pos[1] = py;
sparkle->speed[0] = vx;
sparkle->speed[1] = vy;
sparkle->color[0] = r;
sparkle->color[1] = g;
sparkle->color[2] = b;
sparkle->color[3] = a;
sparkle->size = size;
sparkle->ttl = ttl;
sparkle->age = 0.;
return (1);
}
int Sparkle2D::add(float pos[2], float speed[2], float ttl, float size, float color[4]) {
Sparkle *sparkle;
if ((sparkle = create_and_insert()) == NULL) return (0);
sparkle->pos[0] = pos[0];
sparkle->pos[1] = pos[1];
sparkle->speed[0] = speed[0];
sparkle->speed[1] = speed[1];
sparkle->color[0] = color[0];
sparkle->color[1] = color[1];
sparkle->color[2] = color[2];
sparkle->color[3] = color[3];
sparkle->size = size;
sparkle->ttl = ttl;
sparkle->age = 0.;
return (1);
}
int Sparkle2D::add(float pos[2], float speed[2], float ttl, float size) {
float col[4] = {1., 0.9, 0.1, 0.9};
return add(pos, speed, ttl, size, col);
}
int Sparkle2D::add(float pos[2], float speed[2], float ttl) {
float col[4] = {1., 0.9, 0.1, 0.9};
return add(pos, speed, ttl, 1., col);
}
int Sparkle2D::add(float pos[2], float ttl) {
float col[4] = {1., 0.9, 0.1, 0.9};
float speed[2];
speed[0] = 2. * frandom() - 1.;
speed[1] = 2. * frandom() - 1.;
return add(pos, speed, ttl, 1., col);
}
int Sparkle2D::add(float pos[2]) {
float col[4] = {1., 0.9, 0.1, 0.9};
float speed[2];
speed[0] = 2. * frandom() - 1.;
speed[1] = 2. * frandom() - 1.;
return add(pos, speed, 0.5 + frandom() * 2., 1., col);
}
/*
* draw existing glitters
*/
void Sparkle2D::draw() {
Sparkle *skl = sparkle_first;
while (skl != NULL) {
float age = skl->age;
if (age >= 0.) {
float tmp;
if (age < 0.1) {
tmp = 1. - age * 10.;
} else
tmp = age / skl->ttl;
float alpha = 1.0 - tmp;
float *clr = skl->color;
tmp = alpha * alpha;
float ex = tmp * skl->size * 0.8;
float ey = tmp * skl->size * 1. + (0.02 * age);
float *pos = skl->pos;
draw2DRectangle(pos[0] - ex, pos[1] - ey, 2 * ex, 2 * ey, 0., 0., 1., 1., clr[0], clr[1],
clr[2], clr[3] * alpha, textureGlitter);
}
skl = skl->next;
}
}
/*
* update existing glitters
*/
void Sparkle2D::tick(Real t) {
Sparkle *skl = sparkle_first;
while (skl != NULL) {
skl->age += t;
if (skl->age > skl->ttl) {
Sparkle *tmp = skl;
skl = skl->next;
remove_sparkle(tmp);
continue;
}
/* update pos */
skl->pos[0] += skl->speed[0] * t;
skl->pos[1] += skl->speed[1] * t;
skl->speed[1] += 200.0 * t; // MB. fix for varying framerates
skl = skl->next;
}
}
|