summaryrefslogtreecommitdiffstats
path: root/kscreensaver/kdesavers/firesaver.cpp
blob: 3695acb87d330702610391046cf83d8bb30e5308 (plain)
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
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
//     This file is part of KFireSaver3D.

//     KFireSaver3D 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.

//     KFireSaver3D 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 KFireSaver3D; if not, write to the Free Software
//     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA


//     Author: Enrico Ros, based on the great work of David Sansome (kfiresaver)
//     Email:  asy@libero.it

#include <math.h>
#include <sys/time.h>
#include <stdlib.h>
#include <kconfig.h>
#include <kdebug.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include <kurl.h>
#include <kiconloader.h>
#include <kmessagebox.h>
#include <arts/kmedia2.h>
#include <arts/kplayobject.h>
#include <arts/kplayobjectfactory.h>

#include "firesaversetup.h"
#include "firesaverparticle.h"
#include "firesaverwriter.h"
#include "firesaver.h"


/* Factory code for KScreensaver begins *here* *\
\*                                             */

#include <kscreensaver.h>
#include <kdialogbase.h>

class KFireSaverKSS : public KScreenSaver
{
    public:
	KFireSaverKSS( WId id )
	    : KScreenSaver( id )
	{
	    setBackgroundColor( black );
	    erase();
	    saver = new KFireSaver;
	    embed(saver);
	    saver->show();
	}

	~KFireSaverKSS()
	{
	    delete saver;
	}

    private:
	KFireSaver* saver;
};

class KFireSaverSetupKDB : public KDialogBase
{
    public:
	KFireSaverSetupKDB( TQWidget* parent = 0, const char* name = 0 )
	    : KDialogBase( parent, name, true, i18n("Setup Screen Saver"),
	    Ok | Cancel | Help, Ok, true )
	{
		setup = new KFireSaverSetup( this );
		setMainWidget( setup );
		setButtonText( KDialogBase::Help, i18n( "A&bout" ) );
	}

    private slots:
	void slotHelp()
	{
		KMessageBox::about(this, i18n("<h3>KFireSaver 3D 1.0</h3>\n<p>TEST Koral - Enrico Ros::2004</p>") );
	}
	void slotOk()
	{
		setup->writeConfig();
		accept();
	}

    private:
	KFireSaverSetup * setup;
};

extern "C"
{
    KDE_EXPORT const char *kss_applicationName = "kfiresaver.kss";
    KDE_EXPORT const char *kss_description = I18N_NOOP( "Fireworks 3D (GL)" );
    KDE_EXPORT const char *kss_version = "0.7";

    KDE_EXPORT KScreenSaver *kss_create( WId id )
    {
	return new KFireSaverKSS( id );
    }

    KDE_EXPORT TQDialog *kss_setup()
    {
	return new KFireSaverSetupKDB;
    }
}

/*                                             *\
\* Factory code for KScreensaver ends *here*   */


KFireSaver :: KFireSaver( TQWidget *parent, const char *name )
	: TQGLWidget( parent, name )
{
	// set random seed to initialize drand48() calls
	timeval tv;
	gettimeofday(&tv,NULL);
	srand48( (long)tv.tv_usec );

	readConfig();

	particleList.setAutoDelete(true);
	starList.setAutoDelete(true);
	imageList.setAutoDelete(true);
	playObjectList.setAutoDelete(true);

	//initialize openGL context before managing GL calls
	makeCurrent();
	loadTexture( locate("data","kfiresaver/kfs_particle.png"), particleTexture );
	loadTexture( locate("data","kfiresaver/kfs_particle_flare.png"), flareTexture );
	loadTexture( locate("data","kfiresaver/kfs_particle_diastar.png"), diastarTexture );
	starTexture = particleTexture;

	//generate stars
	if (parameters.enableStars) {
		int number = parameters.starsNumber + 1;
		number *= 10 * number;
		for (int i=0 ; i<number ; i++)
		{
			Particle * star = new Particle( Particle::StarParticle );
			star->initializeValues();
			star->texture = starTexture;
			if (parameters.enableStarGradient)
			{
				GLfloat red = star->colour[0],
					green = star->colour[1],
					blue = star->colour[2],
					tint = 0.5 + star->ypos / FIELDWIDTH,
					merge = 0.3 + DRAND*0.7,
					mergen = 1 - merge;
				star->colour[0] = red * merge + (1.0-tint) * mergen;
				star->colour[1] = green * merge + tint * mergen;
				star->colour[2] = blue * merge + tint * mergen;
			}
			starList.append( star );
		}
	}

	//generate bottom fire
	if (parameters.enableBottomFire) {
		float cRed = (float)parameters.bottomFireColor.red() / 255.0,
		      cGreen = (float)parameters.bottomFireColor.green() / 255.0,
		      cBlue = (float)parameters.bottomFireColor.blue() / 255.0;
		for (int i=0 ; i<NUMBER_OF_FIREPARTICLES ; i++)
		{
			Particle* particle = new Particle( Particle::FireParticle );
			particle->initializeValues();
			particle->texture = particleTexture;
			particle->zspeed *= 4.0;
			particle->colour[0] = cRed * (0.6 + 0.4*DRAND);
			particle->colour[1] = cGreen * (0.6 + 0.4*DRAND);
			particle->colour[2] = cBlue * (0.6 + 0.4*DRAND);
			particleList.append(particle);
		}
	}

	//get sound files
	if (parameters.enableSound) {
		sound_explosion = locate("data","kfiresaver/kfs_explode.ogg");
		sound_debris = locate("data","kfiresaver/kfs_debris.ogg");
	}

	//create the writer class that manages flying writings.
	if ( parameters.enableWritings )
		writer = new Writer("kfs_letters.desc");

	showp.forceBicolour =
	showp.forceColour =
	showp.forcePower =
	showp.forceType = false;
	showp.timeStamp = 0.0;
	startTimer(MSECPERIOD);

	//force initialization of "show" variables for the first time
	timerEvent(NULL);
}


