Debian Codegraph @

effects.cpp

raw symbols SCIP
1 /* ************************************************************************* *
2 SDL-Ball - DX-Ball/Breakout remake with openGL and SDL for Linux
3 Copyright (C) 2008 Jimmy Christensen ( dusted at dusted dot dk )
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * ************************************************************************* */
18
19 struct effect_vars {
20 int type; //Hvilken slags effekt er det?
21 bool coldet; //er der collision detection på denne?
22 bool active; //Er denne effekt aktiv
23 GLfloat col[3]; //Hvilken farve har effekten?
24 GLfloat speed; //Hvor hurtigt bevæger den sig
25 GLfloat spread; //Hvor stor er spredningen (i grader)
26 GLfloat size; //Skalering af elementerne
27 struct pos rect; //rectangular size (for particleField)
28 GLfloat gravity; //Hvor stor er tyndgekraften
29 int num; //Hvor mange elementer er der i den
30 int life; //hvor mange ms lever den?
31 textureClass tex; //Texture
32 int effectId; //unique id for this effect
33 };
34
35 class transit_effect_class {
36 private:
37 GLfloat opacity;
38
39 public:
40 int age;
41 effect_vars vars;
42 void init()
43 {
44 age=0;
45 opacity = 0.0f;
46 var.transition_half_done=0;
47
48 }
49
50 void draw()
51 {
52 age += globalTicksSinceLastDraw;
53
54 if(age < vars.life/2.0) //første halvdel
55 {
56 //Gør solid
57 opacity += vars.life/500.0*globalMilliTicksSinceLastDraw;
58 } else {
59 var.transition_half_done=1;
60 //Gør trans
61 opacity -= vars.life/500.0*globalMilliTicksSinceLastDraw;
62 }
63
64 glLoadIdentity();
65 glDisable( GL_TEXTURE_2D );
66 glTranslatef(0.0, 0.0, -3.0);
67 glColor4f( vars.col[0], vars.col[1], vars.col[2], opacity );
68 glBegin( GL_QUADS );
69 glVertex3f(-1.66, 1.25, 0.0);
70 glVertex3f( 1.66, 1.25, 0.0);
71 glVertex3f( 1.66,-1.25, 0.0);
72 glVertex3f(-1.66,-1.25, 0.0);
73 glEnd( );
74 glEnable( GL_TEXTURE_2D );
75 }
76
77 };
78
79 class sparkle {
80 public:
81 bool active;
82 GLfloat size;
83 GLfloat ang;
84 int life;
85 int lifeleft;
86 pos p,v; //position og vel
87 effect_vars vars;
88 GLfloat bounce,f; //bounce og friktion
89 sparkle()
90 {
91 bounce=0;
92 f=0;
93 active=1;
94 }
95
96
97 void draw()
98 {
99 if(lifeleft > 0)
100 {
101 //sanitize
102 lifeleft -= globalTicksSinceLastDraw;
103
104 //er vi indenfor skærmen?
105 if(p.x > 1.67 || p.y < -1.7 || p.x < -1.67)
106 {
107 active=0;
108 }
109
110 v.y -= vars.gravity*globalMilliTicksSinceLastDraw;
111 v.y -= bounce*globalMilliTicksSinceLastDraw;
112
113 if(v.x < 0)
114 {
115 v.x += f*globalMilliTicksSinceLastDraw;
116 } else {
117 v.x -= f*globalMilliTicksSinceLastDraw;
118 }
119
120 p.x += v.x * globalMilliTicksSinceLastDraw;
121 p.y += v.y * globalMilliTicksSinceLastDraw;
122
123
124
125
126
127 glColor4f(vars.col[0], vars.col[1],vars.col[2], (1.0/(float)life)*(float)lifeleft);
128
129 GLfloat curSize = size/(float)life * (float)lifeleft;
130
131 glBindTexture( GL_TEXTURE_2D, vars.tex.prop.texture );
132 glBegin( GL_QUADS );
133 glTexCoord2f(0,0); glVertex3f(p.x-curSize,p.y+curSize, 0.0);
134 glTexCoord2f(0,1); glVertex3f(p.x+curSize,p.y+curSize, 0.0);
135 glTexCoord2f(1,1); glVertex3f(p.x+curSize,p.y-curSize, 0.0);
136 glTexCoord2f(1,0); glVertex3f(p.x-curSize,p.y-curSize, 0.0);
137 glEnd( );
138
139 } else {
140 active=0;
141 }
142
143 }
144
145 void coldet(brick & b)
146 {
147 if(p.x > b.posx-b.width && p.x < b.posx+b.width)
148 {
149 if(p.y > b.posy-b.height && p.y < b.posy+b.height)
150 {
151 //Hvor ramte vi fra?
152 if(p.y < b.posy && !b.n(3))
153 {
154 //bunden
155 v.y *= -1;
156 //p.y = b.posy-b.height-0.01;
157 } else
158
159 if(p.y > b.posy && !b.n(2))
160 {
161 //toppen
162 p.y = b.posy+b.height;
163 if(v.y < 0)
164 {
165 v.y *= -1;
166 v.y /= 2.0;
167 f += 0.01;
168 }
169 } else
170
171 if(p.x > b.posx && !b.n(1))
172 {
173 //p.x = b.posx+b.width;
174 //højre
175 if(v.x < 0)
176 v.x *= -1;
177 } else
178
179 if(p.x < b.posx && !b.n(0))
180 {
181 //p.x = b.posx-b.width;
182 //venstre
183 if(v.x > 0)
184 v.x *= -1;
185 }
186 }
187 }
188 }
189
190 void pcoldet(paddle_class & b)
191 {
192 if(p.x > b.posx-b.width && p.x < b.posx+b.width)
193 {
194 if(p.y > b.posy-b.height && p.y < b.posy+b.height)
195 {
196
197 //Hvor ramte vi fra?
198 if(p.y < b.posy)
199 {
200 //bunden
201 p.y = b.posy-b.height;
202 }
203
204 if(p.y > b.posy)
205 {
206 //toppen
207 p.y = b.posy+b.height;
208 if(v.y < 0)
209 {
210 v.y *= -1;
211 v.y /= 2.0;
212 f += 0.01;
213 }
214 } else
215
216 if(p.x > b.posx)
217 {
218 p.x = b.posx+b.width;
219 //højre
220 if(v.x < 0)
221 v.x *= -1;
222 } else
223
224 if(p.x < b.posx)
225 {
226 p.x = b.posx-b.width;
227 //venstre
228 if(v.x > 0)
229 v.x *= -1;
230 }
231 }
232 }
233 }
234 };
235
236 class particleFieldClass {
237 private:
238 int spawnTimeout; //This is used to spawn sparkles at a random time
239 sparkle *sparks;
240 void spawnSpark(int sparkNum);
241 public:
242 struct pos p;
243 struct effect_vars vars;
244 void init(struct effect_vars varsP, struct pos p);
245 void draw();
246 void move(struct pos p);
247 void coldet(brick & b);
248 void pcoldet(paddle_class & b);
249 ~particleFieldClass();
250 };
251
252 void particleFieldClass::init(struct effect_vars varsP, struct pos spawnPos)
253 {
254 vars=varsP;
255 spawnTimeout=0;
256 vars.active=1;
257 sparks = new sparkle[vars.num];
258 p=spawnPos;
259
260 for(int i=0; i < vars.num; i++)
261 {
262 sparks[i].active=1;
263 sparks[i].vars = vars; //NOTE:remove effect_vars from sparkle class?
264 spawnSpark(i);
265 }
266 }
267
268 particleFieldClass::~particleFieldClass()
269 {
270 delete[] sparks; //free the sparks
271 }
272
273 void particleFieldClass::spawnSpark(int sparkNum)
274 {
275 GLfloat angle=(RAD/vars.num-1)*(rndflt(vars.num,0.0)); //FIXME: Make this elegant: Choose a random angle (in radients)
276 spawnTimeout = rand()%10;
277 sparks[sparkNum].life = rand()%vars.life;
278 sparks[sparkNum].lifeleft = sparks[sparkNum].life;
279 sparks[sparkNum].v.x = (vars.speed*rndflt(vars.speed*2.0,0.0)) * sin(angle);
280 sparks[sparkNum].v.y = (vars.speed*rndflt(vars.speed*2.0,0.0)) * cos(angle);
281 sparks[sparkNum].size = rndflt(vars.size,0);
282
283 sparks[sparkNum].p=p;
284
285 sparks[sparkNum].p.x += rndflt(vars.rect.x, (vars.rect.x/2.0));
286 sparks[sparkNum].p.y += rndflt(vars.rect.y, (vars.rect.y/2.0));
287 sparks[sparkNum].active=1;
288 }
289
290 void particleFieldClass::draw()
291 {
292 spawnTimeout -= globalTicksSinceLastDraw;
293
294 glLoadIdentity();
295 glTranslatef(0.0,0.0 , -3.0);
296 int t=0;
297
298 for(int i=0; i<vars.num; i++)
299 {
300 if(sparks[i].active)
301 {
302 t++;
303 sparks[i].draw();
304 }
305 }
306
307 vars.life -=globalTicksSinceLastDraw;
308 if(t==0)
309 {
310 vars.active=0;
311 }
312 }
313
314 void particleFieldClass::coldet(brick & b)
315 {
316 for(int i=0; i < vars.num; i++)
317 {
318 if(sparks[i].active)
319 sparks[i].coldet(b);
320 }
321 }
322
323 void particleFieldClass::pcoldet(paddle_class & b)
324 {
325 for(int i=0; i < vars.num; i++)
326 {
327 if(sparks[i].active)
328 sparks[i].pcoldet(b);
329 }
330 }
331
332 class effect_class {
333 private:
334 pos spawn_pos;
335 sparkle* sparks; //wohoo ;)
336
337
338 public:
339 transit_effect_class transit;
340 particleFieldClass *pf;
341 struct effect_vars vars;
342
343 effect_class()
344 {
345 vars.active=0; //når den bliver oprettet i hukkommelsen er den ikke i brug endnu
346 }
347
348 void init(pos p)
349 {
350 int i;
351 GLfloat angle=0.0;
352 vars.active=1;
353 spawn_pos=p;
354 int life;
355 //cout << "Spawned effect type " << vars.type << " at " << p.x << "," << p.y << " with " << vars.life << "ms life" << endl;
356
357 switch(vars.type)
358 {
359 case FX_SPARKS:
360 sparks = new sparkle[vars.num];
361 if(sparks == NULL)
362 {
363 cout << "Could not allocate " << vars.num << " sparks" << endl;
364 i=vars.num+10;
365 }
366
367 for(i=0; i < vars.num; i++)
368 {
369 sparks[i].size = rndflt(vars.size,0);
370 life = rand()%vars.life;
371 angle=(RAD/vars.num-1)*(rndflt(vars.num,0.0)); //FIXME: Make this elegant: Choose a random angle (in radients)
372 sparks[i].life = life;
373 sparks[i].lifeleft = life;
374 sparks[i].v.x = (vars.speed*rndflt(vars.speed*2.0,0.0)) * sin(angle);
375 sparks[i].v.y = (vars.speed*rndflt(vars.speed*2.0,0.0)) * cos(angle);
376
377 sparks[i].vars = vars;
378
379 sparks[i].p = spawn_pos;
380
381
382 }
383 break;
384
385 case FX_TRANSIT:
386 transit.vars = vars;
387 transit.init();
388 break;
389
390 case FX_PARTICLEFIELD:
391 pf=NULL;
392 pf = new particleFieldClass;
393 pf->init(vars, p);
394 break;
395
396 }
397
398 }
399
400 void draw()
401 {
402 //find ud af hvor længe der er gået siden sidst
403 int i;
404 bool stay=0;
405 switch(vars.type)
406 {
407 case FX_SPARKS:
408 glLoadIdentity();
409 glTranslatef(0.0,0.0 , -3.0);
410 for(i=0; i < vars.num; i++)
411 {
412 if(sparks[i].active)
413 {
414 sparks[i].draw();
415 stay=1;
416 }
417 }
418 break;
419
420 case FX_TRANSIT:
421 transit.draw();
422 if(transit.age <= vars.life)
423 stay=1;
424 break;
425
426 case FX_PARTICLEFIELD:
427 pf->draw();
428 if(pf->vars.active)
429 stay=1;
430 break;
431
432 }
433
434 //Ugley hack, to prevent transit from flickering when spark effects are deleted.
435 if(var.effectnum != -1 && vars.type != FX_TRANSIT)
436 stay=1;
437
438 if(!stay)
439 {
440 vars.active=0;
441 switch(vars.type)
442 {
443 case FX_SPARKS:
444 delete[] sparks; //Free sparks again ;)
445 break;
446
447 case FX_PARTICLEFIELD:
448 delete pf; //remove the particlefield
449 break;
450 }
451
452 }
453 }
454
455 void coldet(brick & b)
456 {
457 int i;
458 if(vars.type == FX_SPARKS)
459 {
460 for(i=0; i < vars.num; i++)
461 {
462 if(sparks[i].active)
463 {
464 sparks[i].coldet(b);
465 }
466 }
467 } else if(vars.type == FX_PARTICLEFIELD)
468 {
469 pf->coldet(b);
470 }
471 }
472
473 void pcoldet(paddle_class & b)
474 {
475 int i;
476 if(vars.type == FX_SPARKS)
477 {
478 for(i=0; i < vars.num; i++)
479 {
480 if(sparks[i].active)
481 {
482 sparks[i].pcoldet(b);
483 }
484 }
485 } else if(vars.type == FX_PARTICLEFIELD)
486 {
487 pf->pcoldet(b);
488 }
489 }
490 };
491
492 class effectManager {
493 private:
494 struct effect_vars vars; //denne kopieres over i den næste effekt der bliver spawned
495 int effectId; //ever rising number of a spawned effect.
496
497 public:
498 list<effect_class>effects;
499
500 effectManager()
501 {
502 effects.clear();
503 effectId=0;
504 }
505
506 void set(int var, GLfloat val)
507 {
508 switch(var)
509 {
510 case FX_VAR_SPEED:
511 vars.speed = val;
512 break;
513 case FX_VAR_SPREAD:
514 vars.spread = val;
515 break;
516 case FX_VAR_SIZE:
517 vars.size = val;
518 break;
519 case FX_VAR_GRAVITY:
520 vars.gravity = val;
521 }
522 }
523
524 void set(int var, int val)
525 {
526 switch(var)
527 {
528 case FX_VAR_NUM:
529 vars.num=val;
530 break;
531 case FX_VAR_LIFE:
532 vars.life=val;
533 break;
534 case FX_VAR_TYPE:
535 vars.type=val;
536 break;
537 case FX_VAR_COLDET:
538 vars.coldet=val;
539 break;
540 }
541
542 }
543
544 void set(int var, GLfloat r, GLfloat g, GLfloat b)
545 {
546 switch(var)
547 {
548 case FX_VAR_COLOR:
549 vars.col[0] = r;
550 vars.col[1] = g;
551 vars.col[2] = b;
552 break;
553 }
554 }
555
556 void set(int var, textureClass tex)
557 {
558 switch(var)
559 {
560 case FX_VAR_TEXTURE:
561 vars.tex = tex;
562 break;
563 }
564 }
565
566 void set(int var, struct pos p)
567 {
568 switch(var)
569 {
570 case FX_VAR_RECTANGLE:
571 vars.rect = p;
572 break;
573 }
574 }
575
576 //retunerer effektid så der kan checkes om det er aktivt
577 int spawn(pos p)
578 {
579 effectId++;
580 effect_class tempEffect;
581 vars.effectId = effectId;
582 tempEffect.vars = vars; //kopier det over som er sat op
583 tempEffect.init(p);
584 effects.push_back(tempEffect);
585
586 return(effectId);
587 }
588
589 void draw()
590 {
591 for(list <effect_class>::iterator it = effects.begin(); it != effects.end(); ++it)
592 {
593
594 it->draw();
595
596 if(!it->vars.active)
597 {
598 it = effects.erase(it);
599 }
600 }
601 }
602
603 void coldet(brick & b)
604 {
605 if(b.collide && b.active)
606 {
607 for(list <effect_class>::iterator it = effects.begin(); it != effects.end(); ++it)
608 {
609 if(it->vars.coldet)
610 it->coldet(b);
611
612 }
613 }
614 }
615
616 void pcoldet(paddle_class & b)
617 {
618
619 for(list<effect_class>::iterator it = effects.begin(); it != effects.end(); ++it)
620 {
621 if(it->vars.coldet)
622 it->pcoldet(b);
623 }
624 }
625
626 int isActive(int id)
627 {
628 for(list<effect_class>::iterator it = effects.begin(); it != effects.end(); ++it)
629 {
630 if(it->vars.effectId == id && it->vars.active)
631 {
632 return(1);
633 }
634 }
635 return(0);
636 }
637
638 void kill(int id)
639 {
640 for(list<effect_class>::iterator it = effects.begin(); it != effects.end(); ++it)
641 {
642 if(it->vars.effectId)
643 {
644 it->vars.active=0;
645 }
646 }
647 }
648 };
649
650 class glAnnounceMessageClass {
651 private:
652 int age;
653 GLfloat zoom,fade;
654 bool fadedir;
655 public:
656 bool active;
657 int life;
658 string text;
659 int font;
660
661 glAnnounceMessageClass()
662 {
663 active=1;
664 age=0;
665 fade=0;
666 fadedir=0;
667 zoom=0;
668 }
669
670 void draw()
671 {
672 GLfloat s;
673 zoom += 4000.0/life * globalMilliTicksSinceLastDraw;
674
675 if(fadedir)
676 {
677 fade -= 4000.0/life * globalMilliTicksSinceLastDraw;
678 } else {
679 fade += 4000.0/life * globalMilliTicksSinceLastDraw;
680 }
681
682 glLoadIdentity();
683 glTranslatef(0.0,0.0,-3.0);
684
685 glColor4f(1.0,0.0,0.0,fade);
686 s=zoom*0.85;
687 glText->write(text, font, 1, s, 0.0, 0.0);
688
689 glColor4f(0.0,1.0,0.0,fade);
690 s=zoom*0.90;
691 glText->write(text, font, 1, s, 0.0, 0.0);
692
693 glColor4f(0.0,0.0,1.0,fade);
694 s=zoom*0.95;
695 glText->write(text, font, 1, s, 0.0, 0.0);
696
697 glColor4f(1.0,1.0,1.0,fade);
698 s=zoom;
699 glText->write(text, font, 1, s, 0.0, 0.0);
700
701
702 age += globalTicksSinceLastDraw;
703 if(age > life*0.50)
704 {
705 fadedir=1;
706 }
707
708 if(age > life)
709 active=0;
710 }
711
712 };
713
714 #define MAXMSG 10
715 class glAnnounceTextClass {
716 private:
717 int len; //hvor mange mangler vi at vise
718 list<glAnnounceMessageClass> msg;
719 list<glAnnounceMessageClass>::iterator it;
720 public:
721 glAnnounceTextClass()
722 {
723 len=0;
724 }
725
726 void write(const char *text, int ttl, int font)
727 {
728 len++;
729
730 msg.resize(len);
731
732 it=msg.end();
733 --it;
734
735 it->life=ttl;
736 it->text=text;
737 it->font=font;
738 }
739
740 void draw()
741 {
742 if(len>0)
743 {
744 it=msg.begin();
745 if(it->active)
746 {
747 it->draw();
748 } else {
749
750 msg.erase(it);
751 len--;
752 }
753 }
754 }
755
756 };