summaryrefslogtreecommitdiffstats
path: root/tdescreensaver/kdesavers/gravity.cpp
blob: 411d90c6861c00a4ed5bf7db0fb27d8bf96f5752 (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
//-----------------------------------------------------------------------------
//
// kgravity - Partical Gravity Screen Saver for TDE 2
//
// Copyright (c)  Ian Reinhart Geiser 2001
//
// TDEConfig code and KScreenSaver "Setup..." improvements by
// Nick Betcher <nbetcher@usinternet.com> 2001
//
#include <stdlib.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tdeapplication.h>
#include <tdelocale.h>
#include <tdeconfig.h>
#include <kcolordialog.h>
#include <kbuttonbox.h>
#include <kcolorbutton.h>
#include "gravity.h"
#include "gravity.moc"
#ifdef TQ_WS_MACX
#include <OpenGL/glu.h>
#include <OpenGL/gl.h>
#else
#include <GL/glu.h>
#include <GL/gl.h>
#endif
#include <tqimage.h>
#include <kdebug.h>
#include <tqpainter.h>
#include <tqradiobutton.h>
#include <tqspinbox.h>
#include <kstandarddirs.h>
#include <math.h>
#include <tdemessagebox.h>
// libtdescreensaver interface
extern "C"
{
	KDE_EXPORT const char *kss_applicationName = "kgravity.kss";
	KDE_EXPORT const char *kss_description = I18N_NOOP( "Particle Gravity Screen Saver" );
	KDE_EXPORT const char *kss_version = "2.2.0";

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

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

//-----------------------------------------------------------------------------
// dialog to setup screen saver parameters
//
KGravitySetup::KGravitySetup( TQWidget *parent, const char *name )
	: SetupUi( parent, name, TRUE )
{
	readSettings();

	preview->setFixedSize( 220, 170 );
	preview->setBackgroundColor( black );
#ifdef TQ_WS_X11
	preview->show();    // otherwise saver does not get correct size
#endif
	saver = new KGravitySaver( preview->winId() );
;
	connect( PushButton1, TQT_SIGNAL( clicked() ), TQT_SLOT( slotOkPressed() ) );
	connect( PushButton2, TQT_SIGNAL( clicked() ), TQT_SLOT( reject() ) );
	connect( PushButton3, TQT_SIGNAL( clicked() ), TQT_SLOT( aboutPressed() ) );
	connect(  SpinBox1, TQT_SIGNAL( valueChanged(int)), saver, TQT_SLOT( updateSize(int)));
	connect( RadioButton1, TQT_SIGNAL( toggled(bool)), saver, TQT_SLOT( doStars(bool)));

}

KGravitySetup::~KGravitySetup()
{
    delete saver;
}

// read settings from config file
void KGravitySetup::readSettings()
{
	TDEConfig config("kssgravityrc", false, false);

	config.setGroup( "Settings" );
	TQString boolval = config.readEntry( "Stars", "false" );
	if (boolval == "true") {
		RadioButton1->setDown(true);
		RadioButton1_2->setDown(false);
	} else {
		if (boolval == "false")
		{
			RadioButton1->setDown(false);
			RadioButton1_2->setDown(true);
		}
	}

	TQString starammount = config.readEntry("StarSize", "75");
	SpinBox1->setValue(starammount.toInt());

}

// Ok pressed - save settings and exit
void KGravitySetup::slotOkPressed()
{
	TDEConfig config("kssgravityrc", false, false);
	config.setGroup( "Settings" );

	if (RadioButton1->isOn() == true)
	{
		config.writeEntry( "Stars", "true" );
	} else {
		if (RadioButton1_2->isOn() == true)
		{
			config.writeEntry( "Stars", "false" );
		}
	}
	config.writeEntry( "StarSize", TQString::number(SpinBox1->value()) );

	config.sync();

	accept();
}

void KGravitySetup::aboutPressed()
{
    KMessageBox::about(this,
        i18n("<h3>Gravity</h3>\n<p>Particle Gravity Screen Saver for TDE</p>\nCopyright (c)  Ian Reinhart Geiser 2001<br>\n\n<p>TDEConfig code and KScreenSaver \"Setup...\" improvements by Nick Betcher <nbetcher@usinternet.com> 2001</p>"));
}
//-----------------------------------------------------------------------------


KGravitySaver::KGravitySaver( WId id ) : KScreenSaver( id )
{

	kdDebug() << "Blank" << endl;

	timer = new TQTimer( this );
    	timer->start( 25, TRUE );
	setBackgroundColor( black );
        erase();
	gravity = new Gravity();
	embed(gravity);
#ifdef TQ_WS_X11
	gravity->show();
#endif
	connect( timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(blank()) );
}

KGravitySaver::~KGravitySaver()
{

}

// read configuration settings from config file
void KGravitySaver::readSettings()
{
// Please remove me

}

void KGravitySaver::blank()
{
	// Play gravity

	gravity->updateGL();
	timer->start( 25, TRUE );

}
Gravity::Gravity( TQWidget * parent, const char * name) : TQGLWidget (parent,name)
{
	rainbow=true;
	slowdown=2.0f;
	zoom=-50.0f;
	index=0;
	size = 3.95f;
//	obj = gluNewQuadric();

// This has to be here because you can't update the gravity until 'gravity' is created!
	TDEConfig config("kssgravityrc", false, false);
	config.setGroup( "Settings" );
	TQString boolval = config.readEntry( "Stars", "false" );
	if (boolval == "true") {
		setStars(true);
	} else {
		if (boolval == "false")
		{
			setStars(false);
		}
	}

	TQString starammount = config.readEntry("StarSize", "75");
	float passvalue = (starammount.toInt() / 100.0);
	setSize(passvalue);

}

Gravity::~Gravity()
{
	glDeleteTextures( 1, &texture[0] );
	gluDeleteQuadric(obj);
}

/** load the particle file */
bool Gravity::loadParticle()
{
    /* Status indicator */
    bool Status = TRUE;
	TQImage buf;

    kdDebug() << "Loading: " << locate("data", "tdescreensaver/particle.png") << endl;
 if (buf.load( locate("data", "tdescreensaver/particle.png") ) )

        {
		tex = convertToGLFormat(buf);  // flipped 32bit RGBA
		kdDebug() << "Texture loaded: " << tex.numBytes () << endl;
	}
	else
	{
		TQImage dummy( 32, 32, 32 );
  		dummy.fill( TQt::white.rgb() );
        	buf = dummy;
		tex = convertToGLFormat( buf );
	}

            /* Set the status to true */
            //Status = TRUE;
	glGenTextures(1, &texture[0]);   /* create three textures */
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	/* use linear filtering */
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	/* actually generate the texture */
	glTexImage2D(GL_TEXTURE_2D, 0, 4, tex.width(), tex.height(), 0,
	GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());



    return Status;
}
/** setup the GL enviroment */
void Gravity::initializeGL ()
{

	kdDebug() << "InitGL" << endl;

	if (loadParticle())						// Jump To Texture Loading Routine
	{
    /* Enable smooth shading */
    glShadeModel( GL_SMOOTH );

    /* Set the background black */
    glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );

    /* Depth buffer setup */
    glClearDepth( 1.0f );

    /* Enables Depth Testing */
    glDisable( GL_DEPTH_TEST );

    /* Enable Blending */
    glEnable( GL_BLEND );
    /* Type Of Blending To Perform */
    glBlendFunc( GL_SRC_ALPHA, GL_ONE );


    /* Really Nice Perspective Calculations */
    glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
    /* Really Nice Point Smoothing */
    glHint( GL_POINT_SMOOTH_HINT, GL_NICEST );

    /* Enable Texture Mapping */
    glEnable( GL_TEXTURE_2D );
    /* Select Our Texture */
    glBindTexture( GL_TEXTURE_2D, texture[0] );

		for (loop=0;loop<MAX_PARTICLES;loop++)				// Initials All The Textures
		 {
			buildParticle(loop);
		}
	}
	else
		exit(0);
}
/** resize the gl view */
void Gravity::resizeGL ( int width, int height )
{
	kdDebug() << "ResizeGL " << width << "," <<height<< endl;
	if (height==0)							// Prevent A Divide By Zero By
	{
		height=1;						// Making Height Equal One
	}

	glViewport(0,0,width,height);					// Reset The Current Viewport

	glMatrixMode(GL_PROJECTION);					// Select The Projection Matrix
	glLoadIdentity();						// Reset The Projection Matrix

	// Calculate The Aspect Ratio Of The Window
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

	glMatrixMode(GL_MODELVIEW);					// Select The Modelview Matrix
	glLoadIdentity();
}
/** paint the GL view */
void Gravity::paintGL ()
{
	//kdDebug() << "PaintGL" << endl;
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// Clear Screen And Depth Buffer
	glLoadIdentity();
						// Reset The ModelView Matrix
	transIndex++;
	//glRotatef(transIndex, 1,0,0);
	//glRotatef(transIndex, 0,1,0);
	//glRotatef(transIndex, 0,0,1);
	float xmax = 5.0;
	float ymax = 5.0;
	glTranslatef( GLfloat(xmax*sin(3.14*transIndex/360)-xmax),
			GLfloat(ymax*cos(3.14*transIndex/360)-ymax),
			0.0 );
	//glRotatef(transIndex, 0,GLfloat(zmax*cos(3.14*transIndex/360000)), GLfloat(zmax*cos(3.14*transIndex/360000)));

	for (loop=0;loop<MAX_PARTICLES;loop++)				// Loop Through All The Particles
	{
		if (particle[loop].active)				// If The Particle Is Active
		{
			float x=particle[loop].x;			// Grab Our Particle X Position
			float y=particle[loop].y;			// Grab Our Particle Y Position
			float z=particle[loop].z+zoom;			// Particle Z Pos + Zoom
    /* Select Our Texture */

                    /* Draw The Particle Using Our RGB Values,
                     * Fade The Particle Based On It's Life
                     */
			particle[loop].life=(particle[loop].index/particle[loop].indexo)*2.0f;
                    glColor4f( particle[loop].r,
                               particle[loop].g,
                               particle[loop].b,
                               particle[loop].life );

                    /* Build Quad From A Triangle Strip */
		  if( !stars )
		    glBegin( GL_TRIANGLE_STRIP );
		  else
		glBegin( GL_TRIANGLE_FAN );
			/* Top Right */
			glTexCoord2d( 1, 1 );
			glVertex3f( x + particle[loop].size, y + particle[loop].size, z );
			/* Top Left */
			glTexCoord2d( 0, 1 );
			glVertex3f( x - particle[loop].size, y + particle[loop].size, z );
			/* Bottom Right */
			glTexCoord2d( 1, 0 );
			glVertex3f( x + particle[loop].size, y - particle[loop].size, z );
			/* Bottom Left */
			glTexCoord2d( 0, 0 );
			glVertex3f( x - particle[loop].size, y - particle[loop].size, z );
		glEnd( );
			particle[loop].x=(particle[loop].xo*sin(particle[loop].index))*pow((double) particle[loop].index/particle[loop].indexo,(double) 8.0);
			particle[loop].y=(particle[loop].yo*sin(particle[loop].index))*pow((double) particle[loop].index/particle[loop].indexo,(double) 8.0);
			particle[loop].z=(particle[loop].zo*sin(particle[loop].index))*pow((double) particle[loop].index/particle[loop].indexo,(double) 8.0);
			particle[loop].index-=0.05;
			if (particle[loop].index<0.0f )			// If Particle Is Burned Out
			{
				buildParticle(loop);
			}
			// Lets stir some things up
		}
	}

	glFlush();
}
void Gravity::setSize( float newSize )
{
	size = newSize;
}
void Gravity::setStars( bool doStars )
{
	stars = doStars;
}

void KGravitySaver::updateSize(int newSize)
{
	gravity->setSize(newSize/100);
}
void KGravitySaver::doStars(bool starState)
{
	gravity->setStars(starState);
}

void Gravity::buildParticle(int loop)
{
	GLfloat colors[12][3]=
	{{1.0f,0.5f,0.5f},{1.0f,0.75f,0.5f},{1.0f,1.0f,0.5f},{0.75f,1.0f,0.5f},
	{0.5f,1.0f,0.5f},{0.5f,1.0f,0.75f},{0.5f,1.0f,1.0f},{0.5f,0.75f,1.0f},
	{0.5f,0.5f,1.0f},{0.75f,0.5f,1.0f},{1.0f,0.5f,1.0f},{1.0f,0.5f,0.75f}};
	col = ( ++col ) % 12;
	particle[loop].active=true;
	particle[loop].index=TDEApplication::random()%100;
	particle[loop].indexo=particle[loop].index;
	particle[loop].fade=float(TDEApplication::random()%100)/1000.0f+0.003f;	// Random Fade Value
	particle[loop].r=colors[col][0];			// Select Red From Color Table
	particle[loop].g=colors[col][1];			// Select Green From Color Table
	particle[loop].b=colors[col][2];			// Select Blue From Color Table
	particle[loop].size=size;
	particle[loop].x = float(TDEApplication::random()%100-50)*4.0;
	particle[loop].y = float(TDEApplication::random()%20-10)*4.0;
	particle[loop].z = float(TDEApplication::random()%100-50)*4.0;
	particle[loop].xo = particle[loop].x;
	if ((1+(TDEApplication::random() % 10) > 5))
		particle[loop].yo = particle[loop].y;
	else
		particle[loop].yo = 0.0;
	particle[loop].zo = particle[loop].z;

}