KFireSaver :: ~KFireSaver()
{
	freeTexture( particleTexture );
	freeTexture( starTexture );
	particleList.clear();
	starList.clear();
	imageList.clear();
	playObjectList.clear();
	if ( parameters.enableWritings )
		delete writer;
}


void KFireSaver :: initializeGL()
{
	glDisable(GL_DEPTH_TEST);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE);
	glShadeModel( GL_SMOOTH );

	resizeGL( 640, 480 );
}


void KFireSaver :: resizeGL( int width, int height )
{
	glViewport( 0, 0, width, height );

	glMatrixMode(GL_PROJECTION);
	 glLoadIdentity();
	 glOrtho( -FIELDW_2, FIELDW_2, -FIELDW_2, FIELDW_2, 5.0, 60.0 );

	glMatrixMode(GL_MODELVIEW);
	 glLoadIdentity();

	float ratio = (float)width / (float)height,
	      numH = 750 - 90 * parameters.particleSize,
	      numW = 1000 - 120 * parameters.particleSize;
	if ( ratio >= (4.0/3.0) ) {
		unitX = FIELDWIDTH / (numH * ratio);
		unitY = FIELDWIDTH / numH;
	} else {
		unitX = FIELDWIDTH / numW;
		unitY = FIELDWIDTH / (numW / ratio);
	}

	timeval tv;
	gettimeofday(&tv,NULL);
	timeStampFrame = (double)tv.tv_sec + (double)tv.tv_usec/1000000.0;

	firstGLFrame = true;
}


