-
Notifications
You must be signed in to change notification settings - Fork 8
/
effects.cpp
756 lines (641 loc) · 15.8 KB
/
effects.cpp
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/* ************************************************************************* *
SDL-Ball - DX-Ball/Breakout remake with openGL and SDL for Linux
Copyright (C) 2008 Jimmy Christensen ( dusted at dusted dot dk )
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 3 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, see <http://www.gnu.org/licenses/>.
* ************************************************************************* */
struct effect_vars {
int type; //Hvilken slags effekt er det?
bool coldet; //er der collision detection på denne?
bool active; //Er denne effekt aktiv
GLfloat col[3]; //Hvilken farve har effekten?
GLfloat speed; //Hvor hurtigt bevæger den sig
GLfloat spread; //Hvor stor er spredningen (i grader)
GLfloat size; //Skalering af elementerne
struct pos rect; //rectangular size (for particleField)
GLfloat gravity; //Hvor stor er tyndgekraften
int num; //Hvor mange elementer er der i den
int life; //hvor mange ms lever den?
textureClass tex; //Texture
int effectId; //unique id for this effect
};
class transit_effect_class {
private:
GLfloat opacity;
public:
int age;
effect_vars vars;
void init()
{
age=0;
opacity = 0.0f;
var.transition_half_done=0;
}
void draw()
{
age += globalTicksSinceLastDraw;
if(age < vars.life/2.0) //første halvdel
{
//Gør solid
opacity += vars.life/500.0*globalMilliTicksSinceLastDraw;
} else {
var.transition_half_done=1;
//Gør trans
opacity -= vars.life/500.0*globalMilliTicksSinceLastDraw;
}
glLoadIdentity();
glDisable( GL_TEXTURE_2D );
glTranslatef(0.0, 0.0, -3.0);
glColor4f( vars.col[0], vars.col[1], vars.col[2], opacity );
glBegin( GL_QUADS );
glVertex3f(-1.66, 1.25, 0.0);
glVertex3f( 1.66, 1.25, 0.0);
glVertex3f( 1.66,-1.25, 0.0);
glVertex3f(-1.66,-1.25, 0.0);
glEnd( );
glEnable( GL_TEXTURE_2D );
}
};
class sparkle {
public:
bool active;
GLfloat size;
GLfloat ang;
int life;
int lifeleft;
pos p,v; //position og vel
effect_vars vars;
GLfloat bounce,f; //bounce og friktion
sparkle()
{
bounce=0;
f=0;
active=1;
}
void draw()
{
if(lifeleft > 0)
{
//sanitize
lifeleft -= globalTicksSinceLastDraw;
//er vi indenfor skærmen?
if(p.x > 1.67 || p.y < -1.7 || p.x < -1.67)
{
active=0;
}
v.y -= vars.gravity*globalMilliTicksSinceLastDraw;
v.y -= bounce*globalMilliTicksSinceLastDraw;
if(v.x < 0)
{
v.x += f*globalMilliTicksSinceLastDraw;
} else {
v.x -= f*globalMilliTicksSinceLastDraw;
}
p.x += v.x * globalMilliTicksSinceLastDraw;
p.y += v.y * globalMilliTicksSinceLastDraw;
glColor4f(vars.col[0], vars.col[1],vars.col[2], (1.0/(float)life)*(float)lifeleft);
GLfloat curSize = size/(float)life * (float)lifeleft;
glBindTexture( GL_TEXTURE_2D, vars.tex.prop.texture );
glBegin( GL_QUADS );
glTexCoord2f(0,0); glVertex3f(p.x-curSize,p.y+curSize, 0.0);
glTexCoord2f(0,1); glVertex3f(p.x+curSize,p.y+curSize, 0.0);
glTexCoord2f(1,1); glVertex3f(p.x+curSize,p.y-curSize, 0.0);
glTexCoord2f(1,0); glVertex3f(p.x-curSize,p.y-curSize, 0.0);
glEnd( );
} else {
active=0;
}
}
void coldet(brick & b)
{
if(p.x > b.posx-b.width && p.x < b.posx+b.width)
{
if(p.y > b.posy-b.height && p.y < b.posy+b.height)
{
//Hvor ramte vi fra?
if(p.y < b.posy && !b.n(3))
{
//bunden
v.y *= -1;
//p.y = b.posy-b.height-0.01;
} else
if(p.y > b.posy && !b.n(2))
{
//toppen
p.y = b.posy+b.height;
if(v.y < 0)
{
v.y *= -1;
v.y /= 2.0;
f += 0.01;
}
} else
if(p.x > b.posx && !b.n(1))
{
//p.x = b.posx+b.width;
//højre
if(v.x < 0)
v.x *= -1;
} else
if(p.x < b.posx && !b.n(0))
{
//p.x = b.posx-b.width;
//venstre
if(v.x > 0)
v.x *= -1;
}
}
}
}
void pcoldet(paddle_class & b)
{
if(p.x > b.posx-b.width && p.x < b.posx+b.width)
{
if(p.y > b.posy-b.height && p.y < b.posy+b.height)
{
//Hvor ramte vi fra?
if(p.y < b.posy)
{
//bunden
p.y = b.posy-b.height;
}
if(p.y > b.posy)
{
//toppen
p.y = b.posy+b.height;
if(v.y < 0)
{
v.y *= -1;
v.y /= 2.0;
f += 0.01;
}
} else
if(p.x > b.posx)
{
p.x = b.posx+b.width;
//højre
if(v.x < 0)
v.x *= -1;
} else
if(p.x < b.posx)
{
p.x = b.posx-b.width;
//venstre
if(v.x > 0)
v.x *= -1;
}
}
}
}
};
class particleFieldClass {
private:
int spawnTimeout; //This is used to spawn sparkles at a random time
sparkle *sparks;
void spawnSpark(int sparkNum);
public:
struct pos p;
struct effect_vars vars;
void init(struct effect_vars varsP, struct pos p);
void draw();
void move(struct pos p);
void coldet(brick & b);
void pcoldet(paddle_class & b);
~particleFieldClass();
};
void particleFieldClass::init(struct effect_vars varsP, struct pos spawnPos)
{
vars=varsP;
spawnTimeout=0;
vars.active=1;
sparks = new sparkle[vars.num];
p=spawnPos;
for(int i=0; i < vars.num; i++)
{
sparks[i].active=1;
sparks[i].vars = vars; //NOTE:remove effect_vars from sparkle class?
spawnSpark(i);
}
}
particleFieldClass::~particleFieldClass()
{
delete[] sparks; //free the sparks
}
void particleFieldClass::spawnSpark(int sparkNum)
{
GLfloat angle=(RAD/vars.num-1)*(rndflt(vars.num,0.0)); //FIXME: Make this elegant: Choose a random angle (in radients)
spawnTimeout = rand()%10;
sparks[sparkNum].life = rand()%vars.life;
sparks[sparkNum].lifeleft = sparks[sparkNum].life;
sparks[sparkNum].v.x = (vars.speed*rndflt(vars.speed*2.0,0.0)) * sin(angle);
sparks[sparkNum].v.y = (vars.speed*rndflt(vars.speed*2.0,0.0)) * cos(angle);
sparks[sparkNum].size = rndflt(vars.size,0);
sparks[sparkNum].p=p;
sparks[sparkNum].p.x += rndflt(vars.rect.x, (vars.rect.x/2.0));
sparks[sparkNum].p.y += rndflt(vars.rect.y, (vars.rect.y/2.0));
sparks[sparkNum].active=1;
}
void particleFieldClass::draw()
{
spawnTimeout -= globalTicksSinceLastDraw;
glLoadIdentity();
glTranslatef(0.0,0.0 , -3.0);
int t=0;
for(int i=0; i<vars.num; i++)
{
if(sparks[i].active)
{
t++;
sparks[i].draw();
}
}
vars.life -=globalTicksSinceLastDraw;
if(t==0)
{
vars.active=0;
}
}
void particleFieldClass::coldet(brick & b)
{
for(int i=0; i < vars.num; i++)
{
if(sparks[i].active)
sparks[i].coldet(b);
}
}
void particleFieldClass::pcoldet(paddle_class & b)
{
for(int i=0; i < vars.num; i++)
{
if(sparks[i].active)
sparks[i].pcoldet(b);
}
}
class effect_class {
private:
pos spawn_pos;
sparkle* sparks; //wohoo ;)
public:
transit_effect_class transit;
particleFieldClass *pf;
struct effect_vars vars;
effect_class()
{
vars.active=0; //når den bliver oprettet i hukkommelsen er den ikke i brug endnu
}
void init(pos p)
{
int i;
GLfloat angle=0.0;
vars.active=1;
spawn_pos=p;
int life;
//cout << "Spawned effect type " << vars.type << " at " << p.x << "," << p.y << " with " << vars.life << "ms life" << endl;
switch(vars.type)
{
case FX_SPARKS:
sparks = new sparkle[vars.num];
if(sparks == NULL)
{
cout << "Could not allocate " << vars.num << " sparks" << endl;
i=vars.num+10;
}
for(i=0; i < vars.num; i++)
{
sparks[i].size = rndflt(vars.size,0);
life = rand()%vars.life;
angle=(RAD/vars.num-1)*(rndflt(vars.num,0.0)); //FIXME: Make this elegant: Choose a random angle (in radients)
sparks[i].life = life;
sparks[i].lifeleft = life;
sparks[i].v.x = (vars.speed*rndflt(vars.speed*2.0,0.0)) * sin(angle);
sparks[i].v.y = (vars.speed*rndflt(vars.speed*2.0,0.0)) * cos(angle);
sparks[i].vars = vars;
sparks[i].p = spawn_pos;
}
break;
case FX_TRANSIT:
transit.vars = vars;
transit.init();
break;
case FX_PARTICLEFIELD:
pf=NULL;
pf = new particleFieldClass;
pf->init(vars, p);
break;
}
}
void draw()
{
//find ud af hvor længe der er gået siden sidst
int i;
bool stay=0;
switch(vars.type)
{
case FX_SPARKS:
glLoadIdentity();
glTranslatef(0.0,0.0 , -3.0);
for(i=0; i < vars.num; i++)
{
if(sparks[i].active)
{
sparks[i].draw();
stay=1;
}
}
break;
case FX_TRANSIT:
transit.draw();
if(transit.age <= vars.life)
stay=1;
break;
case FX_PARTICLEFIELD:
pf->draw();
if(pf->vars.active)
stay=1;
break;
}
//Ugley hack, to prevent transit from flickering when spark effects are deleted.
if(var.effectnum != -1 && vars.type != FX_TRANSIT)
stay=1;
if(!stay)
{
vars.active=0;
switch(vars.type)
{
case FX_SPARKS:
delete[] sparks; //Free sparks again ;)
break;
case FX_PARTICLEFIELD:
delete pf; //remove the particlefield
break;
}
}
}
void coldet(brick & b)
{
int i;
if(vars.type == FX_SPARKS)
{
for(i=0; i < vars.num; i++)
{
if(sparks[i].active)
{
sparks[i].coldet(b);
}
}
} else if(vars.type == FX_PARTICLEFIELD)
{
pf->coldet(b);
}
}
void pcoldet(paddle_class & b)
{
int i;
if(vars.type == FX_SPARKS)
{
for(i=0; i < vars.num; i++)
{
if(sparks[i].active)
{
sparks[i].pcoldet(b);
}
}
} else if(vars.type == FX_PARTICLEFIELD)
{
pf->pcoldet(b);
}
}
};
class effectManager {
private:
struct effect_vars vars; //denne kopieres over i den næste effekt der bliver spawned
int effectId; //ever rising number of a spawned effect.
public:
list<effect_class>effects;
effectManager()
{
effects.clear();
effectId=0;
}
void set(int var, GLfloat val)
{
switch(var)
{
case FX_VAR_SPEED:
vars.speed = val;
break;
case FX_VAR_SPREAD:
vars.spread = val;
break;
case FX_VAR_SIZE:
vars.size = val;
break;
case FX_VAR_GRAVITY:
vars.gravity = val;
}
}
void set(int var, int val)
{
switch(var)
{
case FX_VAR_NUM:
vars.num=val;
break;
case FX_VAR_LIFE:
vars.life=val;
break;
case FX_VAR_TYPE:
vars.type=val;
break;
case FX_VAR_COLDET:
vars.coldet=val;
break;
}
}
void set(int var, GLfloat r, GLfloat g, GLfloat b)
{
switch(var)
{
case FX_VAR_COLOR:
vars.col[0] = r;
vars.col[1] = g;
vars.col[2] = b;
break;
}
}
void set(int var, textureClass tex)
{
switch(var)
{
case FX_VAR_TEXTURE:
vars.tex = tex;
break;
}
}
void set(int var, struct pos p)
{
switch(var)
{
case FX_VAR_RECTANGLE:
vars.rect = p;
break;
}
}
//retunerer effektid så der kan checkes om det er aktivt
int spawn(pos p)
{
effectId++;
effect_class tempEffect;
vars.effectId = effectId;
tempEffect.vars = vars; //kopier det over som er sat op
tempEffect.init(p);
effects.push_back(tempEffect);
return(effectId);
}
void draw()
{
for(list <effect_class>::iterator it = effects.begin(); it != effects.end(); ++it)
{
it->draw();
if(!it->vars.active)
{
it = effects.erase(it);
}
}
}
void coldet(brick & b)
{
if(b.collide && b.active)
{
for(list <effect_class>::iterator it = effects.begin(); it != effects.end(); ++it)
{
if(it->vars.coldet)
it->coldet(b);
}
}
}
void pcoldet(paddle_class & b)
{
for(list<effect_class>::iterator it = effects.begin(); it != effects.end(); ++it)
{
if(it->vars.coldet)
it->pcoldet(b);
}
}
int isActive(int id)
{
for(list<effect_class>::iterator it = effects.begin(); it != effects.end(); ++it)
{
if(it->vars.effectId == id && it->vars.active)
{
return(1);
}
}
return(0);
}
void kill(int id)
{
for(list<effect_class>::iterator it = effects.begin(); it != effects.end(); ++it)
{
if(it->vars.effectId)
{
it->vars.active=0;
}
}
}
};
class glAnnounceMessageClass {
private:
int age;
GLfloat zoom,fade;
bool fadedir;
public:
bool active;
int life;
string text;
int font;
glAnnounceMessageClass()
{
active=1;
age=0;
fade=0;
fadedir=0;
zoom=0;
}
void draw()
{
GLfloat s;
zoom += 4000.0/life * globalMilliTicksSinceLastDraw;
if(fadedir)
{
fade -= 4000.0/life * globalMilliTicksSinceLastDraw;
} else {
fade += 4000.0/life * globalMilliTicksSinceLastDraw;
}
glLoadIdentity();
glTranslatef(0.0,0.0,-3.0);
glColor4f(1.0,0.0,0.0,fade);
s=zoom*0.85;
glText->write(text, font, 1, s, 0.0, 0.0);
glColor4f(0.0,1.0,0.0,fade);
s=zoom*0.90;
glText->write(text, font, 1, s, 0.0, 0.0);
glColor4f(0.0,0.0,1.0,fade);
s=zoom*0.95;
glText->write(text, font, 1, s, 0.0, 0.0);
glColor4f(1.0,1.0,1.0,fade);
s=zoom;
glText->write(text, font, 1, s, 0.0, 0.0);
age += globalTicksSinceLastDraw;
if(age > life*0.50)
{
fadedir=1;
}
if(age > life)
active=0;
}
};
#define MAXMSG 10
class glAnnounceTextClass {
private:
int len; //hvor mange mangler vi at vise
list<glAnnounceMessageClass> msg;
list<glAnnounceMessageClass>::iterator it;
public:
glAnnounceTextClass()
{
len=0;
}
void write(const char *text, int ttl, int font)
{
len++;
msg.resize(len);
it=msg.end();
--it;
it->life=ttl;
it->text=text;
it->font=font;
}
void draw()
{
if(len>0)
{
it=msg.begin();
if(it->active)
{
it->draw();
} else {
msg.erase(it);
len--;
}
}
}
};