void KFireSaver :: paintGL ()
/* Main procedure. It does the following:
   - calculate time diff between current and previous frame
   - clear the color buffer
   - simple render of stars
   - advanced render of particles
     - render
     - update physics based on time difference
     - check die/change conditions
       - call to explode_firework if a leader dies
   - if random -> start a new firework
   - if random -> explode a penquin or kde logo
*/
{
	/* calculate TIME ELAPSED between current and previous frame */

	timeval tv;
	gettimeofday(&tv,NULL);
	double timeCurrent = (double)tv.tv_sec + (double)tv.tv_usec/1000000.0;
	double timeDiff = (MSECPERIOD / 1000.0);
	if (parameters.enableRealtime)
	{
		timeDiff = timeCurrent - timeStampFrame;
		timeStampFrame = timeCurrent;
		timeDiff = (timeDiff > 0.5) ? 0.5 : (timeDiff < 0.005) ? 0.005 : timeDiff;
	}

	/* CLEAR SCREEN: to do it there are 2 ways */

	glLoadIdentity();
	glTranslatef( 0, 0, -50.0 );
	glDisable( GL_TEXTURE_2D );

	if ( !parameters.enableFade || firstGLFrame )
	{	// quick - clear the OpenGL color buffer
		glClearColor( 0.0, 0.0, 0.0, 1.0 );
		glClear( GL_COLOR_BUFFER_BIT );
		firstGLFrame = false;
	}
	else
	{	// good looking
		/* superpose a semi-transparent black rectangle so we
		   can see a sort of 'tail' for each particle drawn. */
		const GLfloat conv_tab[10] = {
			0.50, 0.33, 0.22, 0.15, 0.10,
			0.07, 0.05, 0.03, 0.02, 0.01 };
		glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
		glColor4f(0.0,0.0,0.0, conv_tab[parameters.fadeAmount]);
		glBegin( GL_TRIANGLE_STRIP );
		  glVertex2f( FIELDW_2, FIELDW_2 );
		  glVertex2f( -FIELDW_2, FIELDW_2 );
		  glVertex2f( FIELDW_2, -FIELDW_2 );
		  glVertex2f( -FIELDW_2, -FIELDW_2 );
		glEnd();
		glBlendFunc(GL_SRC_ALPHA,GL_ONE);
	}

	/* render STARS */

	if (parameters.enableStars) {
		if ( starTexture ) {
			glEnable( GL_TEXTURE_2D );
			glBindTexture( GL_TEXTURE_2D, currentTexture = starTexture );
		} else
			glDisable( GL_TEXTURE_2D );

		glBegin( GL_QUADS );
		bool	flickers = parameters.enableStarFlickering;
		float	alpha = flickers ? 0.5 : 1.0;
		Particle * star = starList.first();
		for (; star; star = starList.next())
		{
			if (flickers && DRAND<0.6)
				continue;

			GLfloat sizeX = star->pixelSize * unitX,
				sizeY = star->pixelSize * unitY,
				pLeft = star->xpos - sizeX,
				pRight = star->xpos + sizeX,
				pTop = star->ypos + sizeY,
				pBottom = star->ypos - sizeY;
			glColor4f(star->colour[0], star->colour[1], star->colour[2], alpha);
			glTexCoord2f( 0, 0 );	// Bottom Left
			glVertex2f( pLeft, pBottom );
			glTexCoord2f( 0, 1 );	// Top Left
			glVertex2f( pLeft, pTop );
			glTexCoord2f( 1, 1 );	// Top Right
			glVertex2f( pRight, pTop );
			glTexCoord2f( 1, 0 );	// Bottom Right
			glVertex2f( pRight, pBottom );
		}
		glEnd();
	}

	/* render FIREWORKS */

	glBegin( GL_QUADS );
	bool playedExplodeSound = false;
	bool flashedScreen = false;
	Particle * particle = particleList.first();
	for (; particle; particle = particleList.next())
	{
		//bind the texture for current particle (if not already bound)
		if ( !particle->texture ) {
			glEnd();
			glDisable( GL_TEXTURE_2D );
			glBegin( GL_QUADS );
			currentTexture = 0;
		} else if ( particle->texture != currentTexture ) {
			glEnd();
			glEnable( GL_TEXTURE_2D );
			glBindTexture( GL_TEXTURE_2D, currentTexture = particle->texture );
			glBegin( GL_QUADS );
		}

		//perspective projection (done by hand to make it funnier than opengl's :-)
		float mfactor = PERSP_MAG_FACTOR * particle->ypos;
		if ( mfactor < -246.0 ) {
			particleList.remove();
			particleList.prev();
			continue;
		}
		float sfactor = 256.0 / (256.0 + mfactor),
		      posx = sfactor * particle->xpos,
		      posy = sfactor * particle->zpos - 4.0;
		//size computation (magnify if enableMegaFlares is set)
		if ( parameters.enableMegaFlares ) {
			mfactor = parameters.megaFlares*particle->ypos;
			if ( mfactor < -255.0 || mfactor > 512.0 ) {
				particleList.remove();
				particleList.prev();
				continue;
			}
			sfactor = 256.0 / (256.0 + mfactor);
			if ( sfactor > 64 )
				sfactor = 76.8 - sfactor / 5.0;
		}
		float size = sfactor * particle->pixelSize,
		      sizeX = size * unitX,
		      sizeY = size * unitY;

		//determine brightness (alpha component) for the particle
		if ( particle->useLife ) {
			float life = particle->life,
			      startLife = particle->startLife;
			//bright changes with the following curve: "2*k - k^2" (or "k(2-k)")
			if ( life > startLife )
				particle->colour[3] = startLife + 1 - life;
			else
				particle->colour[3] = life / startLife;
			//apply flickering if enabled
			if (particle->flicker < 0) {
				particle->colour[3] = 0.2;
				if (++particle->flicker >= 0)
					particle->flicker = FLICKER_FRAMES_DELAY;
			} else if (particle->flicker > 0) {
				if ( life <= startLife )
					particle->colour[3] = 1.0;
				if (--particle->flicker <= 0)
					particle->flicker = -FLICKER_FRAMES_DELAY;
			}
			glColor4fv( particle->colour );
		} else
			glColor3fv( particle->colour );

		//draw particle
		float pLeft = posx - sizeX,
		      pTop = posy + sizeY,
		      pRight = posx + sizeX,
		      pBottom = posy - sizeY;
		glTexCoord2f( 0, 0 );	// Bottom Left
		glVertex2f( pLeft, pBottom );
		glTexCoord2f( 0, 1 );	// Top Left
		glVertex2f( pLeft, pTop );
		glTexCoord2f( 1, 1 );	// Top Right
		glVertex2f( pRight, pTop );
		glTexCoord2f( 1, 0 );	// Bottom Right
		glVertex2f( pRight, pBottom );

		//phisically update parameters of the particle
		particle->updateParameters( timeDiff );

		//check for particle death / explosion
		switch (particle->particleType)
		{
			//a Fireparticle is restarted when in right conditions
		    case Particle::FireParticle:
			if ( posx < -FIELDW_2 || posx > FIELDW_2 ||
				(particle->zpos < -10.0 && posy < -FIELDW_2) )
			{
				particle->initializeValues();
				if ( DRAND > 0.9995 )
					particle->zspeed *= 4;
			}
			break;

			//a leader explodes when his z speed drops to zero
			//or, if it uses life, at death
		    case Particle::FireWorkLeaderParticle:
			if ((particle->zspeed <= 0.0f && !particle->useLife) ||
			    (particle->useLife && particle->life <= 0.0) )
			{
				// play sound if enabled (and once per frame)
				if (parameters.enableSound && !playedExplodeSound)
				{
					playSound(sound_explosion);
					playedExplodeSound = true;
				}
				// flash screen if enabled
				if (parameters.enableFlash && !flashedScreen) {
					glEnd();
					glDisable( GL_TEXTURE_2D );
					glColor4f( 1,1,1, parameters.flashOpacity / 10.0 );
					glBegin( GL_TRIANGLE_STRIP );
					glVertex2f( FIELDW_2, FIELDW_2 );
					glVertex2f( -FIELDW_2, FIELDW_2 );
					glVertex2f( FIELDW_2, -FIELDW_2 );
					glVertex2f( -FIELDW_2, -FIELDW_2 );
					glEnd();
					if ( particleTexture )
						glEnable( GL_TEXTURE_2D );
					glBegin( GL_QUADS );
					flashedScreen = true;
				}
				// generating tqchildren and removing parent
				int elementIndex = particleList.at();
				explodeFirework(particle);
				particleList.remove(elementIndex);
				particleList.prev();
			} else if ( parameters.enableTrails && DRAND < 0.4 ) {
				// leave trail behind the particle (it'a small and slow red debris)
				Particle * p = new Particle( Particle::FireWorkDebrisParticle );
				p->initializeValues( 0, particle, 1, 1 );
				p->texture = particleTexture;
				p->xspeed /= 4;
				p->yspeed /= 4;
				p->zspeed /= 8;
				p->zacc /= 4;
				p->pixelSize = 2;
				p->colour[0] /= 2;
				int elementIndex = particleList.at();
				particleList.append( p );
				particleList.at( elementIndex );
			}
			break;

			//remove if dead or outside field
		    default:
			if (particle->life <= 0.0 || posx<-FIELDW_2 || posx>FIELDW_2 || posy<-FIELDW_2) {
				particleList.remove();
				particleList.prev();
			}
			break;
		}
	}
	glEnd();

	/* render WRITINGS */

	if ( parameters.enableWritings )
	{
		int chance = (int) (1000.0 * DRAND);
		if ( !chance ) {
			static const TQString someStrings[] = {
				i18n("www.kde.org"),
				i18n("My KDE, please!"),
				i18n("KoNqUeR the World"),
				i18n("KFIRESAVER 3D"),
				i18n("Gimme your eyes..."),
				i18n("Thank you for using KDE"),
				i18n("Going insane tonight"),
			};
			int n = (int)(6.0 * DRAND);
			writer->spawnWords( someStrings[n], Writer::Fun1 );
		}
		writer->render( timeDiff );
	}

	/* generate a new FIREWORK_LEADER */

	int random = (int) ((float)parameters.fireworksFrequency * DRAND);
	if (showp.ShowType == Show)
	{
		//double the chances ('frequency') to raise a new leaderParticle
		//but envelop it under a sine function
		float step = (showp.timeStamp - timeCurrent) / showp.timeGap;
		if (DRAND > sin(M_PI*step))
			random = -1;
		if (showp.type == AngelHairs && DRAND < 0.5)
			random = -1;
	}
	if ( !random )
	{
		Particle * particle = new Particle( Particle::FireWorkLeaderParticle );
		particle->initializeValues();
		particle->texture = flareTexture;
		particleList.append( particle );
	}

	/* explode a logo */

	int logoImages = imageList.count();
	if ( logoImages > 0 ) {
		random = (int) (parameters.logoFrequency * logoImages * 200.0 * DRAND);
		if ( random < logoImages )
		{
			if (parameters.enableFlash && !flashedScreen) {
				glDisable( GL_TEXTURE_2D );
				glColor4f( 1,1,1, parameters.flashOpacity / 10.0 );
				glBegin( GL_TRIANGLE_STRIP );
				glVertex2f( FIELDW_2, FIELDW_2 );
				glVertex2f( -FIELDW_2, FIELDW_2 );
				glVertex2f( FIELDW_2, -FIELDW_2 );
				glVertex2f( -FIELDW_2, -FIELDW_2 );
				glEnd();
			}
			burnLogo( imageList.at(random) );
		}
	}
}


int KFireSaver :: pickColour()
{
	int color = (int) (DRAND * parameters.colorsCount);
	return parameters.colorsT[ color ];
}


KFireSaver :: enumFireworkType KFireSaver :: pickType()
{
	int type = (int) (DRAND * parameters.typesCount);
	return parameters.typesT[ type ];
}


void KFireSaver :: explodeFirework(Particle* leaderParticle)
{
	GLfloat displace[3] = {0.0,0.0,0.0};
	float tmp1 = 0.0, tmp2 = 0.0, tmp3 = 0.0, tmp4 = 0.0, tmp5 = 0.0;

	// color of exploded particles
	bool bicolor = parameters.enableCombos && (showp.forceBicolour || DRAND > 0.95),
	     flickers = false;
	int cscheme = showp.forceColour ? showp.colour : pickColour(),
	    cscheme2 = showp.forceColour ? showp.colourSec : pickColour();

	// randomize type of exploding firework
	enumFireworkType fwType =
		showp.forceType ? (enumFireworkType) showp.type : pickType();

	// other options for generated particles
	int number = (int) ((DRAND + DRAND) * 150.0);
	float power = showp.forcePower ?
		showp.powerEnvelop * (0.8 + 0.3*DRAND) :
		DRAND * 11.0 + 2.0,
	      powermin = DRAND * power;

	// now some rules ...
        //a splitter can't split up more than 2 times
	if (fwType == Splitter && leaderParticle->explosionsDepth > 1) {
		if (parameters.typesCount == 1)
			return;
		if (showp.forceType)
			fwType = showp.typeSec;
		if (fwType == Splitter)
			while ( (fwType = pickType()) == Splitter );
	}

	// PRE-ADJUST parameters for the firework we're creating
	switch ( fwType )
	{
		//no need to handle this. it's the default configuration.
	    case Sphere:
		break;

		//explosion whithout emitting particles, only a flash
	    case NoFW:
		number = 1;
		power = powermin = 0;
		break;

		//splits up into 'number' orange pieces. tmp1 holds base_life
	    case Splitter:
		cscheme = showp.forceColour ? showp.colour : 1;
		bicolor = false;
		number = 3 + (int) (DRAND * 4);
		power /= 2.0;
		powermin = power / 2.0;
		tmp1 = 0.4 + DRAND * 1.5;
		break;

		//randomize a couple of angles (phi - theta) for exploding circle
	    case BiCircle:
		number *= 2;
	    case Circle:
		power = DRAND * 5.0 + 4.0;
		tmp1 = DRAND * M_PI;
		tmp2 = DRAND * M_PI;
		tmp4 = cos(tmp2);	//c2
		tmp3 = sin(tmp2);	//s2
		tmp2 = cos(tmp1);	//c1
		tmp1 = sin(tmp1);	//s1
		break;

		//cascade of flickering orange particles
	    case AngelHairs:
		cscheme = showp.forceColour ? showp.colour : 1;
		bicolor = false;
		flickers = true;
		power = 0.8 + DRAND * 1.9;
		powermin = DRAND*0.5;
		number = 100 + (int)(DRAND * 150);
		displace[0] = -leaderParticle->xspeed/2;
		displace[1] = -leaderParticle->yspeed/2;
		displace[2] = power;
		break;

		//behave as a standard spherical firework
	    case Spirals:
		break;

		//not yet implemented, suppressing particles
	    case SuperNova:
	    case NoRender:
		number = 0;
		break;
	}

	//limit number of particles as we are getting to the capacity saturation
	float currentParticles = (float) particleList.count();
	const float particleCapacity = 15000;
	const float particleGap = 8000;
	if ( number > 10 && currentParticles > (particleCapacity - particleGap) )
	{
		//NoFW, Splitter and NoRender aren't limited.
		number = (int)( (float)number * (particleCapacity - currentParticles) / particleGap );
		if ( number < 10 )
			number = 0;
	}

	int newExplosionsDepth = leaderParticle->explosionsDepth + 1;
	for (int i=0 ; i<number ; i++)
	{
		Particle * particle;
		if ( fwType == Spirals )
			particle = new TurningParticle( Particle::FireWorkDebrisParticle );
		else
			particle = new Particle( Particle::FireWorkDebrisParticle );

		particle->initializeValues (
			bicolor && (i>number/2) ? cscheme2 : cscheme,
			leaderParticle, powermin, power,
			flickers, displace );
		particle->texture = particleTexture;
		particle->explosionsDepth = newExplosionsDepth;

		// POST-ADJUST particle coefficients adapting to current FireworkType
		switch ( fwType )
		{
			//create a big, white particle, simulating explosion
		    case NoFW:
			if (parameters.enableFade)
				particle->startLife = particle->life = 0.030;
			else
				particle->startLife = particle->life = 0.075;
			particle->texture = flareTexture;
			particle->colour[0]=
			particle->colour[1]=
			particle->colour[2]=1.0;
			particle->pixelSize = 50.0 + 75.0 * DRAND;
			particle->zacc = 0;
			break;

			//default. no need to change parameters, only create the
			//'sphere light' as the first big particle if set.
		    case Sphere:
			if (i==0 && parameters.enableSphereLight && number > 40) {
				particle->texture = flareTexture;
				particle->xspeed = leaderParticle->xspeed;
				particle->yspeed = leaderParticle->yspeed;
				particle->zspeed = 0.0;
				particle->colour[0] /= 2.0;
				particle->colour[1] /= 2.0;
				particle->colour[2] /= 2.0;
				float impression = power * (float)number/5.0;
				particle->pixelSize = 25.0 * DRAND + impression;
				if (parameters.enableFade) {
					particle->startLife = particle->life = 0.040;
				} else {
					particle->startLife = 1.3;
					particle->life = 1.8;
				}
			}
			break;

			//
		    case Splitter:
			particle->particleType = Particle::FireWorkLeaderParticle;
			particle->pixelSize *= 3.0;
			particle->startLife = particle->life = tmp1 * (0.75 + DRAND/3.0);
			if (particle->zspeed < 0)
				particle->zspeed = -particle->zspeed;
			break;

		    case Circle:
		    case BiCircle:
			tmp5 = 2 * M_PI * DRAND;
			//MMX can be useful.. if you know how to use it :-)
			if ( fwType == BiCircle && i > number/2 ) {
				GLfloat ey = cos(tmp5),
					ez = sin(tmp5);
				particle->xspeed = power * (                tmp3*ez );
				particle->yspeed = power * ( tmp2*ey - tmp1*tmp4*ez );
				particle->zspeed = power * ( tmp1*ey + tmp2*tmp4*ez );
			} else {
				GLfloat ex = sin(tmp5),
					ey = cos(tmp5);
				particle->xspeed = power * (       tmp4*ex           );
				particle->yspeed = power * (  tmp1*tmp3*ex + tmp2*ey );
				particle->zspeed = power * ( -tmp2*tmp3*ex + tmp1*ey );
			}
			break;

		    case AngelHairs:
			particle->zacc = -9.807 * (0.05 + DRAND*0.20);
			particle->life = 2.0 + DRAND*2.5;
			particle->startLife = particle->life - 1;
			if (particle->zspeed < 0)
				particle->zspeed *= -1;
			//particle->pixelSize = 5.0;
			break;

		    case Spirals:
			particle->texture = diastarTexture;
			break;

			//discard cases
		    case SuperNova:
		    case NoRender:
			break;
		}
		particleList.append(particle);
	}
}

void KFireSaver :: timerEvent(TQTimerEvent*)
{
	timeval tv;
	gettimeofday(&tv,NULL);
	double currentTime = (double)tv.tv_sec + (double)tv.tv_usec/1000000.0;

	bool	chooseType = false,
		chooseColor = false,
		chooseOthers = false,
		updateTimings = false;
	bool	firstTime = showp.timeStamp == 0.0;
	bool	endOfScene = currentTime >= showp.timeStamp;

	if (firstTime)
	switch (showp.ShowType)
	{
	    case Monotype:
		/* first time choose the type, color and attributes will
		   be choosed randomly for every firework which explodes*/
		showp.forceType = true;
		chooseType = true;
		break;

	    case Monochrome:
		/* first time choose the color, type and attributes will
		   be choosed randomly for every firework which explodes*/
		showp.forceColour = true;
		chooseColor =
		chooseOthers = true;
		break;

	    default: break;
	}

	if (endOfScene || firstTime)
	switch (showp.ShowType)
	{
	    case Show:
		/* if a scene ended, randomize global parameters for the
		   whole next scene */
		showp.forceType = true;
		showp.forceColour = true;
		showp.forcePower = true;
		chooseOthers =
		chooseColor =
		chooseType = true;
		updateTimings = true;
		break;

	    default: break;
	}

	if ( chooseType )
	{
		showp.type = pickType();
		if (parameters.typesCount < 2)
			showp.typeSec = NoRender;
		else
			while ((showp.typeSec = pickType()) == showp.type);
	}
	if ( chooseColor ) {
		showp.colour = pickColour();
		showp.colourSec = pickColour();
	}
	if ( chooseOthers )
	{
		showp.powerEnvelop = DRAND * 8.0 + 3.0;
		if (DRAND > 0.9)
		{
			showp.forceBicolour = true;
			showp.colourSec = pickColour();
		} else
			showp.forceBicolour = false;
	}
	if ( firstTime || updateTimings )
	{
		if (DRAND < 0.2)
			showp.timeGap = 1.0 + DRAND * 2.0;
		else
			showp.timeGap = 3.0 + DRAND * 10.0;
		showp.timeStamp = currentTime + showp.timeGap;
		showp.timeGap /= 1.5; //hack to introduce delay in sine func
	}

	updateGL();
}

void KFireSaver :: burnLogo(TQImage * image)
{
	if (!image || image->isNull())
		return;
	int step = parameters.enableReduceLogo ? 2 : 1,
	    imageW = image->width(),
	    imageH = image->height(),
	    offsetX = imageW / 2,
	    offsetY = imageH / 2;
	float speed = FIELDW_4 / (imageW > imageH ? imageW : imageH),
	      speedXOffs = 5 * (DRAND - DRAND),
	      speedYOffs = DRAND + DRAND + 1;
	//if image is too big, lower sample points
	while ((imageW/step)>96 || (imageH/step)>96)
		step *= 2;
	for (int y=0 ; y<imageH ; y+=step)
	{
		for (int x=0 ; x<imageW ; x+=step)
		{
			TQRgb pixel = image->pixel(x,y);
			if ( tqAlpha(pixel) < 250 )
				continue;
			//if ( DRAND > 0.9 )
			//	continue;

			Particle* particle = new Particle( Particle::LogoParticle );
			particle->initializeValues();
			particle->texture = particleTexture;

			float xI = (x - offsetX) ,
			      yI = (offsetY - y) ;
			particle->xpos = xI * speed * 0.5;
			particle->zpos = yI * speed * 0.5 + 5;
			particle->xspeed = xI * speed + speedXOffs;
			particle->zspeed = yI * speed + speedYOffs;

			particle->colour[0] = tqRed(pixel) / 255.0f;
			particle->colour[1] = tqGreen(pixel) / 255.0f;
			particle->colour[2] = tqBlue(pixel) / 255.0f;

			particleList.append(particle);
		}
	}
	if (parameters.enableSound)
		playSound(sound_debris);
}

void KFireSaver :: playSound(TQString file)
{
	//flush inactive players
	KPlayObject * playObject = playObjectList.first();
	while ( playObject )
	{
		if ( playObject->state() != Arts::posPlaying )
		{
			playObjectList.remove();
			playObject = playObjectList.current();
		} else
			playObject = playObjectList.next();
	}

	//discart this sound if the player queue is already full (4 channels playing)
	if ( playObjectList.count() >= 6 )
		return;

	// not needed when all of the files are in the distribution
	//if (!TQFile::exists(file))
		//return;

	KPlayObjectFactory factory(artsServer.server());
	playObject = factory.createPlayObject(KURL(file), true);

	if (playObject && !playObject->isNull())
	{
		playObject->play();
		playObjectList.append(playObject);
	}
}

bool KFireSaver :: loadTexture( TQString fileName, unsigned int & textureID )
{
	//reset texture ID to the default EMPTY value
	textureID = 0;

	//load image
	TQImage tmp;
	if ( !tmp.load( fileName ) )
		return false;

	//convert it to suitable format (flipped RGBA)
	TQImage texture = TQGLWidget::convertToGLFormat( tmp );
	if ( texture.isNull() )
		return false;

	//get texture number and bind loaded image to that texture
	glGenTextures( 1, &textureID );
	glBindTexture( GL_TEXTURE_2D, textureID );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
	glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
	glTexImage2D( GL_TEXTURE_2D, 0, 4 /* 3 ??? */, texture.width(), texture.height(),
		0, GL_RGBA, GL_UNSIGNED_BYTE, texture.bits() );
	return true;
}

void KFireSaver :: freeTexture( unsigned int & textureID )
{
	if ( textureID > 0 )
	    glDeleteTextures( 1, &textureID );
	textureID = 0;
}

void KFireSaver :: readConfig ()
{
	KConfig config("kfiresaverrc",true,false);

	// show
	config.setGroup( "Show" );
	showp.ShowType = (enum enumShowType)config.readNumEntry( "ShowType", 1 );
	parameters.fireworksFrequency = 11 - config.readNumEntry( "FireworksFrequency", 7 );
	if ( parameters.fireworksFrequency < 1 )
		parameters.fireworksFrequency = 1;
	if ( parameters.fireworksFrequency > 11 )
		parameters.fireworksFrequency = 11;
	parameters.fireworksFrequency *= (parameters.fireworksFrequency + 1); //*karl gauss's sum*
	parameters.particleSize = config.readNumEntry( "ParticlesSize", 0 );
	if ( parameters.particleSize < -5 )
		parameters.particleSize = -5;
	if ( parameters.particleSize > 5 )
		parameters.particleSize = 5;
	if ( parameters.enableBottomFire = config.readBoolEntry( "enable-BottomFire", true ) )
	{
		TQColor blue = TQt::darkBlue;
		parameters.bottomFireColor = config.readColorEntry( "BottomFireColor", &blue );
	}
	parameters.enableSound = config.readBoolEntry( "enable-Sounds", false );
	parameters.enableNoOverhead = config.readBoolEntry( "enable-NoOverhead", true );
	parameters.enableRealtime = config.readBoolEntry( "enable-FrameSkip", true );

	// fireworks
	config.setGroup( "Fireworks" );
	parameters.typesCount = 0;
	if ( config.readBoolEntry( "use-Classic", true ) )
		parameters.typesT[parameters.typesCount++] = Sphere;
	if ( config.readBoolEntry( "use-Explosion", false ) )
		parameters.typesT[parameters.typesCount++] = NoFW;
	if ( config.readBoolEntry( "use-FlameRing", false ) )
		parameters.typesT[parameters.typesCount++] = Circle;
	if ( config.readBoolEntry( "use-FlameWorld", false ) )
		parameters.typesT[parameters.typesCount++] = BiCircle;
	if ( config.readBoolEntry( "use-Fall", false ) )
		parameters.typesT[parameters.typesCount++] = AngelHairs;
	if ( config.readBoolEntry( "use-Splitter", false ) )
		parameters.typesT[parameters.typesCount++] = Splitter;
	if ( config.readBoolEntry( "use-Spirals", false ) )
		parameters.typesT[parameters.typesCount++] = Spirals;
	if ( config.readBoolEntry( "use-SuperNova", false ) )
		parameters.typesT[parameters.typesCount++] = SuperNova;
	if ( !parameters.typesCount ) {
		kdWarning() << "KFireSaver3D: Warning, no fireworks enabled in config file" << endl;
		kdWarning() << "    enabling 'Classic Spherical'" << endl;
		parameters.typesCount = 1;
		parameters.typesT[0] = Sphere;
	}
	parameters.typesT[ parameters.typesCount ] =
		parameters.typesT[ parameters.typesCount-1 ];
	parameters.colorsCount = 0;
	if ( config.readBoolEntry( "use-Red", false ) )
		parameters.colorsT[parameters.colorsCount++] = 0;
	if ( config.readBoolEntry( "use-Orange", true ) )
		parameters.colorsT[parameters.colorsCount++] = 1;
	if ( config.readBoolEntry( "use-Green", false ) )
		parameters.colorsT[parameters.colorsCount++] = 2;
	if ( config.readBoolEntry( "use-Blue", false ) )
		parameters.colorsT[parameters.colorsCount++] = 3;
	if ( config.readBoolEntry( "use-White", true ) )
		parameters.colorsT[parameters.colorsCount++] = 4;
	if ( config.readBoolEntry( "use-Purple", false ) )
		parameters.colorsT[parameters.colorsCount++] = 5;
	if ( config.readBoolEntry( "use-DeepGreen", true ) )
		parameters.colorsT[parameters.colorsCount++] = 6;
	if ( !parameters.colorsCount )
	{
		kdWarning() << "KFireSaver3D: Warning enable at least one color" << endl;
		kdWarning() << "    enabling 'Blinding White'" << endl;
		parameters.colorsCount = 1;
		parameters.colorsT[0] = 4;
	}
	parameters.colorsT[ parameters.colorsCount ] =
		parameters.colorsT[ parameters.colorsCount-1 ];
	parameters.enableCombos = config.readBoolEntry( "use-Multicolor", true );

	// specials
	config.setGroup( "Specials" );
	if ( parameters.enableLogos = config.readBoolEntry( "enable-Logos", true ) )
	{
		TQImage tempImage;
		tempImage.setAlphaBuffer( true );
		if ( config.readBoolEntry( "LogosTux", true ) )
			if ( tempImage.load(locate("data","kfiresaver/kfs_tux.png")) )
				imageList.append( new TQImage(tempImage) );
		if ( config.readBoolEntry( "LogosKonqui", true ) )
			if ( tempImage.load(locate("data","kfiresaver/kfs_kde.png")) )
				imageList.append( new TQImage(tempImage) );
		if ( config.readBoolEntry( "LogosKDEIcons", true ) ) {
			const TQString icons[] = {
				"3floppy_unmount", "cdrom_unmount", "hdd_mount", "kmix",
				"network", "my-computer", "folder_home", "konqueror",
				"kmail", "penguin", "personal" };
			for ( int i = 0; i < 11; i++ )
				imageList.append( new TQImage(DesktopIcon(icons[i],64).convertToImage()) );
		}
		parameters.enableReduceLogo = config.readBoolEntry( "LogosReduceDetail", true );
		parameters.logoFrequency = 11 - config.readNumEntry( "LogosFrequency", 4 );
		if ( parameters.logoFrequency < 1 )
			parameters.logoFrequency = 1;
		if ( parameters.logoFrequency > 11 )
			parameters.logoFrequency = 11;
	}
	if ( parameters.enableStars = config.readBoolEntry( "enable-Stars", true ) )
	{
		parameters.enableStarFlickering = config.readBoolEntry( "StarsFlicker", false );
		parameters.enableStarGradient = config.readBoolEntry( "StarsGradient", true );
		parameters.starsNumber = config.readNumEntry( "StarsNumber", 4 );
		if ( parameters.starsNumber < 0 )
			parameters.starsNumber = 0;
		if ( parameters.starsNumber > 10 )
			parameters.starsNumber = 10;
	}
	parameters.enableWritings = config.readBoolEntry( "enable-Writings", true );

	// effects
	config.setGroup( "Effects" );
	parameters.enableSphereLight = config.readBoolEntry( "enable-SphericalLight", true );
	if ( parameters.enableFlash = config.readBoolEntry( "enable-Flash", false ) )
	{
		parameters.flashOpacity = config.readNumEntry( "FlashOpacity", 5 );
		if ( parameters.flashOpacity < 0 )
			parameters.flashOpacity = 0;
		if ( parameters.flashOpacity > 10 )
			parameters.flashOpacity = 10;
	}
	if ( parameters.enableFade = config.readBoolEntry( "enable-Fade", false ) )
	{
		parameters.fadeAmount = config.readNumEntry( "FadeIntensity", 3 );
		if ( parameters.fadeAmount < 0 )
			parameters.fadeAmount = 0;
		if ( parameters.fadeAmount > 9 )
			parameters.fadeAmount = 9;
	}
	if ( parameters.enableMegaFlares = config.readBoolEntry( "enable-Flares", true ) )
	{
		parameters.megaFlares = config.readNumEntry( "FlaresDimension", 5 );
		if ( parameters.megaFlares < 0 )
			parameters.megaFlares = 0;
		if ( parameters.megaFlares > 10 )
			parameters.megaFlares = 10;
		parameters.megaFlares += 4;
		parameters.megaFlares *= 2;
	}
	parameters.enableTrails = config.readBoolEntry( "enable-Trail", false );
}